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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/* 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 "ForwardedInputTrack.h"
#include <algorithm>
#include "AudioChannelService.h"
#include "AudioNodeEngine.h"
#include "AudioNodeExternalInputTrack.h"
#include "AudioNodeTrack.h"
#include "AudioSegment.h"
#include "DOMMediaStream.h"
#include "GeckoProfiler.h"
#include "ImageContainer.h"
#include "MediaTrackGraph.h"
#include "mozilla/Attributes.h"
#include "mozilla/Logging.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Unused.h"
#include "nsContentUtils.h"
#include "nsPrintfCString.h"
#include "nsServiceManagerUtils.h"
#include "nsWidgetsCID.h"
#include "prerror.h"
#include "Tracing.h"
#include "VideoSegment.h"
#include "webaudio/MediaStreamAudioDestinationNode.h"
using namespace mozilla::layers;
using namespace mozilla::dom;
using namespace mozilla::gfx;
namespace mozilla {
#ifdef TRACK_LOG
# undef TRACK_LOG
#endif
LazyLogModule gForwardedInputTrackLog("ForwardedInputTrack");
#define TRACK_LOG(type, msg) MOZ_LOG(gForwardedInputTrackLog, type, msg)
ForwardedInputTrack::ForwardedInputTrack(TrackRate aSampleRate,
MediaSegment::Type aType)
: ProcessedMediaTrack(
aSampleRate, aType,
aType == MediaSegment::AUDIO
? static_cast<MediaSegment*>(new AudioSegment())
: static_cast<MediaSegment*>(new VideoSegment())) {}
void ForwardedInputTrack::AddInput(MediaInputPort* aPort) {
SetInput(aPort);
ProcessedMediaTrack::AddInput(aPort);
}
void ForwardedInputTrack::RemoveInput(MediaInputPort* aPort) {
TRACK_LOG(LogLevel::Debug,
("ForwardedInputTrack %p removing input %p", this, aPort));
MOZ_ASSERT(aPort == mInputPort);
for (const auto& listener : mOwnedDirectListeners) {
MediaTrack* source = mInputPort->GetSource();
TRACK_LOG(LogLevel::Debug,
("ForwardedInputTrack %p removing direct listener "
"%p. Forwarding to input track %p.",
this, listener.get(), aPort->GetSource()));
source->RemoveDirectListenerImpl(listener);
}
DisabledTrackMode oldMode = CombinedDisabledMode();
mInputDisabledMode = DisabledTrackMode::ENABLED;
NotifyIfDisabledModeChangedFrom(oldMode);
mInputPort = nullptr;
ProcessedMediaTrack::RemoveInput(aPort);
}
void ForwardedInputTrack::SetInput(MediaInputPort* aPort) {
MOZ_ASSERT(aPort);
MOZ_ASSERT(aPort->GetSource());
MOZ_ASSERT(aPort->GetSource()->GetData());
MOZ_ASSERT(!mInputPort);
MOZ_ASSERT(mInputDisabledMode == DisabledTrackMode::ENABLED);
mInputPort = aPort;
for (const auto& listener : mOwnedDirectListeners) {
MediaTrack* source = mInputPort->GetSource();
TRACK_LOG(LogLevel::Debug, ("ForwardedInputTrack %p adding direct listener "
"%p. Forwarding to input track %p.",
this, listener.get(), aPort->GetSource()));
source->AddDirectListenerImpl(do_AddRef(listener));
}
DisabledTrackMode oldMode = CombinedDisabledMode();
mInputDisabledMode = mInputPort->GetSource()->CombinedDisabledMode();
NotifyIfDisabledModeChangedFrom(oldMode);
}
void ForwardedInputTrack::ProcessInputImpl(MediaTrack* aSource,
MediaSegment* aSegment,
GraphTime aFrom, GraphTime aTo,
uint32_t aFlags) {
GraphTime next;
for (GraphTime t = aFrom; t < aTo; t = next) {
MediaInputPort::InputInterval interval =
MediaInputPort::GetNextInputInterval(mInputPort, t);
interval.mEnd = std::min(interval.mEnd, aTo);
const bool inputEnded =
!aSource ||
(aSource->Ended() &&
aSource->GetEnd() <=
aSource->GraphTimeToTrackTimeWithBlocking(interval.mEnd));
TrackTime ticks = interval.mEnd - interval.mStart;
next = interval.mEnd;
if (interval.mStart >= interval.mEnd) {
break;
}
if (inputEnded) {
if (mAutoend && (aFlags & ALLOW_END)) {
mEnded = true;
break;
}
aSegment->AppendNullData(ticks);
TRACK_LOG(LogLevel::Verbose,
("ForwardedInputTrack %p appending %lld ticks "
"of null data (ended input)",
this, (long long)ticks));
} else if (interval.mInputIsBlocked) {
aSegment->AppendNullData(ticks);
TRACK_LOG(LogLevel::Verbose,
("ForwardedInputTrack %p appending %lld ticks "
"of null data (blocked input)",
this, (long long)ticks));
} else if (InMutedCycle()) {
aSegment->AppendNullData(ticks);
} else if (aSource->IsSuspended()) {
aSegment->AppendNullData(ticks);
} else {
MOZ_ASSERT(GetEnd() == GraphTimeToTrackTimeWithBlocking(interval.mStart),
"Samples missing");
TrackTime inputStart =
aSource->GraphTimeToTrackTimeWithBlocking(interval.mStart);
TrackTime inputEnd =
aSource->GraphTimeToTrackTimeWithBlocking(interval.mEnd);
aSegment->AppendSlice(*aSource->GetData(), inputStart, inputEnd);
}
ApplyTrackDisabling(aSegment);
for (const auto& listener : mTrackListeners) {
listener->NotifyQueuedChanges(Graph(), GetEnd(), *aSegment);
}
mSegment->AppendFrom(aSegment);
}
}
void ForwardedInputTrack::ProcessInput(GraphTime aFrom, GraphTime aTo,
uint32_t aFlags) {
TRACE_COMMENT("ForwardedInputTrack::ProcessInput", "ForwardedInputTrack %p",
this);
if (mEnded) {
return;
}
MediaInputPort* input = mInputPort;
MediaTrack* source = input ? input->GetSource() : nullptr;
if (mType == MediaSegment::AUDIO) {
AudioSegment audio;
ProcessInputImpl(source, &audio, aFrom, aTo, aFlags);
} else if (mType == MediaSegment::VIDEO) {
VideoSegment video;
ProcessInputImpl(source, &video, aFrom, aTo, aFlags);
} else {
MOZ_CRASH("Unknown segment type");
}
if (mEnded) {
RemoveAllDirectListenersImpl();
}
}
DisabledTrackMode ForwardedInputTrack::CombinedDisabledMode() const {
if (mDisabledMode == DisabledTrackMode::SILENCE_BLACK ||
mInputDisabledMode == DisabledTrackMode::SILENCE_BLACK) {
return DisabledTrackMode::SILENCE_BLACK;
}
if (mDisabledMode == DisabledTrackMode::SILENCE_FREEZE ||
mInputDisabledMode == DisabledTrackMode::SILENCE_FREEZE) {
return DisabledTrackMode::SILENCE_FREEZE;
}
return DisabledTrackMode::ENABLED;
}
void ForwardedInputTrack::SetDisabledTrackModeImpl(DisabledTrackMode aMode) {
bool enabled = aMode == DisabledTrackMode::ENABLED;
TRACK_LOG(LogLevel::Info, ("ForwardedInputTrack %p was explicitly %s", this,
enabled ? "enabled" : "disabled"));
for (DirectMediaTrackListener* listener : mOwnedDirectListeners) {
DisabledTrackMode oldMode = mDisabledMode;
bool oldEnabled = oldMode == DisabledTrackMode::ENABLED;
if (!oldEnabled && enabled) {
TRACK_LOG(LogLevel::Debug, ("ForwardedInputTrack %p setting "
"direct listener enabled",
this));
listener->DecreaseDisabled(oldMode);
} else if (oldEnabled && !enabled) {
TRACK_LOG(LogLevel::Debug, ("ForwardedInputTrack %p setting "
"direct listener disabled",
this));
listener->IncreaseDisabled(aMode);
}
}
MediaTrack::SetDisabledTrackModeImpl(aMode);
}
void ForwardedInputTrack::OnInputDisabledModeChanged(
DisabledTrackMode aInputMode) {
MOZ_ASSERT(mInputs.Length() == 1);
MOZ_ASSERT(mInputs[0]->GetSource());
DisabledTrackMode oldMode = CombinedDisabledMode();
if (mInputDisabledMode == DisabledTrackMode::SILENCE_BLACK &&
aInputMode == DisabledTrackMode::SILENCE_FREEZE) {
// Don't allow demoting from SILENCE_BLACK to SILENCE_FREEZE. Frames will
// remain black so we shouldn't notify that the track got enabled.
aInputMode = DisabledTrackMode::SILENCE_BLACK;
}
mInputDisabledMode = aInputMode;
NotifyIfDisabledModeChangedFrom(oldMode);
}
uint32_t ForwardedInputTrack::NumberOfChannels() const {
MOZ_DIAGNOSTIC_ASSERT(mSegment->GetType() == MediaSegment::AUDIO);
if (!mInputPort || !mInputPort->GetSource()) {
return GetData<AudioSegment>()->MaxChannelCount();
}
return mInputPort->GetSource()->NumberOfChannels();
}
void ForwardedInputTrack::AddDirectListenerImpl(
already_AddRefed<DirectMediaTrackListener> aListener) {
RefPtr<DirectMediaTrackListener> listener = aListener;
mOwnedDirectListeners.AppendElement(listener);
DisabledTrackMode currentMode = mDisabledMode;
if (currentMode != DisabledTrackMode::ENABLED) {
listener->IncreaseDisabled(currentMode);
}
if (mInputPort) {
MediaTrack* source = mInputPort->GetSource();
TRACK_LOG(LogLevel::Debug, ("ForwardedInputTrack %p adding direct listener "
"%p. Forwarding to input track %p.",
this, listener.get(), source));
source->AddDirectListenerImpl(listener.forget());
}
}
void ForwardedInputTrack::RemoveDirectListenerImpl(
DirectMediaTrackListener* aListener) {
for (size_t i = 0; i < mOwnedDirectListeners.Length(); ++i) {
if (mOwnedDirectListeners[i] == aListener) {
TRACK_LOG(LogLevel::Debug,
("ForwardedInputTrack %p removing direct listener %p", this,
aListener));
DisabledTrackMode currentMode = mDisabledMode;
if (currentMode != DisabledTrackMode::ENABLED) {
// Reset the listener's state.
aListener->DecreaseDisabled(currentMode);
}
mOwnedDirectListeners.RemoveElementAt(i);
break;
}
}
if (mInputPort) {
// Forward to the input
MediaTrack* source = mInputPort->GetSource();
source->RemoveDirectListenerImpl(aListener);
}
}
void ForwardedInputTrack::RemoveAllDirectListenersImpl() {
for (const auto& listener : mOwnedDirectListeners.Clone()) {
RemoveDirectListenerImpl(listener);
}
MOZ_DIAGNOSTIC_ASSERT(mOwnedDirectListeners.IsEmpty());
}
} // namespace mozilla
|