2013-03-27 05:32:51 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef MEDIASTREAMTRACK_H_
|
|
|
|
#define MEDIASTREAMTRACK_H_
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2016-04-06 15:46:56 +03:00
|
|
|
#include "nsError.h"
|
2013-03-27 05:32:51 +04:00
|
|
|
#include "nsID.h"
|
|
|
|
#include "StreamBuffer.h"
|
2015-09-21 01:45:57 +03:00
|
|
|
#include "MediaTrackConstraints.h"
|
2013-03-27 05:32:51 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2013-09-06 00:25:17 +04:00
|
|
|
|
|
|
|
class DOMMediaStream;
|
2016-04-06 15:46:56 +03:00
|
|
|
class MediaEnginePhotoCallback;
|
2013-09-06 00:25:17 +04:00
|
|
|
|
2013-03-27 05:32:51 +04:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class AudioStreamTrack;
|
|
|
|
class VideoStreamTrack;
|
|
|
|
|
2016-04-06 15:46:56 +03:00
|
|
|
/**
|
|
|
|
* Common interface through which a MediaStreamTrack can communicate with its
|
|
|
|
* producer on the main thread.
|
|
|
|
*
|
|
|
|
* Kept alive by a strong ref in all MediaStreamTracks (original and clones)
|
|
|
|
* sharing this source.
|
|
|
|
*/
|
|
|
|
class MediaStreamTrackSource : public nsISupports
|
|
|
|
{
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(MediaStreamTrackSource)
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MediaStreamTrackSource(const bool aIsRemote)
|
|
|
|
: mNrSinks(0), mIsRemote(aIsRemote), mStopped(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MediaStreamTrackSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the source's MediaSourceEnum for usage by PeerConnections.
|
|
|
|
*/
|
|
|
|
virtual MediaSourceEnum GetMediaSource() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether the track is remote or not per the MediaCapture and
|
|
|
|
* Streams spec.
|
|
|
|
*/
|
|
|
|
virtual bool IsRemote() const { return mIsRemote; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forwards a photo request to backends that support it. Other backends return
|
|
|
|
* NS_ERROR_NOT_IMPLEMENTED to indicate that a MediaStreamGraph-based fallback
|
|
|
|
* should be used.
|
|
|
|
*/
|
|
|
|
virtual nsresult TakePhoto(MediaEnginePhotoCallback*) const { return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the source interface when all registered sinks have unregistered.
|
|
|
|
*/
|
|
|
|
virtual void Stop() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by each MediaStreamTrack clone on initialization.
|
|
|
|
*/
|
|
|
|
void RegisterSink()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mStopped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++mNrSinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by each MediaStreamTrack clone on track.Stop().
|
|
|
|
*/
|
|
|
|
void UnregisterSink()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ASSERTION(mNrSinks > 0, "Unmatched UnregisterSink()");
|
|
|
|
--mNrSinks;
|
|
|
|
if (mNrSinks == 0 && !IsRemote()) {
|
|
|
|
Stop();
|
|
|
|
mStopped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~MediaStreamTrackSource()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MediaStreamTrackSource);
|
|
|
|
NS_ASSERTION(mNrSinks == 0, "Some sinks did not unregister");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Number of currently registered sinks.
|
|
|
|
size_t mNrSinks;
|
|
|
|
|
|
|
|
// True if this is a remote track source, i.e., a PeerConnection.
|
|
|
|
const bool mIsRemote;
|
|
|
|
|
|
|
|
// True if this source is not remote, all MediaStreamTrack users have
|
|
|
|
// unregistered from this source and Stop() has been called.
|
|
|
|
bool mStopped;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic implementation of MediaStreamTrackSource that ignores Stop().
|
|
|
|
*/
|
|
|
|
class BasicUnstoppableTrackSource : public MediaStreamTrackSource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit BasicUnstoppableTrackSource(const MediaSourceEnum aMediaSource =
|
|
|
|
MediaSourceEnum::Other)
|
|
|
|
: MediaStreamTrackSource(true), mMediaSource(aMediaSource) {}
|
|
|
|
|
|
|
|
MediaSourceEnum GetMediaSource() const override { return mMediaSource; }
|
|
|
|
|
|
|
|
void Stop() override {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~BasicUnstoppableTrackSource() {}
|
|
|
|
|
|
|
|
const MediaSourceEnum mMediaSource;
|
|
|
|
};
|
|
|
|
|
2013-03-27 05:32:51 +04:00
|
|
|
/**
|
|
|
|
* Class representing a track in a DOMMediaStream.
|
|
|
|
*/
|
2014-04-01 10:13:50 +04:00
|
|
|
class MediaStreamTrack : public DOMEventTargetHelper {
|
2013-03-27 05:32:51 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* aTrackID is the MediaStreamGraph track ID for the track in the
|
|
|
|
* MediaStream owned by aStream.
|
|
|
|
*/
|
2016-04-06 15:46:56 +03:00
|
|
|
MediaStreamTrack(DOMMediaStream* aStream, TrackID aTrackID,
|
|
|
|
const nsString& aLabel,
|
|
|
|
MediaStreamTrackSource* aSource);
|
2013-03-27 05:32:51 +04:00
|
|
|
|
2013-05-01 15:24:16 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaStreamTrack,
|
|
|
|
DOMEventTargetHelper)
|
2013-03-27 05:32:51 +04:00
|
|
|
|
2015-09-30 04:31:54 +03:00
|
|
|
DOMMediaStream* GetParentObject() const { return mOwningStream; }
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override = 0;
|
2013-03-27 05:32:51 +04:00
|
|
|
|
2015-09-30 04:31:54 +03:00
|
|
|
/**
|
|
|
|
* Returns the DOMMediaStream owning this track.
|
|
|
|
*/
|
|
|
|
DOMMediaStream* GetStream() const { return mOwningStream; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the TrackID this stream has in its owning DOMMediaStream's Owned
|
|
|
|
* stream.
|
|
|
|
*/
|
2013-03-27 05:32:51 +04:00
|
|
|
TrackID GetTrackID() const { return mTrackID; }
|
|
|
|
virtual AudioStreamTrack* AsAudioStreamTrack() { return nullptr; }
|
|
|
|
virtual VideoStreamTrack* AsVideoStreamTrack() { return nullptr; }
|
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
virtual void GetKind(nsAString& aKind) = 0;
|
2014-12-10 22:17:09 +03:00
|
|
|
void GetId(nsAString& aID) const;
|
2016-01-16 15:39:00 +03:00
|
|
|
void GetLabel(nsAString& aLabel) { aLabel.Assign(mLabel); }
|
2013-05-30 08:44:43 +04:00
|
|
|
bool Enabled() { return mEnabled; }
|
|
|
|
void SetEnabled(bool aEnabled);
|
2014-08-27 09:03:49 +04:00
|
|
|
void Stop();
|
2015-09-21 01:45:57 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
ApplyConstraints(const dom::MediaTrackConstraints& aConstraints, ErrorResult &aRv);
|
2013-03-27 05:32:51 +04:00
|
|
|
|
2015-09-30 04:32:06 +03:00
|
|
|
bool Ended() const { return mEnded; }
|
2013-03-27 05:32:51 +04:00
|
|
|
// Notifications from the MediaStreamGraph
|
|
|
|
void NotifyEnded() { mEnded = true; }
|
|
|
|
|
2016-04-06 15:46:56 +03:00
|
|
|
MediaStreamTrackSource& GetSource() const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mSource, "The track source is only removed on destruction");
|
|
|
|
return *mSource;
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:17:09 +03:00
|
|
|
// Webrtc allows the remote side to name tracks whatever it wants, and we
|
|
|
|
// need to surface this to content.
|
|
|
|
void AssignId(const nsAString& aID) { mID = aID; }
|
|
|
|
|
2013-03-27 05:32:51 +04:00
|
|
|
protected:
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~MediaStreamTrack();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMMediaStream> mOwningStream;
|
2013-03-27 05:32:51 +04:00
|
|
|
TrackID mTrackID;
|
2016-04-06 15:46:56 +03:00
|
|
|
TrackID mInputTrackID;
|
|
|
|
RefPtr<MediaStreamTrackSource> mSource;
|
2016-01-05 05:16:21 +03:00
|
|
|
RefPtr<MediaStreamTrack> mOriginalTrack;
|
2014-12-10 22:17:09 +03:00
|
|
|
nsString mID;
|
2016-01-16 15:39:00 +03:00
|
|
|
nsString mLabel;
|
2013-03-27 05:32:51 +04:00
|
|
|
bool mEnded;
|
2013-05-30 08:44:43 +04:00
|
|
|
bool mEnabled;
|
2016-04-06 15:46:56 +03:00
|
|
|
const bool mRemote;
|
|
|
|
bool mStopped;
|
2013-03-27 05:32:51 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-03-27 05:32:51 +04:00
|
|
|
|
|
|
|
#endif /* MEDIASTREAMTRACK_H_ */
|