2012-06-03 11:35:15 +04: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 15:53:48 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-06-03 11:35:15 +04:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsObserverService.h"
|
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-06-03 11:35:15 +04:00
|
|
|
#include "nsIDOMNavigatorUserMedia.h"
|
2012-06-15 06:31:55 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-06-03 11:35:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-08-17 20:10:08 +04: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 11:35:15 +04: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 15:53:08 +04:00
|
|
|
, mID(aListenId)
|
2012-06-03 11:35:15 +04:00
|
|
|
, mValid(true) {}
|
|
|
|
|
|
|
|
void
|
|
|
|
Invalidate()
|
|
|
|
{
|
|
|
|
if (!mValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mValid = false;
|
|
|
|
mSource->Stop();
|
|
|
|
mSource->Deallocate();
|
2012-08-17 20:10:08 +04:00
|
|
|
|
|
|
|
nsCOMPtr<GetUserMediaNotificationEvent> event =
|
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STOPPING);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2012-06-03 11:35:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyConsumptionChanged(MediaStreamGraph* aGraph, Consumption aConsuming)
|
|
|
|
{
|
|
|
|
if (aConsuming == CONSUMED) {
|
2012-07-12 15:53:08 +04:00
|
|
|
SourceMediaStream* stream = mStream->GetStream()->AsSourceStream();
|
|
|
|
mSource->Start(stream, mID);
|
2012-08-17 20:10:08 +04:00
|
|
|
nsCOMPtr<GetUserMediaNotificationEvent> event =
|
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STARTING);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
|
2012-06-03 11:35:15 +04: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 19:56:38 +04:00
|
|
|
uint32_t aTrackEvents, const MediaSegment& aQueuedMedia) {}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
|
|
|
private:
|
2012-07-12 15:53:08 +04:00
|
|
|
nsRefPtr<MediaEngineSource> mSource;
|
2012-06-03 11:35:15 +04:00
|
|
|
nsCOMPtr<nsDOMMediaStream> mStream;
|
2012-07-12 15:53:08 +04:00
|
|
|
TrackID mID;
|
2012-06-03 11:35:15 +04:00
|
|
|
bool mValid;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<nsRefPtr<GetUserMediaCallbackMediaStreamListener> > StreamListeners;
|
|
|
|
typedef nsClassHashtable<nsUint64HashKey, StreamListeners> WindowTable;
|
|
|
|
|
2012-09-20 23:54:00 +04: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 11:35:15 +04:00
|
|
|
public:
|
|
|
|
static MediaManager* Get() {
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new MediaManager();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->AddObserver(sSingleton, "xpcom-shutdown", false);
|
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
MediaEngine* GetBackend();
|
|
|
|
WindowTable* GetActiveWindows();
|
|
|
|
|
2012-09-20 23:54:00 +04:00
|
|
|
nsresult GetUserMedia(bool aPrivileged, nsPIDOMWindow* aWindow,
|
|
|
|
nsIMediaStreamOptions* aParams,
|
2012-06-03 11:35:15 +04:00
|
|
|
nsIDOMGetUserMediaSuccessCallback* onSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* onError);
|
2012-09-20 23:54:00 +04:00
|
|
|
nsresult GetUserMediaDevices(nsPIDOMWindow* aWindow,
|
|
|
|
nsIGetUserMediaDevicesSuccessCallback* onSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* onError);
|
2012-08-22 19:56:38 +04:00
|
|
|
void OnNavigation(uint64_t aWindowID);
|
2012-06-03 11:35:15 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Make private because we want only one instance of this class
|
|
|
|
MediaManager()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mBackend(nullptr)
|
|
|
|
, mMediaThread(nullptr) {
|
2012-06-03 11:35:15 +04:00
|
|
|
mActiveWindows.Init();
|
|
|
|
};
|
|
|
|
MediaManager(MediaManager const&) {};
|
|
|
|
|
|
|
|
~MediaManager() {
|
|
|
|
delete mBackend;
|
|
|
|
};
|
|
|
|
|
|
|
|
MediaEngine* mBackend;
|
|
|
|
nsCOMPtr<nsIThread> mMediaThread;
|
|
|
|
WindowTable mActiveWindows;
|
|
|
|
|
|
|
|
static nsRefPtr<MediaManager> sSingleton;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|