Bug 1276838 - improve coding style of calling cubeb functions. r=kinetik.

MozReview-Commit-ID: 1KjjF6StM0a

--HG--
extra : rebase_source : 5174bedd6400c88b01cb0018f9ff64f1eadb2d4a
This commit is contained in:
JW Wang 2016-05-30 21:12:35 +08:00
parent 29923a5e97
commit 378ce6e9b8
2 changed files with 18 additions and 25 deletions

View File

@ -315,6 +315,13 @@ 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)
@ -414,16 +421,11 @@ 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");
@ -444,11 +446,7 @@ AudioStream::Pause()
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;
}
@ -462,11 +460,7 @@ AudioStream::Resume()
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;
}
@ -514,13 +508,9 @@ AudioStream::GetPositionInFramesUnlocked()
}
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);
}

View File

@ -340,6 +340,9 @@ private:
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;