content/media/webaudio/MediaStreamAudioDestinationNode.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "MediaStreamAudioDestinationNode.h"
michael@0 8 #include "nsIDocument.h"
michael@0 9 #include "mozilla/dom/MediaStreamAudioDestinationNodeBinding.h"
michael@0 10 #include "AudioNodeEngine.h"
michael@0 11 #include "AudioNodeStream.h"
michael@0 12 #include "DOMMediaStream.h"
michael@0 13 #include "TrackUnionStream.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace dom {
michael@0 17
michael@0 18 NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaStreamAudioDestinationNode, AudioNode, mDOMStream)
michael@0 19
michael@0 20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaStreamAudioDestinationNode)
michael@0 21 NS_INTERFACE_MAP_END_INHERITING(AudioNode)
michael@0 22
michael@0 23 NS_IMPL_ADDREF_INHERITED(MediaStreamAudioDestinationNode, AudioNode)
michael@0 24 NS_IMPL_RELEASE_INHERITED(MediaStreamAudioDestinationNode, AudioNode)
michael@0 25
michael@0 26 static const int MEDIA_STREAM_DEST_TRACK_ID = 2;
michael@0 27 static_assert(MEDIA_STREAM_DEST_TRACK_ID != AudioNodeStream::AUDIO_TRACK,
michael@0 28 "MediaStreamAudioDestinationNode::MEDIA_STREAM_DEST_TRACK_ID must be a different value than AudioNodeStream::AUDIO_TRACK");
michael@0 29
michael@0 30 class MediaStreamDestinationEngine : public AudioNodeEngine {
michael@0 31 public:
michael@0 32 MediaStreamDestinationEngine(AudioNode* aNode, ProcessedMediaStream* aOutputStream)
michael@0 33 : AudioNodeEngine(aNode)
michael@0 34 , mOutputStream(aOutputStream)
michael@0 35 {
michael@0 36 MOZ_ASSERT(mOutputStream);
michael@0 37 }
michael@0 38
michael@0 39 virtual void ProcessBlock(AudioNodeStream* aStream,
michael@0 40 const AudioChunk& aInput,
michael@0 41 AudioChunk* aOutput,
michael@0 42 bool* aFinished) MOZ_OVERRIDE
michael@0 43 {
michael@0 44 *aOutput = aInput;
michael@0 45 StreamBuffer::Track* track = mOutputStream->EnsureTrack(MEDIA_STREAM_DEST_TRACK_ID,
michael@0 46 aStream->SampleRate());
michael@0 47 AudioSegment* segment = track->Get<AudioSegment>();
michael@0 48 segment->AppendAndConsumeChunk(aOutput);
michael@0 49 }
michael@0 50
michael@0 51 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
michael@0 52 {
michael@0 53 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 54 }
michael@0 55
michael@0 56 private:
michael@0 57 ProcessedMediaStream* mOutputStream;
michael@0 58 };
michael@0 59
michael@0 60 // This callback is used to ensure that only the audio data for this track is audible
michael@0 61 static bool FilterAudioNodeStreamTrack(StreamBuffer::Track* aTrack)
michael@0 62 {
michael@0 63 return aTrack->GetID() == MEDIA_STREAM_DEST_TRACK_ID;
michael@0 64 }
michael@0 65
michael@0 66 MediaStreamAudioDestinationNode::MediaStreamAudioDestinationNode(AudioContext* aContext)
michael@0 67 : AudioNode(aContext,
michael@0 68 2,
michael@0 69 ChannelCountMode::Explicit,
michael@0 70 ChannelInterpretation::Speakers)
michael@0 71 , mDOMStream(DOMAudioNodeMediaStream::CreateTrackUnionStream(GetOwner(),
michael@0 72 MOZ_THIS_IN_INITIALIZER_LIST(),
michael@0 73 DOMMediaStream::HINT_CONTENTS_AUDIO))
michael@0 74 {
michael@0 75 TrackUnionStream* tus = static_cast<TrackUnionStream*>(mDOMStream->GetStream());
michael@0 76 MOZ_ASSERT(tus == mDOMStream->GetStream()->AsProcessedStream());
michael@0 77 tus->SetTrackIDFilter(FilterAudioNodeStreamTrack);
michael@0 78
michael@0 79 MediaStreamDestinationEngine* engine = new MediaStreamDestinationEngine(this, tus);
michael@0 80 mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::INTERNAL_STREAM);
michael@0 81 mPort = tus->AllocateInputPort(mStream, 0);
michael@0 82
michael@0 83 nsIDocument* doc = aContext->GetParentObject()->GetExtantDoc();
michael@0 84 if (doc) {
michael@0 85 mDOMStream->CombineWithPrincipal(doc->NodePrincipal());
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 size_t
michael@0 90 MediaStreamAudioDestinationNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 91 {
michael@0 92 // Future:
michael@0 93 // - mDOMStream
michael@0 94 size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
michael@0 95 amount += mPort->SizeOfIncludingThis(aMallocSizeOf);
michael@0 96 return amount;
michael@0 97 }
michael@0 98
michael@0 99 size_t
michael@0 100 MediaStreamAudioDestinationNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 101 {
michael@0 102 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 103 }
michael@0 104
michael@0 105 void
michael@0 106 MediaStreamAudioDestinationNode::DestroyMediaStream()
michael@0 107 {
michael@0 108 AudioNode::DestroyMediaStream();
michael@0 109 if (mPort) {
michael@0 110 mPort->Destroy();
michael@0 111 mPort = nullptr;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 JSObject*
michael@0 116 MediaStreamAudioDestinationNode::WrapObject(JSContext* aCx)
michael@0 117 {
michael@0 118 return MediaStreamAudioDestinationNodeBinding::Wrap(aCx, this);
michael@0 119 }
michael@0 120
michael@0 121 }
michael@0 122 }

mercurial