2012-05-11 17:35:36 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
2012-04-30 03:11:34 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-02-15 08:01:58 +00:00
|
|
|
#include "DOMMediaStream.h"
|
2012-04-30 03:11:34 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-02-15 08:04:11 +00:00
|
|
|
#include "mozilla/dom/MediaStreamBinding.h"
|
|
|
|
#include "mozilla/dom/LocalMediaStreamBinding.h"
|
2013-05-21 19:17:47 +00:00
|
|
|
#include "mozilla/dom/AudioNode.h"
|
2014-05-23 09:34:14 +00:00
|
|
|
#include "mozilla/dom/AudioTrack.h"
|
|
|
|
#include "mozilla/dom/AudioTrackList.h"
|
|
|
|
#include "mozilla/dom/VideoTrack.h"
|
|
|
|
#include "mozilla/dom/VideoTrackList.h"
|
2013-02-15 08:04:11 +00:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-04-17 05:18:24 +00:00
|
|
|
#include "AudioStreamTrack.h"
|
|
|
|
#include "VideoStreamTrack.h"
|
2012-04-30 03:11:34 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-04-17 05:18:24 +00:00
|
|
|
using namespace mozilla::dom;
|
2012-04-30 03:11:34 +00:00
|
|
|
|
2013-04-17 05:18:24 +00:00
|
|
|
class DOMMediaStream::StreamListener : public MediaStreamListener {
|
|
|
|
public:
|
2014-09-01 03:50:23 +00:00
|
|
|
explicit StreamListener(DOMMediaStream* aStream)
|
2013-04-17 05:18:24 +00:00
|
|
|
: mStream(aStream)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Main thread only
|
|
|
|
void Forget() { mStream = nullptr; }
|
|
|
|
DOMMediaStream* GetStream() { return mStream; }
|
|
|
|
|
|
|
|
class TrackChange : public nsRunnable {
|
|
|
|
public:
|
|
|
|
TrackChange(StreamListener* aListener,
|
2014-09-18 05:20:43 +00:00
|
|
|
TrackID aID, StreamTime aTrackOffset,
|
2013-04-17 05:18:24 +00:00
|
|
|
uint32_t aEvents, MediaSegment::Type aType)
|
|
|
|
: mListener(aListener), mID(aID), mEvents(aEvents), mType(aType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "main thread only");
|
|
|
|
|
|
|
|
DOMMediaStream* stream = mListener->GetStream();
|
|
|
|
if (!stream) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MediaStreamTrack> track;
|
|
|
|
if (mEvents & MediaStreamListener::TRACK_EVENT_CREATED) {
|
2014-10-13 03:13:36 +00:00
|
|
|
track = stream->BindDOMTrack(mID, mType);
|
|
|
|
if (!track) {
|
|
|
|
stream->CreateDOMTrack(mID, mType);
|
|
|
|
track = stream->BindDOMTrack(mID, mType);
|
|
|
|
}
|
2014-11-24 22:53:00 +00:00
|
|
|
stream->NotifyMediaStreamTrackCreated(track);
|
2013-04-17 05:18:24 +00:00
|
|
|
} else {
|
|
|
|
track = stream->GetDOMTrackFor(mID);
|
|
|
|
}
|
|
|
|
if (mEvents & MediaStreamListener::TRACK_EVENT_ENDED) {
|
2014-10-03 08:18:00 +00:00
|
|
|
if (track) {
|
|
|
|
track->NotifyEnded();
|
|
|
|
stream->NotifyMediaStreamTrackEnded(track);
|
|
|
|
} else {
|
|
|
|
NS_ERROR("track ended but not found");
|
|
|
|
}
|
2013-04-17 05:18:24 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamTime mEndTime;
|
|
|
|
nsRefPtr<StreamListener> mListener;
|
|
|
|
TrackID mID;
|
|
|
|
uint32_t mEvents;
|
|
|
|
MediaSegment::Type mType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify that changes to one of the stream tracks have been queued.
|
|
|
|
* aTrackEvents can be any combination of TRACK_EVENT_CREATED and
|
|
|
|
* TRACK_EVENT_ENDED. aQueuedMedia is the data being added to the track
|
|
|
|
* at aTrackOffset (relative to the start of the stream).
|
|
|
|
* aQueuedMedia can be null if there is no output.
|
|
|
|
*/
|
|
|
|
virtual void NotifyQueuedTrackChanges(MediaStreamGraph* aGraph, TrackID aID,
|
2014-09-18 05:20:43 +00:00
|
|
|
StreamTime aTrackOffset,
|
2013-04-17 05:18:24 +00:00
|
|
|
uint32_t aTrackEvents,
|
2013-05-30 04:44:40 +00:00
|
|
|
const MediaSegment& aQueuedMedia) MOZ_OVERRIDE
|
2013-04-17 05:18:24 +00:00
|
|
|
{
|
|
|
|
if (aTrackEvents & (TRACK_EVENT_CREATED | TRACK_EVENT_ENDED)) {
|
|
|
|
nsRefPtr<TrackChange> runnable =
|
|
|
|
new TrackChange(this, aID, aTrackOffset, aTrackEvents,
|
|
|
|
aQueuedMedia.GetType());
|
2014-11-20 18:10:00 +00:00
|
|
|
aGraph->DispatchToMainThreadAfterStreamStateUpdate(runnable.forget());
|
2013-04-17 05:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// These fields may only be accessed on the main thread
|
|
|
|
DOMMediaStream* mStream;
|
|
|
|
};
|
|
|
|
|
2014-11-01 23:56:10 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMMediaStream)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DOMMediaStream,
|
|
|
|
DOMEventTargetHelper)
|
2014-11-10 15:26:00 +00:00
|
|
|
tmp->Destroy();
|
2014-11-01 23:56:10 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTracks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsumersToKeepAlive)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DOMMediaStream,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTracks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsumersToKeepAlive)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(DOMMediaStream, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DOMMediaStream, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMMediaStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(DOMMediaStream)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(DOMLocalMediaStream, DOMMediaStream)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DOMLocalMediaStream, DOMMediaStream)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(DOMLocalMediaStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(DOMLocalMediaStream)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMAudioNodeMediaStream, DOMMediaStream,
|
|
|
|
mStreamNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(DOMAudioNodeMediaStream, DOMMediaStream)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DOMAudioNodeMediaStream, DOMMediaStream)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMAudioNodeMediaStream)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream)
|
|
|
|
|
2013-04-17 05:18:24 +00:00
|
|
|
DOMMediaStream::DOMMediaStream()
|
2013-05-03 05:07:37 +00:00
|
|
|
: mLogicalStreamStartTime(0),
|
|
|
|
mStream(nullptr), mHintContents(0), mTrackTypesAvailable(0),
|
2014-12-17 17:03:34 +00:00
|
|
|
mNotifiedOfMediaStreamGraphShutdown(false), mCORSMode(CORS_NONE)
|
2013-04-17 05:18:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:01:58 +00:00
|
|
|
DOMMediaStream::~DOMMediaStream()
|
2013-05-01 11:24:16 +00:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::Destroy()
|
2012-04-30 03:11:34 +00:00
|
|
|
{
|
2013-04-17 05:18:24 +00:00
|
|
|
if (mListener) {
|
|
|
|
mListener->Forget();
|
2013-05-01 11:24:16 +00:00
|
|
|
mListener = nullptr;
|
2013-04-17 05:18:24 +00:00
|
|
|
}
|
2012-04-30 03:11:34 +00:00
|
|
|
if (mStream) {
|
|
|
|
mStream->Destroy();
|
2013-05-01 11:24:16 +00:00
|
|
|
mStream = nullptr;
|
2012-04-30 03:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
DOMMediaStream::WrapObject(JSContext* aCx)
|
2012-04-30 03:11:34 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return dom::MediaStreamBinding::Wrap(aCx, this);
|
2012-04-30 03:11:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
double
|
|
|
|
DOMMediaStream::CurrentTime()
|
2013-01-07 02:31:34 +00:00
|
|
|
{
|
2013-05-03 05:07:37 +00:00
|
|
|
if (!mStream) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
2014-06-12 04:44:58 +00:00
|
|
|
return mStream->
|
|
|
|
StreamTimeToSeconds(mStream->GetCurrentTime() - mLogicalStreamStartTime);
|
2013-01-07 02:31:34 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 05:18:37 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::GetAudioTracks(nsTArray<nsRefPtr<AudioStreamTrack> >& aTracks)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
AudioStreamTrack* t = mTracks[i]->AsAudioStreamTrack();
|
|
|
|
if (t) {
|
|
|
|
aTracks.AppendElement(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::GetVideoTracks(nsTArray<nsRefPtr<VideoStreamTrack> >& aTracks)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
VideoStreamTrack* t = mTracks[i]->AsVideoStreamTrack();
|
|
|
|
if (t) {
|
|
|
|
aTracks.AppendElement(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 01:40:54 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::GetTracks(nsTArray<nsRefPtr<MediaStreamTrack> >& aTracks)
|
|
|
|
{
|
|
|
|
aTracks.AppendElements(mTracks);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMMediaStream::HasTrack(const MediaStreamTrack& aTrack) const
|
|
|
|
{
|
|
|
|
return mTracks.Contains(&aTrack);
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
bool
|
|
|
|
DOMMediaStream::IsFinished()
|
2012-10-24 23:21:32 +00:00
|
|
|
{
|
2013-02-15 08:04:11 +00:00
|
|
|
return !mStream || mStream->IsFinished();
|
2012-10-24 23:21:32 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
void
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMMediaStream::InitSourceStream(nsIDOMWindow* aWindow, TrackTypeHints aHintContents)
|
2012-04-30 03:11:40 +00:00
|
|
|
{
|
2013-02-15 08:04:11 +00:00
|
|
|
mWindow = aWindow;
|
|
|
|
SetHintContents(aHintContents);
|
2014-08-25 13:27:25 +00:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance(aHintContents);
|
2013-04-17 05:18:24 +00:00
|
|
|
InitStreamCommon(gm->CreateSourceStream(this));
|
2012-04-30 03:11:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
void
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMMediaStream::InitTrackUnionStream(nsIDOMWindow* aWindow, TrackTypeHints aHintContents)
|
2012-10-24 23:21:32 +00:00
|
|
|
{
|
2013-02-15 08:04:11 +00:00
|
|
|
mWindow = aWindow;
|
|
|
|
SetHintContents(aHintContents);
|
2014-08-25 13:27:25 +00:00
|
|
|
MediaStreamGraph* gm = MediaStreamGraph::GetInstance(aHintContents);
|
2013-04-17 05:18:24 +00:00
|
|
|
InitStreamCommon(gm->CreateTrackUnionStream(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::InitStreamCommon(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
mStream = aStream;
|
|
|
|
|
|
|
|
// Setup track listener
|
|
|
|
mListener = new StreamListener(this);
|
|
|
|
aStream->AddListener(mListener);
|
2012-10-24 23:21:32 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 08:01:58 +00:00
|
|
|
already_AddRefed<DOMMediaStream>
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMMediaStream::CreateSourceStream(nsIDOMWindow* aWindow, TrackTypeHints aHintContents)
|
2012-07-31 12:17:22 +00:00
|
|
|
{
|
2013-02-15 08:01:58 +00:00
|
|
|
nsRefPtr<DOMMediaStream> stream = new DOMMediaStream();
|
2013-02-15 08:04:11 +00:00
|
|
|
stream->InitSourceStream(aWindow, aHintContents);
|
2012-07-31 12:17:22 +00:00
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
already_AddRefed<DOMMediaStream>
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMMediaStream::CreateTrackUnionStream(nsIDOMWindow* aWindow, TrackTypeHints aHintContents)
|
2012-10-24 23:21:32 +00:00
|
|
|
{
|
2013-02-15 08:04:11 +00:00
|
|
|
nsRefPtr<DOMMediaStream> stream = new DOMMediaStream();
|
|
|
|
stream->InitTrackUnionStream(aWindow, aHintContents);
|
2012-10-24 23:21:32 +00:00
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-26 06:07:19 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::SetTrackEnabled(TrackID aTrackID, bool aEnabled)
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
mStream->SetTrackEnabled(aTrackID, aEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:03:49 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::StopTrack(TrackID aTrackID)
|
|
|
|
{
|
|
|
|
if (mStream && mStream->AsSourceStream()) {
|
|
|
|
mStream->AsSourceStream()->EndTrack(aTrackID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 03:11:34 +00:00
|
|
|
bool
|
2013-02-15 08:01:58 +00:00
|
|
|
DOMMediaStream::CombineWithPrincipal(nsIPrincipal* aPrincipal)
|
2012-04-30 03:11:34 +00:00
|
|
|
{
|
2014-04-25 14:30:00 +00:00
|
|
|
bool changed =
|
|
|
|
nsContentUtils::CombineResourcePrincipals(&mPrincipal, aPrincipal);
|
|
|
|
if (changed) {
|
|
|
|
NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::SetPrincipal(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
mPrincipal = aPrincipal;
|
|
|
|
NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:03:34 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::SetCORSMode(CORSMode aCORSMode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mCORSMode = aCORSMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
CORSMode
|
|
|
|
DOMMediaStream::GetCORSMode()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mCORSMode;
|
|
|
|
}
|
|
|
|
|
2014-04-25 14:30:00 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::NotifyPrincipalChanged()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mPrincipalChangeObservers.Length(); ++i) {
|
|
|
|
mPrincipalChangeObservers[i]->PrincipalChanged(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMMediaStream::AddPrincipalChangeObserver(PrincipalChangeObserver* aObserver)
|
|
|
|
{
|
|
|
|
return mPrincipalChangeObservers.AppendElement(aObserver) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMMediaStream::RemovePrincipalChangeObserver(PrincipalChangeObserver* aObserver)
|
|
|
|
{
|
|
|
|
return mPrincipalChangeObservers.RemoveElement(aObserver);
|
2012-04-30 03:11:34 +00:00
|
|
|
}
|
2013-02-15 08:04:11 +00:00
|
|
|
|
2014-10-13 03:13:36 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::SetHintContents(TrackTypeHints aHintContents)
|
|
|
|
{
|
2014-11-24 22:53:00 +00:00
|
|
|
TrackTypeHints oldHintContents = mHintContents;
|
|
|
|
mHintContents |= aHintContents;
|
|
|
|
if (aHintContents & HINT_CONTENTS_VIDEO &&
|
|
|
|
!(oldHintContents & HINT_CONTENTS_VIDEO)) {
|
2014-10-13 03:13:36 +00:00
|
|
|
CreateDOMTrack(kVideoTrack, MediaSegment::VIDEO);
|
|
|
|
}
|
2014-11-24 22:53:00 +00:00
|
|
|
if (aHintContents & HINT_CONTENTS_AUDIO &&
|
|
|
|
!(oldHintContents & HINT_CONTENTS_AUDIO)) {
|
|
|
|
CreateDOMTrack(kAudioTrack, MediaSegment::AUDIO);
|
|
|
|
}
|
2014-10-13 03:13:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 05:18:24 +00:00
|
|
|
MediaStreamTrack*
|
|
|
|
DOMMediaStream::CreateDOMTrack(TrackID aTrackID, MediaSegment::Type aType)
|
|
|
|
{
|
|
|
|
MediaStreamTrack* track;
|
|
|
|
switch (aType) {
|
|
|
|
case MediaSegment::AUDIO:
|
2014-11-24 22:53:00 +00:00
|
|
|
mHintContents |= HINT_CONTENTS_AUDIO;
|
2013-04-17 05:18:24 +00:00
|
|
|
track = new AudioStreamTrack(this, aTrackID);
|
|
|
|
break;
|
|
|
|
case MediaSegment::VIDEO:
|
2014-11-24 22:53:00 +00:00
|
|
|
mHintContents |= HINT_CONTENTS_VIDEO;
|
2013-04-17 05:18:24 +00:00
|
|
|
track = new VideoStreamTrack(this, aTrackID);
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-29 01:38:30 +00:00
|
|
|
MOZ_CRASH("Unhandled track type");
|
2013-04-17 05:18:24 +00:00
|
|
|
}
|
2013-05-05 09:51:47 +00:00
|
|
|
mTracks.AppendElement(track);
|
2013-05-03 05:02:55 +00:00
|
|
|
|
2014-10-13 03:13:36 +00:00
|
|
|
return track;
|
|
|
|
}
|
2013-05-03 05:02:55 +00:00
|
|
|
|
2014-10-13 03:13:36 +00:00
|
|
|
MediaStreamTrack*
|
|
|
|
DOMMediaStream::BindDOMTrack(TrackID aTrackID, MediaSegment::Type aType)
|
|
|
|
{
|
|
|
|
MediaStreamTrack* track = nullptr;
|
2014-12-12 03:54:12 +00:00
|
|
|
bool bindSuccess = false;
|
2014-10-13 03:13:36 +00:00
|
|
|
switch (aType) {
|
|
|
|
case MediaSegment::AUDIO: {
|
|
|
|
for (size_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
track = mTracks[i]->AsAudioStreamTrack();
|
2014-12-12 03:54:12 +00:00
|
|
|
if (track && track->GetTrackID() == aTrackID) {
|
|
|
|
bindSuccess = true;
|
2014-11-08 04:25:22 +00:00
|
|
|
mTrackTypesAvailable |= HINT_CONTENTS_AUDIO;
|
2014-10-13 03:13:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MediaSegment::VIDEO: {
|
|
|
|
for (size_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
track = mTracks[i]->AsVideoStreamTrack();
|
2014-12-12 03:54:12 +00:00
|
|
|
if (track && track->GetTrackID() == aTrackID) {
|
|
|
|
bindSuccess = true;
|
2014-11-08 04:25:22 +00:00
|
|
|
mTrackTypesAvailable |= HINT_CONTENTS_VIDEO;
|
2014-10-13 03:13:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unhandled track type");
|
|
|
|
}
|
2014-12-12 03:54:12 +00:00
|
|
|
if (bindSuccess) {
|
2014-10-13 03:13:36 +00:00
|
|
|
CheckTracksAvailable();
|
|
|
|
}
|
2014-12-12 03:54:12 +00:00
|
|
|
return bindSuccess ? track : nullptr;
|
2013-04-17 05:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamTrack*
|
|
|
|
DOMMediaStream::GetDOMTrackFor(TrackID aTrackID)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
MediaStreamTrack* t = mTracks[i];
|
|
|
|
// We may add streams to our track list that are actually owned by
|
|
|
|
// a different DOMMediaStream. Ignore those.
|
|
|
|
if (t->GetTrackID() == aTrackID && t->GetStream() == this) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-03 05:02:55 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::NotifyMediaStreamGraphShutdown()
|
|
|
|
{
|
|
|
|
// No more tracks will ever be added, so just clear these callbacks now
|
|
|
|
// to prevent leaks.
|
|
|
|
mNotifiedOfMediaStreamGraphShutdown = true;
|
|
|
|
mRunOnTracksAvailable.Clear();
|
2013-07-25 02:07:34 +00:00
|
|
|
|
|
|
|
mConsumersToKeepAlive.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::NotifyStreamStateChanged()
|
|
|
|
{
|
|
|
|
if (IsFinished()) {
|
|
|
|
mConsumersToKeepAlive.Clear();
|
|
|
|
}
|
2013-05-03 05:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::OnTracksAvailable(OnTracksAvailableCallback* aRunnable)
|
|
|
|
{
|
|
|
|
if (mNotifiedOfMediaStreamGraphShutdown) {
|
|
|
|
// No more tracks will ever be added, so just delete the callback now.
|
|
|
|
delete aRunnable;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mRunOnTracksAvailable.AppendElement(aRunnable);
|
|
|
|
CheckTracksAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::CheckTracksAvailable()
|
|
|
|
{
|
2013-11-22 20:14:29 +00:00
|
|
|
if (mTrackTypesAvailable == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-03 05:02:55 +00:00
|
|
|
nsTArray<nsAutoPtr<OnTracksAvailableCallback> > callbacks;
|
|
|
|
callbacks.SwapElements(mRunOnTracksAvailable);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < callbacks.Length(); ++i) {
|
|
|
|
OnTracksAvailableCallback* cb = callbacks[i];
|
|
|
|
if (~mTrackTypesAvailable & cb->GetExpectedTracks()) {
|
|
|
|
// Some expected tracks not available yet. Try this callback again later.
|
|
|
|
*mRunOnTracksAvailable.AppendElement() = callbacks[i].forget();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cb->NotifyTracksAvailable(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 09:34:14 +00:00
|
|
|
already_AddRefed<AudioTrack>
|
|
|
|
DOMMediaStream::CreateAudioTrack(AudioStreamTrack* aStreamTrack)
|
|
|
|
{
|
|
|
|
nsAutoString id;
|
|
|
|
nsAutoString label;
|
|
|
|
aStreamTrack->GetId(id);
|
|
|
|
aStreamTrack->GetLabel(label);
|
|
|
|
|
|
|
|
return MediaTrackList::CreateAudioTrack(id, NS_LITERAL_STRING("main"),
|
|
|
|
label, EmptyString(),
|
|
|
|
aStreamTrack->Enabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<VideoTrack>
|
|
|
|
DOMMediaStream::CreateVideoTrack(VideoStreamTrack* aStreamTrack)
|
|
|
|
{
|
|
|
|
nsAutoString id;
|
|
|
|
nsAutoString label;
|
|
|
|
aStreamTrack->GetId(id);
|
|
|
|
aStreamTrack->GetLabel(label);
|
|
|
|
|
|
|
|
return MediaTrackList::CreateVideoTrack(id, NS_LITERAL_STRING("main"),
|
|
|
|
label, EmptyString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::ConstructMediaTracks(AudioTrackList* aAudioTrackList,
|
|
|
|
VideoTrackList* aVideoTrackList)
|
|
|
|
{
|
|
|
|
if (mHintContents & DOMMediaStream::HINT_CONTENTS_AUDIO) {
|
|
|
|
MediaTrackListListener listener(aAudioTrackList);
|
|
|
|
mMediaTrackListListeners.AppendElement(listener);
|
|
|
|
}
|
|
|
|
if (mHintContents & DOMMediaStream::HINT_CONTENTS_VIDEO) {
|
|
|
|
MediaTrackListListener listener(aVideoTrackList);
|
|
|
|
mMediaTrackListListeners.AppendElement(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
int firstEnabledVideo = -1;
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
if (AudioStreamTrack* t = mTracks[i]->AsAudioStreamTrack()) {
|
|
|
|
nsRefPtr<AudioTrack> track = CreateAudioTrack(t);
|
|
|
|
aAudioTrackList->AddTrack(track);
|
|
|
|
} else if (VideoStreamTrack* t = mTracks[i]->AsVideoStreamTrack()) {
|
|
|
|
nsRefPtr<VideoTrack> track = CreateVideoTrack(t);
|
|
|
|
aVideoTrackList->AddTrack(track);
|
|
|
|
firstEnabledVideo = (t->Enabled() && firstEnabledVideo < 0)
|
|
|
|
? (aVideoTrackList->Length() - 1)
|
|
|
|
: firstEnabledVideo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aVideoTrackList->Length() > 0) {
|
|
|
|
// If media resource does not indicate a particular set of video tracks to
|
|
|
|
// enable, the one that is listed first in the element's videoTracks object
|
|
|
|
// must be selected.
|
|
|
|
int index = firstEnabledVideo >= 0 ? firstEnabledVideo : 0;
|
|
|
|
(*aVideoTrackList)[index]->SetEnabledInternal(true, MediaTrack::FIRE_NO_EVENTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 21:29:29 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::DisconnectTrackListListeners(const AudioTrackList* aAudioTrackList,
|
|
|
|
const VideoTrackList* aVideoTrackList)
|
|
|
|
{
|
|
|
|
for (auto i = mMediaTrackListListeners.Length(); i > 0; ) { // unsigned!
|
|
|
|
--i; // 0 ... Length()-1 range
|
|
|
|
if (mMediaTrackListListeners[i].mMediaTrackList == aAudioTrackList ||
|
|
|
|
mMediaTrackListListeners[i].mMediaTrackList == aVideoTrackList) {
|
|
|
|
mMediaTrackListListeners.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 09:34:14 +00:00
|
|
|
void
|
|
|
|
DOMMediaStream::NotifyMediaStreamTrackCreated(MediaStreamTrack* aTrack)
|
|
|
|
{
|
2014-12-12 03:54:12 +00:00
|
|
|
MOZ_ASSERT(aTrack);
|
|
|
|
|
2014-05-23 09:34:14 +00:00
|
|
|
for (uint32_t i = 0; i < mMediaTrackListListeners.Length(); ++i) {
|
|
|
|
if (AudioStreamTrack* t = aTrack->AsAudioStreamTrack()) {
|
|
|
|
nsRefPtr<AudioTrack> track = CreateAudioTrack(t);
|
|
|
|
mMediaTrackListListeners[i].NotifyMediaTrackCreated(track);
|
|
|
|
} else if (VideoStreamTrack* t = aTrack->AsVideoStreamTrack()) {
|
|
|
|
nsRefPtr<VideoTrack> track = CreateVideoTrack(t);
|
|
|
|
mMediaTrackListListeners[i].NotifyMediaTrackCreated(track);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMMediaStream::NotifyMediaStreamTrackEnded(MediaStreamTrack* aTrack)
|
|
|
|
{
|
2014-12-12 03:54:12 +00:00
|
|
|
MOZ_ASSERT(aTrack);
|
|
|
|
|
2014-05-23 09:34:14 +00:00
|
|
|
nsAutoString id;
|
|
|
|
aTrack->GetId(id);
|
|
|
|
for (uint32_t i = 0; i < mMediaTrackListListeners.Length(); ++i) {
|
|
|
|
mMediaTrackListListeners[i].NotifyMediaTrackEnded(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 08:04:11 +00:00
|
|
|
DOMLocalMediaStream::~DOMLocalMediaStream()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
// Make sure Listeners of this stream know it's going away
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
DOMLocalMediaStream::WrapObject(JSContext* aCx)
|
2013-02-15 08:04:11 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return dom::LocalMediaStreamBinding::Wrap(aCx, this);
|
2013-02-15 08:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DOMLocalMediaStream::Stop()
|
|
|
|
{
|
|
|
|
if (mStream && mStream->AsSourceStream()) {
|
|
|
|
mStream->AsSourceStream()->EndAllTrackAndFinish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMLocalMediaStream>
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMLocalMediaStream::CreateSourceStream(nsIDOMWindow* aWindow,
|
|
|
|
TrackTypeHints aHintContents)
|
2013-02-15 08:04:11 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<DOMLocalMediaStream> stream = new DOMLocalMediaStream();
|
|
|
|
stream->InitSourceStream(aWindow, aHintContents);
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMLocalMediaStream>
|
2013-05-03 05:02:55 +00:00
|
|
|
DOMLocalMediaStream::CreateTrackUnionStream(nsIDOMWindow* aWindow,
|
|
|
|
TrackTypeHints aHintContents)
|
2013-02-15 08:04:11 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<DOMLocalMediaStream> stream = new DOMLocalMediaStream();
|
|
|
|
stream->InitTrackUnionStream(aWindow, aHintContents);
|
|
|
|
return stream.forget();
|
|
|
|
}
|
2013-05-21 19:17:47 +00:00
|
|
|
|
|
|
|
DOMAudioNodeMediaStream::DOMAudioNodeMediaStream(AudioNode* aNode)
|
|
|
|
: mStreamNode(aNode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-24 14:21:21 +00:00
|
|
|
DOMAudioNodeMediaStream::~DOMAudioNodeMediaStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:17:47 +00:00
|
|
|
already_AddRefed<DOMAudioNodeMediaStream>
|
|
|
|
DOMAudioNodeMediaStream::CreateTrackUnionStream(nsIDOMWindow* aWindow,
|
|
|
|
AudioNode* aNode,
|
|
|
|
TrackTypeHints aHintContents)
|
|
|
|
{
|
|
|
|
nsRefPtr<DOMAudioNodeMediaStream> stream = new DOMAudioNodeMediaStream(aNode);
|
|
|
|
stream->InitTrackUnionStream(aWindow, aHintContents);
|
|
|
|
return stream.forget();
|
|
|
|
}
|