1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AudioSink.h"
#include "AudioSinkWrapper.h"
#include "CubebUtils.h"
#include "MockCubeb.h"
#include "TimeUnits.h"
#include "gmock/gmock.h"
#include "gtest/gtest-printers.h"
#include "gtest/gtest.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/gtest/WaitFor.h"
#include "nsThreadManager.h"
#include "nsThreadUtils.h"
using namespace mozilla;
// This is a crashtest to check that AudioSinkWrapper::mEndedPromiseHolder is
// not settled twice when sync and async AudioSink initializations race.
TEST(TestAudioSinkWrapper, AsyncInitFailureWithSyncInitSuccess)
{
MockCubeb* cubeb = new MockCubeb();
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());
MediaQueue<AudioData> audioQueue;
MediaInfo info;
info.EnableAudio();
auto audioSinkCreator = [&]() {
return UniquePtr<AudioSink>{new AudioSink(AbstractThread::GetCurrent(),
audioQueue, info.mAudio,
/*resistFingerprinting*/ false)};
};
const double initialVolume = 0.0; // so that there is initially no AudioSink
RefPtr wrapper = new AudioSinkWrapper(
AbstractThread::GetCurrent(), audioQueue, std::move(audioSinkCreator),
initialVolume, /*playbackRate*/ 1.0, /*preservesPitch*/ true,
/*sinkDevice*/ nullptr);
wrapper->Start(media::TimeUnit::Zero(), info);
// The first AudioSink init occurs on a background thread. Listen for this,
// but don't process any events on the current thread so that the
// AudioSinkWrapper does not yet handle the result of AudioSink
// initialization.
RefPtr backgroundQueue =
nsThreadManager::get().CreateBackgroundTaskQueue(__func__);
Monitor monitor(__func__);
bool initDone = false;
MediaEventListener initListener = cubeb->StreamInitEvent().Connect(
backgroundQueue, [&](RefPtr<SmartMockCubebStream> aStream) {
EXPECT_EQ(aStream, nullptr);
MonitorAutoLock lock(monitor);
initDone = true;
lock.Notify();
});
cubeb->ForceStreamInitError();
wrapper->SetVolume(0.5); // triggers async sink init, which fails
{
// Wait for the async init to complete.
MonitorAutoLock lock(monitor);
while (!initDone) {
lock.Wait();
}
}
initListener.Disconnect();
wrapper->SetPlaying(false);
// The second AudioSink init is synchronous.
nsIThread* currentThread = NS_GetCurrentThread();
RefPtr<SmartMockCubebStream> stream;
initListener = cubeb->StreamInitEvent().Connect(
currentThread, [&](RefPtr<SmartMockCubebStream> aStream) {
stream = std::move(aStream);
});
wrapper->SetPlaying(true); // sync sink init, which succeeds
// Let AudioSinkWrapper handle the (first) AudioSink initialization failure
// and allow `stream` to be set.
NS_ProcessPendingEvents(currentThread);
initListener.Disconnect();
cubeb_state state = CUBEB_STATE_STARTED;
MediaEventListener stateListener = stream->StateEvent().Connect(
currentThread, [&](cubeb_state aState) { state = aState; });
// Run AudioSinkWrapper::OnAudioEnded().
// This test passes if there is no crash. Bug 1845811.
audioQueue.Finish();
SpinEventLoopUntil("stream state change"_ns,
[&] { return state != CUBEB_STATE_STARTED; });
stateListener.Disconnect();
EXPECT_EQ(state, CUBEB_STATE_DRAINED);
wrapper->Stop();
wrapper->Shutdown();
}
// This is a crashtest to check that AudioSinkWrapper::mEndedPromiseHolder is
// not settled twice when the audio ends during async AudioSink initialization.
TEST(TestAudioSinkWrapper, AsyncInitWithEndOfAudio)
{
MockCubeb* cubeb = new MockCubeb();
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());
MediaQueue<AudioData> audioQueue;
MediaInfo info;
info.EnableAudio();
auto audioSinkCreator = [&]() {
return UniquePtr<AudioSink>{new AudioSink(AbstractThread::GetCurrent(),
audioQueue, info.mAudio,
/*resistFingerprinting*/ false)};
};
const double initialVolume = 0.0; // so that there is initially no AudioSink
RefPtr wrapper = new AudioSinkWrapper(
AbstractThread::GetCurrent(), audioQueue, std::move(audioSinkCreator),
initialVolume, /*playbackRate*/ 1.0, /*preservesPitch*/ true,
/*sinkDevice*/ nullptr);
wrapper->Start(media::TimeUnit::Zero(), info);
// The first AudioSink init occurs on a background thread. Listen for this,
// but don't process any events on the current thread so that the
// AudioSinkWrapper does not yet use the initialized AudioSink.
RefPtr backgroundQueue =
nsThreadManager::get().CreateBackgroundTaskQueue(__func__);
Monitor monitor(__func__);
RefPtr<SmartMockCubebStream> stream;
MediaEventListener initListener = cubeb->StreamInitEvent().Connect(
backgroundQueue, [&](RefPtr<SmartMockCubebStream> aStream) {
EXPECT_NE(aStream, nullptr);
MonitorAutoLock lock(monitor);
stream = std::move(aStream);
lock.Notify();
});
wrapper->SetVolume(0.5); // triggers async sink init
{
// Wait for the async init to complete.
MonitorAutoLock lock(monitor);
while (!stream) {
lock.Wait();
}
}
initListener.Disconnect();
// Finish the audio before AudioSinkWrapper considers using the initialized
// AudioSink.
audioQueue.Finish();
// Wait for AudioSinkWrapper to destroy the initialized stream.
// This test passes if there is no crash. Bug 1846854.
WaitFor(cubeb->StreamDestroyEvent());
wrapper->Stop();
wrapper->Shutdown();
}
|