Skip to content

Commit

Permalink
Merge pull request #126 from stream-labs/volmeter-fix
Browse files Browse the repository at this point in the history
Simplify volmeter logic by removing data swap
  • Loading branch information
computerquip-streamlabs authored Oct 19, 2018
2 parents b2f2e82 + 058ff5c commit 77baf51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
1 change: 0 additions & 1 deletion obs-studio-client/source/volmeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ void osn::VolMeter::worker()
totalSleepMS = m_sleep_interval - dur.count();
std::this_thread::sleep_for(std::chrono::milliseconds(totalSleepMS));
}
return;
}

void osn::VolMeter::set_keepalive(v8::Local<v8::Object> obj)
Expand Down
42 changes: 14 additions & 28 deletions obs-studio-server/source/osn-volmeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,25 +293,20 @@ void osn::VolMeter::Query(
return;
}

std::unique_lock<std::mutex> ulock(meter->current_data_mtx);
rval.push_back(ipc::value((uint64_t)ErrorCode::Ok));
if (meter->current_data) {
rval.push_back(ipc::value(meter->current_data->ch));
for (size_t ch = 0; ch < meter->current_data->ch; ch++) {
rval.push_back(ipc::value(meter->current_data->magnitude[ch]));
rval.push_back(ipc::value(meter->current_data->peak[ch]));
rval.push_back(ipc::value(meter->current_data->input_peak[ch]));
}
} else {
rval.push_back(ipc::value(0));
}

if (meter->current_data) {
std::unique_lock<std::mutex> flock(meter->free_data_mtx);
meter->free_data = meter->current_data;
meter->current_data = nullptr;
std::unique_lock<std::mutex> ulock(meter->current_data_mtx);

rval.push_back(ipc::value(meter->current_data.ch));

for (size_t ch = 0; ch < meter->current_data.ch; ch++) {
rval.push_back(ipc::value(meter->current_data.magnitude[ch]));
rval.push_back(ipc::value(meter->current_data.peak[ch]));
rval.push_back(ipc::value(meter->current_data.input_peak[ch]));
}

ulock.unlock();

AUTO_DEBUG;
}

Expand All @@ -329,22 +324,13 @@ void osn::VolMeter::OBSCallback(
#define MAKE_FLOAT_SANE(db) (std::isfinite(db) ? db : (db > 0 ? 0.0f : -65535.0f))
#define PREVIOUS_FRAME_WEIGHT

// This is a simple swap operation.
std::unique_lock<std::mutex> ulock(meter->current_data_mtx);
if (!meter->current_data) {
std::unique_lock<std::mutex> ulock(meter->free_data_mtx);
if (!meter->free_data) {
meter->free_data = std::make_shared<AudioData>();
}
meter->current_data = meter->free_data;
meter->free_data = nullptr;
}

meter->current_data->ch = obs_volmeter_get_nr_channels(meter->self);
meter->current_data.ch = obs_volmeter_get_nr_channels(meter->self);
for (size_t ch = 0; ch < MAX_AUDIO_CHANNELS; ch++) {
meter->current_data->magnitude[ch] = MAKE_FLOAT_SANE(magnitude[ch]);
meter->current_data->peak[ch] = MAKE_FLOAT_SANE(peak[ch]);
meter->current_data->input_peak[ch] = MAKE_FLOAT_SANE(input_peak[ch]);
meter->current_data.magnitude[ch] = MAKE_FLOAT_SANE(magnitude[ch]);
meter->current_data.peak[ch] = MAKE_FLOAT_SANE(peak[ch]);
meter->current_data.input_peak[ch] = MAKE_FLOAT_SANE(input_peak[ch]);
}

#undef MAKE_FLOAT_SANE
Expand Down
5 changes: 2 additions & 3 deletions obs-studio-server/source/osn-volmeter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ namespace osn
float input_peak[MAX_AUDIO_CHANNELS] = {0};
int32_t ch = 0;
};
std::shared_ptr<AudioData> current_data;

AudioData current_data;
std::mutex current_data_mtx;
std::shared_ptr<AudioData> free_data;
std::mutex free_data_mtx;

public:
VolMeter(obs_fader_type type);
Expand Down

0 comments on commit 77baf51

Please sign in to comment.