2019-04-10 15:14:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "MediaStreamTrackAudioSourceNode.h"
|
|
|
|
#include "mozilla/dom/MediaStreamTrackAudioSourceNodeBinding.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
2019-10-02 13:23:02 +03:00
|
|
|
#include "AudioNodeExternalInputTrack.h"
|
2019-04-10 15:14:17 +03:00
|
|
|
#include "AudioStreamTrack.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2019-04-10 15:14:17 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MediaStreamTrackAudioSourceNode)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaStreamTrackAudioSourceNode)
|
|
|
|
tmp->Destroy();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputTrack)
|
2020-02-25 22:44:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
|
2019-04-10 15:14:17 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
|
|
|
|
MediaStreamTrackAudioSourceNode, AudioNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputTrack)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamTrackAudioSourceNode)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaStreamTrackAudioSourceNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaStreamTrackAudioSourceNode, AudioNode)
|
|
|
|
|
|
|
|
MediaStreamTrackAudioSourceNode::MediaStreamTrackAudioSourceNode(
|
|
|
|
AudioContext* aContext)
|
|
|
|
: AudioNode(aContext, 2, ChannelCountMode::Max,
|
|
|
|
ChannelInterpretation::Speakers),
|
|
|
|
mTrackListener(this) {}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<MediaStreamTrackAudioSourceNode>
|
|
|
|
MediaStreamTrackAudioSourceNode::Create(
|
|
|
|
AudioContext& aAudioContext,
|
|
|
|
const MediaStreamTrackAudioSourceOptions& aOptions, ErrorResult& aRv) {
|
2020-02-12 20:59:41 +03:00
|
|
|
// 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?");
|
2019-04-10 15:14:17 +03:00
|
|
|
|
2019-11-13 16:13:48 +03:00
|
|
|
if (!aOptions.mMediaStreamTrack->Ended() &&
|
|
|
|
aAudioContext.Graph() != aOptions.mMediaStreamTrack->Graph()) {
|
2019-04-10 15:14:17 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> pWindow = aAudioContext.GetParentObject();
|
|
|
|
Document* document = pWindow ? pWindow->GetExtantDoc() : nullptr;
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Web Audio"_ns,
|
|
|
|
document, nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"MediaStreamAudioSourceNodeDifferentRate");
|
2020-02-12 20:59:41 +03:00
|
|
|
// 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.");
|
2019-04-10 15:14:17 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaStreamTrackAudioSourceNode> node =
|
|
|
|
new MediaStreamTrackAudioSourceNode(&aAudioContext);
|
|
|
|
|
|
|
|
node->Init(aOptions.mMediaStreamTrack, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStreamTrackAudioSourceNode::Init(MediaStreamTrack* aMediaStreamTrack,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(aMediaStreamTrack);
|
|
|
|
|
|
|
|
if (!aMediaStreamTrack->AsAudioStreamTrack()) {
|
2020-02-12 20:59:41 +03:00
|
|
|
aRv.ThrowInvalidStateError("\"mediaStreamTrack\" must be an audio track");
|
2019-04-10 15:14:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-13 16:13:48 +03:00
|
|
|
if (aMediaStreamTrack->Ended()) {
|
|
|
|
// The track is ended and will never produce any data. Pretend like this is
|
|
|
|
// fine.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:25:05 +03:00
|
|
|
MarkActive();
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
MediaTrackGraph* graph = Context()->Graph();
|
2019-04-10 15:14:17 +03:00
|
|
|
|
|
|
|
AudioNodeEngine* engine = new MediaStreamTrackAudioSourceNodeEngine(this);
|
2019-10-02 13:23:02 +03:00
|
|
|
mTrack = AudioNodeExternalInputTrack::Create(graph, engine);
|
2019-04-10 15:14:17 +03:00
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
MOZ_ASSERT(mTrack);
|
2019-04-10 15:14:17 +03:00
|
|
|
|
|
|
|
mInputTrack = aMediaStreamTrack;
|
2019-10-02 13:23:02 +03:00
|
|
|
ProcessedMediaTrack* outputTrack =
|
|
|
|
static_cast<ProcessedMediaTrack*>(mTrack.get());
|
|
|
|
mInputPort = mInputTrack->ForwardTrackContentsTo(outputTrack);
|
2019-04-10 15:14:17 +03:00
|
|
|
PrincipalChanged(mInputTrack); // trigger enabling/disabling of the connector
|
|
|
|
mInputTrack->AddPrincipalChangeObserver(this);
|
|
|
|
|
|
|
|
mInputTrack->AddConsumer(&mTrackListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStreamTrackAudioSourceNode::Destroy() {
|
|
|
|
if (mInputTrack) {
|
2019-11-13 16:13:48 +03:00
|
|
|
mTrackListener.NotifyEnded(mInputTrack);
|
2019-04-10 15:14:17 +03:00
|
|
|
mInputTrack->RemovePrincipalChangeObserver(this);
|
|
|
|
mInputTrack->RemoveConsumer(&mTrackListener);
|
|
|
|
mInputTrack = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInputPort) {
|
|
|
|
mInputPort->Destroy();
|
|
|
|
mInputPort = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamTrackAudioSourceNode::~MediaStreamTrackAudioSourceNode() {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the principal. Note that this will be called on the main thread, but
|
2019-10-02 13:23:02 +03:00
|
|
|
* changes will be enacted on the MediaTrackGraph thread. If the principal
|
|
|
|
* change results in the document principal losing access to the track, then
|
2019-04-10 15:14:17 +03:00
|
|
|
* there needs to be other measures in place to ensure that any media that is
|
2019-10-02 13:23:02 +03:00
|
|
|
* governed by the new track principal is not available to the MediaTrackGraph
|
2019-04-10 15:14:17 +03:00
|
|
|
* 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
|
2019-10-02 13:23:02 +03:00
|
|
|
* another change request to the MediaTrackGraph thread that allows the content
|
2019-04-10 15:14:17 +03:00
|
|
|
* under the new principal to flow. This might be unnecessary if the principal
|
|
|
|
* change is changing to be the document principal.
|
|
|
|
*/
|
|
|
|
void MediaStreamTrackAudioSourceNode::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
auto track = static_cast<AudioNodeExternalInputTrack*>(mTrack.get());
|
2019-07-09 00:15:10 +03:00
|
|
|
bool enabled = subsumes;
|
2019-10-02 13:23:02 +03:00
|
|
|
track->SetInt32Parameter(MediaStreamTrackAudioSourceNodeEngine::ENABLE,
|
|
|
|
enabled);
|
2019-04-10 15:14:17 +03:00
|
|
|
fprintf(stderr, "NOW: %s", enabled ? "enabled" : "disabled");
|
|
|
|
|
|
|
|
if (!enabled && doc) {
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, "Web Audio"_ns,
|
|
|
|
doc, nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
CrossOriginErrorString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MediaStreamTrackAudioSourceNode::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
if (mInputPort) {
|
|
|
|
amount += mInputPort->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MediaStreamTrackAudioSourceNode::SizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
void MediaStreamTrackAudioSourceNode::DestroyMediaTrack() {
|
2019-04-10 15:14:17 +03:00
|
|
|
if (mInputPort) {
|
|
|
|
mInputPort->Destroy();
|
|
|
|
mInputPort = nullptr;
|
|
|
|
}
|
2019-10-02 13:23:02 +03:00
|
|
|
AudioNode::DestroyMediaTrack();
|
2019-04-10 15:14:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* MediaStreamTrackAudioSourceNode::WrapObject(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return MediaStreamTrackAudioSourceNode_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|