2013-05-21 12:17:47 -07:00
|
|
|
/* -*- 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 "MediaStreamAudioDestinationNode.h"
|
2013-07-22 20:54:32 -07:00
|
|
|
#include "nsIDocument.h"
|
2013-05-21 12:17:47 -07:00
|
|
|
#include "mozilla/dom/MediaStreamAudioDestinationNodeBinding.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "DOMMediaStream.h"
|
|
|
|
#include "TrackUnionStream.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(MediaStreamAudioDestinationNode, AudioNode, mDOMStream)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaStreamAudioDestinationNode)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaStreamAudioDestinationNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaStreamAudioDestinationNode, AudioNode)
|
|
|
|
|
|
|
|
static const int MEDIA_STREAM_DEST_TRACK_ID = 2;
|
2013-11-12 05:37:17 -08:00
|
|
|
static_assert(MEDIA_STREAM_DEST_TRACK_ID != AudioNodeStream::AUDIO_TRACK,
|
|
|
|
"MediaStreamAudioDestinationNode::MEDIA_STREAM_DEST_TRACK_ID must be a different value than AudioNodeStream::AUDIO_TRACK");
|
2013-05-21 12:17:47 -07:00
|
|
|
|
|
|
|
class MediaStreamDestinationEngine : public AudioNodeEngine {
|
|
|
|
public:
|
|
|
|
MediaStreamDestinationEngine(AudioNode* aNode, ProcessedMediaStream* aOutputStream)
|
|
|
|
: AudioNodeEngine(aNode)
|
|
|
|
, mOutputStream(aOutputStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOutputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProduceAudioBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
|
|
|
bool* aFinished) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
*aOutput = aInput;
|
|
|
|
StreamBuffer::Track* track = mOutputStream->EnsureTrack(MEDIA_STREAM_DEST_TRACK_ID,
|
|
|
|
aStream->SampleRate());
|
|
|
|
AudioSegment* segment = track->Get<AudioSegment>();
|
|
|
|
segment->AppendAndConsumeChunk(aOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProcessedMediaStream* mOutputStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This callback is used to ensure that only the audio data for this track is audible
|
|
|
|
static bool FilterAudioNodeStreamTrack(StreamBuffer::Track* aTrack)
|
|
|
|
{
|
|
|
|
return aTrack->GetID() == MEDIA_STREAM_DEST_TRACK_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamAudioDestinationNode::MediaStreamAudioDestinationNode(AudioContext* aContext)
|
|
|
|
: AudioNode(aContext,
|
|
|
|
2,
|
|
|
|
ChannelCountMode::Explicit,
|
|
|
|
ChannelInterpretation::Speakers)
|
|
|
|
, mDOMStream(DOMAudioNodeMediaStream::CreateTrackUnionStream(GetOwner(),
|
2013-06-27 04:30:41 -07:00
|
|
|
MOZ_THIS_IN_INITIALIZER_LIST(),
|
2013-05-21 12:17:47 -07:00
|
|
|
DOMMediaStream::HINT_CONTENTS_AUDIO))
|
|
|
|
{
|
|
|
|
TrackUnionStream* tus = static_cast<TrackUnionStream*>(mDOMStream->GetStream());
|
|
|
|
MOZ_ASSERT(tus == mDOMStream->GetStream()->AsProcessedStream());
|
|
|
|
tus->SetTrackIDFilter(FilterAudioNodeStreamTrack);
|
|
|
|
|
|
|
|
MediaStreamDestinationEngine* engine = new MediaStreamDestinationEngine(this, tus);
|
|
|
|
mStream = aContext->Graph()->CreateAudioNodeStream(engine, MediaStreamGraph::INTERNAL_STREAM);
|
|
|
|
mPort = tus->AllocateInputPort(mStream, 0);
|
2013-07-22 20:54:32 -07:00
|
|
|
|
|
|
|
nsIDocument* doc = aContext->GetParentObject()->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
mDOMStream->CombineWithPrincipal(doc->NodePrincipal());
|
|
|
|
}
|
2013-05-21 12:17:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamAudioDestinationNode::DestroyMediaStream()
|
|
|
|
{
|
|
|
|
AudioNode::DestroyMediaStream();
|
|
|
|
if (mPort) {
|
|
|
|
mPort->Destroy();
|
|
|
|
mPort = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaStreamAudioDestinationNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return MediaStreamAudioDestinationNodeBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|