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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "MediaStreamAudioSourceNode.h"
#include "mozilla/dom/MediaStreamAudioSourceNodeBinding.h"
#include "AudioNodeEngine.h"
#include "AudioNodeExternalInputTrack.h"
#include "AudioStreamTrack.h"
#include "mozilla/dom/Document.h"
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "nsID.h"
#include "Tracing.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_CLASS(MediaStreamAudioSourceNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaStreamAudioSourceNode)
tmp->Destroy();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputStream)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputTrack)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaStreamAudioSourceNode,
AudioNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputStream)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputTrack)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamAudioSourceNode)
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
NS_IMPL_ADDREF_INHERITED(MediaStreamAudioSourceNode, AudioNode)
NS_IMPL_RELEASE_INHERITED(MediaStreamAudioSourceNode, AudioNode)
MediaStreamAudioSourceNode::MediaStreamAudioSourceNode(
AudioContext* aContext, TrackChangeBehavior aBehavior)
: AudioNode(aContext, 2, ChannelCountMode::Max,
ChannelInterpretation::Speakers),
mBehavior(aBehavior) {}
/* static */
already_AddRefed<MediaStreamAudioSourceNode> MediaStreamAudioSourceNode::Create(
AudioContext& aAudioContext, const MediaStreamAudioSourceOptions& aOptions,
ErrorResult& aRv) {
// The spec has a pointless check here. See
// https://github.com/WebAudio/web-audio-api/issues/2149
MOZ_RELEASE_ASSERT(!aAudioContext.IsOffline(), "Bindings messed up?");
RefPtr<MediaStreamAudioSourceNode> node =
new MediaStreamAudioSourceNode(&aAudioContext, LockOnTrackPicked);
// aOptions.mMediaStream is not nullable.
node->Init(*aOptions.mMediaStream, aRv);
if (aRv.Failed()) {
return nullptr;
}
return node.forget();
}
void MediaStreamAudioSourceNode::Init(DOMMediaStream& aMediaStream,
ErrorResult& aRv) {
mInputStream = &aMediaStream;
AudioNodeEngine* engine = new MediaStreamAudioSourceNodeEngine(this);
mTrack = AudioNodeExternalInputTrack::Create(Context()->Graph(), engine);
mInputStream->AddConsumerToKeepAlive(ToSupports(this));
mInputStream->RegisterTrackListener(this);
if (mInputStream->Audible()) {
NotifyAudible();
}
AttachToRightTrack(mInputStream, aRv);
}
void MediaStreamAudioSourceNode::Destroy() {
if (mInputStream) {
mInputStream->UnregisterTrackListener(this);
mInputStream = nullptr;
}
DetachFromTrack();
}
MediaStreamAudioSourceNode::~MediaStreamAudioSourceNode() { Destroy(); }
void MediaStreamAudioSourceNode::AttachToTrack(
const RefPtr<MediaStreamTrack>& aTrack, ErrorResult& aRv) {
MOZ_ASSERT(!mInputTrack);
MOZ_ASSERT(aTrack->AsAudioStreamTrack());
MOZ_DIAGNOSTIC_ASSERT(!aTrack->Ended());
if (!mTrack) {
return;
}
if (NS_WARN_IF(Context()->Graph() != aTrack->Graph())) {
nsCOMPtr<nsPIDOMWindowInner> pWindow = Context()->GetParentObject();
Document* document = pWindow ? pWindow->GetExtantDoc() : nullptr;
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Web Audio"_ns,
document, nsContentUtils::eDOM_PROPERTIES,
"MediaStreamAudioSourceNodeDifferentRate");
// This is not a spec-required exception, just a limitation of our
// implementation.
aRv.ThrowNotSupportedError(
"Connecting AudioNodes from AudioContexts with different sample-rate "
"is currently not supported.");
return;
}
mInputTrack = aTrack;
ProcessedMediaTrack* outputTrack =
static_cast<ProcessedMediaTrack*>(mTrack.get());
mInputPort = mInputTrack->ForwardTrackContentsTo(outputTrack);
PrincipalChanged(mInputTrack); // trigger enabling/disabling of the connector
mInputTrack->AddPrincipalChangeObserver(this);
MarkActive();
}
void MediaStreamAudioSourceNode::DetachFromTrack() {
if (mInputTrack) {
mInputTrack->RemovePrincipalChangeObserver(this);
mInputTrack = nullptr;
}
if (mInputPort) {
mInputPort->Destroy();
mInputPort = nullptr;
}
}
static int AudioTrackCompare(const RefPtr<AudioStreamTrack>& aLhs,
const RefPtr<AudioStreamTrack>& aRhs) {
nsAutoStringN<NSID_LENGTH> IDLhs;
nsAutoStringN<NSID_LENGTH> IDRhs;
aLhs->GetId(IDLhs);
aRhs->GetId(IDRhs);
return Compare(NS_ConvertUTF16toUTF8(IDLhs), NS_ConvertUTF16toUTF8(IDRhs));
}
void MediaStreamAudioSourceNode::AttachToRightTrack(
const RefPtr<DOMMediaStream>& aMediaStream, ErrorResult& aRv) {
nsTArray<RefPtr<AudioStreamTrack>> tracks;
aMediaStream->GetAudioTracks(tracks);
if (tracks.IsEmpty() && mBehavior == LockOnTrackPicked) {
aRv.ThrowInvalidStateError("No audio tracks in MediaStream");
return;
}
// Sort the track to have a stable order, on their ID by lexicographic
// ordering on sequences of code unit values.
tracks.Sort(AudioTrackCompare);
for (const RefPtr<AudioStreamTrack>& track : tracks) {
if (mBehavior == FollowChanges) {
if (track->Ended()) {
continue;
}
}
if (!track->Ended()) {
AttachToTrack(track, aRv);
}
return;
}
// There was no track available. We'll allow the node to be garbage collected.
MarkInactive();
}
void MediaStreamAudioSourceNode::NotifyTrackAdded(
const RefPtr<MediaStreamTrack>& aTrack) {
if (mBehavior != FollowChanges) {
return;
}
if (mInputTrack) {
return;
}
if (!aTrack->AsAudioStreamTrack()) {
return;
}
AttachToTrack(aTrack, IgnoreErrors());
}
void MediaStreamAudioSourceNode::NotifyTrackRemoved(
const RefPtr<MediaStreamTrack>& aTrack) {
if (mBehavior == FollowChanges) {
if (aTrack != mInputTrack) {
return;
}
DetachFromTrack();
AttachToRightTrack(mInputStream, IgnoreErrors());
}
}
void MediaStreamAudioSourceNode::NotifyAudible() {
MOZ_ASSERT(mInputStream);
Context()->StartBlockedAudioContextIfAllowed();
}
/**
* Changes the principal. Note that this will be called on the main thread, but
* changes will be enacted on the MediaTrackGraph thread. If the principal
* change results in the document principal losing access to the stream, then
* there needs to be other measures in place to ensure that any media that is
* governed by the new stream principal is not available to the MediaTrackGraph
* before this change completes. Otherwise, a site could get access to
* media that they are not authorized to receive.
*
* One solution is to block the altered content, call this method, then dispatch
* another change request to the MediaTrackGraph thread that allows the content
* under the new principal to flow. This might be unnecessary if the principal
* change is changing to be the document principal.
*/
void MediaStreamAudioSourceNode::PrincipalChanged(
MediaStreamTrack* aMediaStreamTrack) {
MOZ_ASSERT(aMediaStreamTrack == mInputTrack);
bool subsumes = false;
Document* doc = nullptr;
if (nsPIDOMWindowInner* parent = Context()->GetParentObject()) {
doc = parent->GetExtantDoc();
if (doc) {
nsIPrincipal* docPrincipal = doc->NodePrincipal();
nsIPrincipal* trackPrincipal = aMediaStreamTrack->GetPrincipal();
if (!trackPrincipal ||
NS_FAILED(docPrincipal->Subsumes(trackPrincipal, &subsumes))) {
subsumes = false;
}
}
}
auto track = static_cast<AudioNodeExternalInputTrack*>(mTrack.get());
bool enabled = subsumes;
track->SetInt32Parameter(MediaStreamAudioSourceNodeEngine::ENABLE, enabled);
if (!enabled && doc) {
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Web Audio"_ns,
doc, nsContentUtils::eDOM_PROPERTIES,
CrossOriginErrorString());
}
}
size_t MediaStreamAudioSourceNode::SizeOfExcludingThis(
MallocSizeOf aMallocSizeOf) const {
// Future:
// - mInputStream
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
if (mInputPort) {
amount += mInputPort->SizeOfIncludingThis(aMallocSizeOf);
}
return amount;
}
size_t MediaStreamAudioSourceNode::SizeOfIncludingThis(
MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
void MediaStreamAudioSourceNode::DestroyMediaTrack() {
if (mInputPort) {
mInputPort->Destroy();
mInputPort = nullptr;
}
AudioNode::DestroyMediaTrack();
}
JSObject* MediaStreamAudioSourceNode::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return MediaStreamAudioSourceNode_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace mozilla::dom
|