2012-06-03 07:35:15 +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/. */
|
|
|
|
|
|
|
|
#include "MediaEngine.h"
|
|
|
|
#include "mozilla/Services.h"
|
2013-01-25 02:45:36 +00:00
|
|
|
#include "mozilla/unused.h"
|
2012-12-22 08:09:36 +00:00
|
|
|
#include "nsIMediaManager.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
|
|
|
|
#include "nsHashKeys.h"
|
2012-07-12 11:53:48 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
#include "nsClassHashtable.h"
|
2012-10-06 00:20:47 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
#include "nsObserverService.h"
|
2013-03-04 21:02:17 +00:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2012-06-10 23:44:50 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
#include "nsIDOMNavigatorUserMedia.h"
|
2013-01-25 02:45:36 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-06-15 02:31:55 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-02-25 11:50:42 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-12-22 08:09:36 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-09-16 06:34:57 +00:00
|
|
|
#include "mozilla/dom/MediaStreamTrackBinding.h"
|
2012-10-15 20:41:46 +00:00
|
|
|
#include "prlog.h"
|
2013-05-03 05:07:37 +00:00
|
|
|
#include "DOMMediaStream.h"
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-01-29 16:55:09 +00:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
#include "mtransport/runnable_utils.h"
|
|
|
|
#endif
|
|
|
|
|
2013-05-02 12:00:12 +00:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "DOMCameraManager.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-03 07:35:15 +00:00
|
|
|
namespace mozilla {
|
2013-09-16 06:34:57 +00:00
|
|
|
namespace dom {
|
|
|
|
class MediaStreamConstraints;
|
|
|
|
class NavigatorUserMediaSuccessCallback;
|
|
|
|
class NavigatorUserMediaErrorCallback;
|
|
|
|
}
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2012-10-15 20:41:46 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 23:32:10 +00:00
|
|
|
extern PRLogModuleInfo* GetMediaManagerLog();
|
|
|
|
#define MM_LOG(msg) PR_LOG(GetMediaManagerLog(), PR_LOG_DEBUG, msg)
|
2012-10-15 20:41:46 +00:00
|
|
|
#else
|
|
|
|
#define MM_LOG(msg)
|
|
|
|
#endif
|
|
|
|
|
2012-12-31 23:12:15 +00:00
|
|
|
/**
|
|
|
|
* This class is an implementation of MediaStreamListener. This is used
|
|
|
|
* to Start() and Stop() the underlying MediaEngineSource when MediaStreams
|
|
|
|
* are assigned and deassigned in content.
|
|
|
|
*/
|
|
|
|
class GetUserMediaCallbackMediaStreamListener : public MediaStreamListener
|
|
|
|
{
|
|
|
|
public:
|
2013-01-07 02:31:32 +00:00
|
|
|
// Create in an inactive state
|
2012-12-31 23:12:15 +00:00
|
|
|
GetUserMediaCallbackMediaStreamListener(nsIThread *aThread,
|
2013-01-07 02:31:32 +00:00
|
|
|
uint64_t aWindowID)
|
2012-12-31 23:12:15 +00:00
|
|
|
: mMediaThread(aThread)
|
2013-01-08 02:44:43 +00:00
|
|
|
, mWindowID(aWindowID)
|
2013-03-03 02:49:29 +00:00
|
|
|
, mStopped(false)
|
2013-03-03 03:04:40 +00:00
|
|
|
, mFinished(false)
|
2013-01-08 02:44:43 +00:00
|
|
|
, mLock("mozilla::GUMCMSL")
|
|
|
|
, mRemoved(false) {}
|
2012-12-31 23:12:15 +00:00
|
|
|
|
|
|
|
~GetUserMediaCallbackMediaStreamListener()
|
|
|
|
{
|
2013-01-08 02:44:43 +00:00
|
|
|
// It's OK to release mStream on any thread; they have thread-safe
|
2013-01-04 17:16:32 +00:00
|
|
|
// refcounts.
|
2012-12-31 23:12:15 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 02:31:32 +00:00
|
|
|
void Activate(already_AddRefed<SourceMediaStream> aStream,
|
|
|
|
MediaEngineSource* aAudioSource,
|
|
|
|
MediaEngineSource* aVideoSource)
|
|
|
|
{
|
2013-01-08 02:44:43 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2013-03-03 02:49:29 +00:00
|
|
|
mStream = aStream;
|
2013-01-07 02:31:32 +00:00
|
|
|
mAudioSource = aAudioSource;
|
|
|
|
mVideoSource = aVideoSource;
|
|
|
|
mLastEndTimeAudio = 0;
|
|
|
|
mLastEndTimeVideo = 0;
|
|
|
|
|
|
|
|
mStream->AddListener(this);
|
|
|
|
}
|
|
|
|
|
2013-03-03 02:49:29 +00:00
|
|
|
MediaStream *Stream() // Can be used to test if Activate was called
|
2013-01-04 17:16:32 +00:00
|
|
|
{
|
|
|
|
return mStream;
|
|
|
|
}
|
2012-12-31 23:12:15 +00:00
|
|
|
SourceMediaStream *GetSourceStream()
|
|
|
|
{
|
2013-01-09 03:10:09 +00:00
|
|
|
NS_ASSERTION(mStream,"Getting stream from never-activated GUMCMSListener");
|
|
|
|
if (!mStream) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-01-04 17:16:32 +00:00
|
|
|
return mStream->AsSourceStream();
|
2012-12-31 23:12:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 05:53:10 +00:00
|
|
|
// mVideo/AudioSource are set by Activate(), so we assume they're capturing
|
|
|
|
// if set and represent a real capture device.
|
2013-02-27 20:36:06 +00:00
|
|
|
bool CapturingVideo()
|
|
|
|
{
|
2013-03-03 02:49:29 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2014-02-25 11:50:42 +00:00
|
|
|
return mVideoSource && !mStopped &&
|
|
|
|
(!mVideoSource->IsFake() ||
|
|
|
|
Preferences::GetBool("media.navigator.permission.fake"));
|
2013-02-27 20:36:06 +00:00
|
|
|
}
|
|
|
|
bool CapturingAudio()
|
|
|
|
{
|
2013-03-03 02:49:29 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2014-02-25 11:50:42 +00:00
|
|
|
return mAudioSource && !mStopped &&
|
|
|
|
(!mAudioSource->IsFake() ||
|
|
|
|
Preferences::GetBool("media.navigator.permission.fake"));
|
2013-03-03 02:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetStopped()
|
|
|
|
{
|
|
|
|
mStopped = true;
|
2013-02-27 20:36:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 02:31:32 +00:00
|
|
|
// implement in .cpp to avoid circular dependency with MediaOperationRunnable
|
2013-01-08 02:44:43 +00:00
|
|
|
// Can be invoked from EITHER MainThread or MSG thread
|
|
|
|
void Invalidate();
|
2012-12-31 23:12:15 +00:00
|
|
|
|
2013-01-29 16:55:09 +00:00
|
|
|
void
|
|
|
|
AudioConfig(bool aEchoOn, uint32_t aEcho,
|
|
|
|
bool aAgcOn, uint32_t aAGC,
|
|
|
|
bool aNoiseOn, uint32_t aNoise)
|
|
|
|
{
|
|
|
|
if (mAudioSource) {
|
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
// Right now these configs are only of use if webrtc is available
|
|
|
|
RUN_ON_THREAD(mMediaThread,
|
|
|
|
WrapRunnable(nsRefPtr<MediaEngineSource>(mAudioSource), // threadsafe
|
|
|
|
&MediaEngineSource::Config,
|
|
|
|
aEchoOn, aEcho, aAgcOn, aAGC, aNoiseOn, aNoise),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 23:12:15 +00:00
|
|
|
void
|
|
|
|
Remove()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2013-01-08 02:44:43 +00:00
|
|
|
// allow calling even if inactive (!mStream) for easier cleanup
|
2012-12-31 23:12:15 +00:00
|
|
|
// Caller holds strong reference to us, so no death grip required
|
2013-01-08 02:44:43 +00:00
|
|
|
MutexAutoLock lock(mLock); // protect access to mRemoved
|
|
|
|
if (mStream && !mRemoved) {
|
|
|
|
MM_LOG(("Listener removed on purpose, mFinished = %d", (int) mFinished));
|
|
|
|
mRemoved = true; // RemoveListener is async, avoid races
|
2013-01-17 07:38:21 +00:00
|
|
|
// If it's destroyed, don't call - listener will be removed and we'll be notified!
|
|
|
|
if (!mStream->IsDestroyed()) {
|
|
|
|
mStream->RemoveListener(this);
|
|
|
|
}
|
2013-01-08 02:44:43 +00:00
|
|
|
}
|
2012-12-31 23:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy NotifyPull() to sources
|
2013-01-08 02:44:43 +00:00
|
|
|
virtual void
|
|
|
|
NotifyPull(MediaStreamGraph* aGraph, StreamTime aDesiredTime) MOZ_OVERRIDE
|
2012-12-31 23:12:15 +00:00
|
|
|
{
|
|
|
|
// Currently audio sources ignore NotifyPull, but they could
|
|
|
|
// watch it especially for fake audio.
|
|
|
|
if (mAudioSource) {
|
2013-01-04 17:16:32 +00:00
|
|
|
mAudioSource->NotifyPull(aGraph, mStream, kAudioTrack, aDesiredTime, mLastEndTimeAudio);
|
2012-12-31 23:12:15 +00:00
|
|
|
}
|
|
|
|
if (mVideoSource) {
|
2013-01-04 17:16:32 +00:00
|
|
|
mVideoSource->NotifyPull(aGraph, mStream, kVideoTrack, aDesiredTime, mLastEndTimeVideo);
|
2012-12-31 23:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 02:44:43 +00:00
|
|
|
virtual void
|
|
|
|
NotifyFinished(MediaStreamGraph* aGraph) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
NotifyRemoved(MediaStreamGraph* aGraph) MOZ_OVERRIDE;
|
2012-12-31 23:12:15 +00:00
|
|
|
|
|
|
|
private:
|
2013-01-08 02:44:43 +00:00
|
|
|
// Set at construction
|
2012-12-31 23:12:15 +00:00
|
|
|
nsCOMPtr<nsIThread> mMediaThread;
|
2013-01-07 02:31:32 +00:00
|
|
|
uint64_t mWindowID;
|
2013-01-08 02:44:43 +00:00
|
|
|
|
2013-03-03 02:49:29 +00:00
|
|
|
bool mStopped; // MainThread only
|
|
|
|
|
2013-01-08 02:44:43 +00:00
|
|
|
// Set at Activate on MainThread
|
|
|
|
|
|
|
|
// Accessed from MediaStreamGraph thread, MediaManager thread, and MainThread
|
|
|
|
// No locking needed as they're only addrefed except on the MediaManager thread
|
|
|
|
nsRefPtr<MediaEngineSource> mAudioSource; // threadsafe refcnt
|
|
|
|
nsRefPtr<MediaEngineSource> mVideoSource; // threadsafe refcnt
|
|
|
|
nsRefPtr<SourceMediaStream> mStream; // threadsafe refcnt
|
2012-12-31 23:12:15 +00:00
|
|
|
TrackTicks mLastEndTimeAudio;
|
|
|
|
TrackTicks mLastEndTimeVideo;
|
2013-01-08 02:44:43 +00:00
|
|
|
bool mFinished;
|
|
|
|
|
|
|
|
// Accessed from MainThread and MSG thread
|
|
|
|
Mutex mLock; // protects mRemoved access from MainThread
|
|
|
|
bool mRemoved;
|
2012-12-31 23:12:15 +00:00
|
|
|
};
|
|
|
|
|
2013-03-03 02:49:29 +00:00
|
|
|
class GetUserMediaNotificationEvent: public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum GetUserMediaStatus {
|
|
|
|
STARTING,
|
|
|
|
STOPPING
|
|
|
|
};
|
|
|
|
GetUserMediaNotificationEvent(GetUserMediaCallbackMediaStreamListener* aListener,
|
2013-10-23 11:10:42 +00:00
|
|
|
GetUserMediaStatus aStatus,
|
|
|
|
bool aIsAudio, bool aIsVideo, uint64_t aWindowID)
|
|
|
|
: mListener(aListener) , mStatus(aStatus) , mIsAudio(aIsAudio)
|
|
|
|
, mIsVideo(aIsVideo), mWindowID(aWindowID) {}
|
2013-03-03 02:49:29 +00:00
|
|
|
|
2013-05-03 05:07:37 +00:00
|
|
|
GetUserMediaNotificationEvent(GetUserMediaStatus aStatus,
|
|
|
|
already_AddRefed<DOMMediaStream> aStream,
|
2013-10-23 11:10:42 +00:00
|
|
|
DOMMediaStream::OnTracksAvailableCallback* aOnTracksAvailableCallback,
|
2013-12-05 14:29:07 +00:00
|
|
|
bool aIsAudio, bool aIsVideo, uint64_t aWindowID,
|
2013-12-05 21:30:50 +00:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aError)
|
2013-05-03 05:07:37 +00:00
|
|
|
: mStream(aStream), mOnTracksAvailableCallback(aOnTracksAvailableCallback),
|
2013-12-05 14:29:07 +00:00
|
|
|
mStatus(aStatus), mIsAudio(aIsAudio), mIsVideo(aIsVideo), mWindowID(aWindowID),
|
|
|
|
mError(aError) {}
|
2013-05-03 05:07:37 +00:00
|
|
|
virtual ~GetUserMediaNotificationEvent()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-03-03 02:49:29 +00:00
|
|
|
|
2013-08-28 04:14:57 +00:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE;
|
2013-03-03 02:49:29 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<GetUserMediaCallbackMediaStreamListener> mListener; // threadsafe
|
2013-05-03 05:07:37 +00:00
|
|
|
nsRefPtr<DOMMediaStream> mStream;
|
|
|
|
nsAutoPtr<DOMMediaStream::OnTracksAvailableCallback> mOnTracksAvailableCallback;
|
2013-03-03 02:49:29 +00:00
|
|
|
GetUserMediaStatus mStatus;
|
2013-10-23 11:10:42 +00:00
|
|
|
bool mIsAudio;
|
|
|
|
bool mIsVideo;
|
|
|
|
uint64_t mWindowID;
|
2013-12-05 21:30:50 +00:00
|
|
|
nsRefPtr<nsIDOMGetUserMediaErrorCallback> mError;
|
2013-03-03 02:49:29 +00:00
|
|
|
};
|
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
typedef enum {
|
|
|
|
MEDIA_START,
|
2012-12-04 06:17:38 +00:00
|
|
|
MEDIA_STOP
|
2012-10-24 23:21:15 +00:00
|
|
|
} MediaOperation;
|
|
|
|
|
2013-12-05 14:29:07 +00:00
|
|
|
class MediaManager;
|
2014-01-08 21:51:33 +00:00
|
|
|
class GetUserMediaRunnable;
|
2013-12-05 14:29:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an error back to content. The error is the form a string.
|
|
|
|
* Do this only on the main thread. The success callback is also passed here
|
|
|
|
* so it can be released correctly.
|
|
|
|
*/
|
|
|
|
class ErrorCallbackRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ErrorCallbackRunnable(
|
2013-12-05 21:30:50 +00:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaSuccessCallback> aSuccess,
|
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aError,
|
2013-12-05 14:29:07 +00:00
|
|
|
const nsAString& aErrorMsg, uint64_t aWindowID);
|
|
|
|
NS_IMETHOD Run();
|
|
|
|
private:
|
2013-12-05 21:30:50 +00:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaSuccessCallback> mSuccess;
|
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> mError;
|
2013-12-05 14:29:07 +00:00
|
|
|
const nsString mErrorMsg;
|
|
|
|
uint64_t mWindowID;
|
|
|
|
nsRefPtr<MediaManager> mManager; // get ref to this when creating the runnable
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReleaseMediaOperationResource : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReleaseMediaOperationResource(already_AddRefed<DOMMediaStream> aStream,
|
|
|
|
DOMMediaStream::OnTracksAvailableCallback* aOnTracksAvailableCallback):
|
|
|
|
mStream(aStream),
|
|
|
|
mOnTracksAvailableCallback(aOnTracksAvailableCallback) {}
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE {return NS_OK;}
|
|
|
|
private:
|
|
|
|
nsRefPtr<DOMMediaStream> mStream;
|
|
|
|
nsAutoPtr<DOMMediaStream::OnTracksAvailableCallback> mOnTracksAvailableCallback;
|
|
|
|
};
|
|
|
|
|
2012-12-28 20:27:57 +00:00
|
|
|
// Generic class for running long media operations like Start off the main
|
|
|
|
// thread, and then (because nsDOMMediaStreams aren't threadsafe),
|
|
|
|
// ProxyReleases mStream since it's cycle collected.
|
2012-10-24 23:21:15 +00:00
|
|
|
class MediaOperationRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-12-19 07:07:37 +00:00
|
|
|
// so we can send Stop without AddRef()ing from the MSG thread
|
2012-10-24 23:21:15 +00:00
|
|
|
MediaOperationRunnable(MediaOperation aType,
|
2012-12-31 23:12:15 +00:00
|
|
|
GetUserMediaCallbackMediaStreamListener* aListener,
|
2013-05-03 05:07:37 +00:00
|
|
|
DOMMediaStream* aStream,
|
|
|
|
DOMMediaStream::OnTracksAvailableCallback* aOnTracksAvailableCallback,
|
2012-10-24 23:21:15 +00:00
|
|
|
MediaEngineSource* aAudioSource,
|
2013-01-07 02:31:32 +00:00
|
|
|
MediaEngineSource* aVideoSource,
|
2013-10-23 11:10:42 +00:00
|
|
|
bool aNeedsFinish,
|
2013-12-05 14:29:07 +00:00
|
|
|
uint64_t aWindowID,
|
2013-12-05 21:30:50 +00:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aError)
|
2012-10-24 23:21:15 +00:00
|
|
|
: mType(aType)
|
2013-05-03 05:07:37 +00:00
|
|
|
, mStream(aStream)
|
|
|
|
, mOnTracksAvailableCallback(aOnTracksAvailableCallback)
|
2012-10-24 23:21:15 +00:00
|
|
|
, mAudioSource(aAudioSource)
|
|
|
|
, mVideoSource(aVideoSource)
|
2012-12-31 23:12:15 +00:00
|
|
|
, mListener(aListener)
|
2013-01-07 02:31:32 +00:00
|
|
|
, mFinish(aNeedsFinish)
|
2013-10-23 11:10:42 +00:00
|
|
|
, mWindowID(aWindowID)
|
2013-12-05 14:29:07 +00:00
|
|
|
, mError(aError)
|
|
|
|
{}
|
2012-10-24 23:21:15 +00:00
|
|
|
|
2012-12-04 06:17:38 +00:00
|
|
|
~MediaOperationRunnable()
|
|
|
|
{
|
2013-01-04 17:16:32 +00:00
|
|
|
// MediaStreams can be released on any thread.
|
2012-12-04 06:17:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 14:29:07 +00:00
|
|
|
nsresult returnAndCallbackError(nsresult rv, const char* errorLog)
|
|
|
|
{
|
|
|
|
MM_LOG(("%s , rv=%d", errorLog, rv));
|
|
|
|
NS_DispatchToMainThread(new ReleaseMediaOperationResource(mStream.forget(),
|
|
|
|
mOnTracksAvailableCallback.forget()));
|
|
|
|
nsString log;
|
|
|
|
|
|
|
|
log.AssignASCII(errorLog, strlen(errorLog));
|
2013-12-05 21:30:50 +00:00
|
|
|
NS_DispatchToMainThread(new ErrorCallbackRunnable(nullptr, mError.forget(),
|
|
|
|
log, mWindowID));
|
2013-12-05 14:29:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
NS_IMETHOD
|
2013-06-05 16:15:48 +00:00
|
|
|
Run() MOZ_OVERRIDE
|
2012-10-24 23:21:15 +00:00
|
|
|
{
|
2013-01-04 17:16:32 +00:00
|
|
|
SourceMediaStream *source = mListener->GetSourceStream();
|
2012-12-04 06:17:38 +00:00
|
|
|
// No locking between these is required as all the callbacks for the
|
|
|
|
// same MediaStream will occur on the same thread.
|
2013-01-09 03:10:09 +00:00
|
|
|
if (!source) // means the stream was never Activated()
|
|
|
|
return NS_OK;
|
2012-12-31 23:12:15 +00:00
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
switch (mType) {
|
|
|
|
case MEDIA_START:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Never call on main thread");
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-12-31 23:12:15 +00:00
|
|
|
source->SetPullEnabled(true);
|
2012-10-24 23:21:15 +00:00
|
|
|
|
2013-05-03 05:07:37 +00:00
|
|
|
DOMMediaStream::TrackTypeHints expectedTracks = 0;
|
2012-10-24 23:21:15 +00:00
|
|
|
if (mAudioSource) {
|
2012-12-31 23:12:15 +00:00
|
|
|
rv = mAudioSource->Start(source, kAudioTrack);
|
2013-05-03 05:07:37 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
expectedTracks |= DOMMediaStream::HINT_CONTENTS_AUDIO;
|
|
|
|
} else {
|
2013-12-05 14:29:07 +00:00
|
|
|
return returnAndCallbackError(rv, "Starting audio failed");
|
2012-10-24 23:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mVideoSource) {
|
2012-12-31 23:12:15 +00:00
|
|
|
rv = mVideoSource->Start(source, kVideoTrack);
|
2013-05-03 05:07:37 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
expectedTracks |= DOMMediaStream::HINT_CONTENTS_VIDEO;
|
|
|
|
} else {
|
2013-12-05 14:29:07 +00:00
|
|
|
return returnAndCallbackError(rv, "Starting video failed");
|
2012-10-24 23:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 05:07:37 +00:00
|
|
|
mOnTracksAvailableCallback->SetExpectedTracks(expectedTracks);
|
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
MM_LOG(("started all sources"));
|
2013-05-03 05:07:37 +00:00
|
|
|
// Forward mOnTracksAvailableCallback to GetUserMediaNotificationEvent,
|
|
|
|
// because mOnTracksAvailableCallback needs to be added to mStream
|
|
|
|
// on the main thread.
|
2013-12-05 21:30:51 +00:00
|
|
|
nsIRunnable *event =
|
2013-05-03 05:07:37 +00:00
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STARTING,
|
|
|
|
mStream.forget(),
|
2013-10-23 11:10:42 +00:00
|
|
|
mOnTracksAvailableCallback.forget(),
|
|
|
|
mAudioSource != nullptr,
|
|
|
|
mVideoSource != nullptr,
|
2013-12-05 21:30:50 +00:00
|
|
|
mWindowID, mError.forget());
|
2013-12-05 21:30:51 +00:00
|
|
|
// event must always be released on mainthread due to the JS callbacks
|
|
|
|
// in the TracksAvailableCallback
|
2012-10-24 23:21:15 +00:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEDIA_STOP:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Never call on main thread");
|
|
|
|
if (mAudioSource) {
|
2012-12-31 23:12:15 +00:00
|
|
|
mAudioSource->Stop(source, kAudioTrack);
|
2012-10-24 23:21:15 +00:00
|
|
|
mAudioSource->Deallocate();
|
|
|
|
}
|
|
|
|
if (mVideoSource) {
|
2012-12-31 23:12:15 +00:00
|
|
|
mVideoSource->Stop(source, kVideoTrack);
|
2012-10-24 23:21:15 +00:00
|
|
|
mVideoSource->Deallocate();
|
|
|
|
}
|
|
|
|
// Do this after stopping all tracks with EndTrack()
|
2013-01-07 02:31:32 +00:00
|
|
|
if (mFinish) {
|
|
|
|
source->Finish();
|
|
|
|
}
|
2013-12-05 21:30:51 +00:00
|
|
|
nsIRunnable *event =
|
2013-10-23 11:10:42 +00:00
|
|
|
new GetUserMediaNotificationEvent(mListener,
|
|
|
|
GetUserMediaNotificationEvent::STOPPING,
|
|
|
|
mAudioSource != nullptr,
|
|
|
|
mVideoSource != nullptr,
|
|
|
|
mWindowID);
|
2013-12-05 21:30:51 +00:00
|
|
|
// event must always be released on mainthread due to the JS callbacks
|
|
|
|
// in the TracksAvailableCallback
|
2012-10-24 23:21:15 +00:00
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
break;
|
2012-12-28 20:27:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false,"invalid MediaManager operation");
|
|
|
|
break;
|
2012-10-24 23:21:15 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MediaOperation mType;
|
2013-05-03 05:07:37 +00:00
|
|
|
nsRefPtr<DOMMediaStream> mStream;
|
|
|
|
nsAutoPtr<DOMMediaStream::OnTracksAvailableCallback> mOnTracksAvailableCallback;
|
2012-12-04 06:17:38 +00:00
|
|
|
nsRefPtr<MediaEngineSource> mAudioSource; // threadsafe
|
|
|
|
nsRefPtr<MediaEngineSource> mVideoSource; // threadsafe
|
2012-12-31 23:12:15 +00:00
|
|
|
nsRefPtr<GetUserMediaCallbackMediaStreamListener> mListener; // threadsafe
|
2013-01-07 02:31:32 +00:00
|
|
|
bool mFinish;
|
2013-10-23 11:10:42 +00:00
|
|
|
uint64_t mWindowID;
|
2013-12-05 21:30:50 +00:00
|
|
|
nsRefPtr<nsIDOMGetUserMediaErrorCallback> mError;
|
2012-06-03 07:35:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<nsRefPtr<GetUserMediaCallbackMediaStreamListener> > StreamListeners;
|
|
|
|
typedef nsClassHashtable<nsUint64HashKey, StreamListeners> WindowTable;
|
|
|
|
|
2012-09-20 19:54:00 +00:00
|
|
|
class MediaDevice : public nsIMediaDevice
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-09-20 19:54:00 +00:00
|
|
|
NS_DECL_NSIMEDIADEVICE
|
|
|
|
|
2013-09-16 06:34:57 +00:00
|
|
|
MediaDevice(MediaEngineVideoSource* aSource);
|
|
|
|
MediaDevice(MediaEngineAudioSource* aSource);
|
2012-12-09 17:23:19 +00:00
|
|
|
virtual ~MediaDevice() {}
|
2012-09-20 19:54:00 +00:00
|
|
|
|
|
|
|
MediaEngineSource* GetSource();
|
|
|
|
private:
|
|
|
|
nsString mName;
|
|
|
|
nsString mType;
|
2012-12-17 13:28:24 +00:00
|
|
|
nsString mID;
|
2013-09-16 06:34:57 +00:00
|
|
|
bool mHasFacingMode;
|
|
|
|
dom::VideoFacingModeEnum mFacingMode;
|
2012-09-20 19:54:00 +00:00
|
|
|
nsRefPtr<MediaEngineSource> mSource;
|
|
|
|
};
|
|
|
|
|
2012-12-22 08:09:36 +00:00
|
|
|
class MediaManager MOZ_FINAL : public nsIMediaManagerService,
|
|
|
|
public nsIObserver
|
2012-09-20 19:54:00 +00:00
|
|
|
{
|
2012-06-03 07:35:15 +00:00
|
|
|
public:
|
2012-12-22 08:09:36 +00:00
|
|
|
static already_AddRefed<MediaManager> GetInstance();
|
|
|
|
|
2013-02-14 19:59:27 +00:00
|
|
|
// NOTE: never Dispatch(....,NS_DISPATCH_SYNC) to the MediaManager
|
|
|
|
// thread from the MainThread, as we NS_DISPATCH_SYNC to MainThread
|
|
|
|
// from MediaManager thread.
|
2013-03-04 21:02:17 +00:00
|
|
|
static MediaManager* Get();
|
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
static nsIThread* GetThread() {
|
2012-10-26 00:14:47 +00:00
|
|
|
return Get()->mMediaThread;
|
2012-10-24 23:21:15 +00:00
|
|
|
}
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-11-26 06:22:16 +00:00
|
|
|
static nsresult NotifyRecordingStatusChange(nsPIDOMWindow* aWindow,
|
|
|
|
const nsString& aMsg,
|
|
|
|
const bool& aIsAudio,
|
|
|
|
const bool& aIsVideo);
|
|
|
|
|
2013-07-19 02:21:20 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-06-03 07:35:15 +00:00
|
|
|
NS_DECL_NSIOBSERVER
|
2012-12-22 08:09:36 +00:00
|
|
|
NS_DECL_NSIMEDIAMANAGERSERVICE
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-11-06 03:32:42 +00:00
|
|
|
MediaEngine* GetBackend(uint64_t aWindowId = 0);
|
2012-11-13 21:55:02 +00:00
|
|
|
StreamListeners *GetWindowListeners(uint64_t aWindowId) {
|
2012-10-26 00:14:47 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only access windowlist on main thread");
|
|
|
|
|
2012-11-13 21:55:02 +00:00
|
|
|
return mActiveWindows.Get(aWindowId);
|
|
|
|
}
|
2013-01-07 02:31:32 +00:00
|
|
|
void RemoveWindowID(uint64_t aWindowId) {
|
|
|
|
mActiveWindows.Remove(aWindowId);
|
|
|
|
}
|
2012-11-13 21:55:02 +00:00
|
|
|
bool IsWindowStillActive(uint64_t aWindowId) {
|
|
|
|
return !!GetWindowListeners(aWindowId);
|
2012-10-26 00:14:47 +00:00
|
|
|
}
|
2013-01-07 02:31:32 +00:00
|
|
|
// Note: also calls aListener->Remove(), even if inactive
|
|
|
|
void RemoveFromWindowList(uint64_t aWindowID,
|
|
|
|
GetUserMediaCallbackMediaStreamListener *aListener);
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-09-16 06:34:57 +00:00
|
|
|
nsresult GetUserMedia(JSContext* aCx, bool aPrivileged,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
const dom::MediaStreamConstraints& aRawConstraints,
|
2012-06-03 07:35:15 +00:00
|
|
|
nsIDOMGetUserMediaSuccessCallback* onSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* onError);
|
2013-09-16 06:34:57 +00:00
|
|
|
|
2012-09-20 19:54:00 +00:00
|
|
|
nsresult GetUserMediaDevices(nsPIDOMWindow* aWindow,
|
2013-09-16 06:34:57 +00:00
|
|
|
const dom::MediaStreamConstraintsInternal& aConstraints,
|
2012-09-20 19:54:00 +00:00
|
|
|
nsIGetUserMediaDevicesSuccessCallback* onSuccess,
|
2014-02-14 19:32:58 +00:00
|
|
|
nsIDOMGetUserMediaErrorCallback* onError,
|
|
|
|
uint64_t aInnerWindowID = 0);
|
2012-08-22 15:56:38 +00:00
|
|
|
void OnNavigation(uint64_t aWindowID);
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-03-04 21:02:17 +00:00
|
|
|
MediaEnginePrefs mPrefs;
|
|
|
|
|
2012-06-03 07:35:15 +00:00
|
|
|
private:
|
2012-10-26 00:14:47 +00:00
|
|
|
WindowTable *GetActiveWindows() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only access windowlist on main thread");
|
|
|
|
return &mActiveWindows;
|
2012-12-09 17:23:19 +00:00
|
|
|
}
|
2012-10-26 00:14:47 +00:00
|
|
|
|
2013-03-04 21:02:17 +00:00
|
|
|
void GetPref(nsIPrefBranch *aBranch, const char *aPref,
|
|
|
|
const char *aData, int32_t *aVal);
|
2013-12-06 19:34:40 +00:00
|
|
|
void GetPrefBool(nsIPrefBranch *aBranch, const char *aPref,
|
|
|
|
const char *aData, bool *aVal);
|
2013-03-04 21:02:17 +00:00
|
|
|
void GetPrefs(nsIPrefBranch *aBranch, const char *aData);
|
|
|
|
|
2012-06-03 07:35:15 +00:00
|
|
|
// Make private because we want only one instance of this class
|
2013-03-04 21:02:17 +00:00
|
|
|
MediaManager();
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2014-02-09 20:34:40 +00:00
|
|
|
~MediaManager() {}
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2013-02-27 20:36:06 +00:00
|
|
|
nsresult MediaCaptureWindowStateInternal(nsIDOMWindow* aWindow, bool* aVideo,
|
|
|
|
bool* aAudio);
|
|
|
|
|
2013-09-13 08:51:48 +00:00
|
|
|
void StopMediaStreams();
|
|
|
|
|
2012-10-26 00:14:47 +00:00
|
|
|
// ONLY access from MainThread so we don't need to lock
|
|
|
|
WindowTable mActiveWindows;
|
2014-01-08 21:51:33 +00:00
|
|
|
nsRefPtrHashtable<nsStringHashKey, GetUserMediaRunnable> mActiveCallbacks;
|
|
|
|
nsClassHashtable<nsUint64HashKey, nsTArray<nsString>> mCallIds;
|
2012-10-26 00:14:47 +00:00
|
|
|
// Always exists
|
|
|
|
nsCOMPtr<nsIThread> mMediaThread;
|
|
|
|
|
2012-10-24 23:21:15 +00:00
|
|
|
Mutex mMutex;
|
|
|
|
// protected with mMutex:
|
2014-02-09 20:34:40 +00:00
|
|
|
RefPtr<MediaEngine> mBackend;
|
2012-06-03 07:35:15 +00:00
|
|
|
|
2012-12-22 08:09:36 +00:00
|
|
|
static StaticRefPtr<MediaManager> sSingleton;
|
2013-05-02 12:00:12 +00:00
|
|
|
|
2014-02-09 20:34:40 +00:00
|
|
|
#ifdef MOZ_B2G_CAMERA
|
2013-05-02 12:00:12 +00:00
|
|
|
nsRefPtr<nsDOMCameraManager> mCameraManager;
|
|
|
|
#endif
|
2012-06-03 07:35:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|