Bug 1286096 - Move AllocationHandle used for cameras to MediaEngineSource base class to reuse for microphones. r=padenot

MozReview-Commit-ID: BcmlKnHhe0o

--HG--
extra : rebase_source : 45342801548033ddbb2c0e060a6c6685bcb910b9
This commit is contained in:
Jan-Ivar Bruaroey 2016-07-12 23:25:07 -04:00
parent 2982c3ee86
commit 9f23bcc615
10 changed files with 193 additions and 201 deletions

View File

@ -96,7 +96,7 @@ protected:
nsString mID;
dom::MediaSourceEnum mMediaSource;
RefPtr<MediaEngineSource> mSource;
RefPtr<MediaEngineSource::BaseAllocationHandle> mAllocationHandle;
RefPtr<MediaEngineSource::AllocationHandle> mAllocationHandle;
public:
dom::MediaSourceEnum GetMediaSource() {
return mMediaSource;

View File

@ -32,7 +32,6 @@ enum {
*/
class MediaEngineVideoSource;
class MediaEngineAudioSource;
class MediaEnginePrefs;
enum MediaEngineState {
kAllocated,
@ -79,148 +78,6 @@ protected:
virtual ~MediaEngine() {}
};
/**
* Callback interface for TakePhoto(). Either PhotoComplete() or PhotoError()
* should be called.
*/
class MediaEnginePhotoCallback {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEnginePhotoCallback)
// aBlob is the image captured by MediaEngineSource. It is
// called on main thread.
virtual nsresult PhotoComplete(already_AddRefed<dom::Blob> aBlob) = 0;
// It is called on main thread. aRv is the error code.
virtual nsresult PhotoError(nsresult aRv) = 0;
protected:
virtual ~MediaEnginePhotoCallback() {}
};
/**
* Common abstract base class for audio and video sources.
*/
class MediaEngineSource : public nsISupports
{
public:
// code inside webrtc.org assumes these sizes; don't use anything smaller
// without verifying it's ok
static const unsigned int kMaxDeviceNameLength = 128;
static const unsigned int kMaxUniqueIdLength = 256;
virtual ~MediaEngineSource() {}
virtual void Shutdown() = 0;
/* Populate the human readable name of this device in the nsAString */
virtual void GetName(nsAString&) const = 0;
/* Populate the UUID of this device in the nsACString */
virtual void GetUUID(nsACString&) const = 0;
class BaseAllocationHandle
{
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BaseAllocationHandle);
protected:
virtual ~BaseAllocationHandle() {}
};
/* Release the device back to the system. */
virtual nsresult Deallocate(BaseAllocationHandle* aHandle) = 0;
/* Start the device and add the track to the provided SourceMediaStream, with
* the provided TrackID. You may start appending data to the track
* immediately after. */
virtual nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) = 0;
/* tell the source if there are any direct listeners attached */
virtual void SetDirectListeners(bool) = 0;
/* Called when the stream wants more data */
virtual void NotifyPull(MediaStreamGraph* aGraph,
SourceMediaStream *aSource,
TrackID aId,
StreamTime aDesiredTime,
const PrincipalHandle& aPrincipalHandle) = 0;
/* Stop the device and release the corresponding MediaStream */
virtual nsresult Stop(SourceMediaStream *aSource, TrackID aID) = 0;
/* Restart with new capability */
virtual nsresult Restart(BaseAllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const char** aOutBadConstraint) = 0;
/* Returns true if a source represents a fake capture device and
* false otherwise
*/
virtual bool IsFake() = 0;
/* Returns the type of media source (camera, microphone, screen, window, etc) */
virtual dom::MediaSourceEnum GetMediaSource() const = 0;
/* If implementation of MediaEngineSource supports TakePhoto(), the picture
* should be return via aCallback object. Otherwise, it returns NS_ERROR_NOT_IMPLEMENTED.
* Currently, only Gonk MediaEngineSource implementation supports it.
*/
virtual nsresult TakePhoto(MediaEnginePhotoCallback* aCallback) = 0;
/* Return false if device is currently allocated or started */
bool IsAvailable() {
if (mState == kAllocated || mState == kStarted) {
return false;
} else {
return true;
}
}
/* It is an error to call Start() before an Allocate(), and Stop() before
* a Start(). Only Allocate() may be called after a Deallocate(). */
/* This call reserves but does not start the device. */
virtual nsresult Allocate(const dom::MediaTrackConstraints &aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
const char** aOutBadConstraint) = 0;
virtual uint32_t GetBestFitnessDistance(
const nsTArray<const NormalizedConstraintSet*>& aConstraintSets,
const nsString& aDeviceId) const = 0;
void GetSettings(dom::MediaTrackSettings& aOutSettings)
{
MOZ_ASSERT(NS_IsMainThread());
aOutSettings = mSettings;
}
protected:
// Only class' own members can be initialized in constructor initializer list.
explicit MediaEngineSource(MediaEngineState aState)
: mState(aState)
#ifdef DEBUG
, mOwningThread(PR_GetCurrentThread())
#endif
{}
void AssertIsOnOwningThread()
{
MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread);
}
MediaEngineState mState;
#ifdef DEBUG
PRThread* mOwningThread;
#endif
// Main-thread only:
dom::MediaTrackSettings mSettings;
};
/**
* Video source and friends.
*/
@ -295,6 +152,162 @@ private:
}
};
/**
* Callback interface for TakePhoto(). Either PhotoComplete() or PhotoError()
* should be called.
*/
class MediaEnginePhotoCallback {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEnginePhotoCallback)
// aBlob is the image captured by MediaEngineSource. It is
// called on main thread.
virtual nsresult PhotoComplete(already_AddRefed<dom::Blob> aBlob) = 0;
// It is called on main thread. aRv is the error code.
virtual nsresult PhotoError(nsresult aRv) = 0;
protected:
virtual ~MediaEnginePhotoCallback() {}
};
/**
* Common abstract base class for audio and video sources.
*/
class MediaEngineSource : public nsISupports
{
public:
// code inside webrtc.org assumes these sizes; don't use anything smaller
// without verifying it's ok
static const unsigned int kMaxDeviceNameLength = 128;
static const unsigned int kMaxUniqueIdLength = 256;
virtual ~MediaEngineSource() {}
virtual void Shutdown() = 0;
/* Populate the human readable name of this device in the nsAString */
virtual void GetName(nsAString&) const = 0;
/* Populate the UUID of this device in the nsACString */
virtual void GetUUID(nsACString&) const = 0;
class AllocationHandle
{
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AllocationHandle);
protected:
~AllocationHandle() {}
public:
AllocationHandle(const dom::MediaTrackConstraints& aConstraints,
const nsACString& aOrigin,
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId)
: mConstraints(aConstraints),
mOrigin(aOrigin),
mPrefs(aPrefs),
mDeviceId(aDeviceId) {}
public:
NormalizedConstraints mConstraints;
nsCString mOrigin;
MediaEnginePrefs mPrefs;
nsString mDeviceId;
};
/* Release the device back to the system. */
virtual nsresult Deallocate(AllocationHandle* aHandle) = 0;
/* Start the device and add the track to the provided SourceMediaStream, with
* the provided TrackID. You may start appending data to the track
* immediately after. */
virtual nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) = 0;
/* tell the source if there are any direct listeners attached */
virtual void SetDirectListeners(bool) = 0;
/* Called when the stream wants more data */
virtual void NotifyPull(MediaStreamGraph* aGraph,
SourceMediaStream *aSource,
TrackID aId,
StreamTime aDesiredTime,
const PrincipalHandle& aPrincipalHandle) = 0;
/* Stop the device and release the corresponding MediaStream */
virtual nsresult Stop(SourceMediaStream *aSource, TrackID aID) = 0;
/* Restart with new capability */
virtual nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const char** aOutBadConstraint) = 0;
/* Returns true if a source represents a fake capture device and
* false otherwise
*/
virtual bool IsFake() = 0;
/* Returns the type of media source (camera, microphone, screen, window, etc) */
virtual dom::MediaSourceEnum GetMediaSource() const = 0;
/* If implementation of MediaEngineSource supports TakePhoto(), the picture
* should be return via aCallback object. Otherwise, it returns NS_ERROR_NOT_IMPLEMENTED.
* Currently, only Gonk MediaEngineSource implementation supports it.
*/
virtual nsresult TakePhoto(MediaEnginePhotoCallback* aCallback) = 0;
/* Return false if device is currently allocated or started */
bool IsAvailable() {
if (mState == kAllocated || mState == kStarted) {
return false;
} else {
return true;
}
}
/* It is an error to call Start() before an Allocate(), and Stop() before
* a Start(). Only Allocate() may be called after a Deallocate(). */
/* This call reserves but does not start the device. */
virtual nsresult Allocate(const dom::MediaTrackConstraints &aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) = 0;
virtual uint32_t GetBestFitnessDistance(
const nsTArray<const NormalizedConstraintSet*>& aConstraintSets,
const nsString& aDeviceId) const = 0;
void GetSettings(dom::MediaTrackSettings& aOutSettings)
{
MOZ_ASSERT(NS_IsMainThread());
aOutSettings = mSettings;
}
protected:
// Only class' own members can be initialized in constructor initializer list.
explicit MediaEngineSource(MediaEngineState aState)
: mState(aState)
#ifdef DEBUG
, mOwningThread(PR_GetCurrentThread())
#endif
{}
void AssertIsOnOwningThread()
{
MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread);
}
MediaEngineState mState;
#ifdef DEBUG
PRThread* mOwningThread;
#endif
// Main-thread only:
dom::MediaTrackSettings mSettings;
};
class MediaEngineVideoSource : public MediaEngineSource
{
public:

View File

@ -84,7 +84,7 @@ MediaEngineDefaultVideoSource::Allocate(const dom::MediaTrackConstraints &aConst
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint)
{
if (mState != kReleased) {
@ -111,7 +111,7 @@ MediaEngineDefaultVideoSource::Allocate(const dom::MediaTrackConstraints &aConst
}
nsresult
MediaEngineDefaultVideoSource::Deallocate(BaseAllocationHandle* aHandle)
MediaEngineDefaultVideoSource::Deallocate(AllocationHandle* aHandle)
{
MOZ_ASSERT(!aHandle);
if (mState != kStopped && mState != kAllocated) {
@ -207,7 +207,7 @@ MediaEngineDefaultVideoSource::Stop(SourceMediaStream *aSource, TrackID aID)
nsresult
MediaEngineDefaultVideoSource::Restart(
BaseAllocationHandle* aHandle,
AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
@ -398,7 +398,7 @@ MediaEngineDefaultAudioSource::Allocate(const dom::MediaTrackConstraints &aConst
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint)
{
if (mState != kReleased) {
@ -420,7 +420,7 @@ MediaEngineDefaultAudioSource::Allocate(const dom::MediaTrackConstraints &aConst
}
nsresult
MediaEngineDefaultAudioSource::Deallocate(BaseAllocationHandle* aHandle)
MediaEngineDefaultAudioSource::Deallocate(AllocationHandle* aHandle)
{
MOZ_ASSERT(!aHandle);
if (mState != kStopped && mState != kAllocated) {
@ -496,7 +496,7 @@ MediaEngineDefaultAudioSource::Stop(SourceMediaStream *aSource, TrackID aID)
}
nsresult
MediaEngineDefaultAudioSource::Restart(BaseAllocationHandle* aHandle,
MediaEngineDefaultAudioSource::Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,

View File

@ -48,12 +48,12 @@ public:
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override;
nsresult Deallocate(BaseAllocationHandle* aHandle) override;
nsresult Deallocate(AllocationHandle* aHandle) override;
nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) override;
nsresult Stop(SourceMediaStream*, TrackID) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
@ -123,12 +123,12 @@ public:
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override;
nsresult Deallocate(BaseAllocationHandle* aHandle) override;
nsresult Deallocate(AllocationHandle* aHandle) override;
nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) override;
nsresult Stop(SourceMediaStream*, TrackID) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,

View File

@ -108,7 +108,7 @@ MediaEngineRemoteVideoSource::Allocate(
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint)
{
LOG((__PRETTY_FUNCTION__));
@ -143,12 +143,12 @@ MediaEngineRemoteVideoSource::Allocate(
}
nsresult
MediaEngineRemoteVideoSource::Deallocate(BaseAllocationHandle* aHandle)
MediaEngineRemoteVideoSource::Deallocate(AllocationHandle* aHandle)
{
LOG((__PRETTY_FUNCTION__));
AssertIsOnOwningThread();
MOZ_ASSERT(aHandle);
RefPtr<AllocationHandle> handle = static_cast<AllocationHandle*>(aHandle);
RefPtr<AllocationHandle> handle = aHandle;
class Comparator {
public:
@ -267,7 +267,7 @@ MediaEngineRemoteVideoSource::Stop(mozilla::SourceMediaStream* aSource,
}
nsresult
MediaEngineRemoteVideoSource::Restart(BaseAllocationHandle* aHandle,
MediaEngineRemoteVideoSource::Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
@ -280,8 +280,7 @@ MediaEngineRemoteVideoSource::Restart(BaseAllocationHandle* aHandle,
}
MOZ_ASSERT(aHandle);
NormalizedConstraints constraints(aConstraints);
return UpdateExisting(static_cast<AllocationHandle*>(aHandle), &constraints,
aPrefs, aDeviceId, aOutBadConstraint);
return UpdateExisting(aHandle, &constraints, aPrefs, aDeviceId, aOutBadConstraint);
}
nsresult

View File

@ -71,36 +71,16 @@ public:
dom::MediaSourceEnum aMediaSource,
const char* aMonitorName = "RemoteVideo.Monitor");
class AllocationHandle : public BaseAllocationHandle
{
public:
AllocationHandle(const dom::MediaTrackConstraints& aConstraints,
const nsACString& aOrigin,
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId)
: mConstraints(aConstraints),
mOrigin(aOrigin),
mPrefs(aPrefs),
mDeviceId(aDeviceId) {}
private:
~AllocationHandle() override {}
public:
NormalizedConstraints mConstraints;
nsCString mOrigin;
MediaEnginePrefs mPrefs;
nsString mDeviceId;
};
nsresult Allocate(const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override;
nsresult Deallocate(BaseAllocationHandle* aHandle) override;
nsresult Deallocate(AllocationHandle* aHandle) override;
nsresult Start(SourceMediaStream*, TrackID, const PrincipalHandle&) override;
nsresult Stop(SourceMediaStream*, TrackID) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,

View File

@ -140,7 +140,7 @@ MediaEngineTabVideoSource::Allocate(const dom::MediaTrackConstraints& aConstrain
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint)
{
// windowId is not a proper constraint, so just read it.
@ -153,7 +153,7 @@ MediaEngineTabVideoSource::Allocate(const dom::MediaTrackConstraints& aConstrain
}
nsresult
MediaEngineTabVideoSource::Restart(BaseAllocationHandle* aHandle,
MediaEngineTabVideoSource::Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const mozilla::MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
@ -184,7 +184,7 @@ MediaEngineTabVideoSource::Restart(BaseAllocationHandle* aHandle,
}
nsresult
MediaEngineTabVideoSource::Deallocate(BaseAllocationHandle* aHandle)
MediaEngineTabVideoSource::Deallocate(AllocationHandle* aHandle)
{
MOZ_ASSERT(!aHandle);
return NS_OK;

View File

@ -26,14 +26,14 @@ class MediaEngineTabVideoSource : public MediaEngineVideoSource, nsIDOMEventList
const mozilla::MediaEnginePrefs&,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override;
nsresult Deallocate(BaseAllocationHandle* aHandle) override;
nsresult Deallocate(AllocationHandle* aHandle) override;
nsresult Start(mozilla::SourceMediaStream*, mozilla::TrackID, const mozilla::PrincipalHandle&) override;
void SetDirectListeners(bool aHasDirectListeners) override {};
void NotifyPull(mozilla::MediaStreamGraph*, mozilla::SourceMediaStream*, mozilla::TrackID, mozilla::StreamTime, const mozilla::PrincipalHandle& aPrincipalHandle) override;
nsresult Stop(mozilla::SourceMediaStream*, mozilla::TrackID) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const mozilla::MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,

View File

@ -78,14 +78,14 @@ public:
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override
{
// Nothing to do here, everything is managed in MediaManager.cpp
aOutHandle = nullptr;
return NS_OK;
}
nsresult Deallocate(BaseAllocationHandle* aHandle) override
nsresult Deallocate(AllocationHandle* aHandle) override
{
// Nothing to do here, everything is managed in MediaManager.cpp
MOZ_ASSERT(!aHandle);
@ -99,7 +99,7 @@ public:
TrackID aId,
const PrincipalHandle& aPrincipalHandle) override;
nsresult Stop(SourceMediaStream* aMediaStream, TrackID aId) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
@ -458,14 +458,14 @@ public:
const MediaEnginePrefs& aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint) override;
nsresult Deallocate(BaseAllocationHandle* aHandle) override;
nsresult Deallocate(AllocationHandle* aHandle) override;
nsresult Start(SourceMediaStream* aStream,
TrackID aID,
const PrincipalHandle& aPrincipalHandle) override;
nsresult Stop(SourceMediaStream* aSource, TrackID aID) override;
nsresult Restart(BaseAllocationHandle* aHandle,
nsresult Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,

View File

@ -223,7 +223,7 @@ MediaEngineWebRTCMicrophoneSource::Allocate(const dom::MediaTrackConstraints &aC
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
const nsACString& aOrigin,
BaseAllocationHandle** aOutHandle,
AllocationHandle** aOutHandle,
const char** aOutBadConstraint)
{
AssertIsOnOwningThread();
@ -265,7 +265,7 @@ MediaEngineWebRTCMicrophoneSource::Allocate(const dom::MediaTrackConstraints &aC
}
nsresult
MediaEngineWebRTCMicrophoneSource::Restart(BaseAllocationHandle* aHandle,
MediaEngineWebRTCMicrophoneSource::Restart(AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,
@ -315,7 +315,7 @@ MediaEngineWebRTCMicrophoneSource::Restart(BaseAllocationHandle* aHandle,
}
nsresult
MediaEngineWebRTCMicrophoneSource::Deallocate(BaseAllocationHandle* aHandle)
MediaEngineWebRTCMicrophoneSource::Deallocate(AllocationHandle* aHandle)
{
AssertIsOnOwningThread();
MOZ_ASSERT(!aHandle);
@ -847,7 +847,7 @@ MediaEngineWebRTCAudioCaptureSource::Stop(SourceMediaStream *aMediaStream,
nsresult
MediaEngineWebRTCAudioCaptureSource::Restart(
BaseAllocationHandle* aHandle,
AllocationHandle* aHandle,
const dom::MediaTrackConstraints& aConstraints,
const MediaEnginePrefs &aPrefs,
const nsString& aDeviceId,