Bug 1276838 - improve coding style of calling cubeb functions. r=kinetik.
MozReview-Commit-ID: 1KjjF6StM0a
--- a/dom/media/AudioStream.cpp
+++ b/dom/media/AudioStream.cpp
@@ -310,16 +310,23 @@ struct ToCubebFormat {
static const cubeb_sample_format value = CUBEB_SAMPLE_FLOAT32NE;
};
template <>
struct ToCubebFormat<AUDIO_FORMAT_S16> {
static const cubeb_sample_format value = CUBEB_SAMPLE_S16NE;
};
+template <typename Function, typename... Args>
+int AudioStream::InvokeCubeb(Function aFunction, Args&&... aArgs)
+{
+ MonitorAutoUnlock mon(mMonitor);
+ return aFunction(mCubebStream.get(), Forward<Args>(aArgs)...);
+}
+
nsresult
AudioStream::Init(uint32_t aNumChannels, uint32_t aRate,
const dom::AudioChannel aAudioChannel)
{
auto startTime = TimeStamp::Now();
mIsFirst = CubebUtils::GetFirstStream();
if (!CubebUtils::GetCubebContext()) {
@@ -409,26 +416,21 @@ AudioStream::SetVolume(double aVolume)
}
}
void
AudioStream::Start()
{
MonitorAutoLock mon(mMonitor);
if (mState == INITIALIZED) {
+ // DataCallback might be called before InvokeCubeb returns
+ // if cubeb_stream_start() succeeds. mState must be set to STARTED
+ // beforehand.
mState = STARTED;
- int r;
- {
- MonitorAutoUnlock mon(mMonitor);
- r = cubeb_stream_start(mCubebStream.get());
- // DataCallback might be called before we exit this scope
- // if cubeb_stream_start() succeeds. mState must be set to STARTED
- // beforehand.
- }
- if (r != CUBEB_OK) {
+ if (InvokeCubeb(cubeb_stream_start) != CUBEB_OK) {
mState = ERRORED;
}
LOG("started, state %s", mState == STARTED ? "STARTED" : "ERRORED");
}
}
void
AudioStream::Pause()
@@ -439,39 +441,31 @@ AudioStream::Pause()
return;
}
if (mState != STARTED && mState != RUNNING) {
mState = STOPPED; // which also tells async OpenCubeb not to start, just init
return;
}
- int r;
- {
- MonitorAutoUnlock mon(mMonitor);
- r = cubeb_stream_stop(mCubebStream.get());
- }
+ int r = InvokeCubeb(cubeb_stream_stop);
if (mState != ERRORED && r == CUBEB_OK) {
mState = STOPPED;
}
}
void
AudioStream::Resume()
{
MonitorAutoLock mon(mMonitor);
if (mState != STOPPED) {
return;
}
- int r;
- {
- MonitorAutoUnlock mon(mMonitor);
- r = cubeb_stream_start(mCubebStream.get());
- }
+ int r = InvokeCubeb(cubeb_stream_start);
if (mState != ERRORED && r == CUBEB_OK) {
mState = STARTED;
}
}
void
AudioStream::Shutdown()
{
@@ -509,23 +503,19 @@ AudioStream::GetPositionInFramesUnlocked
{
mMonitor.AssertCurrentThreadOwns();
if (mState == ERRORED) {
return -1;
}
uint64_t position = 0;
- {
- MonitorAutoUnlock mon(mMonitor);
- if (cubeb_stream_get_position(mCubebStream.get(), &position) != CUBEB_OK) {
- return -1;
- }
+ if (InvokeCubeb(cubeb_stream_get_position, &position) != CUBEB_OK) {
+ return -1;
}
-
return std::min<uint64_t>(position, INT64_MAX);
}
bool
AudioStream::IsPaused()
{
MonitorAutoLock mon(mMonitor);
return mState == STOPPED;
--- a/dom/media/AudioStream.h
+++ b/dom/media/AudioStream.h
@@ -335,16 +335,19 @@ private:
// Return true if audio frames are valid (correct sampling rate and valid
// channel count) otherwise false.
bool IsValidAudioFormat(Chunk* aChunk);
void GetUnprocessed(AudioBufferWriter& aWriter);
void GetTimeStretched(AudioBufferWriter& aWriter);
+ template <typename Function, typename... Args>
+ int InvokeCubeb(Function aFunction, Args&&... aArgs);
+
// The monitor is held to protect all access to member variables.
Monitor mMonitor;
// Input rate in Hz (characteristic of the media being played)
uint32_t mInRate;
// Output rate in Hz (characteristic of the playback rate)
uint32_t mOutRate;
uint32_t mChannels;