2012-06-03 00:35:15 -07: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"
|
|
|
|
|
|
|
|
#include "nsHashKeys.h"
|
2012-07-12 04:53:48 -07:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-06-03 00:35:15 -07:00
|
|
|
#include "nsClassHashtable.h"
|
2012-10-05 17:20:47 -07:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2012-06-03 00:35:15 -07:00
|
|
|
#include "nsObserverService.h"
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-06-03 00:35:15 -07:00
|
|
|
#include "nsIDOMNavigatorUserMedia.h"
|
2012-06-14 19:31:55 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-06-03 00:35:15 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-08-17 09:10:08 -07:00
|
|
|
class GetUserMediaNotificationEvent: public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum GetUserMediaStatus {
|
|
|
|
STARTING,
|
|
|
|
STOPPING
|
|
|
|
};
|
|
|
|
GetUserMediaNotificationEvent(GetUserMediaStatus aStatus)
|
|
|
|
: mStatus(aStatus) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
NS_WARNING("Could not get the Observer service for GetUserMedia recording notification.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (mStatus) {
|
|
|
|
obs->NotifyObservers(nullptr,
|
|
|
|
"recording-device-events",
|
|
|
|
NS_LITERAL_STRING("starting").get());
|
|
|
|
} else {
|
|
|
|
obs->NotifyObservers(nullptr,
|
|
|
|
"recording-device-events",
|
|
|
|
NS_LITERAL_STRING("shutdown").get());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GetUserMediaStatus mStatus;
|
|
|
|
};
|
|
|
|
|
2012-06-03 00:35:15 -07: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:
|
|
|
|
GetUserMediaCallbackMediaStreamListener(MediaEngineSource* aSource,
|
|
|
|
nsDOMMediaStream* aStream, TrackID aListenId)
|
|
|
|
: mSource(aSource)
|
|
|
|
, mStream(aStream)
|
2012-07-12 04:53:08 -07:00
|
|
|
, mID(aListenId)
|
2012-06-03 00:35:15 -07:00
|
|
|
, mValid(true) {}
|
|
|
|
|
|
|
|
void
|
|
|
|
Invalidate()
|
|
|
|
{
|
|
|
|
if (!mValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mValid = false;
|
|
|
|
mSource->Stop();
|
|
|
|
mSource->Deallocate();
|
2012-08-17 09:10:08 -07:00
|
|
|
|
|
|
|
nsCOMPtr<GetUserMediaNotificationEvent> event =
|
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STOPPING);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2012-06-03 00:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyConsumptionChanged(MediaStreamGraph* aGraph, Consumption aConsuming)
|
|
|
|
{
|
|
|
|
if (aConsuming == CONSUMED) {
|
2012-07-12 04:53:08 -07:00
|
|
|
SourceMediaStream* stream = mStream->GetStream()->AsSourceStream();
|
|
|
|
mSource->Start(stream, mID);
|
2012-08-17 09:10:08 -07:00
|
|
|
nsCOMPtr<GetUserMediaNotificationEvent> event =
|
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STARTING);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2012-06-03 00:35:15 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT_CONSUMED
|
|
|
|
Invalidate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyBlockingChanged(MediaStreamGraph* aGraph, Blocking aBlocked) {}
|
|
|
|
void NotifyOutput(MediaStreamGraph* aGraph) {}
|
|
|
|
void NotifyFinished(MediaStreamGraph* aGraph) {}
|
|
|
|
void NotifyQueuedTrackChanges(MediaStreamGraph* aGraph, TrackID aID,
|
|
|
|
TrackRate aTrackRate, TrackTicks aTrackOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aTrackEvents, const MediaSegment& aQueuedMedia) {}
|
2012-06-03 00:35:15 -07:00
|
|
|
|
|
|
|
private:
|
2012-07-12 04:53:08 -07:00
|
|
|
nsRefPtr<MediaEngineSource> mSource;
|
2012-06-03 00:35:15 -07:00
|
|
|
nsCOMPtr<nsDOMMediaStream> mStream;
|
2012-07-12 04:53:08 -07:00
|
|
|
TrackID mID;
|
2012-06-03 00:35:15 -07:00
|
|
|
bool mValid;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<nsRefPtr<GetUserMediaCallbackMediaStreamListener> > StreamListeners;
|
|
|
|
typedef nsClassHashtable<nsUint64HashKey, StreamListeners> WindowTable;
|
|
|
|
|
2012-09-20 12:54:00 -07:00
|
|
|
class MediaDevice : public nsIMediaDevice
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEDIADEVICE
|
|
|
|
|
|
|
|
MediaDevice(MediaEngineVideoSource* aSource) {
|
|
|
|
mSource = aSource;
|
|
|
|
mType.Assign(NS_LITERAL_STRING("video"));
|
|
|
|
mSource->GetName(mName);
|
|
|
|
};
|
|
|
|
MediaDevice(MediaEngineAudioSource* aSource) {
|
|
|
|
mSource = aSource;
|
|
|
|
mType.Assign(NS_LITERAL_STRING("audio"));
|
|
|
|
mSource->GetName(mName);
|
|
|
|
};
|
|
|
|
virtual ~MediaDevice() {};
|
|
|
|
|
|
|
|
MediaEngineSource* GetSource();
|
|
|
|
private:
|
|
|
|
nsString mName;
|
|
|
|
nsString mType;
|
|
|
|
nsRefPtr<MediaEngineSource> mSource;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaManager MOZ_FINAL : public nsIObserver
|
|
|
|
{
|
2012-06-03 00:35:15 -07:00
|
|
|
public:
|
|
|
|
static MediaManager* Get() {
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new MediaManager();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->AddObserver(sSingleton, "xpcom-shutdown", false);
|
2012-10-05 17:20:47 -07:00
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:response:allow", false);
|
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:response:deny", false);
|
2012-06-03 00:35:15 -07:00
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
MediaEngine* GetBackend();
|
|
|
|
WindowTable* GetActiveWindows();
|
|
|
|
|
2012-09-20 12:54:00 -07:00
|
|
|
nsresult GetUserMedia(bool aPrivileged, nsPIDOMWindow* aWindow,
|
|
|
|
nsIMediaStreamOptions* aParams,
|
2012-06-03 00:35:15 -07:00
|
|
|
nsIDOMGetUserMediaSuccessCallback* onSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* onError);
|
2012-09-20 12:54:00 -07:00
|
|
|
nsresult GetUserMediaDevices(nsPIDOMWindow* aWindow,
|
|
|
|
nsIGetUserMediaDevicesSuccessCallback* onSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* onError);
|
2012-08-22 08:56:38 -07:00
|
|
|
void OnNavigation(uint64_t aWindowID);
|
2012-06-03 00:35:15 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Make private because we want only one instance of this class
|
|
|
|
MediaManager()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mBackend(nullptr)
|
|
|
|
, mMediaThread(nullptr) {
|
2012-06-03 00:35:15 -07:00
|
|
|
mActiveWindows.Init();
|
2012-10-05 17:20:47 -07:00
|
|
|
mActiveCallbacks.Init();
|
2012-06-03 00:35:15 -07:00
|
|
|
};
|
|
|
|
MediaManager(MediaManager const&) {};
|
|
|
|
|
|
|
|
~MediaManager() {
|
|
|
|
delete mBackend;
|
|
|
|
};
|
|
|
|
|
|
|
|
MediaEngine* mBackend;
|
|
|
|
nsCOMPtr<nsIThread> mMediaThread;
|
|
|
|
WindowTable mActiveWindows;
|
2012-10-05 17:20:47 -07:00
|
|
|
nsRefPtrHashtable<nsStringHashKey, nsRunnable> mActiveCallbacks;
|
2012-06-03 00:35:15 -07:00
|
|
|
|
|
|
|
static nsRefPtr<MediaManager> sSingleton;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|