Skip to content

Commit

Permalink
Bug 1846191 remove stream from mLiveStreams on audio thread when drai…
Browse files Browse the repository at this point in the history
…ned or on error r=pehrsons

This removes one potentially dangling MockCubeb pointer, from the background
task, but this is mostly about the simplicity of removing while the lock is
still held.

Depends on D185024

Differential Revision: https://phabricator.services.mozilla.com/D185025
  • Loading branch information
karlt committed Aug 23, 2023
1 parent 946baf0 commit 0caacdb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
28 changes: 11 additions & 17 deletions dom/media/gtest/MockCubeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace mozilla {

using KeepProcessing = MockCubebStream::KeepProcessing;

void PrintDevice(cubeb_device_info aInfo) {
printf(
"id: %zu\n"
Expand Down Expand Up @@ -339,11 +341,7 @@ MediaEventSource<void>& MockCubebStream::DeviceChangeForcedEvent() {
return mDeviceChangedForcedEvent;
}

void MockCubebStream::Process10Ms() {
if (mStreamStop) {
return;
}

KeepProcessing MockCubebStream::Process10Ms() {
uint32_t rate = mHasOutput ? mOutputParams.rate : mInputParams.rate;
const long nrFrames =
static_cast<long>(static_cast<float>(rate * 10) * mDriftFactor) /
Expand Down Expand Up @@ -373,22 +371,13 @@ void MockCubebStream::Process10Ms() {

if (outframes < nrFrames) {
NotifyState(CUBEB_STATE_DRAINED);
mStreamStop = true;
return;
return KeepProcessing::No;
}
if (mForceErrorState) {
mForceErrorState = false;
// Let the audio thread (this thread!) run to completion before
// being released, by joining and deleting on another thread.
NS_DispatchBackgroundTask(NS_NewRunnableFunction(
__func__, [cubeb = MockCubeb::AsMock(context), this,
self = RefPtr<SmartMockCubebStream>(mSelf)] {
cubeb->StopStream(this);
}));
NotifyState(CUBEB_STATE_ERROR);
mErrorForcedEvent.Notify();
mStreamStop = true;
return;
return KeepProcessing::No;
}
if (mForceDeviceChanged) {
mForceDeviceChanged = false;
Expand All @@ -402,6 +391,7 @@ void MockCubebStream::Process10Ms() {
mDeviceChangedForcedEvent.Notify();
}));
}
return KeepProcessing::Yes;
}

void MockCubebStream::NotifyState(cubeb_state aState) {
Expand Down Expand Up @@ -654,8 +644,12 @@ void MockCubeb::ThreadFunction() {
{
auto streams = mLiveStreams.Lock();
for (auto& stream : *streams) {
stream->Process10Ms();
auto keepProcessing = stream->Process10Ms();
if (keepProcessing == KeepProcessing::No) {
stream = nullptr;
}
}
streams->RemoveElementsBy([](const auto& stream) { return !stream; });
MOZ_ASSERT(mFakeAudioThread);
if (streams->IsEmpty() && !mForcedAudioThread) {
NS_DispatchBackgroundTask(NS_NewRunnableFunction(
Expand Down
6 changes: 4 additions & 2 deletions dom/media/gtest/MockCubeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ class MockCubebStream {
MediaEventSource<void>& ErrorForcedEvent();
MediaEventSource<void>& DeviceChangeForcedEvent();

void Process10Ms();
enum class KeepProcessing { No, Yes };
KeepProcessing Process10Ms();

public:
const bool mHasInput;
Expand All @@ -216,7 +217,8 @@ class MockCubebStream {
// Whether this stream should wait for an explicit start request before
// starting. Protected by FrozenStartMonitor.
bool mFrozenStart;
// Signal to the audio thread that stream is stopped.
// Used to abort a frozen start if cubeb_stream_start() is called currently
// with a blocked cubeb_stream_start() call.
std::atomic_bool mStreamStop{true};
// Whether or not the output-side of this stream (what is written from the
// callback output buffer) is recorded in an internal buffer. The data is then
Expand Down

0 comments on commit 0caacdb

Please sign in to comment.