diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index de6618f831..fb63fe2daf 100755 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -70,6 +70,8 @@ using namespace std; static char STAR_FILE[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars.txt"; static char STAR_CACHE_FILE[] = "cachedStars.txt"; +static const int BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH = 6; // farther dragged clicks are ignored + const glm::vec3 START_LOCATION(4.f, 0.f, 5.f); // Where one's own agent begins in the world // (will be overwritten if avatar data file is found) @@ -483,14 +485,14 @@ void Application::broadcastToAgents(unsigned char* data, size_t bytes, const cha int n = AgentList::getInstance()->broadcastToAgents(data, bytes, &type, 1); - unsigned channel; + BandwidthMeter::ChannelIndex channel; switch (type) { case AGENT_TYPE_AVATAR: case AGENT_TYPE_AVATAR_MIXER: - channel = 1; + channel = BandwidthMeter::AVATARS; break; case AGENT_TYPE_VOXEL_SERVER: - channel = 2; + channel = BandwidthMeter::VOXELS; break; default: return; @@ -1004,7 +1006,7 @@ void Application::checkBandwidthMeterClick() { // ... to be called upon button release if (_bandwidthDisplayOn->isChecked() && - glm::compMax(glm::abs(glm::ivec2(_mouseX - _mouseDragStartedX, _mouseY - _mouseDragStartedY))) <= 6 && + glm::compMax(glm::abs(glm::ivec2(_mouseX - _mouseDragStartedX, _mouseY - _mouseDragStartedY))) <= BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH && _bandwidthMeter.isWithinArea(_mouseX, _mouseY, _glWidget->width(), _glWidget->height())) { // The bandwidth meter is visible, the click didn't get dragged too far and @@ -2843,7 +2845,7 @@ void* Application::networkReceive(void* args) { AgentList::getInstance()->processBulkAgentData(&senderAddress, app->_incomingPacket, bytesReceived); - getInstance()->_bandwidthMeter.inputStream(1).updateValue(bytesReceived); + getInstance()->_bandwidthMeter.inputStream(BandwidthMeter::AVATARS).updateValue(bytesReceived); break; case PACKET_HEADER_AVATAR_VOXEL_URL: processAvatarVoxelURLMessage(app->_incomingPacket, bytesReceived); diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index b60f060e5d..cfc4f6de19 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -124,7 +124,7 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o dataPacket, BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes); - interface->getBandwidthMeter()->outputStream(0) + interface->getBandwidthMeter()->outputStream(BandwidthMeter::AUDIO) .updateValue(BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes); } @@ -448,7 +448,7 @@ void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBy _ringBuffer.parseData((unsigned char*) receivedData, PACKET_LENGTH_BYTES + sizeof(PACKET_HEADER)); - Application::getInstance()->getBandwidthMeter()->inputStream(0) + Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::AUDIO) .updateValue(PACKET_LENGTH_BYTES + sizeof(PACKET_HEADER)); _lastReceiveTime = currentReceiveTime; diff --git a/interface/src/BandwidthMeter.cpp b/interface/src/BandwidthMeter.cpp index f7905cade7..ba89864807 100644 --- a/interface/src/BandwidthMeter.cpp +++ b/interface/src/BandwidthMeter.cpp @@ -36,7 +36,7 @@ namespace { // .cpp-local int const INITIAL_SCALE_MAXIMUM_INDEX = 250; // / 9: exponent, % 9: mantissa - 2, 0 o--o 2 * 10^-10 } -BandwidthMeter::ChannelInfo BandwidthMeter::_DEFAULT_CHANNELS[] = { +BandwidthMeter::ChannelInfo BandwidthMeter::_CHANNELS[] = { { "Audio" , "Kbps", 8000.0 / 1024.0, 0x40ff40d0 }, { "Avatars" , "Kbps", 8000.0 / 1024.0, 0xffef40c0 }, { "Voxels" , "Kbps", 8000.0 / 1024.0, 0xd0d0d0a0 } @@ -46,7 +46,13 @@ BandwidthMeter::BandwidthMeter() : _textRenderer(SANS_FONT_FAMILY, -1, -1, false, TextRenderer::SHADOW_EFFECT), _scaleMaxIndex(INITIAL_SCALE_MAXIMUM_INDEX) { - memcpy(_channels, _DEFAULT_CHANNELS, sizeof(_DEFAULT_CHANNELS)); + _channels = static_cast( malloc(sizeof(_CHANNELS)) ); + memcpy(_channels, _CHANNELS, sizeof(_CHANNELS)); +} + +BandwidthMeter::~BandwidthMeter() { + + free(_channels); } BandwidthMeter::Stream::Stream(float msToAverage) : @@ -118,8 +124,8 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) { float totalIn = 0.0f, totalOut = 0.0f; for (int i = 0; i < N_CHANNELS; ++i) { - totalIn += inputStream(i).getValue(); - totalOut += outputStream(i).getValue(); + totalIn += inputStream(ChannelIndex(i)).getValue(); + totalOut += outputStream(ChannelIndex(i)).getValue(); } totalIn *= UNIT_SCALE; totalOut *= UNIT_SCALE; @@ -189,10 +195,11 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) { int xIn = 0, xOut = 0; for (int i = 0; i < N_CHANNELS; ++i) { - int wIn = int(barWidth * inputStream(i).getValue() * UNIT_SCALE / scaleMax); - int wOut = int(barWidth * outputStream(i).getValue() * UNIT_SCALE / scaleMax); + ChannelIndex chIdx = ChannelIndex(i); + int wIn = int(barWidth * inputStream(chIdx).getValue() * UNIT_SCALE / scaleMax); + int wOut = int(barWidth * outputStream(chIdx).getValue() * UNIT_SCALE / scaleMax); - setColorRGBA(channelInfo(i).colorRGBA); + setColorRGBA(channelInfo(chIdx).colorRGBA); if (wIn > 0) { renderBox(xIn, 0, wIn, barHeight); diff --git a/interface/src/BandwidthMeter.h b/interface/src/BandwidthMeter.h index d3641a21f0..0221306ed7 100644 --- a/interface/src/BandwidthMeter.h +++ b/interface/src/BandwidthMeter.h @@ -19,6 +19,7 @@ class BandwidthMeter { public: BandwidthMeter(); + ~BandwidthMeter(); void render(int screenWidth, int screenHeight); bool isWithinArea(int x, int y, int screenWidth, int screenHeight); @@ -27,13 +28,16 @@ public: static size_t const N_CHANNELS = 3; static size_t const N_STREAMS = N_CHANNELS * 2; + // Channel usage. + enum ChannelIndex { AUDIO, AVATARS, VOXELS }; + // Meta information held for a communication channel (bidirectional). struct ChannelInfo { - char const* caption; - char const* unitCaption; - double unitScale; - unsigned colorRGBA; + char const* const caption; + char const* unitCaption; + double unitScale; + unsigned colorRGBA; }; // Representation of a data stream (unidirectional; input or output). @@ -52,12 +56,12 @@ public: }; // Data model accessors - Stream& inputStream(unsigned channelIndex) { return _streams[channelIndex * 2]; } - Stream const& inputStream(unsigned channelIndex) const { return _streams[channelIndex * 2]; } - Stream& outputStream(unsigned channelIndex) { return _streams[channelIndex * 2 + 1]; } - Stream const& outputStream(unsigned channelIndex) const { return _streams[channelIndex * 2 + 1]; } - ChannelInfo& channelInfo(unsigned index) { return _channels[index]; } - ChannelInfo const& channelInfo(unsigned index) const { return _channels[index]; } + Stream& inputStream(ChannelIndex i) { return _streams[i * 2]; } + Stream const& inputStream(ChannelIndex i) const { return _streams[i * 2]; } + Stream& outputStream(ChannelIndex i) { return _streams[i * 2 + 1]; } + Stream const& outputStream(ChannelIndex i) const { return _streams[i * 2 + 1]; } + ChannelInfo& channelInfo(ChannelIndex i) { return _channels[i]; } + ChannelInfo const& channelInfo(ChannelIndex i) const { return _channels[i]; } private: static void setColorRGBA(unsigned c); @@ -67,10 +71,10 @@ private: static inline int centered(int subject, int object); - static ChannelInfo _DEFAULT_CHANNELS[]; + static ChannelInfo _CHANNELS[]; TextRenderer _textRenderer; - ChannelInfo _channels[N_CHANNELS]; + ChannelInfo* _channels; Stream _streams[N_STREAMS]; int _scaleMaxIndex; }; diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 5de9b89594..55d7e76d62 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -161,7 +161,7 @@ int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) { pthread_mutex_unlock(&_treeLock); - Application::getInstance()->getBandwidthMeter()->inputStream(2).updateValue(numBytes); + Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::VOXELS).updateValue(numBytes); return numBytes; } diff --git a/interface/src/ui/BandwidthDialog.cpp b/interface/src/ui/BandwidthDialog.cpp index 7879816d7c..1e0e2e616e 100644 --- a/interface/src/ui/BandwidthDialog.cpp +++ b/interface/src/ui/BandwidthDialog.cpp @@ -23,9 +23,8 @@ BandwidthDialog::BandwidthDialog(QWidget* parent, BandwidthMeter* model) : // Setup labels for (int i = 0; i < BandwidthMeter::N_STREAMS; ++i) { - int chIdx = i / 2; bool input = i % 2 == 0; - BandwidthMeter::ChannelInfo& ch = _model->channelInfo(chIdx); + BandwidthMeter::ChannelInfo& ch = _model->channelInfo(BandwidthMeter::ChannelIndex(i / 2)); QLabel* label = _labels[i] = new QLabel(); label->setAlignment(Qt::AlignRight); @@ -46,7 +45,7 @@ void BandwidthDialog::paintEvent(QPaintEvent* event) { // Update labels char strBuf[64]; for (int i = 0; i < BandwidthMeter::N_STREAMS; ++i) { - int chIdx = i / 2; + BandwidthMeter::ChannelIndex chIdx = BandwidthMeter::ChannelIndex(i / 2); bool input = i % 2 == 0; BandwidthMeter::ChannelInfo& ch = _model->channelInfo(chIdx); BandwidthMeter::Stream& s = input ? _model->inputStream(chIdx) : _model->outputStream(chIdx);