2014-03-06 05:58:13 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
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 "MediaManager.h"
|
|
|
|
|
|
|
|
#include "MediaStreamGraph.h"
|
2014-08-27 09:03:50 +04:00
|
|
|
#include "mozilla/dom/MediaStreamTrack.h"
|
2013-09-16 10:34:57 +04:00
|
|
|
#include "GetUserMediaRequest.h"
|
2016-06-30 10:07:48 +03:00
|
|
|
#include "MediaStreamListener.h"
|
2016-02-17 00:55:33 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-10-23 15:10:42 +04:00
|
|
|
#include "nsHashPropertyBag.h"
|
2013-09-13 12:51:48 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "nsIAudioManager.h"
|
|
|
|
#endif
|
2012-06-03 11:35:15 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2012-10-06 04:20:47 +04:00
|
|
|
#include "nsIUUIDGenerator.h"
|
2012-06-03 11:35:15 +04:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
2014-02-25 15:50:42 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-06-11 03:44:50 +04:00
|
|
|
#include "nsIPopupWindowManager.h"
|
2012-12-22 12:09:36 +04:00
|
|
|
#include "nsISupportsArray.h"
|
2013-02-28 00:36:06 +04:00
|
|
|
#include "nsIDocShell.h"
|
2013-05-18 00:17:53 +04:00
|
|
|
#include "nsIDocument.h"
|
2013-08-15 22:17:48 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2014-07-31 01:03:20 +04:00
|
|
|
#include "nsIIDNService.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2014-07-11 00:14:57 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-02-23 19:50:48 +03:00
|
|
|
#include "nsPrincipal.h"
|
2015-03-03 17:51:05 +03:00
|
|
|
#include "nsICryptoHash.h"
|
|
|
|
#include "nsICryptoHMAC.h"
|
|
|
|
#include "nsIKeyModule.h"
|
2015-03-03 17:51:05 +03:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsILineInputStream.h"
|
2015-09-17 15:44:50 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-04-18 22:00:16 +04:00
|
|
|
#include "mozilla/Types.h"
|
2014-05-01 14:51:00 +04:00
|
|
|
#include "mozilla/PeerIdentity.h"
|
2013-11-26 10:22:16 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-04-04 13:54:25 +04:00
|
|
|
#include "mozilla/dom/MediaStreamBinding.h"
|
2013-09-16 10:34:57 +04:00
|
|
|
#include "mozilla/dom/MediaStreamTrackBinding.h"
|
|
|
|
#include "mozilla/dom/GetUserMediaRequestBinding.h"
|
2016-02-17 00:55:33 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-03-03 17:51:05 +03:00
|
|
|
#include "mozilla/Base64.h"
|
2015-03-29 20:43:43 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2015-03-03 17:51:05 +03:00
|
|
|
#include "mozilla/media/MediaChild.h"
|
2015-11-19 06:40:41 +03:00
|
|
|
#include "mozilla/media/MediaTaskUtils.h"
|
2014-04-18 23:16:08 +04:00
|
|
|
#include "MediaTrackConstraints.h"
|
2015-03-03 17:51:05 +03:00
|
|
|
#include "VideoUtils.h"
|
2013-10-26 02:13:42 +04:00
|
|
|
#include "Latency.h"
|
2015-04-14 20:59:59 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2015-04-13 21:47:41 +03:00
|
|
|
#include "nsNullPrincipal.h"
|
2015-10-07 18:17:42 +03:00
|
|
|
#include "nsVariant.h"
|
2013-10-26 02:13:42 +04:00
|
|
|
|
2016-02-17 22:23:39 +03:00
|
|
|
// For snprintf
|
|
|
|
#include "mozilla/Snprintf.h"
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
#include "nsJSUtils.h"
|
2012-06-11 03:44:50 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2015-02-23 19:50:48 +03:00
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nss.h"
|
|
|
|
#include "pk11pub.h"
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2012-07-12 15:53:08 +04:00
|
|
|
/* Using WebRTC backend on Desktops (Mac, Windows, Linux), otherwise default */
|
2012-09-29 02:26:00 +04:00
|
|
|
#include "MediaEngineDefault.h"
|
2012-07-12 15:53:08 +04:00
|
|
|
#if defined(MOZ_WEBRTC)
|
|
|
|
#include "MediaEngineWebRTC.h"
|
2014-06-09 22:45:14 +04:00
|
|
|
#include "browser_logging/WebRtcLog.h"
|
2012-07-12 15:53:08 +04:00
|
|
|
#endif
|
|
|
|
|
2014-02-10 00:34:40 +04:00
|
|
|
#ifdef MOZ_B2G
|
2013-09-16 10:39:03 +04:00
|
|
|
#include "MediaPermissionGonk.h"
|
|
|
|
#endif
|
|
|
|
|
2014-08-02 08:30:50 +04:00
|
|
|
#if defined (XP_WIN)
|
|
|
|
#include "mozilla/WindowsVersion.h"
|
2016-01-07 22:57:42 +03:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#include <tchar.h>
|
2014-08-02 08:30:50 +04:00
|
|
|
#endif
|
|
|
|
|
2013-05-03 09:07:37 +04:00
|
|
|
// GetCurrentTime is defined in winbase.h as zero argument macro forwarding to
|
|
|
|
// GetTickCount() and conflicts with MediaStream::GetCurrentTime.
|
|
|
|
#ifdef GetCurrentTime
|
|
|
|
#undef GetCurrentTime
|
|
|
|
#endif
|
|
|
|
|
2014-05-11 13:47:11 +04:00
|
|
|
// XXX Workaround for bug 986974 to maintain the existing broken semantics
|
|
|
|
template<>
|
|
|
|
struct nsIMediaDevice::COMTypeInfo<mozilla::VideoDevice, void> {
|
2014-06-02 16:08:24 +04:00
|
|
|
static const nsIID kIID;
|
2014-05-11 13:47:11 +04:00
|
|
|
};
|
|
|
|
const nsIID nsIMediaDevice::COMTypeInfo<mozilla::VideoDevice, void>::kIID = NS_IMEDIADEVICE_IID;
|
|
|
|
template<>
|
|
|
|
struct nsIMediaDevice::COMTypeInfo<mozilla::AudioDevice, void> {
|
2014-06-02 16:08:24 +04:00
|
|
|
static const nsIID kIID;
|
2014-05-11 13:47:11 +04:00
|
|
|
};
|
|
|
|
const nsIID nsIMediaDevice::COMTypeInfo<mozilla::AudioDevice, void>::kIID = NS_IMEDIADEVICE_IID;
|
|
|
|
|
2015-11-18 23:42:16 +03:00
|
|
|
namespace {
|
|
|
|
already_AddRefed<nsIAsyncShutdownClient> GetShutdownPhase() {
|
2016-02-17 00:55:33 +03:00
|
|
|
nsCOMPtr<nsIAsyncShutdownService> svc = mozilla::services::GetAsyncShutdown();
|
2015-11-18 23:42:16 +03:00
|
|
|
MOZ_RELEASE_ASSERT(svc);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase;
|
|
|
|
nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(shutdownPhase));
|
|
|
|
if (!shutdownPhase) {
|
2016-06-02 01:05:53 +03:00
|
|
|
// We are probably in a content process. We need to do cleanup at
|
|
|
|
// XPCOM shutdown in leakchecking builds.
|
|
|
|
rv = svc->GetXpcomWillShutdown(getter_AddRefs(shutdownPhase));
|
2015-11-18 23:42:16 +03:00
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(shutdownPhase);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return shutdownPhase.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-01-25 06:45:36 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
LogModule*
|
2012-10-30 03:32:10 +04:00
|
|
|
GetMediaManagerLog()
|
|
|
|
{
|
2015-11-15 16:49:01 +03:00
|
|
|
static LazyLogModule sLog("MediaManager");
|
2012-10-30 03:32:10 +04:00
|
|
|
return sLog;
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(msg) MOZ_LOG(GetMediaManagerLog(), mozilla::LogLevel::Debug, msg)
|
2012-10-07 09:34:30 +04:00
|
|
|
|
2016-04-06 15:47:43 +03:00
|
|
|
using dom::BasicUnstoppableTrackSource;
|
2016-02-17 00:55:33 +03:00
|
|
|
using dom::ConstrainDOMStringParameters;
|
2015-05-18 16:52:26 +03:00
|
|
|
using dom::File;
|
2016-02-17 00:55:33 +03:00
|
|
|
using dom::GetUserMediaRequest;
|
|
|
|
using dom::MediaSourceEnum;
|
2015-02-21 01:06:26 +03:00
|
|
|
using dom::MediaStreamConstraints;
|
2014-10-27 22:42:56 +03:00
|
|
|
using dom::MediaStreamError;
|
2016-02-17 00:55:33 +03:00
|
|
|
using dom::MediaStreamTrack;
|
2016-04-06 15:46:56 +03:00
|
|
|
using dom::MediaStreamTrackSource;
|
2016-02-17 00:55:33 +03:00
|
|
|
using dom::MediaTrackConstraints;
|
|
|
|
using dom::MediaTrackConstraintSet;
|
2014-04-18 22:00:16 +04:00
|
|
|
using dom::OwningBooleanOrMediaTrackConstraints;
|
2016-02-17 00:55:33 +03:00
|
|
|
using dom::OwningStringOrStringSequence;
|
|
|
|
using dom::OwningStringOrStringSequenceOrConstrainDOMStringParameters;
|
|
|
|
using dom::Promise;
|
|
|
|
using dom::Sequence;
|
2015-06-18 18:46:36 +03:00
|
|
|
using media::NewRunnableFrom;
|
|
|
|
using media::NewTaskFrom;
|
2016-02-17 00:55:33 +03:00
|
|
|
using media::Pledge;
|
|
|
|
using media::Refcountable;
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2015-05-29 21:28:03 +03:00
|
|
|
static Atomic<bool> sInShutdown;
|
|
|
|
|
2014-08-08 02:12:26 +04:00
|
|
|
static bool
|
|
|
|
HostInDomain(const nsCString &aHost, const nsCString &aPattern)
|
|
|
|
{
|
2015-03-17 04:15:10 +03:00
|
|
|
int32_t patternOffset = 0;
|
|
|
|
int32_t hostOffset = 0;
|
2014-08-08 02:12:26 +04:00
|
|
|
|
|
|
|
// Act on '*.' wildcard in the left-most position in a domain pattern.
|
|
|
|
if (aPattern.Length() > 2 && aPattern[0] == '*' && aPattern[1] == '.') {
|
|
|
|
patternOffset = 2;
|
|
|
|
|
|
|
|
// Ignore the lowest level sub-domain for the hostname.
|
|
|
|
hostOffset = aHost.FindChar('.') + 1;
|
|
|
|
|
|
|
|
if (hostOffset <= 1) {
|
|
|
|
// Reject a match between a wildcard and a TLD or '.foo' form.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCString hostRoot(aHost, hostOffset);
|
|
|
|
return hostRoot.EqualsIgnoreCase(aPattern.BeginReading() + patternOffset);
|
|
|
|
}
|
2014-07-31 01:03:20 +04:00
|
|
|
|
|
|
|
static bool
|
2016-02-23 23:54:00 +03:00
|
|
|
HostIsHttps(nsIURI &docURI)
|
2014-07-31 01:03:20 +04:00
|
|
|
{
|
2014-09-11 02:01:44 +04:00
|
|
|
bool isHttps;
|
2016-02-23 23:54:00 +03:00
|
|
|
nsresult rv = docURI.SchemeIs("https", &isHttps);
|
2014-09-11 02:01:44 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-23 23:54:00 +03:00
|
|
|
return isHttps;
|
|
|
|
}
|
2014-09-11 02:01:44 +04:00
|
|
|
|
2016-02-23 23:54:00 +03:00
|
|
|
static bool
|
|
|
|
HostHasPermission(nsIURI &docURI)
|
|
|
|
{
|
2014-07-31 01:03:20 +04:00
|
|
|
nsAdoptingCString hostName;
|
|
|
|
docURI.GetAsciiHost(hostName); //normalize UTF8 to ASCII equivalent
|
|
|
|
nsAdoptingCString domainWhiteList =
|
2014-07-31 02:07:08 +04:00
|
|
|
Preferences::GetCString("media.getusermedia.screensharing.allowed_domains");
|
2014-07-31 01:03:20 +04:00
|
|
|
domainWhiteList.StripWhitespace();
|
|
|
|
|
|
|
|
if (domainWhiteList.IsEmpty() || hostName.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get UTF8 to ASCII domain name normalization service
|
2016-02-23 23:54:00 +03:00
|
|
|
nsresult rv;
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsIIDNService> idnService =
|
|
|
|
do_GetService("@mozilla.org/network/idn-service;1", &rv);
|
2014-07-31 01:03:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:39:07 +04:00
|
|
|
uint32_t begin = 0;
|
|
|
|
uint32_t end = 0;
|
2014-07-31 01:03:20 +04:00
|
|
|
nsCString domainName;
|
2014-08-08 02:12:26 +04:00
|
|
|
/*
|
2014-07-31 01:03:20 +04:00
|
|
|
Test each domain name in the comma separated list
|
|
|
|
after converting from UTF8 to ASCII. Each domain
|
2014-09-05 17:43:39 +04:00
|
|
|
must match exactly or have a single leading '*.' wildcard
|
2014-07-31 01:03:20 +04:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
end = domainWhiteList.FindChar(',', begin);
|
2014-08-08 16:39:07 +04:00
|
|
|
if (end == (uint32_t)-1) {
|
2014-07-31 01:03:20 +04:00
|
|
|
// Last or only domain name in the comma separated list
|
|
|
|
end = domainWhiteList.Length();
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:30:49 +03:00
|
|
|
rv = idnService->ConvertUTF8toACE(Substring(domainWhiteList, begin, end - begin),
|
2014-07-31 01:03:20 +04:00
|
|
|
domainName);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-08-08 02:12:26 +04:00
|
|
|
if (HostInDomain(hostName, domainName)) {
|
2014-07-31 01:03:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to convert UTF-8 host to ASCII");
|
|
|
|
}
|
|
|
|
|
|
|
|
begin = end + 1;
|
|
|
|
} while (end < domainWhiteList.Length());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-30 10:07:48 +03: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:
|
|
|
|
// Create in an inactive state
|
|
|
|
GetUserMediaCallbackMediaStreamListener(base::Thread *aThread,
|
|
|
|
uint64_t aWindowID,
|
|
|
|
const PrincipalHandle& aPrincipalHandle)
|
|
|
|
: mMediaThread(aThread)
|
|
|
|
, mMainThreadCheck(nullptr)
|
|
|
|
, mWindowID(aWindowID)
|
|
|
|
, mPrincipalHandle(aPrincipalHandle)
|
|
|
|
, mStopped(false)
|
|
|
|
, mFinished(false)
|
|
|
|
, mRemoved(false)
|
|
|
|
, mAudioStopped(false)
|
|
|
|
, mVideoStopped(false) {}
|
|
|
|
|
|
|
|
~GetUserMediaCallbackMediaStreamListener()
|
|
|
|
{
|
|
|
|
Unused << mMediaThread;
|
|
|
|
// It's OK to release mStream on any thread; they have thread-safe
|
|
|
|
// refcounts.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Activate(already_AddRefed<SourceMediaStream> aStream,
|
|
|
|
AudioDevice* aAudioDevice,
|
|
|
|
VideoDevice* aVideoDevice)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mMainThreadCheck = PR_GetCurrentThread();
|
|
|
|
mStream = aStream;
|
|
|
|
mAudioDevice = aAudioDevice;
|
|
|
|
mVideoDevice = aVideoDevice;
|
|
|
|
|
|
|
|
mStream->AddListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStream *Stream() // Can be used to test if Activate was called
|
|
|
|
{
|
|
|
|
return mStream;
|
|
|
|
}
|
|
|
|
SourceMediaStream *GetSourceStream()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mStream,"Getting stream from never-activated GUMCMSListener");
|
|
|
|
if (!mStream) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mStream->AsSourceStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopSharing();
|
|
|
|
|
|
|
|
void StopTrack(TrackID aID);
|
|
|
|
|
|
|
|
typedef media::Pledge<bool, dom::MediaStreamError*> PledgeVoid;
|
|
|
|
|
|
|
|
already_AddRefed<PledgeVoid>
|
|
|
|
ApplyConstraintsToTrack(nsPIDOMWindowInner* aWindow,
|
|
|
|
TrackID aID,
|
|
|
|
const dom::MediaTrackConstraints& aConstraints);
|
|
|
|
|
|
|
|
// mVideo/AudioDevice are set by Activate(), so we assume they're capturing
|
|
|
|
// if set and represent a real capture device.
|
|
|
|
bool CapturingVideo()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVideoDevice && !mStopped &&
|
|
|
|
!mVideoDevice->GetSource()->IsAvailable() &&
|
|
|
|
mVideoDevice->GetMediaSource() == dom::MediaSourceEnum::Camera &&
|
|
|
|
(!mVideoDevice->GetSource()->IsFake() ||
|
|
|
|
Preferences::GetBool("media.navigator.permission.fake"));
|
|
|
|
}
|
|
|
|
bool CapturingAudio()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mAudioDevice && !mStopped &&
|
|
|
|
!mAudioDevice->GetSource()->IsAvailable() &&
|
|
|
|
(!mAudioDevice->GetSource()->IsFake() ||
|
|
|
|
Preferences::GetBool("media.navigator.permission.fake"));
|
|
|
|
}
|
|
|
|
bool CapturingScreen()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVideoDevice && !mStopped &&
|
|
|
|
!mVideoDevice->GetSource()->IsAvailable() &&
|
|
|
|
mVideoDevice->GetMediaSource() == dom::MediaSourceEnum::Screen;
|
|
|
|
}
|
|
|
|
bool CapturingWindow()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVideoDevice && !mStopped &&
|
|
|
|
!mVideoDevice->GetSource()->IsAvailable() &&
|
|
|
|
mVideoDevice->GetMediaSource() == dom::MediaSourceEnum::Window;
|
|
|
|
}
|
|
|
|
bool CapturingApplication()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVideoDevice && !mStopped &&
|
|
|
|
!mVideoDevice->GetSource()->IsAvailable() &&
|
|
|
|
mVideoDevice->GetMediaSource() == dom::MediaSourceEnum::Application;
|
|
|
|
}
|
|
|
|
bool CapturingBrowser()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVideoDevice && !mStopped &&
|
|
|
|
mVideoDevice->GetSource()->IsAvailable() &&
|
|
|
|
mVideoDevice->GetMediaSource() == dom::MediaSourceEnum::Browser;
|
|
|
|
}
|
|
|
|
|
2016-06-20 07:38:25 +03:00
|
|
|
void GetSettings(dom::MediaTrackSettings& aOutSettings)
|
|
|
|
{
|
|
|
|
if (mVideoDevice) {
|
|
|
|
mVideoDevice->GetSource()->GetSettings(aOutSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 10:07:48 +03:00
|
|
|
// implement in .cpp to avoid circular dependency with MediaOperationTask
|
|
|
|
// Can be invoked from EITHER MainThread or MSG thread
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioConfig(bool aEchoOn, uint32_t aEcho,
|
|
|
|
bool aAgcOn, uint32_t aAGC,
|
|
|
|
bool aNoiseOn, uint32_t aNoise,
|
|
|
|
int32_t aPlayoutDelay);
|
|
|
|
|
|
|
|
void
|
|
|
|
Remove()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// allow calling even if inactive (!mStream) for easier cleanup
|
|
|
|
// Caller holds strong reference to us, so no death grip required
|
|
|
|
if (mStream && !mRemoved) {
|
|
|
|
MM_LOG(("Listener removed on purpose, mFinished = %d", (int) mFinished));
|
|
|
|
mRemoved = true; // RemoveListener is async, avoid races
|
|
|
|
// If it's destroyed, don't call - listener will be removed and we'll be notified!
|
|
|
|
if (!mStream->IsDestroyed()) {
|
|
|
|
mStream->RemoveListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy NotifyPull() to sources
|
|
|
|
void
|
|
|
|
NotifyPull(MediaStreamGraph* aGraph, StreamTime aDesiredTime) override
|
|
|
|
{
|
|
|
|
// Currently audio sources ignore NotifyPull, but they could
|
|
|
|
// watch it especially for fake audio.
|
|
|
|
if (mAudioDevice) {
|
|
|
|
mAudioDevice->GetSource()->NotifyPull(aGraph, mStream, kAudioTrack,
|
|
|
|
aDesiredTime, mPrincipalHandle);
|
|
|
|
}
|
|
|
|
if (mVideoDevice) {
|
|
|
|
mVideoDevice->GetSource()->NotifyPull(aGraph, mStream, kVideoTrack,
|
|
|
|
aDesiredTime, mPrincipalHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyEvent(MediaStreamGraph* aGraph,
|
|
|
|
MediaStreamGraphEvent aEvent) override
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
|
|
|
|
switch (aEvent) {
|
|
|
|
case MediaStreamGraphEvent::EVENT_FINISHED:
|
|
|
|
rv = NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
NS_ASSERTION(false, "Mainthread not available; running on current thread");
|
|
|
|
// Ensure this really *was* MainThread (NS_GetCurrentThread won't work)
|
|
|
|
MOZ_RELEASE_ASSERT(mMainThreadCheck == PR_GetCurrentThread());
|
|
|
|
NotifyFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thread->Dispatch(NewRunnableMethod(this, &GetUserMediaCallbackMediaStreamListener::NotifyFinished),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
case MediaStreamGraphEvent::EVENT_REMOVED:
|
|
|
|
rv = NS_GetMainThread(getter_AddRefs(thread));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
NS_ASSERTION(false, "Mainthread not available; running on current thread");
|
|
|
|
// Ensure this really *was* MainThread (NS_GetCurrentThread won't work)
|
|
|
|
MOZ_RELEASE_ASSERT(mMainThreadCheck == PR_GetCurrentThread());
|
|
|
|
NotifyRemoved();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
thread->Dispatch(NewRunnableMethod(this, &GetUserMediaCallbackMediaStreamListener::NotifyRemoved),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
break;
|
|
|
|
case MediaStreamGraphEvent::EVENT_HAS_DIRECT_LISTENERS:
|
|
|
|
NotifyDirectListeners(aGraph, true);
|
|
|
|
break;
|
|
|
|
case MediaStreamGraphEvent::EVENT_HAS_NO_DIRECT_LISTENERS:
|
|
|
|
NotifyDirectListeners(aGraph, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyFinished();
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyRemoved();
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyDirectListeners(MediaStreamGraph* aGraph, bool aHasListeners);
|
|
|
|
|
|
|
|
PrincipalHandle GetPrincipalHandle() const { return mPrincipalHandle; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Set at construction
|
|
|
|
base::Thread* mMediaThread;
|
|
|
|
// never ever indirect off this; just for assertions
|
|
|
|
PRThread* mMainThreadCheck;
|
|
|
|
|
|
|
|
uint64_t mWindowID;
|
|
|
|
const PrincipalHandle mPrincipalHandle;
|
|
|
|
|
|
|
|
// true after this listener has sent MEDIA_STOP. MainThread only.
|
|
|
|
bool mStopped;
|
|
|
|
|
|
|
|
// true after the stream this listener is listening to has finished in the
|
|
|
|
// MediaStreamGraph. MainThread only.
|
|
|
|
bool mFinished;
|
|
|
|
|
|
|
|
// true after this listener has been removed from its MediaStream.
|
|
|
|
// MainThread only.
|
|
|
|
bool mRemoved;
|
|
|
|
|
|
|
|
// true if we have sent MEDIA_STOP or MEDIA_STOP_TRACK for mAudioDevice.
|
|
|
|
// MainThread only.
|
|
|
|
bool mAudioStopped;
|
|
|
|
|
|
|
|
// true if we have sent MEDIA_STOP or MEDIA_STOP_TRACK for mVideoDevice.
|
|
|
|
// MainThread only.
|
|
|
|
bool mVideoStopped;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
RefPtr<AudioDevice> mAudioDevice; // threadsafe refcnt
|
|
|
|
RefPtr<VideoDevice> mVideoDevice; // threadsafe refcnt
|
|
|
|
RefPtr<SourceMediaStream> mStream; // threadsafe refcnt
|
|
|
|
};
|
|
|
|
|
2015-09-17 05:44:14 +03: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.
|
2016-04-28 03:06:05 +03:00
|
|
|
class MediaOperationTask : public Runnable
|
2015-09-17 05:44:14 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// so we can send Stop without AddRef()ing from the MSG thread
|
|
|
|
MediaOperationTask(MediaOperation aType,
|
|
|
|
GetUserMediaCallbackMediaStreamListener* aListener,
|
|
|
|
DOMMediaStream* aStream,
|
2016-01-05 05:16:21 +03:00
|
|
|
OnTracksAvailableCallback* aOnTracksAvailableCallback,
|
2015-09-19 07:49:07 +03:00
|
|
|
AudioDevice* aAudioDevice,
|
|
|
|
VideoDevice* aVideoDevice,
|
2015-09-17 05:44:14 +03:00
|
|
|
bool aBool,
|
|
|
|
uint64_t aWindowID,
|
2015-09-21 01:45:57 +03:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aError,
|
|
|
|
const dom::MediaTrackConstraints& aConstraints = dom::MediaTrackConstraints())
|
2015-09-17 05:44:14 +03:00
|
|
|
: mType(aType)
|
|
|
|
, mStream(aStream)
|
|
|
|
, mOnTracksAvailableCallback(aOnTracksAvailableCallback)
|
2015-09-19 07:49:07 +03:00
|
|
|
, mAudioDevice(aAudioDevice)
|
|
|
|
, mVideoDevice(aVideoDevice)
|
2015-09-17 05:44:14 +03:00
|
|
|
, mListener(aListener)
|
|
|
|
, mBool(aBool)
|
|
|
|
, mWindowID(aWindowID)
|
|
|
|
, mOnFailure(aError)
|
2015-09-21 01:45:57 +03:00
|
|
|
, mConstraints(aConstraints)
|
2015-09-17 05:44:14 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
~MediaOperationTask()
|
|
|
|
{
|
|
|
|
// MediaStreams can be released on any thread.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReturnCallbackError(nsresult rv, const char* errorLog);
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
2015-09-17 05:44:14 +03:00
|
|
|
{
|
|
|
|
SourceMediaStream *source = mListener->GetSourceStream();
|
|
|
|
// No locking between these is required as all the callbacks for the
|
|
|
|
// same MediaStream will occur on the same thread.
|
|
|
|
if (!source) // means the stream was never Activated()
|
2016-04-28 03:06:05 +03:00
|
|
|
return NS_OK;
|
2015-09-17 05:44:14 +03:00
|
|
|
|
|
|
|
switch (mType) {
|
|
|
|
case MEDIA_START:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Never call on main thread");
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mAudioDevice) {
|
2016-04-06 15:56:44 +03:00
|
|
|
rv = mAudioDevice->GetSource()->Start(source, kAudioTrack,
|
|
|
|
mListener->GetPrincipalHandle());
|
2015-09-17 05:44:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReturnCallbackError(rv, "Starting audio failed");
|
2016-04-28 03:06:05 +03:00
|
|
|
return NS_OK;
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mVideoDevice) {
|
2016-04-06 15:56:44 +03:00
|
|
|
rv = mVideoDevice->GetSource()->Start(source, kVideoTrack,
|
|
|
|
mListener->GetPrincipalHandle());
|
2015-09-17 05:44:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReturnCallbackError(rv, "Starting video failed");
|
2016-04-28 03:06:05 +03:00
|
|
|
return NS_OK;
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Start() queued the tracks to be added synchronously to avoid races
|
|
|
|
source->FinishAddTracks();
|
|
|
|
|
|
|
|
source->SetPullEnabled(true);
|
|
|
|
source->AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
|
|
|
|
|
|
|
MM_LOG(("started all sources"));
|
|
|
|
// Forward mOnTracksAvailableCallback to GetUserMediaNotificationEvent,
|
|
|
|
// because mOnTracksAvailableCallback needs to be added to mStream
|
|
|
|
// on the main thread.
|
|
|
|
nsIRunnable *event =
|
|
|
|
new GetUserMediaNotificationEvent(GetUserMediaNotificationEvent::STARTING,
|
|
|
|
mStream.forget(),
|
|
|
|
mOnTracksAvailableCallback.forget(),
|
2015-09-19 07:49:07 +03:00
|
|
|
mAudioDevice != nullptr,
|
|
|
|
mVideoDevice != nullptr,
|
2015-09-17 05:44:14 +03:00
|
|
|
mWindowID, mOnFailure.forget());
|
|
|
|
// event must always be released on mainthread due to the JS callbacks
|
|
|
|
// in the TracksAvailableCallback
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEDIA_STOP:
|
|
|
|
case MEDIA_STOP_TRACK:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Never call on main thread");
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mAudioDevice) {
|
|
|
|
mAudioDevice->GetSource()->Stop(source, kAudioTrack);
|
2016-06-30 22:43:24 +03:00
|
|
|
mAudioDevice->Deallocate();
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mVideoDevice) {
|
|
|
|
mVideoDevice->GetSource()->Stop(source, kVideoTrack);
|
2016-06-30 22:43:24 +03:00
|
|
|
mVideoDevice->Deallocate();
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
2016-02-05 15:20:20 +03:00
|
|
|
if (mType == MEDIA_STOP) {
|
|
|
|
source->EndAllTrackAndFinish();
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIRunnable *event =
|
|
|
|
new GetUserMediaNotificationEvent(mListener,
|
|
|
|
mType == MEDIA_STOP ?
|
|
|
|
GetUserMediaNotificationEvent::STOPPING :
|
|
|
|
GetUserMediaNotificationEvent::STOPPED_TRACK,
|
2015-09-19 07:49:07 +03:00
|
|
|
mAudioDevice != nullptr,
|
|
|
|
mVideoDevice != nullptr,
|
2015-09-17 05:44:14 +03:00
|
|
|
mWindowID);
|
|
|
|
// event must always be released on mainthread due to the JS callbacks
|
|
|
|
// in the TracksAvailableCallback
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEDIA_DIRECT_LISTENERS:
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Never call on main thread");
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mVideoDevice) {
|
|
|
|
mVideoDevice->GetSource()->SetDirectListeners(mBool);
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false,"invalid MediaManager operation");
|
|
|
|
break;
|
|
|
|
}
|
2016-04-28 03:06:05 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-09-17 05:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MediaOperation mType;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMMediaStream> mStream;
|
2016-01-05 05:16:21 +03:00
|
|
|
nsAutoPtr<OnTracksAvailableCallback> mOnTracksAvailableCallback;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioDevice> mAudioDevice; // threadsafe
|
|
|
|
RefPtr<VideoDevice> mVideoDevice; // threadsafe
|
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> mListener; // threadsafe
|
2015-09-17 05:44:14 +03:00
|
|
|
bool mBool;
|
|
|
|
uint64_t mWindowID;
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> mOnFailure;
|
2015-09-21 01:45:57 +03:00
|
|
|
dom::MediaTrackConstraints mConstraints;
|
2015-09-17 05:44:14 +03:00
|
|
|
};
|
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
/**
|
|
|
|
* Send an error back to content.
|
|
|
|
* Do this only on the main thread. The onSuccess callback is also passed here
|
|
|
|
* so it can be released correctly.
|
|
|
|
*/
|
|
|
|
template<class SuccessCallbackType>
|
2016-04-26 03:23:21 +03:00
|
|
|
class ErrorCallbackRunnable : public Runnable
|
2015-03-29 08:42:32 +03:00
|
|
|
{
|
2015-02-23 19:50:48 +03:00
|
|
|
public:
|
|
|
|
ErrorCallbackRunnable(
|
|
|
|
nsCOMPtr<SuccessCallbackType>& aOnSuccess,
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback>& aOnFailure,
|
|
|
|
MediaMgrError& aError,
|
|
|
|
uint64_t aWindowID)
|
|
|
|
: mError(&aError)
|
|
|
|
, mWindowID(aWindowID)
|
|
|
|
, mManager(MediaManager::GetInstance())
|
|
|
|
{
|
|
|
|
mOnSuccess.swap(aOnSuccess);
|
|
|
|
mOnFailure.swap(aOnFailure);
|
|
|
|
}
|
2015-03-28 23:24:25 +03:00
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Run()
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-29 05:57:17 +03:00
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
nsCOMPtr<SuccessCallbackType> onSuccess = mOnSuccess.forget();
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure = mOnFailure.forget();
|
2015-03-29 08:42:32 +03:00
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
// Only run if the window is still active.
|
|
|
|
if (!(mManager->IsWindowStillActive(mWindowID))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// This is safe since we're on main-thread, and the windowlist can only
|
|
|
|
// be invalidated from the main-thread (see OnNavigation)
|
2016-01-30 20:05:36 +03:00
|
|
|
if (auto* window = nsGlobalWindow::GetInnerWindowWithId(mWindowID)) {
|
|
|
|
RefPtr<MediaStreamError> error =
|
|
|
|
new MediaStreamError(window->AsInner(), *mError);
|
2015-02-23 19:50:48 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
}
|
2013-12-06 01:30:50 +04:00
|
|
|
return NS_OK;
|
2012-06-03 11:35:15 +04:00
|
|
|
}
|
2015-02-23 19:50:48 +03:00
|
|
|
private:
|
|
|
|
~ErrorCallbackRunnable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mOnSuccess && !mOnFailure);
|
2014-10-27 22:42:56 +03:00
|
|
|
}
|
2015-02-23 19:50:48 +03:00
|
|
|
|
|
|
|
nsCOMPtr<SuccessCallbackType> mOnSuccess;
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> mOnFailure;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaMgrError> mError;
|
2015-02-23 19:50:48 +03:00
|
|
|
uint64_t mWindowID;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mManager; // get ref to this when creating the runnable
|
2015-02-23 19:50:48 +03:00
|
|
|
};
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2013-01-07 06:31:32 +04:00
|
|
|
// Handle removing GetUserMediaCallbackMediaStreamListener from main thread
|
2016-04-26 03:23:21 +03:00
|
|
|
class GetUserMediaListenerRemove: public Runnable
|
2013-01-07 06:31:32 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetUserMediaListenerRemove(uint64_t aWindowID,
|
|
|
|
GetUserMediaCallbackMediaStreamListener *aListener)
|
|
|
|
: mWindowID(aWindowID)
|
|
|
|
, mListener(aListener) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> manager(MediaManager::GetInstance());
|
2013-01-07 06:31:32 +04:00
|
|
|
manager->RemoveFromWindowList(mWindowID, mListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64_t mWindowID;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> mListener;
|
2013-01-07 06:31:32 +04:00
|
|
|
};
|
|
|
|
|
2012-09-20 23:54:00 +04:00
|
|
|
/**
|
|
|
|
* nsIMediaDevice implementation.
|
|
|
|
*/
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MediaDevice, nsIMediaDevice)
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
MediaDevice::MediaDevice(MediaEngineSource* aSource, bool aIsVideo)
|
2015-07-24 15:28:16 +03:00
|
|
|
: mMediaSource(aSource->GetMediaSource())
|
|
|
|
, mSource(aSource)
|
2015-07-03 01:01:52 +03:00
|
|
|
, mIsVideo(aIsVideo)
|
|
|
|
{
|
2013-09-16 10:34:57 +04:00
|
|
|
mSource->GetName(mName);
|
2015-06-18 18:46:36 +03:00
|
|
|
nsCString id;
|
|
|
|
mSource->GetUUID(id);
|
|
|
|
CopyUTF8toUTF16(id, mID);
|
2014-04-18 23:15:10 +04:00
|
|
|
}
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2014-04-18 23:15:10 +04:00
|
|
|
VideoDevice::VideoDevice(MediaEngineVideoSource* aSource)
|
2015-07-03 01:01:52 +03:00
|
|
|
: MediaDevice(aSource, true)
|
2015-07-24 15:28:16 +03:00
|
|
|
{}
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2014-09-24 19:17:33 +04:00
|
|
|
/**
|
|
|
|
* Helper functions that implement the constraints algorithm from
|
|
|
|
* http://dev.w3.org/2011/webrtc/editor/getusermedia.html#methods-5
|
|
|
|
*/
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
bool
|
|
|
|
MediaDevice::StringsContain(const OwningStringOrStringSequence& aStrings,
|
|
|
|
nsString aN)
|
|
|
|
{
|
|
|
|
return aStrings.IsString() ? aStrings.GetAsString() == aN
|
|
|
|
: aStrings.GetAsStringSequence().Contains(aN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ uint32_t
|
|
|
|
MediaDevice::FitnessDistance(nsString aN,
|
|
|
|
const ConstrainDOMStringParameters& aParams)
|
|
|
|
{
|
|
|
|
if (aParams.mExact.WasPassed() && !StringsContain(aParams.mExact.Value(), aN)) {
|
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
|
|
|
if (aParams.mIdeal.WasPassed() && !StringsContain(aParams.mIdeal.Value(), aN)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binding code doesn't templatize well...
|
|
|
|
|
|
|
|
/* static */ uint32_t
|
|
|
|
MediaDevice::FitnessDistance(nsString aN,
|
|
|
|
const OwningStringOrStringSequenceOrConstrainDOMStringParameters& aConstraint)
|
|
|
|
{
|
|
|
|
if (aConstraint.IsString()) {
|
|
|
|
ConstrainDOMStringParameters params;
|
|
|
|
params.mIdeal.Construct();
|
|
|
|
params.mIdeal.Value().SetAsString() = aConstraint.GetAsString();
|
|
|
|
return FitnessDistance(aN, params);
|
|
|
|
} else if (aConstraint.IsStringSequence()) {
|
|
|
|
ConstrainDOMStringParameters params;
|
|
|
|
params.mIdeal.Construct();
|
|
|
|
params.mIdeal.Value().SetAsStringSequence() = aConstraint.GetAsStringSequence();
|
|
|
|
return FitnessDistance(aN, params);
|
|
|
|
} else {
|
|
|
|
return FitnessDistance(aN, aConstraint.GetAsConstrainDOMStringParameters());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 01:06:26 +03:00
|
|
|
uint32_t
|
2015-07-03 01:01:52 +03:00
|
|
|
MediaDevice::GetBestFitnessDistance(
|
2016-06-17 22:20:10 +03:00
|
|
|
const nsTArray<const NormalizedConstraintSet*>& aConstraintSets)
|
2014-09-24 19:17:33 +04:00
|
|
|
{
|
2015-07-03 01:01:52 +03:00
|
|
|
nsString mediaSource;
|
|
|
|
GetMediaSource(mediaSource);
|
|
|
|
|
|
|
|
// This code is reused for audio, where the mediaSource constraint does
|
|
|
|
// not currently have a function, but because it defaults to "camera" in
|
|
|
|
// webidl, we ignore it for audio here.
|
|
|
|
if (!mediaSource.EqualsASCII("microphone")) {
|
|
|
|
for (const auto& constraint : aConstraintSets) {
|
2016-06-17 22:20:10 +03:00
|
|
|
if (constraint->mMediaSource.mIdeal.find(mediaSource) ==
|
|
|
|
constraint->mMediaSource.mIdeal.end()) {
|
2015-07-03 01:01:52 +03:00
|
|
|
return UINT32_MAX;
|
|
|
|
}
|
2014-09-24 19:17:33 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-25 03:00:02 +04:00
|
|
|
// Forward request to underlying object to interrogate per-mode capabilities.
|
2015-07-03 01:01:52 +03:00
|
|
|
// Pass in device's origin-specific id for deviceId constraint comparison.
|
|
|
|
nsString id;
|
|
|
|
GetId(id);
|
|
|
|
return mSource->GetBestFitnessDistance(aConstraintSets, id);
|
2014-09-24 19:17:33 +04:00
|
|
|
}
|
|
|
|
|
2014-04-18 23:15:10 +04:00
|
|
|
AudioDevice::AudioDevice(MediaEngineAudioSource* aSource)
|
2015-07-03 01:01:52 +03:00
|
|
|
: MediaDevice(aSource, false)
|
2014-09-24 19:17:33 +04:00
|
|
|
{
|
2015-07-03 01:01:52 +03:00
|
|
|
mMediaSource = aSource->GetMediaSource();
|
2014-09-24 19:17:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 23:54:00 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaDevice::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
aName.Assign(mName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaDevice::GetType(nsAString& aType)
|
|
|
|
{
|
2014-04-18 23:15:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
VideoDevice::GetType(nsAString& aType)
|
|
|
|
{
|
2014-05-26 22:54:10 +04:00
|
|
|
aType.AssignLiteral(MOZ_UTF16("video"));
|
2014-04-18 23:15:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioDevice::GetType(nsAString& aType)
|
|
|
|
{
|
2014-05-26 22:54:10 +04:00
|
|
|
aType.AssignLiteral(MOZ_UTF16("audio"));
|
2012-09-20 23:54:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-17 17:28:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaDevice::GetId(nsAString& aID)
|
|
|
|
{
|
|
|
|
aID.Assign(mID);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
void
|
|
|
|
MediaDevice::SetId(const nsAString& aID)
|
|
|
|
{
|
|
|
|
mID.Assign(aID);
|
|
|
|
}
|
|
|
|
|
2014-07-08 10:01:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaDevice::GetMediaSource(nsAString& aMediaSource)
|
|
|
|
{
|
2016-02-17 00:55:33 +03:00
|
|
|
if (mMediaSource == MediaSourceEnum::Microphone) {
|
2014-07-18 06:23:00 +04:00
|
|
|
aMediaSource.Assign(NS_LITERAL_STRING("microphone"));
|
2016-02-17 00:55:33 +03:00
|
|
|
} else if (mMediaSource == MediaSourceEnum::AudioCapture) {
|
2015-07-24 15:28:16 +03:00
|
|
|
aMediaSource.Assign(NS_LITERAL_STRING("audioCapture"));
|
2016-02-17 00:55:33 +03:00
|
|
|
} else if (mMediaSource == MediaSourceEnum::Window) { // this will go away
|
2014-07-18 06:23:00 +04:00
|
|
|
aMediaSource.Assign(NS_LITERAL_STRING("window"));
|
|
|
|
} else { // all the rest are shared
|
|
|
|
aMediaSource.Assign(NS_ConvertUTF8toUTF16(
|
|
|
|
dom::MediaSourceEnumValues::strings[uint32_t(mMediaSource)].value));
|
|
|
|
}
|
2014-07-08 10:01:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-24 19:17:33 +04:00
|
|
|
VideoDevice::Source*
|
2014-04-18 23:15:10 +04:00
|
|
|
VideoDevice::GetSource()
|
|
|
|
{
|
2014-09-24 19:17:33 +04:00
|
|
|
return static_cast<Source*>(&*mSource);
|
2014-04-18 23:15:10 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 19:17:33 +04:00
|
|
|
AudioDevice::Source*
|
2014-04-18 23:15:10 +04:00
|
|
|
AudioDevice::GetSource()
|
2012-09-20 23:54:00 +04:00
|
|
|
{
|
2014-09-24 19:17:33 +04:00
|
|
|
return static_cast<Source*>(&*mSource);
|
2012-09-20 23:54:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:43:24 +03:00
|
|
|
nsresult MediaDevice::Allocate(const dom::MediaTrackConstraints &aConstraints,
|
2016-02-17 20:57:26 +03:00
|
|
|
const MediaEnginePrefs &aPrefs,
|
2016-06-16 02:25:07 +03:00
|
|
|
const nsACString& aOrigin,
|
|
|
|
const char** aOutBadConstraint) {
|
2016-06-30 22:43:24 +03:00
|
|
|
return GetSource()->Allocate(aConstraints, aPrefs, mID, aOrigin,
|
2016-06-16 02:25:07 +03:00
|
|
|
getter_AddRefs(mAllocationHandle),
|
|
|
|
aOutBadConstraint);
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:43:24 +03:00
|
|
|
nsresult MediaDevice::Restart(const dom::MediaTrackConstraints &aConstraints,
|
2016-06-16 02:25:07 +03:00
|
|
|
const MediaEnginePrefs &aPrefs,
|
|
|
|
const char** aOutBadConstraint) {
|
|
|
|
return GetSource()->Restart(mAllocationHandle, aConstraints, aPrefs, mID,
|
|
|
|
aOutBadConstraint);
|
2015-09-21 01:45:57 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:43:24 +03:00
|
|
|
nsresult MediaDevice::Deallocate() {
|
|
|
|
return GetSource()->Deallocate(mAllocationHandle);
|
2015-09-21 01:45:57 +03:00
|
|
|
}
|
|
|
|
|
2015-02-23 19:50:48 +03:00
|
|
|
void
|
|
|
|
MediaOperationTask::ReturnCallbackError(nsresult rv, const char* errorLog)
|
|
|
|
{
|
|
|
|
MM_LOG(("%s , rv=%d", errorLog, rv));
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new ReleaseMediaOperationResource(mStream.forget(),
|
|
|
|
mOnTracksAvailableCallback.forget())));
|
2015-02-23 19:50:48 +03:00
|
|
|
nsString log;
|
|
|
|
|
|
|
|
log.AssignASCII(errorLog);
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> onSuccess;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaMgrError> error = new MediaMgrError(
|
2015-02-23 19:50:48 +03:00
|
|
|
NS_LITERAL_STRING("InternalError"), log);
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_DispatchToMainThread(do_AddRef(
|
2015-02-23 19:50:48 +03:00
|
|
|
new ErrorCallbackRunnable<nsIDOMGetUserMediaSuccessCallback>(onSuccess,
|
|
|
|
mOnFailure,
|
|
|
|
*error,
|
2015-07-10 06:21:46 +03:00
|
|
|
mWindowID)));
|
2015-02-23 19:50:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:42:26 +03:00
|
|
|
static bool
|
|
|
|
IsOn(const OwningBooleanOrMediaTrackConstraints &aUnion) {
|
|
|
|
return !aUnion.IsBoolean() || aUnion.GetAsBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MediaTrackConstraints&
|
|
|
|
GetInvariant(const OwningBooleanOrMediaTrackConstraints &aUnion) {
|
|
|
|
static const MediaTrackConstraints empty;
|
|
|
|
return aUnion.IsMediaTrackConstraints() ?
|
|
|
|
aUnion.GetAsMediaTrackConstraints() : empty;
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:47:43 +03:00
|
|
|
/**
|
|
|
|
* This class is only needed since fake tracks are added dynamically.
|
|
|
|
* Instead of refactoring to add them explicitly we let the DOMMediaStream
|
|
|
|
* query us for the source as they become available.
|
|
|
|
* Since they are used only for testing the API surface, we make them very
|
|
|
|
* simple.
|
|
|
|
*/
|
|
|
|
class FakeTrackSourceGetter : public MediaStreamTrackSourceGetter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FakeTrackSourceGetter,
|
|
|
|
MediaStreamTrackSourceGetter)
|
|
|
|
|
|
|
|
explicit FakeTrackSourceGetter(nsIPrincipal* aPrincipal)
|
|
|
|
: mPrincipal(aPrincipal) {}
|
|
|
|
|
|
|
|
already_AddRefed<dom::MediaStreamTrackSource>
|
|
|
|
GetMediaStreamTrackSource(TrackID aInputTrackID) override
|
|
|
|
{
|
|
|
|
NS_ASSERTION(kAudioTrack != aInputTrackID,
|
|
|
|
"Only fake tracks should appear dynamically");
|
|
|
|
NS_ASSERTION(kVideoTrack != aInputTrackID,
|
|
|
|
"Only fake tracks should appear dynamically");
|
|
|
|
return do_AddRef(new BasicUnstoppableTrackSource(mPrincipal));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~FakeTrackSourceGetter() {}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(FakeTrackSourceGetter, MediaStreamTrackSourceGetter)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FakeTrackSourceGetter, MediaStreamTrackSourceGetter)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FakeTrackSourceGetter)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MediaStreamTrackSourceGetter)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(FakeTrackSourceGetter,
|
|
|
|
MediaStreamTrackSourceGetter,
|
|
|
|
mPrincipal)
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
/**
|
2012-07-12 15:53:08 +04:00
|
|
|
* Creates a MediaStream, attaches a listener and fires off a success callback
|
2012-09-20 23:54:00 +04:00
|
|
|
* to the DOM with the stream. We also pass in the error callback so it can
|
|
|
|
* be released correctly.
|
2012-07-12 15:53:08 +04:00
|
|
|
*
|
|
|
|
* All of this must be done on the main thread!
|
2012-10-16 00:41:46 +04:00
|
|
|
*
|
|
|
|
* Note that the various GetUserMedia Runnable classes currently allow for
|
|
|
|
* two streams. If we ever need to support getting more than two streams
|
2015-10-18 08:24:48 +03:00
|
|
|
* at once, we could convert everything to nsTArray<RefPtr<blah> >'s,
|
2012-10-25 03:21:15 +04:00
|
|
|
* though that would complicate the constructors some. Currently the
|
2012-10-16 00:41:46 +04:00
|
|
|
* GetUserMedia spec does not allow for more than 2 streams to be obtained in
|
|
|
|
* one call, to simplify handling of constraints.
|
2012-06-03 11:35:15 +04:00
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class GetUserMediaStreamRunnable : public Runnable
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-09-20 23:54:00 +04:00
|
|
|
GetUserMediaStreamRunnable(
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback>& aOnSuccess,
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback>& aOnFailure,
|
2012-10-16 00:41:46 +04:00
|
|
|
uint64_t aWindowID,
|
2013-01-07 06:31:32 +04:00
|
|
|
GetUserMediaCallbackMediaStreamListener* aListener,
|
2015-07-14 04:56:29 +03:00
|
|
|
const nsCString& aOrigin,
|
2015-11-25 07:42:26 +03:00
|
|
|
const MediaStreamConstraints& aConstraints,
|
2015-09-19 07:49:07 +03:00
|
|
|
AudioDevice* aAudioDevice,
|
|
|
|
VideoDevice* aVideoDevice,
|
2014-05-01 14:51:00 +04:00
|
|
|
PeerIdentity* aPeerIdentity)
|
2015-11-25 07:42:26 +03:00
|
|
|
: mConstraints(aConstraints)
|
|
|
|
, mAudioDevice(aAudioDevice)
|
2015-09-19 07:49:07 +03:00
|
|
|
, mVideoDevice(aVideoDevice)
|
2012-12-29 00:29:52 +04:00
|
|
|
, mWindowID(aWindowID)
|
2013-01-07 06:31:32 +04:00
|
|
|
, mListener(aListener)
|
2015-07-14 04:56:29 +03:00
|
|
|
, mOrigin(aOrigin)
|
2014-05-01 14:51:00 +04:00
|
|
|
, mPeerIdentity(aPeerIdentity)
|
2014-03-15 23:00:15 +04:00
|
|
|
, mManager(MediaManager::GetInstance())
|
|
|
|
{
|
2014-10-18 04:47:46 +04:00
|
|
|
mOnSuccess.swap(aOnSuccess);
|
|
|
|
mOnFailure.swap(aOnFailure);
|
2014-03-15 23:00:15 +04:00
|
|
|
}
|
2012-07-12 15:53:08 +04:00
|
|
|
|
|
|
|
~GetUserMediaStreamRunnable() {}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2016-01-05 05:16:21 +03:00
|
|
|
class TracksAvailableCallback : public OnTracksAvailableCallback
|
2013-05-03 09:07:37 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TracksAvailableCallback(MediaManager* aManager,
|
2013-12-06 01:30:50 +04:00
|
|
|
nsIDOMGetUserMediaSuccessCallback* aSuccess,
|
|
|
|
uint64_t aWindowID,
|
|
|
|
DOMMediaStream* aStream)
|
2014-10-18 04:47:46 +04:00
|
|
|
: mWindowID(aWindowID), mOnSuccess(aSuccess), mManager(aManager),
|
2013-05-03 09:07:37 +04:00
|
|
|
mStream(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void NotifyTracksAvailable(DOMMediaStream* aStream) override
|
2013-05-03 09:07:37 +04:00
|
|
|
{
|
|
|
|
// We're in the main thread, so no worries here.
|
|
|
|
if (!(mManager->IsWindowStillActive(mWindowID))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start currentTime from the point where this stream was successfully
|
|
|
|
// returned.
|
2015-09-30 04:31:54 +03:00
|
|
|
aStream->SetLogicalStreamStartTime(aStream->GetPlaybackStream()->GetCurrentTime());
|
2013-05-03 09:07:37 +04:00
|
|
|
|
|
|
|
// This is safe since we're on main-thread, and the windowlist can only
|
|
|
|
// be invalidated from the main-thread (see OnNavigation)
|
|
|
|
LOG(("Returning success for getUserMedia()"));
|
2014-10-18 04:47:46 +04:00
|
|
|
mOnSuccess->OnSuccess(aStream);
|
2013-05-03 09:07:37 +04:00
|
|
|
}
|
|
|
|
uint64_t mWindowID;
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> mOnSuccess;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mManager;
|
2013-05-03 09:07:37 +04:00
|
|
|
// Keep the DOMMediaStream alive until the NotifyTracksAvailable callback
|
|
|
|
// has fired, otherwise we might immediately destroy the DOMMediaStream and
|
|
|
|
// shut down the underlying MediaStream prematurely.
|
|
|
|
// This creates a cycle which is broken when NotifyTracksAvailable
|
|
|
|
// is fired (which will happen unless the browser shuts down,
|
|
|
|
// since we only add this callback when we've successfully appended
|
|
|
|
// the desired tracks in the MediaStreamGraph) or when
|
|
|
|
// DOMMediaStream::NotifyMediaStreamGraphShutdown is called.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMMediaStream> mStream;
|
2013-05-03 09:07:37 +04:00
|
|
|
};
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* globalWindow = nsGlobalWindow::GetInnerWindowWithId(mWindowID);
|
|
|
|
nsPIDOMWindowInner* window = globalWindow ? globalWindow->AsInner() : nullptr;
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2012-11-14 01:55:02 +04:00
|
|
|
// We're on main-thread, and the windowlist can only
|
|
|
|
// be invalidated from the main-thread (see OnNavigation)
|
2012-12-29 00:29:52 +04:00
|
|
|
StreamListeners* listeners = mManager->GetWindowListeners(mWindowID);
|
2013-02-15 12:04:11 +04:00
|
|
|
if (!listeners || !window || !window->GetExtantDoc()) {
|
2013-01-07 06:31:32 +04:00
|
|
|
// This window is no longer live. mListener has already been removed
|
2012-11-14 01:55:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-25 11:29:51 +03:00
|
|
|
MediaStreamGraph::GraphDriverType graphDriverType =
|
2015-09-19 07:49:07 +03:00
|
|
|
mAudioDevice ? MediaStreamGraph::AUDIO_THREAD_DRIVER
|
2015-08-25 11:29:51 +03:00
|
|
|
: MediaStreamGraph::SYSTEM_THREAD_DRIVER;
|
|
|
|
MediaStreamGraph* msg =
|
|
|
|
MediaStreamGraph::GetInstance(graphDriverType,
|
|
|
|
dom::AudioChannel::Normal);
|
|
|
|
|
2016-02-01 17:46:34 +03:00
|
|
|
RefPtr<DOMMediaStream> domStream;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceMediaStream> stream;
|
2015-07-24 15:28:16 +03:00
|
|
|
// AudioCapture is a special case, here, in the sense that we're not really
|
|
|
|
// using the audio source and the SourceMediaStream, which acts as
|
|
|
|
// placeholders. We re-route a number of stream internaly in the MSG and mix
|
|
|
|
// them down instead.
|
2015-09-19 07:49:07 +03:00
|
|
|
if (mAudioDevice &&
|
2016-02-17 00:55:33 +03:00
|
|
|
mAudioDevice->GetMediaSource() == MediaSourceEnum::AudioCapture) {
|
2015-07-24 15:28:17 +03:00
|
|
|
// It should be possible to pipe the capture stream to anything. CORS is
|
|
|
|
// not a problem here, we got explicit user content.
|
2016-02-01 17:46:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = window->GetExtantDoc()->NodePrincipal();
|
|
|
|
domStream =
|
2016-04-25 06:43:27 +03:00
|
|
|
DOMMediaStream::CreateAudioCaptureStreamAsInput(window, principal, msg);
|
2016-02-01 17:46:34 +03:00
|
|
|
|
2016-06-29 13:27:13 +03:00
|
|
|
stream = msg->CreateSourceStream(); // Placeholder
|
2015-07-24 15:28:16 +03:00
|
|
|
msg->RegisterCaptureStreamForWindow(
|
2015-09-30 04:31:54 +03:00
|
|
|
mWindowID, domStream->GetInputStream()->AsProcessedStream());
|
2015-07-24 15:28:16 +03:00
|
|
|
window->SetAudioCapture(true);
|
|
|
|
} else {
|
2016-04-06 15:46:56 +03:00
|
|
|
class LocalTrackSource : public MediaStreamTrackSource
|
|
|
|
{
|
|
|
|
public:
|
2016-01-05 05:16:24 +03:00
|
|
|
LocalTrackSource(nsIPrincipal* aPrincipal,
|
2016-01-22 11:27:37 +03:00
|
|
|
const nsString& aLabel,
|
2016-01-05 05:16:24 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener* aListener,
|
2016-04-06 15:46:56 +03:00
|
|
|
const MediaSourceEnum aSource,
|
2016-01-05 05:16:29 +03:00
|
|
|
const TrackID aTrackID,
|
|
|
|
const PeerIdentity* aPeerIdentity)
|
2016-01-22 11:27:37 +03:00
|
|
|
: MediaStreamTrackSource(aPrincipal, false, aLabel), mListener(aListener),
|
2016-01-05 05:16:29 +03:00
|
|
|
mSource(aSource), mTrackID(aTrackID), mPeerIdentity(aPeerIdentity) {}
|
2016-04-06 15:46:56 +03:00
|
|
|
|
|
|
|
MediaSourceEnum GetMediaSource() const override
|
|
|
|
{
|
|
|
|
return mSource;
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:16:29 +03:00
|
|
|
const PeerIdentity* GetPeerIdentity() const override
|
|
|
|
{
|
|
|
|
return mPeerIdentity;
|
|
|
|
}
|
|
|
|
|
2015-11-19 06:17:09 +03:00
|
|
|
already_AddRefed<PledgeVoid>
|
2016-02-01 17:43:38 +03:00
|
|
|
ApplyConstraints(nsPIDOMWindowInner* aWindow,
|
2015-11-19 06:17:09 +03:00
|
|
|
const MediaTrackConstraints& aConstraints) override
|
2016-02-01 17:43:38 +03:00
|
|
|
{
|
2016-07-18 10:39:16 +03:00
|
|
|
if (sInShutdown) {
|
2015-11-19 06:17:09 +03:00
|
|
|
RefPtr<PledgeVoid> p = new PledgeVoid();
|
|
|
|
p->Reject(new MediaStreamError(aWindow,
|
|
|
|
NS_LITERAL_STRING("AbortError"),
|
|
|
|
NS_LITERAL_STRING("In shutdown")));
|
|
|
|
return p.forget();
|
2016-02-01 17:43:38 +03:00
|
|
|
}
|
2015-11-19 06:17:09 +03:00
|
|
|
return mListener->ApplyConstraintsToTrack(aWindow, mTrackID, aConstraints);
|
2016-02-01 17:43:38 +03:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:38:25 +03:00
|
|
|
void
|
|
|
|
GetSettings(dom::MediaTrackSettings& aOutSettings) override
|
|
|
|
{
|
|
|
|
mListener->GetSettings(aOutSettings);
|
|
|
|
}
|
2016-02-01 17:43:38 +03:00
|
|
|
|
2016-04-06 15:46:56 +03:00
|
|
|
void Stop() override
|
|
|
|
{
|
|
|
|
if (mListener) {
|
|
|
|
mListener->StopTrack(mTrackID);
|
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~LocalTrackSource() {}
|
|
|
|
|
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> mListener;
|
|
|
|
const MediaSourceEnum mSource;
|
|
|
|
const TrackID mTrackID;
|
2016-01-05 05:16:29 +03:00
|
|
|
const RefPtr<const PeerIdentity> mPeerIdentity;
|
2016-04-06 15:46:56 +03:00
|
|
|
};
|
|
|
|
|
2016-01-05 05:16:24 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
if (mPeerIdentity) {
|
2016-04-21 11:51:25 +03:00
|
|
|
principal = nsNullPrincipal::CreateWithInheritedAttributes(window->GetExtantDoc()->NodePrincipal());
|
2016-01-05 05:16:24 +03:00
|
|
|
} else {
|
|
|
|
principal = window->GetExtantDoc()->NodePrincipal();
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:16 +03:00
|
|
|
// Normal case, connect the source stream to the track union stream to
|
2016-04-06 15:47:43 +03:00
|
|
|
// avoid us blocking. Pass a simple TrackSourceGetter for potential
|
|
|
|
// fake tracks. Apart from them gUM never adds tracks dynamically.
|
2016-01-05 05:16:21 +03:00
|
|
|
domStream =
|
2016-04-25 06:43:27 +03:00
|
|
|
DOMLocalMediaStream::CreateSourceStreamAsInput(window, msg,
|
|
|
|
new FakeTrackSourceGetter(principal));
|
2015-09-30 04:31:54 +03:00
|
|
|
|
|
|
|
if (mAudioDevice) {
|
2016-01-16 15:39:00 +03:00
|
|
|
nsString audioDeviceName;
|
|
|
|
mAudioDevice->GetName(audioDeviceName);
|
2016-04-06 15:46:56 +03:00
|
|
|
const MediaSourceEnum source =
|
|
|
|
mAudioDevice->GetSource()->GetMediaSource();
|
|
|
|
RefPtr<MediaStreamTrackSource> audioSource =
|
2016-01-22 11:27:37 +03:00
|
|
|
new LocalTrackSource(principal, audioDeviceName, mListener, source,
|
|
|
|
kAudioTrack, mPeerIdentity);
|
2015-11-25 07:42:26 +03:00
|
|
|
MOZ_ASSERT(IsOn(mConstraints.mAudio));
|
|
|
|
domStream->CreateDOMTrack(kAudioTrack, MediaSegment::AUDIO, audioSource,
|
|
|
|
GetInvariant(mConstraints.mAudio));
|
2015-09-30 04:31:54 +03:00
|
|
|
}
|
|
|
|
if (mVideoDevice) {
|
2016-01-16 15:39:00 +03:00
|
|
|
nsString videoDeviceName;
|
|
|
|
mVideoDevice->GetName(videoDeviceName);
|
2016-04-06 15:46:56 +03:00
|
|
|
const MediaSourceEnum source =
|
|
|
|
mVideoDevice->GetSource()->GetMediaSource();
|
|
|
|
RefPtr<MediaStreamTrackSource> videoSource =
|
2016-01-22 11:27:37 +03:00
|
|
|
new LocalTrackSource(principal, videoDeviceName, mListener, source,
|
|
|
|
kVideoTrack, mPeerIdentity);
|
2015-11-25 07:42:26 +03:00
|
|
|
MOZ_ASSERT(IsOn(mConstraints.mVideo));
|
|
|
|
domStream->CreateDOMTrack(kVideoTrack, MediaSegment::VIDEO, videoSource,
|
|
|
|
GetInvariant(mConstraints.mVideo));
|
2015-09-30 04:31:54 +03:00
|
|
|
}
|
|
|
|
stream = domStream->GetInputStream()->AsSourceStream();
|
2015-07-24 15:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!domStream || sInShutdown) {
|
2014-10-27 22:42:56 +03:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure = mOnFailure.forget();
|
2012-11-14 01:55:02 +04:00
|
|
|
LOG(("Returning error for getUserMedia() - no stream"));
|
2014-10-27 22:42:56 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
if (auto* window = nsGlobalWindow::GetInnerWindowWithId(mWindowID)) {
|
|
|
|
RefPtr<MediaStreamError> error = new MediaStreamError(window->AsInner(),
|
2014-10-27 22:42:56 +03:00
|
|
|
NS_LITERAL_STRING("InternalError"),
|
2015-05-29 21:28:03 +03:00
|
|
|
sInShutdown ? NS_LITERAL_STRING("In shutdown") :
|
|
|
|
NS_LITERAL_STRING("No stream."));
|
2014-10-27 22:42:56 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
}
|
2012-11-14 01:55:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-12 15:53:48 +04:00
|
|
|
|
2014-05-01 14:51:00 +04:00
|
|
|
// The listener was added at the beginning in an inactive state.
|
2013-01-07 06:31:32 +04:00
|
|
|
// Activate our listener. We'll call Start() on the source when get a callback
|
2012-06-03 11:35:15 +04:00
|
|
|
// that the MediaStream has started consuming. The listener is freed
|
|
|
|
// when the page is invalidated (on navigation or close).
|
2015-09-30 04:31:54 +03:00
|
|
|
MOZ_ASSERT(stream);
|
2015-09-19 07:49:07 +03:00
|
|
|
mListener->Activate(stream.forget(), mAudioDevice, mVideoDevice);
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2013-12-06 01:30:51 +04:00
|
|
|
// Note: includes JS callbacks; must be released on MainThread
|
2013-05-03 09:07:37 +04:00
|
|
|
TracksAvailableCallback* tracksAvailableCallback =
|
2015-07-24 15:28:16 +03:00
|
|
|
new TracksAvailableCallback(mManager, mOnSuccess, mWindowID, domStream);
|
2013-05-03 09:07:37 +04:00
|
|
|
|
2014-04-02 21:58:19 +04:00
|
|
|
// Dispatch to the media thread to ask it to start the sources,
|
|
|
|
// because that can take a while.
|
2015-09-30 04:31:54 +03:00
|
|
|
// Pass ownership of domStream to the MediaOperationTask
|
2014-09-12 18:49:39 +04:00
|
|
|
// to ensure it's kept alive until the MediaOperationTask runs (at least).
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<Runnable> mediaOperation =
|
2015-09-19 07:49:07 +03:00
|
|
|
new MediaOperationTask(MEDIA_START, mListener, domStream,
|
|
|
|
tracksAvailableCallback,
|
|
|
|
mAudioDevice, mVideoDevice,
|
2016-04-28 03:06:05 +03:00
|
|
|
false, mWindowID, mOnFailure.forget());
|
|
|
|
MediaManager::PostTask(mediaOperation.forget());
|
2014-10-18 04:47:46 +04:00
|
|
|
// We won't need mOnFailure now.
|
|
|
|
mOnFailure = nullptr;
|
2015-07-10 17:15:19 +03:00
|
|
|
|
|
|
|
if (!MediaManager::IsPrivateBrowsing(window)) {
|
|
|
|
// Call GetOriginKey again, this time w/persist = true, to promote
|
|
|
|
// deviceIds to persistent, in case they're not already. Fire'n'forget.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Pledge<nsCString>> p = media::GetOriginKey(mOrigin, false, true);
|
2015-07-10 17:15:19 +03:00
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> mOnSuccess;
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> mOnFailure;
|
2015-11-25 07:42:26 +03:00
|
|
|
MediaStreamConstraints mConstraints;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioDevice> mAudioDevice;
|
|
|
|
RefPtr<VideoDevice> mVideoDevice;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t mWindowID;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> mListener;
|
2015-07-14 04:56:29 +03:00
|
|
|
nsCString mOrigin;
|
2016-01-05 05:16:28 +03:00
|
|
|
RefPtr<PeerIdentity> mPeerIdentity;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mManager; // get ref to this when creating the runnable
|
2012-06-03 11:35:15 +04:00
|
|
|
};
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Source getter returning full list
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
template<class DeviceType>
|
2014-09-24 19:17:33 +04:00
|
|
|
static void
|
2016-02-17 00:55:33 +03:00
|
|
|
GetSources(MediaEngine *engine, MediaSourceEnum aSrcType,
|
|
|
|
void (MediaEngine::* aEnumerate)(MediaSourceEnum,
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<typename DeviceType::Source> >*),
|
|
|
|
nsTArray<RefPtr<DeviceType>>& aResult,
|
2015-07-03 01:01:52 +03:00
|
|
|
const char* media_device_name = nullptr)
|
2013-09-16 10:34:57 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<typename DeviceType::Source>> sources;
|
2014-04-04 13:54:25 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
(engine->*aEnumerate)(aSrcType, &sources);
|
|
|
|
/**
|
|
|
|
* We're allowing multiple tabs to access the same camera for parity
|
|
|
|
* with Chrome. See bug 811757 for some of the issues surrounding
|
|
|
|
* this decision. To disallow, we'd filter by IsAvailable() as we used
|
|
|
|
* to.
|
|
|
|
*/
|
|
|
|
if (media_device_name && *media_device_name) {
|
|
|
|
for (auto& source : sources) {
|
|
|
|
nsString deviceName;
|
|
|
|
source->GetName(deviceName);
|
|
|
|
if (deviceName.EqualsASCII(media_device_name)) {
|
|
|
|
aResult.AppendElement(new DeviceType(source));
|
|
|
|
break;
|
2013-11-11 14:39:54 +04:00
|
|
|
}
|
2013-09-16 10:34:57 +04:00
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
} else {
|
|
|
|
for (auto& source : sources) {
|
|
|
|
aResult.AppendElement(new DeviceType(source));
|
|
|
|
}
|
2013-09-16 10:34:57 +04:00
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// TODO: Remove once upgraded to GCC 4.8+ on linux. Bogus error on static func:
|
|
|
|
// error: 'this' was not captured for this lambda function
|
|
|
|
|
|
|
|
static auto& MediaManager_GetInstance = MediaManager::GetInstance;
|
|
|
|
static auto& MediaManager_ToJSArray = MediaManager::ToJSArray;
|
|
|
|
static auto& MediaManager_AnonymizeDevices = MediaManager::AnonymizeDevices;
|
|
|
|
|
|
|
|
already_AddRefed<MediaManager::PledgeChar>
|
|
|
|
MediaManager::SelectSettings(
|
|
|
|
MediaStreamConstraints& aConstraints,
|
2016-02-26 22:21:05 +03:00
|
|
|
RefPtr<Refcountable<UniquePtr<SourceSet>>>& aSources)
|
2015-07-03 01:01:52 +03:00
|
|
|
{
|
2015-10-04 03:42:26 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeChar> p = new PledgeChar();
|
2015-10-04 03:42:26 +03:00
|
|
|
uint32_t id = mOutstandingCharPledges.Append(*p);
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// Algorithm accesses device capabilities code and must run on media thread.
|
|
|
|
// Modifies passed-in aSources.
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
MediaManager::PostTask(NewTaskFrom([id, aConstraints, aSources]() mutable {
|
2015-10-04 03:42:26 +03:00
|
|
|
auto& sources = **aSources;
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// Since the advanced part of the constraints algorithm needs to know when
|
|
|
|
// a candidate set is overconstrained (zero members), we must split up the
|
|
|
|
// list into videos and audios, and put it back together again at the end.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<VideoDevice>> videos;
|
|
|
|
nsTArray<RefPtr<AudioDevice>> audios;
|
2015-09-18 21:04:41 +03:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
for (auto& source : sources) {
|
|
|
|
if (source->mIsVideo) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VideoDevice> video = static_cast<VideoDevice*>(source.get());
|
2015-10-04 03:42:26 +03:00
|
|
|
videos.AppendElement(video);
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioDevice> audio = static_cast<AudioDevice*>(source.get());
|
2015-10-04 03:42:26 +03:00
|
|
|
audios.AppendElement(audio);
|
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2015-10-04 03:42:26 +03:00
|
|
|
sources.Clear();
|
|
|
|
const char* badConstraint = nullptr;
|
2016-05-06 00:11:53 +03:00
|
|
|
bool needVideo = IsOn(aConstraints.mVideo);
|
|
|
|
bool needAudio = IsOn(aConstraints.mAudio);
|
2015-10-04 03:42:26 +03:00
|
|
|
|
2016-05-06 00:11:53 +03:00
|
|
|
if (needVideo && videos.Length()) {
|
2015-10-04 03:42:26 +03:00
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(
|
|
|
|
GetInvariant(aConstraints.mVideo), videos);
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2016-05-06 00:11:53 +03:00
|
|
|
if (!badConstraint && needAudio && audios.Length()) {
|
2015-10-04 03:42:26 +03:00
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(
|
|
|
|
GetInvariant(aConstraints.mAudio), audios);
|
2016-05-05 22:46:04 +03:00
|
|
|
}
|
2016-05-06 00:11:53 +03:00
|
|
|
if (!badConstraint &&
|
|
|
|
!needVideo == !videos.Length() &&
|
|
|
|
!needAudio == !audios.Length()) {
|
2016-05-05 22:46:04 +03:00
|
|
|
for (auto& video : videos) {
|
|
|
|
sources.AppendElement(video);
|
|
|
|
}
|
2015-10-04 03:42:26 +03:00
|
|
|
for (auto& audio : audios) {
|
|
|
|
sources.AppendElement(audio);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableFrom([id, badConstraint]() mutable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
|
|
|
RefPtr<PledgeChar> p = mgr->mOutstandingCharPledges.Remove(id);
|
2015-10-04 03:42:26 +03:00
|
|
|
if (p) {
|
|
|
|
p->Resolve(badConstraint);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2016-04-28 03:06:05 +03:00
|
|
|
}));
|
2015-10-04 03:42:26 +03:00
|
|
|
}));
|
|
|
|
return p.forget();
|
2013-09-16 10:34:57 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
/**
|
|
|
|
* Runs on a seperate thread and is responsible for enumerating devices.
|
|
|
|
* Depending on whether a picture or stream was asked for, either
|
2014-10-13 07:37:37 +04:00
|
|
|
* ProcessGetUserMedia is called, and the results are sent back to the DOM.
|
2012-06-03 11:35:15 +04:00
|
|
|
*
|
2012-07-12 15:53:08 +04:00
|
|
|
* Do not run this on the main thread. The success and error callbacks *MUST*
|
|
|
|
* be dispatched on the main thread!
|
2012-06-03 11:35:15 +04:00
|
|
|
*/
|
2016-04-28 03:06:05 +03:00
|
|
|
class GetUserMediaTask : public Runnable
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-12 18:49:39 +04:00
|
|
|
GetUserMediaTask(
|
2014-04-18 22:00:16 +04:00
|
|
|
const MediaStreamConstraints& aConstraints,
|
2014-10-18 04:47:46 +04:00
|
|
|
already_AddRefed<nsIDOMGetUserMediaSuccessCallback> aOnSuccess,
|
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aOnFailure,
|
2013-01-07 06:31:32 +04:00
|
|
|
uint64_t aWindowID, GetUserMediaCallbackMediaStreamListener *aListener,
|
2013-03-05 01:02:17 +04:00
|
|
|
MediaEnginePrefs &aPrefs,
|
2015-07-14 04:56:29 +03:00
|
|
|
const nsCString& aOrigin,
|
2015-07-03 01:01:52 +03:00
|
|
|
MediaManager::SourceSet* aSourceSet)
|
2013-09-16 10:34:57 +04:00
|
|
|
: mConstraints(aConstraints)
|
2014-10-18 04:47:46 +04:00
|
|
|
, mOnSuccess(aOnSuccess)
|
|
|
|
, mOnFailure(aOnFailure)
|
2012-09-29 02:26:00 +04:00
|
|
|
, mWindowID(aWindowID)
|
2013-01-07 06:31:32 +04:00
|
|
|
, mListener(aListener)
|
2013-03-05 01:02:17 +04:00
|
|
|
, mPrefs(aPrefs)
|
2015-07-14 04:56:29 +03:00
|
|
|
, mOrigin(aOrigin)
|
2012-09-29 02:26:00 +04:00
|
|
|
, mDeviceChosen(false)
|
2015-07-03 01:01:52 +03:00
|
|
|
, mSourceSet(aSourceSet)
|
2013-03-05 01:02:17 +04:00
|
|
|
, mManager(MediaManager::GetInstance())
|
|
|
|
{}
|
2012-09-29 02:26:00 +04:00
|
|
|
|
2014-09-12 18:49:39 +04:00
|
|
|
~GetUserMediaTask() {
|
2012-09-29 02:26:00 +04:00
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2014-01-09 01:51:33 +04:00
|
|
|
void
|
2016-06-14 21:42:46 +03:00
|
|
|
Fail(const nsAString& aName,
|
|
|
|
const nsAString& aMessage = EmptyString(),
|
|
|
|
const nsAString& aConstraint = EmptyString()) {
|
|
|
|
RefPtr<MediaMgrError> error = new MediaMgrError(aName, aMessage, aConstraint);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ErrorCallbackRunnable<nsIDOMGetUserMediaSuccessCallback>> runnable =
|
2015-02-23 19:50:48 +03:00
|
|
|
new ErrorCallbackRunnable<nsIDOMGetUserMediaSuccessCallback>(mOnSuccess,
|
|
|
|
mOnFailure,
|
|
|
|
*error,
|
|
|
|
mWindowID);
|
2014-03-15 23:00:15 +04:00
|
|
|
// These should be empty now
|
2014-10-18 04:47:46 +04:00
|
|
|
MOZ_ASSERT(!mOnSuccess);
|
|
|
|
MOZ_ASSERT(!mOnFailure);
|
2014-03-15 23:00:15 +04:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_DispatchToMainThread(runnable.forget());
|
2015-03-06 21:23:33 +03:00
|
|
|
// Do after ErrorCallbackRunnable Run()s, as it checks active window list
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_DispatchToMainThread(do_AddRef(new GetUserMediaListenerRemove(mWindowID, mListener)));
|
2014-01-09 01:51:33 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
2015-03-03 17:51:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-10-18 04:47:46 +04:00
|
|
|
MOZ_ASSERT(mOnSuccess);
|
|
|
|
MOZ_ASSERT(mOnFailure);
|
2015-07-03 01:01:52 +03:00
|
|
|
MOZ_ASSERT(mDeviceChosen);
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Allocate a video or audio device and return a MediaStream via
|
|
|
|
// a GetUserMediaStreamRunnable.
|
2012-09-29 02:26:00 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
nsresult rv;
|
2016-06-14 21:42:46 +03:00
|
|
|
const char* errorMsg = nullptr;
|
|
|
|
const char* badConstraint = nullptr;
|
2015-07-03 01:01:52 +03:00
|
|
|
|
|
|
|
if (mAudioDevice) {
|
2016-06-14 21:42:46 +03:00
|
|
|
auto& constraints = GetInvariant(mConstraints.mAudio);
|
2016-06-16 02:25:07 +03:00
|
|
|
rv = mAudioDevice->Allocate(constraints, mPrefs, mOrigin, &badConstraint);
|
2015-07-03 01:01:52 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-06-14 21:42:46 +03:00
|
|
|
errorMsg = "Failed to allocate audiosource";
|
2016-06-16 02:25:07 +03:00
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE && !badConstraint) {
|
2016-06-14 21:42:46 +03:00
|
|
|
nsTArray<RefPtr<AudioDevice>> audios;
|
|
|
|
audios.AppendElement(mAudioDevice);
|
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(constraints,
|
|
|
|
audios);
|
|
|
|
}
|
2012-09-20 23:54:00 +04:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 21:42:46 +03:00
|
|
|
if (!errorMsg && mVideoDevice) {
|
|
|
|
auto& constraints = GetInvariant(mConstraints.mVideo);
|
2016-06-16 02:25:07 +03:00
|
|
|
rv = mVideoDevice->Allocate(constraints, mPrefs, mOrigin, &badConstraint);
|
2015-07-03 01:01:52 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-06-14 21:42:46 +03:00
|
|
|
errorMsg = "Failed to allocate videosource";
|
2016-06-16 02:25:07 +03:00
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE && !badConstraint) {
|
2016-06-14 21:42:46 +03:00
|
|
|
nsTArray<RefPtr<VideoDevice>> videos;
|
|
|
|
videos.AppendElement(mVideoDevice);
|
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(constraints,
|
|
|
|
videos);
|
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
if (mAudioDevice) {
|
2016-06-30 22:43:24 +03:00
|
|
|
mAudioDevice->Deallocate();
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 21:42:46 +03:00
|
|
|
if (errorMsg) {
|
|
|
|
LOG(("%s %d", errorMsg, rv));
|
|
|
|
switch (rv) {
|
|
|
|
case NS_ERROR_NOT_AVAILABLE: {
|
|
|
|
MOZ_ASSERT(badConstraint);
|
|
|
|
Fail(NS_LITERAL_STRING("OverconstrainedError"),
|
|
|
|
NS_LITERAL_STRING(""),
|
|
|
|
NS_ConvertUTF8toUTF16(badConstraint));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Fail(NS_LITERAL_STRING("NotReadableError"),
|
|
|
|
NS_ConvertUTF8toUTF16(errorMsg));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
PeerIdentity* peerIdentity = nullptr;
|
|
|
|
if (!mConstraints.mPeerIdentity.IsEmpty()) {
|
|
|
|
peerIdentity = new PeerIdentity(mConstraints.mPeerIdentity);
|
|
|
|
}
|
|
|
|
|
2015-09-19 07:49:07 +03:00
|
|
|
NS_DispatchToMainThread(do_AddRef(
|
|
|
|
new GetUserMediaStreamRunnable(mOnSuccess, mOnFailure, mWindowID,
|
2015-11-25 07:42:26 +03:00
|
|
|
mListener, mOrigin, mConstraints,
|
|
|
|
mAudioDevice, mVideoDevice,
|
|
|
|
peerIdentity)));
|
2015-07-03 01:01:52 +03:00
|
|
|
MOZ_ASSERT(!mOnSuccess);
|
|
|
|
MOZ_ASSERT(!mOnFailure);
|
2016-04-28 03:06:05 +03:00
|
|
|
return NS_OK;
|
2012-06-03 11:35:15 +04:00
|
|
|
}
|
|
|
|
|
2012-10-06 04:20:47 +04:00
|
|
|
nsresult
|
2014-10-27 22:42:56 +03:00
|
|
|
Denied(const nsAString& aName,
|
|
|
|
const nsAString& aMessage = EmptyString())
|
2012-10-06 04:20:47 +04:00
|
|
|
{
|
2014-10-18 04:47:46 +04:00
|
|
|
MOZ_ASSERT(mOnSuccess);
|
|
|
|
MOZ_ASSERT(mOnFailure);
|
2014-01-09 01:51:33 +04:00
|
|
|
|
|
|
|
// We add a disabled listener to the StreamListeners array until accepted
|
|
|
|
// If this was the only active MediaStream, remove the window from the list.
|
2012-10-06 04:20:47 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2012-10-26 04:14:47 +04:00
|
|
|
// This is safe since we're on main-thread, and the window can only
|
|
|
|
// be invalidated from the main-thread (see OnNavigation)
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> onSuccess = mOnSuccess.forget();
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure = mOnFailure.forget();
|
2013-01-07 06:31:32 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
if (auto* window = nsGlobalWindow::GetInnerWindowWithId(mWindowID)) {
|
|
|
|
RefPtr<MediaStreamError> error = new MediaStreamError(window->AsInner(),
|
|
|
|
aName, aMessage);
|
2014-10-27 22:42:56 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
}
|
2013-01-07 06:31:32 +04:00
|
|
|
// Should happen *after* error runs for consistency, but may not matter
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> manager(MediaManager::GetInstance());
|
2013-01-07 06:31:32 +04:00
|
|
|
manager->RemoveFromWindowList(mWindowID, mListener);
|
2012-10-06 04:20:47 +04:00
|
|
|
} else {
|
2012-10-26 04:14:47 +04:00
|
|
|
// This will re-check the window being alive on main-thread
|
2015-03-06 21:23:33 +03:00
|
|
|
// and remove the listener on MainThread as well
|
2014-10-27 22:42:56 +03:00
|
|
|
Fail(aName, aMessage);
|
2012-10-06 04:20:47 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 04:47:46 +04:00
|
|
|
MOZ_ASSERT(!mOnSuccess);
|
|
|
|
MOZ_ASSERT(!mOnFailure);
|
2014-03-15 23:00:15 +04:00
|
|
|
|
2012-10-06 04:20:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-25 15:50:42 +04:00
|
|
|
nsresult
|
2014-04-18 22:00:16 +04:00
|
|
|
SetContraints(const MediaStreamConstraints& aConstraints)
|
2014-02-25 15:50:42 +04:00
|
|
|
{
|
|
|
|
mConstraints = aConstraints;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:54:04 +03:00
|
|
|
const MediaStreamConstraints&
|
|
|
|
GetConstraints()
|
|
|
|
{
|
|
|
|
return mConstraints;
|
|
|
|
}
|
|
|
|
|
2012-10-06 04:20:47 +04:00
|
|
|
nsresult
|
2014-04-18 23:15:10 +04:00
|
|
|
SetAudioDevice(AudioDevice* aAudioDevice)
|
2012-10-16 00:41:46 +04:00
|
|
|
{
|
|
|
|
mAudioDevice = aAudioDevice;
|
|
|
|
mDeviceChosen = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-04-18 23:15:10 +04:00
|
|
|
SetVideoDevice(VideoDevice* aVideoDevice)
|
2012-10-06 04:20:47 +04:00
|
|
|
{
|
2012-10-16 00:41:46 +04:00
|
|
|
mVideoDevice = aVideoDevice;
|
2012-10-06 04:20:47 +04:00
|
|
|
mDeviceChosen = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-20 23:54:00 +04:00
|
|
|
private:
|
2014-04-18 22:00:16 +04:00
|
|
|
MediaStreamConstraints mConstraints;
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> mOnSuccess;
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> mOnFailure;
|
2012-09-20 23:54:00 +04:00
|
|
|
uint64_t mWindowID;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> mListener;
|
|
|
|
RefPtr<AudioDevice> mAudioDevice;
|
|
|
|
RefPtr<VideoDevice> mVideoDevice;
|
2013-03-05 01:02:17 +04:00
|
|
|
MediaEnginePrefs mPrefs;
|
2015-07-14 04:56:29 +03:00
|
|
|
nsCString mOrigin;
|
2012-07-12 15:53:08 +04:00
|
|
|
|
2012-09-29 02:26:00 +04:00
|
|
|
bool mDeviceChosen;
|
2015-07-03 01:01:52 +03:00
|
|
|
public:
|
|
|
|
nsAutoPtr<MediaManager::SourceSet> mSourceSet;
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mManager; // get ref to this when creating the runnable
|
2012-09-20 23:54:00 +04:00
|
|
|
};
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2014-09-12 18:49:39 +04:00
|
|
|
#if defined(ANDROID) && !defined(MOZ_WIDGET_GONK)
|
2016-04-26 03:23:21 +03:00
|
|
|
class GetUserMediaRunnableWrapper : public Runnable
|
2014-09-12 18:49:39 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// This object must take ownership of task
|
|
|
|
GetUserMediaRunnableWrapper(GetUserMediaTask* task) :
|
|
|
|
mTask(task) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~GetUserMediaRunnableWrapper() {
|
|
|
|
}
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-09-12 18:49:39 +04:00
|
|
|
mTask->Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoPtr<GetUserMediaTask> mTask;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-09-20 23:54:00 +04:00
|
|
|
/**
|
2015-06-18 18:46:36 +03:00
|
|
|
* EnumerateRawDevices - Enumerate a list of audio & video devices that
|
|
|
|
* satisfy passed-in constraints. List contains raw id's.
|
2012-09-20 23:54:00 +04:00
|
|
|
*/
|
2013-11-11 14:39:54 +04:00
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
already_AddRefed<MediaManager::PledgeSourceSet>
|
2015-07-24 15:28:17 +03:00
|
|
|
MediaManager::EnumerateRawDevices(uint64_t aWindowId,
|
|
|
|
MediaSourceEnum aVideoType,
|
|
|
|
MediaSourceEnum aAudioType,
|
2015-07-03 01:01:52 +03:00
|
|
|
bool aFake, bool aFakeTracks)
|
2015-06-18 18:46:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-12-03 06:51:03 +03:00
|
|
|
MOZ_ASSERT(aVideoType != MediaSourceEnum::Other ||
|
|
|
|
aAudioType != MediaSourceEnum::Other);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = new PledgeSourceSet();
|
2015-06-18 18:46:36 +03:00
|
|
|
uint32_t id = mOutstandingPledges.Append(*p);
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
nsAdoptingCString audioLoopDev, videoLoopDev;
|
2015-11-09 18:17:42 +03:00
|
|
|
if (!aFake) {
|
|
|
|
// Fake stream not requested. The entire device stack is available.
|
|
|
|
// Loop in loopback devices if they are set, and their respective type is
|
|
|
|
// requested. This is currently used for automated media tests only.
|
|
|
|
if (aVideoType == MediaSourceEnum::Camera) {
|
2015-11-06 22:14:11 +03:00
|
|
|
videoLoopDev = Preferences::GetCString("media.video_loopback_dev");
|
|
|
|
}
|
2015-11-09 18:17:42 +03:00
|
|
|
if (aAudioType == MediaSourceEnum::Microphone) {
|
|
|
|
audioLoopDev = Preferences::GetCString("media.audio_loopback_dev");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aFake) {
|
|
|
|
// Fake tracks only make sense when we have a fake stream.
|
|
|
|
aFakeTracks = false;
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2015-06-18 18:46:36 +03:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
MediaManager::PostTask(NewTaskFrom([id, aWindowId, audioLoopDev,
|
|
|
|
videoLoopDev, aVideoType,
|
|
|
|
aAudioType, aFake,
|
|
|
|
aFakeTracks]() mutable {
|
2015-12-03 06:51:03 +03:00
|
|
|
// Only enumerate what's asked for, and only fake cams and mics.
|
|
|
|
bool hasVideo = aVideoType != MediaSourceEnum::Other;
|
|
|
|
bool hasAudio = aAudioType != MediaSourceEnum::Other;
|
|
|
|
bool fakeCams = aFake && aVideoType == MediaSourceEnum::Camera;
|
|
|
|
bool fakeMics = aFake && aAudioType == MediaSourceEnum::Microphone;
|
|
|
|
|
|
|
|
RefPtr<MediaEngine> fakeBackend, realBackend;
|
|
|
|
if (fakeCams || fakeMics) {
|
|
|
|
fakeBackend = new MediaEngineDefault(aFakeTracks);
|
|
|
|
}
|
|
|
|
if ((!fakeCams && hasVideo) || (!fakeMics && hasAudio)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> manager = MediaManager_GetInstance();
|
2015-12-03 06:51:03 +03:00
|
|
|
realBackend = manager->GetBackend(aWindowId);
|
2015-06-18 18:46:36 +03:00
|
|
|
}
|
2014-09-24 19:17:33 +04:00
|
|
|
|
2016-02-26 22:21:05 +03:00
|
|
|
auto result = MakeUnique<SourceSet>();
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2015-12-03 06:51:03 +03:00
|
|
|
if (hasVideo) {
|
|
|
|
nsTArray<RefPtr<VideoDevice>> videos;
|
|
|
|
GetSources(fakeCams? fakeBackend : realBackend, aVideoType,
|
|
|
|
&MediaEngine::EnumerateVideoDevices, videos, videoLoopDev);
|
|
|
|
for (auto& source : videos) {
|
|
|
|
result->AppendElement(source);
|
|
|
|
}
|
2014-04-18 23:16:08 +04:00
|
|
|
}
|
2015-12-03 06:51:03 +03:00
|
|
|
if (hasAudio) {
|
|
|
|
nsTArray<RefPtr<AudioDevice>> audios;
|
|
|
|
GetSources(fakeMics? fakeBackend : realBackend, aAudioType,
|
|
|
|
&MediaEngine::EnumerateAudioDevices, audios, audioLoopDev);
|
|
|
|
for (auto& source : audios) {
|
|
|
|
result->AppendElement(source);
|
|
|
|
}
|
2013-09-16 02:19:42 +04:00
|
|
|
}
|
2016-02-26 22:21:05 +03:00
|
|
|
SourceSet* handoff = result.release();
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableFrom([id, handoff]() mutable {
|
2016-02-26 22:21:05 +03:00
|
|
|
UniquePtr<SourceSet> result(handoff); // grab result
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-06-18 18:46:36 +03:00
|
|
|
if (!mgr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = mgr->mOutstandingPledges.Remove(id);
|
2015-06-18 18:46:36 +03:00
|
|
|
if (p) {
|
2016-02-26 22:21:05 +03:00
|
|
|
p->Resolve(result.release());
|
2015-06-18 18:46:36 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2016-04-28 03:06:05 +03:00
|
|
|
}));
|
2015-06-18 18:46:36 +03:00
|
|
|
}));
|
|
|
|
return p.forget();
|
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
MediaManager::MediaManager()
|
|
|
|
: mMediaThread(nullptr)
|
|
|
|
, mBackend(nullptr) {
|
2016-01-23 00:46:38 +03:00
|
|
|
mPrefs.mFreq = 1000; // 1KHz test tone
|
|
|
|
mPrefs.mWidth = 0; // adaptive default
|
|
|
|
mPrefs.mHeight = 0; // adaptive default
|
|
|
|
mPrefs.mFPS = MediaEngine::DEFAULT_VIDEO_FPS;
|
|
|
|
mPrefs.mMinFPS = MediaEngine::DEFAULT_VIDEO_MIN_FPS;
|
|
|
|
mPrefs.mAecOn = false;
|
|
|
|
mPrefs.mAgcOn = false;
|
|
|
|
mPrefs.mNoiseOn = false;
|
2016-04-20 03:20:45 +03:00
|
|
|
mPrefs.mExtendedFilter = true;
|
|
|
|
mPrefs.mDelayAgnostic = true;
|
2016-01-23 00:46:38 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
mPrefs.mAec = webrtc::kEcUnchanged;
|
|
|
|
mPrefs.mAgc = webrtc::kAgcUnchanged;
|
|
|
|
mPrefs.mNoise = webrtc::kNsUnchanged;
|
|
|
|
#else
|
|
|
|
mPrefs.mAec = 0;
|
|
|
|
mPrefs.mAgc = 0;
|
|
|
|
mPrefs.mNoise = 0;
|
|
|
|
#endif
|
|
|
|
mPrefs.mPlayoutDelay = 0;
|
2016-01-21 19:51:36 +03:00
|
|
|
mPrefs.mFullDuplex = false;
|
2013-03-05 01:02:17 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
|
|
|
|
if (branch) {
|
|
|
|
GetPrefs(branch, nullptr);
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 00:46:38 +03:00
|
|
|
LOG(("%s: default prefs: %dx%d @%dfps (min %d), %dHz test tones, aec: %s,"
|
|
|
|
"agc: %s, noise: %s, aec level: %d, agc level: %d, noise level: %d,"
|
2016-04-20 03:20:45 +03:00
|
|
|
"playout delay: %d, %sfull_duplex, extended aec %s, delay_agnostic %s",
|
|
|
|
__FUNCTION__, mPrefs.mWidth, mPrefs.mHeight,
|
2016-01-23 00:46:38 +03:00
|
|
|
mPrefs.mFPS, mPrefs.mMinFPS, mPrefs.mFreq, mPrefs.mAecOn ? "on" : "off",
|
|
|
|
mPrefs.mAgcOn ? "on": "off", mPrefs.mNoiseOn ? "on": "off", mPrefs.mAec,
|
2016-04-20 03:20:45 +03:00
|
|
|
mPrefs.mAgc, mPrefs.mNoise, mPrefs.mPlayoutDelay, mPrefs.mFullDuplex ? "" : "not ",
|
|
|
|
mPrefs.mExtendedFilter ? "on" : "off", mPrefs.mDelayAgnostic ? "on" : "off"));
|
2013-03-05 01:02:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MediaManager, nsIMediaManagerService, nsIObserver)
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2012-12-22 12:09:36 +04:00
|
|
|
/* static */ StaticRefPtr<MediaManager> MediaManager::sSingleton;
|
|
|
|
|
2015-03-03 17:51:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
/* static */ bool
|
|
|
|
MediaManager::IsInMediaThread()
|
|
|
|
{
|
|
|
|
return sSingleton?
|
|
|
|
(sSingleton->mMediaThread->thread_id() == PlatformThread::CurrentId()) :
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
// NOTE: never Dispatch(....,NS_DISPATCH_SYNC) to the MediaManager
|
|
|
|
// thread from the MainThread, as we NS_DISPATCH_SYNC to MainThread
|
|
|
|
// from MediaManager thread.
|
2015-09-04 02:09:34 +03:00
|
|
|
|
|
|
|
// Guaranteed never to return nullptr.
|
2013-03-05 01:02:17 +04:00
|
|
|
/* static */ MediaManager*
|
|
|
|
MediaManager::Get() {
|
|
|
|
if (!sSingleton) {
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-08 00:44:25 +03:00
|
|
|
|
2015-03-20 01:43:25 +03:00
|
|
|
static int timesCreated = 0;
|
|
|
|
timesCreated++;
|
2016-03-08 00:44:25 +03:00
|
|
|
MOZ_RELEASE_ASSERT(timesCreated == 1);
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
sSingleton = new MediaManager();
|
|
|
|
|
2014-09-12 18:49:39 +04:00
|
|
|
sSingleton->mMediaThread = new base::Thread("MediaManager");
|
|
|
|
base::Thread::Options options;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
options.message_loop_type = MessageLoop::TYPE_MOZILLA_NONMAINUITHREAD;
|
|
|
|
#else
|
|
|
|
options.message_loop_type = MessageLoop::TYPE_MOZILLA_NONMAINTHREAD;
|
|
|
|
#endif
|
|
|
|
if (!sSingleton->mMediaThread->StartWithOptions(options)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
LOG(("New Media thread for gum"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
2015-07-13 06:55:02 +03:00
|
|
|
obs->AddObserver(sSingleton, "last-pb-context-exited", false);
|
2015-07-03 01:01:52 +03:00
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:privileged:allow", false);
|
2013-03-05 01:02:17 +04:00
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:response:allow", false);
|
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:response:deny", false);
|
|
|
|
obs->AddObserver(sSingleton, "getUserMedia:revoke", false);
|
2013-09-13 12:51:48 +04:00
|
|
|
obs->AddObserver(sSingleton, "phone-state-changed", false);
|
2013-03-05 01:02:17 +04:00
|
|
|
}
|
|
|
|
// else MediaManager won't work properly and will leak (see bug 837874)
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefs) {
|
|
|
|
prefs->AddObserver("media.navigator.video.default_width", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.navigator.video.default_height", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.navigator.video.default_fps", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.navigator.video.default_minfps", sSingleton, false);
|
2016-01-21 19:51:36 +03:00
|
|
|
prefs->AddObserver("media.navigator.audio.fake_frequency", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.navigator.audio.full_duplex", sSingleton, false);
|
2016-01-23 00:46:38 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
prefs->AddObserver("media.getusermedia.aec_enabled", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.aec", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.agc_enabled", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.agc", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.noise_enabled", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.noise", sSingleton, false);
|
|
|
|
prefs->AddObserver("media.getusermedia.playout_delay", sSingleton, false);
|
|
|
|
#endif
|
2013-03-05 01:02:17 +04:00
|
|
|
}
|
2015-10-15 06:54:39 +03:00
|
|
|
|
|
|
|
// Prepare async shutdown
|
|
|
|
|
2015-11-18 23:42:16 +03:00
|
|
|
nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase = GetShutdownPhase();
|
2015-10-15 06:54:39 +03:00
|
|
|
|
|
|
|
class Blocker : public media::ShutdownBlocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Blocker()
|
|
|
|
: media::ShutdownBlocker(NS_LITERAL_STRING(
|
|
|
|
"Media shutdown: blocking on media thread")) {}
|
|
|
|
|
2015-11-18 23:42:16 +03:00
|
|
|
NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient*) override
|
2015-10-15 06:54:39 +03:00
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(MediaManager::GetIfExists());
|
|
|
|
MediaManager::GetIfExists()->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sSingleton->mShutdownBlocker = new Blocker();
|
2015-11-18 23:42:16 +03:00
|
|
|
nsresult rv = shutdownPhase->AddBlocker(sSingleton->mShutdownBlocker,
|
|
|
|
NS_LITERAL_STRING(__FILE__),
|
|
|
|
__LINE__,
|
|
|
|
NS_LITERAL_STRING("Media shutdown"));
|
2015-10-15 06:54:39 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
2015-07-03 01:01:52 +03:00
|
|
|
#ifdef MOZ_B2G
|
|
|
|
// Init MediaPermissionManager before sending out any permission requests.
|
|
|
|
(void) MediaPermissionManager::GetInstance();
|
|
|
|
#endif //MOZ_B2G
|
2013-03-05 01:02:17 +04:00
|
|
|
}
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2015-03-20 01:43:25 +03:00
|
|
|
/* static */ MediaManager*
|
|
|
|
MediaManager::GetIfExists() {
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2012-12-22 12:09:36 +04:00
|
|
|
/* static */ already_AddRefed<MediaManager>
|
|
|
|
MediaManager::GetInstance()
|
|
|
|
{
|
|
|
|
// so we can have non-refcounted getters
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> service = MediaManager::Get();
|
2012-12-22 12:09:36 +04:00
|
|
|
return service.forget();
|
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
media::Parent<media::NonE10s>*
|
|
|
|
MediaManager::GetNonE10sParent()
|
|
|
|
{
|
|
|
|
if (!mNonE10sParent) {
|
2016-02-26 22:21:05 +03:00
|
|
|
mNonE10sParent = MakeUnique<media::Parent<media::NonE10s>>(true);
|
2015-06-18 18:46:36 +03:00
|
|
|
}
|
2016-02-26 22:21:05 +03:00
|
|
|
return mNonE10sParent.get();
|
2015-06-18 18:46:36 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 22:57:42 +03:00
|
|
|
/* static */ void
|
|
|
|
MediaManager::StartupInit()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
if (IsVistaOrLater() && !IsWin8OrLater()) {
|
|
|
|
// Bug 1107702 - Older Windows fail in GetAdaptersInfo (and others) if the
|
|
|
|
// first(?) call occurs after the process size is over 2GB (kb/2588507).
|
|
|
|
// Attempt to 'prime' the pump by making a call at startup.
|
|
|
|
unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO);
|
|
|
|
PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *) moz_xmalloc(out_buf_len);
|
|
|
|
if (GetAdaptersInfo(pAdapterInfo, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
pAdapterInfo = (IP_ADAPTER_INFO *) moz_xmalloc(out_buf_len);
|
|
|
|
GetAdaptersInfo(pAdapterInfo, &out_buf_len);
|
|
|
|
}
|
|
|
|
if (pAdapterInfo) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-12 18:49:39 +04:00
|
|
|
/* static */
|
2015-05-29 21:28:03 +03:00
|
|
|
void
|
2016-04-28 03:06:05 +03:00
|
|
|
MediaManager::PostTask(already_AddRefed<Runnable> task)
|
2014-09-12 18:49:39 +04:00
|
|
|
{
|
2015-05-29 21:28:03 +03:00
|
|
|
if (sInShutdown) {
|
|
|
|
// Can't safely delete task here since it may have items with specific
|
|
|
|
// thread-release requirements.
|
2016-04-28 03:06:05 +03:00
|
|
|
// XXXkhuey well then who is supposed to delete it?! We don't signal
|
|
|
|
// that we failed ...
|
|
|
|
MOZ_CRASH();
|
2015-05-29 21:28:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-09-12 18:49:39 +04:00
|
|
|
NS_ASSERTION(Get(), "MediaManager singleton?");
|
|
|
|
NS_ASSERTION(Get()->mMediaThread, "No thread yet");
|
2016-04-28 03:06:05 +03:00
|
|
|
Get()->mMediaThread->message_loop()->PostTask(Move(task));
|
2014-09-12 18:49:39 +04:00
|
|
|
}
|
|
|
|
|
2013-11-26 10:22:16 +04:00
|
|
|
/* static */ nsresult
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaManager::NotifyRecordingStatusChange(nsPIDOMWindowInner* aWindow,
|
2013-11-26 10:22:16 +04:00
|
|
|
const nsString& aMsg,
|
|
|
|
const bool& aIsAudio,
|
|
|
|
const bool& aIsVideo)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
NS_WARNING("Could not get the Observer service for GetUserMedia recording notification.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHashPropertyBag> props = new nsHashPropertyBag();
|
2013-11-26 10:22:16 +04:00
|
|
|
props->SetPropertyAsBool(NS_LITERAL_STRING("isAudio"), aIsAudio);
|
|
|
|
props->SetPropertyAsBool(NS_LITERAL_STRING("isVideo"), aIsVideo);
|
|
|
|
|
|
|
|
bool isApp = false;
|
|
|
|
nsString requestURL;
|
|
|
|
|
|
|
|
if (nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell()) {
|
|
|
|
nsresult rv = docShell->GetIsApp(&isApp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (isApp) {
|
|
|
|
rv = docShell->GetAppManifestURL(requestURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isApp) {
|
|
|
|
nsCString pageURL;
|
|
|
|
nsCOMPtr<nsIURI> docURI = aWindow->GetDocumentURI();
|
|
|
|
NS_ENSURE_TRUE(docURI, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = docURI->GetSpec(pageURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
requestURL = NS_ConvertUTF8toUTF16(pageURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
props->SetPropertyAsBool(NS_LITERAL_STRING("isApp"), isApp);
|
|
|
|
props->SetPropertyAsAString(NS_LITERAL_STRING("requestURL"), requestURL);
|
|
|
|
|
|
|
|
obs->NotifyObservers(static_cast<nsIPropertyBag2*>(props),
|
2014-05-01 14:51:00 +04:00
|
|
|
"recording-device-events",
|
|
|
|
aMsg.get());
|
2013-11-26 10:22:16 +04:00
|
|
|
|
|
|
|
// Forward recording events to parent process.
|
|
|
|
// The events are gathered in chrome process and used for recording indicator
|
2015-07-04 04:29:00 +03:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused <<
|
2013-11-26 10:22:16 +04:00
|
|
|
dom::ContentChild::GetSingleton()->SendRecordingDeviceEvents(aMsg,
|
|
|
|
requestURL,
|
|
|
|
aIsAudio,
|
|
|
|
aIsVideo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
bool MediaManager::IsPrivileged()
|
|
|
|
{
|
|
|
|
bool permission = nsContentUtils::IsCallerChrome();
|
|
|
|
|
|
|
|
// Developer preference for turning off permission check.
|
|
|
|
if (Preferences::GetBool("media.navigator.permission.disabled", false)) {
|
|
|
|
permission = true;
|
|
|
|
}
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaManager::IsLoop(nsIURI* aDocURI)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDocURI);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> loopURI;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(loopURI), "about:loopconversation");
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool result = false;
|
|
|
|
rv = aDocURI->EqualsExceptRef(loopURI, &result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
bool MediaManager::IsPrivateBrowsing(nsPIDOMWindowInner* window)
|
2015-07-03 01:01:52 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetDoc();
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
|
|
|
|
return loadContext && loadContext->UsePrivateBrowsing();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult MediaManager::GenerateUUID(nsAString& aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Generate a call ID.
|
|
|
|
nsID id;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char buffer[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(buffer);
|
|
|
|
aResult.Assign(NS_ConvertUTF8toUTF16(buffer));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-17 15:44:50 +03:00
|
|
|
enum class GetUserMediaSecurityState {
|
|
|
|
Other = 0,
|
|
|
|
HTTPS = 1,
|
|
|
|
File = 2,
|
|
|
|
App = 3,
|
|
|
|
Localhost = 4,
|
|
|
|
Loop = 5,
|
|
|
|
Privileged = 6
|
|
|
|
};
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
/**
|
|
|
|
* The entry point for this file. A call from Navigator::mozGetUserMedia
|
|
|
|
* will end up here. MediaManager is a singleton that is responsible
|
|
|
|
* for handling all incoming getUserMedia calls from every window.
|
|
|
|
*/
|
|
|
|
nsresult
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaManager::GetUserMedia(nsPIDOMWindowInner* aWindow,
|
2015-07-03 01:01:52 +03:00
|
|
|
const MediaStreamConstraints& aConstraintsPassedIn,
|
2015-06-18 18:46:36 +03:00
|
|
|
nsIDOMGetUserMediaSuccessCallback* aOnSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* aOnFailure)
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
2015-06-18 18:46:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aOnFailure);
|
|
|
|
MOZ_ASSERT(aOnSuccess);
|
2012-09-20 23:54:00 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaSuccessCallback> onSuccess(aOnSuccess);
|
2014-10-18 04:47:46 +04:00
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure(aOnFailure);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t windowID = aWindow->WindowID();
|
2013-09-16 10:34:57 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
MediaStreamConstraints c(aConstraintsPassedIn); // use a modifiable copy
|
2013-01-07 06:31:32 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Do all the validation we can while we're sync (to return an
|
|
|
|
// already-rejected promise on failure).
|
2012-10-25 03:21:15 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
if (!IsOn(c.mVideo) && !IsOn(c.mAudio)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-07-03 01:01:52 +03:00
|
|
|
new MediaStreamError(aWindow,
|
|
|
|
NS_LITERAL_STRING("NotSupportedError"),
|
|
|
|
NS_LITERAL_STRING("audio and/or video is required"));
|
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
2012-10-26 04:14:47 +04:00
|
|
|
}
|
2015-05-29 21:28:03 +03:00
|
|
|
if (sInShutdown) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-07-03 01:01:52 +03:00
|
|
|
new MediaStreamError(aWindow,
|
|
|
|
NS_LITERAL_STRING("AbortError"),
|
|
|
|
NS_LITERAL_STRING("In shutdown"));
|
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
2015-05-29 21:28:03 +03:00
|
|
|
}
|
2012-10-25 03:21:15 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Determine permissions early (while we still have a stack).
|
2014-07-31 01:03:20 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
nsIURI* docURI = aWindow->GetDocumentURI();
|
2015-07-14 04:56:29 +03:00
|
|
|
if (!docURI) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
bool loop = IsLoop(docURI);
|
|
|
|
bool privileged = loop || IsPrivileged();
|
|
|
|
bool isHTTPS = false;
|
2015-07-14 04:56:29 +03:00
|
|
|
docURI->SchemeIs("https", &isHTTPS);
|
2015-09-17 15:44:50 +03:00
|
|
|
nsCString host;
|
|
|
|
nsresult rv = docURI->GetHost(host);
|
|
|
|
// Test for some other schemes that ServiceWorker recognizes
|
|
|
|
bool isFile;
|
|
|
|
docURI->SchemeIs("file", &isFile);
|
|
|
|
bool isApp;
|
|
|
|
docURI->SchemeIs("app", &isApp);
|
|
|
|
// Same localhost check as ServiceWorkers uses
|
2016-04-26 13:30:43 +03:00
|
|
|
// (see IsOriginPotentiallyTrustworthy())
|
2015-09-17 15:44:50 +03:00
|
|
|
bool isLocalhost = NS_SUCCEEDED(rv) &&
|
|
|
|
(host.LowerCaseEqualsLiteral("localhost") ||
|
|
|
|
host.LowerCaseEqualsLiteral("127.0.0.1") ||
|
|
|
|
host.LowerCaseEqualsLiteral("::1"));
|
|
|
|
|
|
|
|
// Record telemetry about whether the source of the call was secure, i.e.,
|
|
|
|
// privileged or HTTPS. We may handle other cases
|
|
|
|
if (loop) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::Loop);
|
|
|
|
} else if (privileged) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::Privileged);
|
|
|
|
} else if (isHTTPS) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::HTTPS);
|
|
|
|
} else if (isFile) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::File);
|
|
|
|
} else if (isApp) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::App);
|
|
|
|
} else if (isLocalhost) {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::Localhost);
|
|
|
|
} else {
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
|
|
|
|
(uint32_t) GetUserMediaSecurityState::Other);
|
|
|
|
}
|
2015-07-14 04:56:29 +03:00
|
|
|
|
|
|
|
nsCString origin;
|
2015-09-17 15:44:50 +03:00
|
|
|
rv = nsPrincipal::GetOriginForURI(docURI, origin);
|
2015-07-14 04:56:29 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2015-02-03 00:53:19 +03:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
if (!Preferences::GetBool("media.navigator.video.enabled", true)) {
|
|
|
|
c.mVideo.SetAsBoolean() = false;
|
2015-02-03 00:53:19 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum videoType = MediaSourceEnum::Other; // none
|
|
|
|
MediaSourceEnum audioType = MediaSourceEnum::Other; // none
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2014-07-31 01:03:20 +04:00
|
|
|
if (c.mVideo.IsMediaTrackConstraints()) {
|
2015-02-21 01:06:26 +03:00
|
|
|
auto& vc = c.mVideo.GetAsMediaTrackConstraints();
|
2015-07-03 01:01:52 +03:00
|
|
|
videoType = StringToEnum(dom::MediaSourceEnumValues::strings,
|
|
|
|
vc.mMediaSource,
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum::Other);
|
2015-09-22 05:20:45 +03:00
|
|
|
Telemetry::Accumulate(loop ? Telemetry::LOOP_GET_USER_MEDIA_TYPE :
|
|
|
|
Telemetry::WEBRTC_GET_USER_MEDIA_TYPE,
|
|
|
|
(uint32_t) videoType);
|
2015-07-03 01:01:52 +03:00
|
|
|
switch (videoType) {
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Camera:
|
2015-07-03 01:01:52 +03:00
|
|
|
break;
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Browser:
|
2015-10-23 00:03:47 +03:00
|
|
|
// If no window id is passed in then default to the caller's window.
|
|
|
|
// Functional defaults are helpful in tests, but also a natural outcome
|
|
|
|
// of the constraints API's limited semantics for requiring input.
|
|
|
|
if (!vc.mBrowserWindow.WasPassed()) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter* outer = aWindow->GetOuterWindow();
|
2015-10-23 00:03:47 +03:00
|
|
|
vc.mBrowserWindow.Construct(outer->WindowID());
|
|
|
|
}
|
2016-01-14 12:42:18 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Screen:
|
|
|
|
case MediaSourceEnum::Application:
|
|
|
|
case MediaSourceEnum::Window:
|
2015-07-03 01:01:52 +03:00
|
|
|
// Deny screensharing request if support is disabled, or
|
|
|
|
// the requesting document is not from a host on the whitelist, or
|
2016-07-07 19:22:41 +03:00
|
|
|
// we're on WinXP until proved that it works
|
2016-02-17 00:55:33 +03:00
|
|
|
if (!Preferences::GetBool(((videoType == MediaSourceEnum::Browser)?
|
2015-07-03 01:01:52 +03:00
|
|
|
"media.getusermedia.browser.enabled" :
|
|
|
|
"media.getusermedia.screensharing.enabled"),
|
|
|
|
false) ||
|
2016-07-07 19:22:41 +03:00
|
|
|
#if defined(XP_WIN)
|
2015-07-03 01:01:52 +03:00
|
|
|
(
|
2016-07-07 19:22:41 +03:00
|
|
|
// Allow tab sharing for all platforms including XP
|
2016-02-17 00:55:33 +03:00
|
|
|
(videoType != MediaSourceEnum::Browser) &&
|
2015-07-03 01:01:52 +03:00
|
|
|
!Preferences::GetBool("media.getusermedia.screensharing.allow_on_old_platforms",
|
2016-07-07 19:22:41 +03:00
|
|
|
false) && !IsVistaOrLater()) ||
|
2015-07-03 01:01:52 +03:00
|
|
|
#endif
|
2016-02-23 23:54:00 +03:00
|
|
|
(!privileged && !HostIsHttps(*docURI)) ||
|
|
|
|
!(loop || HostHasPermission(*docURI))) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-07-03 01:01:52 +03:00
|
|
|
new MediaStreamError(aWindow,
|
2016-05-09 05:29:46 +03:00
|
|
|
NS_LITERAL_STRING("NotAllowedError"));
|
2015-07-03 01:01:52 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Microphone:
|
|
|
|
case MediaSourceEnum::Other:
|
2015-07-03 01:01:52 +03:00
|
|
|
default: {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-09-18 21:04:41 +03:00
|
|
|
new MediaStreamError(aWindow,
|
|
|
|
NS_LITERAL_STRING("OverconstrainedError"),
|
|
|
|
NS_LITERAL_STRING(""),
|
|
|
|
NS_LITERAL_STRING("mediaSource"));
|
2015-07-03 01:01:52 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
if (vc.mAdvanced.WasPassed() && videoType != MediaSourceEnum::Camera) {
|
2015-08-10 19:16:30 +03:00
|
|
|
// iterate through advanced, forcing all unset mediaSources to match "root"
|
|
|
|
const char *unset = EnumToASCII(dom::MediaSourceEnumValues::strings,
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum::Camera);
|
2015-07-03 01:01:52 +03:00
|
|
|
for (MediaTrackConstraintSet& cs : vc.mAdvanced.Value()) {
|
2015-08-10 19:16:30 +03:00
|
|
|
if (cs.mMediaSource.EqualsASCII(unset)) {
|
2015-07-03 01:01:52 +03:00
|
|
|
cs.mMediaSource = vc.mMediaSource;
|
2015-02-21 01:06:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!privileged) {
|
|
|
|
// only allow privileged content to set the window id
|
|
|
|
if (vc.mBrowserWindow.WasPassed()) {
|
2015-07-23 07:44:52 +03:00
|
|
|
vc.mBrowserWindow.Value() = -1;
|
2015-02-21 01:06:26 +03:00
|
|
|
}
|
|
|
|
if (vc.mAdvanced.WasPassed()) {
|
|
|
|
for (MediaTrackConstraintSet& cs : vc.mAdvanced.Value()) {
|
|
|
|
if (cs.mBrowserWindow.WasPassed()) {
|
2015-07-23 07:44:52 +03:00
|
|
|
cs.mBrowserWindow.Value() = -1;
|
2015-02-21 01:06:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 00:53:19 +03:00
|
|
|
// For all but tab sharing, Loop needs to prompt as we are using the
|
|
|
|
// permission menu for selection of the device currently. For tab sharing,
|
|
|
|
// Loop has implicit permissions within Firefox, as it is built-in,
|
|
|
|
// and will manage the active tab and provide appropriate UI.
|
2016-02-17 00:55:33 +03:00
|
|
|
if (loop && (videoType == MediaSourceEnum::Window ||
|
|
|
|
videoType == MediaSourceEnum::Application ||
|
|
|
|
videoType == MediaSourceEnum::Screen)) {
|
2015-02-03 00:53:19 +03:00
|
|
|
privileged = false;
|
|
|
|
}
|
2015-12-03 06:51:03 +03:00
|
|
|
} else if (IsOn(c.mVideo)) {
|
2016-02-17 00:55:33 +03:00
|
|
|
videoType = MediaSourceEnum::Camera;
|
2014-07-08 10:01:27 +04:00
|
|
|
}
|
2015-07-24 15:28:17 +03:00
|
|
|
|
|
|
|
if (c.mAudio.IsMediaTrackConstraints()) {
|
|
|
|
auto& ac = c.mAudio.GetAsMediaTrackConstraints();
|
|
|
|
audioType = StringToEnum(dom::MediaSourceEnumValues::strings,
|
|
|
|
ac.mMediaSource,
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum::Other);
|
2015-08-10 19:16:30 +03:00
|
|
|
// Work around WebIDL default since spec uses same dictionary w/audio & video.
|
2016-02-17 00:55:33 +03:00
|
|
|
if (audioType == MediaSourceEnum::Camera) {
|
|
|
|
audioType = MediaSourceEnum::Microphone;
|
2015-08-10 19:16:30 +03:00
|
|
|
ac.mMediaSource.AssignASCII(EnumToASCII(dom::MediaSourceEnumValues::strings,
|
|
|
|
audioType));
|
|
|
|
}
|
2015-09-22 05:20:45 +03:00
|
|
|
Telemetry::Accumulate(loop ? Telemetry::LOOP_GET_USER_MEDIA_TYPE :
|
|
|
|
Telemetry::WEBRTC_GET_USER_MEDIA_TYPE,
|
|
|
|
(uint32_t) audioType);
|
2015-08-10 19:16:30 +03:00
|
|
|
|
|
|
|
switch (audioType) {
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Microphone:
|
2015-08-10 19:16:30 +03:00
|
|
|
break;
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::AudioCapture:
|
2015-08-10 19:16:30 +03:00
|
|
|
// Only enable AudioCapture if the pref is enabled. If it's not, we can
|
|
|
|
// deny right away.
|
|
|
|
if (!Preferences::GetBool("media.getusermedia.audiocapture.enabled")) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-08-10 19:16:30 +03:00
|
|
|
new MediaStreamError(aWindow,
|
2016-05-09 05:29:46 +03:00
|
|
|
NS_LITERAL_STRING("NotAllowedError"));
|
2015-08-10 19:16:30 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-02-17 00:55:33 +03:00
|
|
|
case MediaSourceEnum::Other:
|
2015-08-10 19:16:30 +03:00
|
|
|
default: {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-09-18 21:04:41 +03:00
|
|
|
new MediaStreamError(aWindow,
|
|
|
|
NS_LITERAL_STRING("OverconstrainedError"),
|
|
|
|
NS_LITERAL_STRING(""),
|
|
|
|
NS_LITERAL_STRING("mediaSource"));
|
2015-08-10 19:16:30 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ac.mAdvanced.WasPassed()) {
|
|
|
|
// iterate through advanced, forcing all unset mediaSources to match "root"
|
|
|
|
const char *unset = EnumToASCII(dom::MediaSourceEnumValues::strings,
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum::Camera);
|
2015-08-10 19:16:30 +03:00
|
|
|
for (MediaTrackConstraintSet& cs : ac.mAdvanced.Value()) {
|
|
|
|
if (cs.mMediaSource.EqualsASCII(unset)) {
|
|
|
|
cs.mMediaSource = ac.mMediaSource;
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 15:28:17 +03:00
|
|
|
}
|
2015-12-03 06:51:03 +03:00
|
|
|
} else if (IsOn(c.mAudio)) {
|
2016-02-17 00:55:33 +03:00
|
|
|
audioType = MediaSourceEnum::Microphone;
|
2015-07-24 15:28:17 +03:00
|
|
|
}
|
2015-12-03 06:51:03 +03:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
StreamListeners* listeners = AddWindowID(windowID);
|
2014-07-08 10:01:27 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Create a disabled listener to act as a placeholder
|
2016-04-06 15:56:44 +03:00
|
|
|
nsIPrincipal* principal = aWindow->GetExtantDoc()->NodePrincipal();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> listener =
|
2016-04-06 15:56:44 +03:00
|
|
|
new GetUserMediaCallbackMediaStreamListener(mMediaThread, windowID,
|
|
|
|
MakePrincipalHandle(principal));
|
2013-05-02 16:00:12 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// No need for locking because we always do this in the main thread.
|
|
|
|
listeners->AppendElement(listener);
|
2014-04-16 10:22:19 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
if (!privileged) {
|
|
|
|
// Check if this site has had persistent permissions denied.
|
2014-02-25 15:50:42 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permManager =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uint32_t audioPerm = nsIPermissionManager::UNKNOWN_ACTION;
|
2014-04-04 13:54:25 +04:00
|
|
|
if (IsOn(c.mAudio)) {
|
2014-02-25 15:50:42 +04:00
|
|
|
rv = permManager->TestExactPermissionFromPrincipal(
|
2016-04-06 15:56:44 +03:00
|
|
|
principal, "microphone", &audioPerm);
|
2014-02-25 15:50:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t videoPerm = nsIPermissionManager::UNKNOWN_ACTION;
|
2014-04-04 13:54:25 +04:00
|
|
|
if (IsOn(c.mVideo)) {
|
2014-02-25 15:50:42 +04:00
|
|
|
rv = permManager->TestExactPermissionFromPrincipal(
|
2016-04-06 15:56:44 +03:00
|
|
|
principal, "camera", &videoPerm);
|
2014-02-25 15:50:42 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:54:04 +03:00
|
|
|
if ((!IsOn(c.mAudio) && !IsOn(c.mVideo)) ||
|
|
|
|
(IsOn(c.mAudio) && audioPerm == nsIPermissionManager::DENY_ACTION) ||
|
|
|
|
(IsOn(c.mVideo) && videoPerm == nsIPermissionManager::DENY_ACTION)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2016-05-09 05:29:46 +03:00
|
|
|
new MediaStreamError(aWindow, NS_LITERAL_STRING("NotAllowedError"));
|
2015-07-03 01:01:52 +03:00
|
|
|
onFailure->OnError(error);
|
|
|
|
RemoveFromWindowList(windowID, listener);
|
|
|
|
return NS_OK;
|
2014-02-25 15:50:42 +04:00
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2014-02-25 15:50:42 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
#if defined(MOZ_B2G_CAMERA) && defined(MOZ_WIDGET_GONK)
|
|
|
|
if (mCameraManager == nullptr) {
|
|
|
|
mCameraManager = nsDOMCameraManager::CreateInstance(aWindow);
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Get list of all devices, with origin-specific device ids.
|
|
|
|
|
|
|
|
MediaEnginePrefs prefs = mPrefs;
|
|
|
|
|
|
|
|
nsString callID;
|
2015-07-14 04:56:29 +03:00
|
|
|
rv = GenerateUUID(callID);
|
2015-07-03 01:01:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool fake = c.mFake.WasPassed()? c.mFake.Value() :
|
|
|
|
Preferences::GetBool("media.navigator.streams.fake");
|
|
|
|
|
|
|
|
bool fakeTracks = c.mFakeTracks.WasPassed()? c.mFakeTracks.Value() : false;
|
|
|
|
|
|
|
|
bool askPermission = !privileged &&
|
|
|
|
(!fake || Preferences::GetBool("media.navigator.permission.fake"));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = EnumerateDevicesImpl(windowID, videoType,
|
2015-12-03 06:51:03 +03:00
|
|
|
audioType, fake,
|
|
|
|
fakeTracks);
|
2015-07-14 04:56:29 +03:00
|
|
|
p->Then([this, onSuccess, onFailure, windowID, c, listener, askPermission,
|
|
|
|
prefs, isHTTPS, callID, origin](SourceSet*& aDevices) mutable {
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2016-02-26 22:21:05 +03:00
|
|
|
RefPtr<Refcountable<UniquePtr<SourceSet>>> devices(
|
|
|
|
new Refcountable<UniquePtr<SourceSet>>(aDevices)); // grab result
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// Ensure that the captured 'this' pointer and our windowID are still good.
|
|
|
|
if (!MediaManager::Exists() ||
|
|
|
|
!nsGlobalWindow::GetInnerWindowWithId(windowID)) {
|
2015-07-03 01:01:52 +03:00
|
|
|
return;
|
|
|
|
}
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// Apply any constraints. This modifies the passed-in list.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeChar> p2 = SelectSettings(c, devices);
|
2015-10-04 03:42:26 +03:00
|
|
|
|
|
|
|
p2->Then([this, onSuccess, onFailure, windowID, c,
|
|
|
|
listener, askPermission, prefs, isHTTPS,
|
|
|
|
callID, origin, devices](const char*& badConstraint) mutable {
|
|
|
|
|
|
|
|
// Ensure that the captured 'this' pointer and our windowID are still good.
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* globalWindow = nsGlobalWindow::GetInnerWindowWithId(windowID);
|
|
|
|
RefPtr<nsPIDOMWindowInner> window = globalWindow ? globalWindow->AsInner()
|
|
|
|
: nullptr;
|
2015-10-04 03:42:26 +03:00
|
|
|
if (!MediaManager::Exists() || !window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badConstraint) {
|
|
|
|
nsString constraint;
|
|
|
|
constraint.AssignASCII(badConstraint);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-10-04 03:42:26 +03:00
|
|
|
new MediaStreamError(window,
|
|
|
|
NS_LITERAL_STRING("OverconstrainedError"),
|
|
|
|
NS_LITERAL_STRING(""),
|
|
|
|
constraint);
|
|
|
|
onFailure->OnError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(*devices)->Length()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2015-10-04 03:42:26 +03:00
|
|
|
new MediaStreamError(window, NS_LITERAL_STRING("NotFoundError"));
|
|
|
|
onFailure->OnError(error);
|
2015-07-03 01:01:52 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-10-04 03:42:26 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsArray> devicesCopy; // before we give up devices below
|
|
|
|
if (!askPermission) {
|
|
|
|
nsresult rv = NS_NewISupportsArray(getter_AddRefs(devicesCopy));
|
2015-07-03 01:01:52 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-04 03:42:26 +03:00
|
|
|
for (auto& device : **devices) {
|
|
|
|
rv = devicesCopy->AppendElement(device);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
// Pass callbacks and MediaStreamListener along to GetUserMediaTask.
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<GetUserMediaTask> task (new GetUserMediaTask(c, onSuccess.forget(),
|
|
|
|
onFailure.forget(),
|
|
|
|
windowID, listener,
|
|
|
|
prefs, origin,
|
|
|
|
devices->release()));
|
2015-10-04 03:42:26 +03:00
|
|
|
// Store the task w/callbacks.
|
|
|
|
mActiveCallbacks.Put(callID, task.forget());
|
|
|
|
|
|
|
|
// Add a WindowID cross-reference so OnNavigation can tear things down
|
|
|
|
nsTArray<nsString>* array;
|
|
|
|
if (!mCallIds.Get(windowID, &array)) {
|
|
|
|
array = new nsTArray<nsString>();
|
|
|
|
mCallIds.Put(windowID, array);
|
|
|
|
}
|
|
|
|
array->AppendElement(callID);
|
2015-07-03 01:01:52 +03:00
|
|
|
|
2015-10-04 03:42:26 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (!askPermission) {
|
|
|
|
obs->NotifyObservers(devicesCopy, "getUserMedia:privileged:allow",
|
|
|
|
callID.BeginReading());
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaRequest> req =
|
2015-10-04 03:42:26 +03:00
|
|
|
new GetUserMediaRequest(window, callID, c, isHTTPS);
|
|
|
|
obs->NotifyObservers(req, "getUserMedia:request", nullptr);
|
|
|
|
}
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2014-06-09 22:45:14 +04:00
|
|
|
#ifdef MOZ_WEBRTC
|
2015-10-04 03:42:26 +03:00
|
|
|
EnableWebRtcLog();
|
2014-06-09 22:45:14 +04:00
|
|
|
#endif
|
2015-10-04 03:42:26 +03:00
|
|
|
}, [onFailure](MediaStreamError*& reason) mutable {
|
|
|
|
onFailure->OnError(reason);
|
|
|
|
});
|
2015-09-20 09:26:41 +03:00
|
|
|
}, [onFailure](MediaStreamError*& reason) mutable {
|
|
|
|
onFailure->OnError(reason);
|
2015-07-03 01:01:52 +03:00
|
|
|
});
|
2012-06-03 11:35:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
/* static */ void
|
|
|
|
MediaManager::AnonymizeDevices(SourceSet& aDevices, const nsACString& aOriginKey)
|
2012-09-20 23:54:00 +04:00
|
|
|
{
|
2015-06-18 18:46:36 +03:00
|
|
|
if (!aOriginKey.IsEmpty()) {
|
|
|
|
for (auto& device : aDevices) {
|
|
|
|
nsString id;
|
|
|
|
device->GetId(id);
|
|
|
|
AnonymizeId(id, aOriginKey);
|
|
|
|
device->SetId(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
/* static */ nsresult
|
|
|
|
MediaManager::AnonymizeId(nsAString& aId, const nsACString& aOriginKey)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-18 22:31:00 +04:00
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIKeyObjectFactory> factory =
|
|
|
|
do_GetService("@mozilla.org/security/keyobjectfactory;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCString rawKey;
|
|
|
|
rv = Base64Decode(aOriginKey, rawKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIKeyObject> key;
|
|
|
|
rv = factory->KeyFromString(nsIKeyObject::HMAC, rawKey, getter_AddRefs(key));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICryptoHMAC> hasher =
|
|
|
|
do_CreateInstance(NS_CRYPTO_HMAC_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = hasher->Init(nsICryptoHMAC::SHA256, key);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
NS_ConvertUTF16toUTF8 id(aId);
|
|
|
|
rv = hasher->Update(reinterpret_cast<const uint8_t*> (id.get()), id.Length());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCString mac;
|
|
|
|
rv = hasher->Finish(true, mac);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
aId = NS_ConvertUTF8toUTF16(mac);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<nsIWritableVariant>
|
|
|
|
MediaManager::ToJSArray(SourceSet& aDevices)
|
|
|
|
{
|
2015-10-09 20:24:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsVariantCC> var = new nsVariantCC();
|
2015-06-18 18:46:36 +03:00
|
|
|
size_t len = aDevices.Length();
|
|
|
|
if (len) {
|
|
|
|
nsTArray<nsIMediaDevice*> tmp(len);
|
|
|
|
for (auto& device : aDevices) {
|
|
|
|
tmp.AppendElement(device);
|
|
|
|
}
|
|
|
|
auto* elements = static_cast<const void*>(tmp.Elements());
|
|
|
|
nsresult rv = var->SetAsArray(nsIDataType::VTYPE_INTERFACE,
|
|
|
|
&NS_GET_IID(nsIMediaDevice), len,
|
|
|
|
const_cast<void*>(elements));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var->SetAsEmptyArray(); // because SetAsArray() fails on zero length arrays.
|
|
|
|
}
|
|
|
|
return var.forget();
|
|
|
|
}
|
2014-03-27 01:57:46 +04:00
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
already_AddRefed<MediaManager::PledgeSourceSet>
|
2015-07-24 15:28:17 +03:00
|
|
|
MediaManager::EnumerateDevicesImpl(uint64_t aWindowId,
|
|
|
|
MediaSourceEnum aVideoType,
|
|
|
|
MediaSourceEnum aAudioType,
|
2015-07-03 01:01:52 +03:00
|
|
|
bool aFake, bool aFakeTracks)
|
2015-06-18 18:46:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-01-05 05:16:20 +03:00
|
|
|
nsPIDOMWindowInner* window =
|
2016-01-30 20:05:36 +03:00
|
|
|
nsGlobalWindow::GetInnerWindowWithId(aWindowId)->AsInner();
|
2015-06-18 18:46:36 +03:00
|
|
|
|
|
|
|
// This function returns a pledge, a promise-like object with the future result
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> pledge = new PledgeSourceSet();
|
2015-06-18 18:46:36 +03:00
|
|
|
uint32_t id = mOutstandingPledges.Append(*pledge);
|
|
|
|
|
|
|
|
// To get a device list anonymized for a particular origin, we must:
|
|
|
|
// 1. Get an origin-key (for either regular or private browsing)
|
|
|
|
// 2. Get the raw devices list
|
|
|
|
// 3. Anonymize the raw list with the origin-key.
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
bool privateBrowsing = IsPrivateBrowsing(window);
|
2015-02-23 19:50:48 +03:00
|
|
|
nsCString origin;
|
2015-07-03 01:01:52 +03:00
|
|
|
nsPrincipal::GetOriginForURI(window->GetDocumentURI(), origin);
|
2015-06-18 18:46:36 +03:00
|
|
|
|
2015-07-10 17:15:19 +03:00
|
|
|
bool persist = IsActivelyCapturingOrHasAPermission(aWindowId);
|
|
|
|
|
2015-06-18 18:46:36 +03:00
|
|
|
// GetOriginKey is an async API that returns a pledge (a promise-like
|
|
|
|
// pattern). We use .Then() to pass in a lambda to run back on this same
|
|
|
|
// thread later once GetOriginKey resolves. Needed variables are "captured"
|
|
|
|
// (passed by value) safely into the lambda.
|
2012-09-20 23:54:00 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Pledge<nsCString>> p = media::GetOriginKey(origin, privateBrowsing,
|
2015-07-10 17:15:19 +03:00
|
|
|
persist);
|
2015-07-24 15:28:17 +03:00
|
|
|
p->Then([id, aWindowId, aVideoType, aAudioType,
|
2015-07-03 01:01:52 +03:00
|
|
|
aFake, aFakeTracks](const nsCString& aOriginKey) mutable {
|
2015-06-18 18:46:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-06-18 18:46:36 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = mgr->EnumerateRawDevices(aWindowId,
|
2015-12-03 06:51:03 +03:00
|
|
|
aVideoType, aAudioType,
|
|
|
|
aFake, aFakeTracks);
|
2015-06-18 18:46:36 +03:00
|
|
|
p->Then([id, aWindowId, aOriginKey](SourceSet*& aDevices) mutable {
|
2016-02-26 22:21:05 +03:00
|
|
|
UniquePtr<SourceSet> devices(aDevices); // secondary result
|
2015-06-18 18:46:36 +03:00
|
|
|
|
|
|
|
// Only run if window is still on our active list.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-06-18 18:46:36 +03:00
|
|
|
if (!mgr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = mgr->mOutstandingPledges.Remove(id);
|
2015-06-18 18:46:36 +03:00
|
|
|
if (!p || !mgr->IsWindowStillActive(aWindowId)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
MediaManager_AnonymizeDevices(*devices, aOriginKey);
|
2016-02-26 22:21:05 +03:00
|
|
|
p->Resolve(devices.release());
|
2015-06-18 18:46:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return pledge.forget();
|
2012-09-20 23:54:00 +04:00
|
|
|
}
|
|
|
|
|
2015-03-03 17:51:05 +03:00
|
|
|
nsresult
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaManager::EnumerateDevices(nsPIDOMWindowInner* aWindow,
|
2015-03-03 17:51:05 +03:00
|
|
|
nsIGetUserMediaDevicesSuccessCallback* aOnSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* aOnFailure)
|
|
|
|
{
|
2015-06-18 18:46:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_TRUE(!sInShutdown, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIGetUserMediaDevicesSuccessCallback> onSuccess(aOnSuccess);
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure(aOnFailure);
|
|
|
|
uint64_t windowId = aWindow->WindowID();
|
|
|
|
|
2015-09-04 02:09:34 +03:00
|
|
|
StreamListeners* listeners = AddWindowID(windowId);
|
|
|
|
|
2016-04-06 15:56:44 +03:00
|
|
|
nsIPrincipal* principal = aWindow->GetExtantDoc()->NodePrincipal();
|
|
|
|
|
2015-09-04 02:09:34 +03:00
|
|
|
// Create a disabled listener to act as a placeholder
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> listener =
|
2016-04-06 15:56:44 +03:00
|
|
|
new GetUserMediaCallbackMediaStreamListener(mMediaThread, windowId,
|
|
|
|
MakePrincipalHandle(principal));
|
2015-09-04 02:09:34 +03:00
|
|
|
|
|
|
|
// No need for locking because we always do this in the main thread.
|
|
|
|
listeners->AppendElement(listener);
|
2015-03-03 17:51:05 +03:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
bool fake = Preferences::GetBool("media.navigator.streams.fake");
|
2015-03-03 17:51:05 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PledgeSourceSet> p = EnumerateDevicesImpl(windowId,
|
2016-02-17 00:55:33 +03:00
|
|
|
MediaSourceEnum::Camera,
|
|
|
|
MediaSourceEnum::Microphone,
|
2015-07-03 01:01:52 +03:00
|
|
|
fake);
|
2015-09-04 02:09:34 +03:00
|
|
|
p->Then([onSuccess, windowId, listener](SourceSet*& aDevices) mutable {
|
2016-02-26 22:21:05 +03:00
|
|
|
UniquePtr<SourceSet> devices(aDevices); // grab result
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-09-04 02:09:34 +03:00
|
|
|
mgr->RemoveFromWindowList(windowId, listener);
|
2015-06-18 18:46:36 +03:00
|
|
|
nsCOMPtr<nsIWritableVariant> array = MediaManager_ToJSArray(*devices);
|
|
|
|
onSuccess->OnSuccess(array);
|
2015-09-20 09:26:41 +03:00
|
|
|
}, [onFailure, windowId, listener](MediaStreamError*& reason) mutable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-09-04 02:09:34 +03:00
|
|
|
mgr->RemoveFromWindowList(windowId, listener);
|
2015-09-20 09:26:41 +03:00
|
|
|
onFailure->OnError(reason);
|
2015-06-18 18:46:36 +03:00
|
|
|
});
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetUserMediaDevices - called by the UI-part of getUserMedia from chrome JS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaManager::GetUserMediaDevices(nsPIDOMWindowInner* aWindow,
|
2015-06-18 18:46:36 +03:00
|
|
|
const MediaStreamConstraints& aConstraints,
|
|
|
|
nsIGetUserMediaDevicesSuccessCallback* aOnSuccess,
|
|
|
|
nsIDOMGetUserMediaErrorCallback* aOnFailure,
|
2016-01-08 01:30:10 +03:00
|
|
|
uint64_t aWindowId,
|
|
|
|
const nsAString& aCallID)
|
2015-06-18 18:46:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIGetUserMediaDevicesSuccessCallback> onSuccess(aOnSuccess);
|
|
|
|
nsCOMPtr<nsIDOMGetUserMediaErrorCallback> onFailure(aOnFailure);
|
|
|
|
if (!aWindowId) {
|
|
|
|
aWindowId = aWindow->WindowID();
|
|
|
|
}
|
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
// Ignore passed-in constraints, instead locate + return already-constrained list.
|
2015-06-18 18:46:36 +03:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
nsTArray<nsString>* callIDs;
|
|
|
|
if (!mCallIds.Get(aWindowId, &callIDs)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& callID : *callIDs) {
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<GetUserMediaTask> task;
|
2016-01-08 01:30:10 +03:00
|
|
|
if (!aCallID.Length() || aCallID == callID) {
|
2016-04-28 03:06:05 +03:00
|
|
|
if (mActiveCallbacks.Get(callID, getter_AddRefs(task))) {
|
2016-01-08 01:30:10 +03:00
|
|
|
nsCOMPtr<nsIWritableVariant> array = MediaManager_ToJSArray(*task->mSourceSet);
|
|
|
|
onSuccess->OnSuccess(array);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-06-18 18:46:36 +03:00
|
|
|
}
|
2015-07-03 01:01:52 +03:00
|
|
|
}
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2015-03-03 17:51:05 +03:00
|
|
|
}
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
MediaEngine*
|
2013-11-06 07:32:42 +04:00
|
|
|
MediaManager::GetBackend(uint64_t aWindowId)
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
2015-11-18 23:03:13 +03:00
|
|
|
MOZ_ASSERT(MediaManager::IsInMediaThread());
|
2012-07-12 15:53:08 +04:00
|
|
|
// Plugin backends as appropriate. The default engine also currently
|
|
|
|
// includes picture support for Android.
|
2012-10-26 04:14:47 +04:00
|
|
|
// This IS called off main-thread.
|
2012-06-03 11:35:15 +04:00
|
|
|
if (!mBackend) {
|
2015-05-29 21:28:03 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!sInShutdown); // we should never create a new backend in shutdown
|
2012-07-12 15:53:08 +04:00
|
|
|
#if defined(MOZ_WEBRTC)
|
2013-12-06 23:34:40 +04:00
|
|
|
mBackend = new MediaEngineWebRTC(mPrefs);
|
2012-07-12 15:53:08 +04:00
|
|
|
#else
|
2013-11-06 07:32:42 +04:00
|
|
|
mBackend = new MediaEngineDefault();
|
2012-07-12 15:53:08 +04:00
|
|
|
#endif
|
2012-06-03 11:35:15 +04:00
|
|
|
}
|
|
|
|
return mBackend;
|
|
|
|
}
|
|
|
|
|
2014-09-20 01:24:28 +04:00
|
|
|
static void
|
|
|
|
StopSharingCallback(MediaManager *aThis,
|
|
|
|
uint64_t aWindowID,
|
|
|
|
StreamListeners *aListeners,
|
|
|
|
void *aData)
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-20 01:24:28 +04:00
|
|
|
if (aListeners) {
|
|
|
|
auto length = aListeners->Length();
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
GetUserMediaCallbackMediaStreamListener *listener = aListeners->ElementAt(i);
|
|
|
|
|
|
|
|
if (listener->Stream()) { // aka HasBeenActivate()ed
|
2016-02-05 15:20:20 +03:00
|
|
|
listener->Stop();
|
2014-09-20 01:24:28 +04:00
|
|
|
}
|
|
|
|
listener->Remove();
|
2015-07-24 15:28:16 +03:00
|
|
|
listener->StopSharing();
|
2014-09-20 01:24:28 +04:00
|
|
|
}
|
|
|
|
aListeners->Clear();
|
|
|
|
aThis->RemoveWindowID(aWindowID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
MediaManager::OnNavigation(uint64_t aWindowID)
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-20 01:24:28 +04:00
|
|
|
LOG(("OnNavigation for %llu", aWindowID));
|
2012-10-26 04:14:47 +04:00
|
|
|
|
2016-02-05 15:20:20 +03:00
|
|
|
// Stop the streams for this window. The runnables check this value before
|
|
|
|
// making a call to content.
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2015-07-03 01:01:52 +03:00
|
|
|
nsTArray<nsString>* callIDs;
|
|
|
|
if (mCallIds.Get(aWindowID, &callIDs)) {
|
|
|
|
for (auto& callID : *callIDs) {
|
|
|
|
mActiveCallbacks.Remove(callID);
|
2014-01-09 01:51:33 +04:00
|
|
|
}
|
|
|
|
mCallIds.Remove(aWindowID);
|
|
|
|
}
|
|
|
|
|
2012-10-26 04:14:47 +04:00
|
|
|
// This is safe since we're on main-thread, and the windowlist can only
|
2013-01-04 22:11:12 +04:00
|
|
|
// be added to from the main-thread
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* window = nsGlobalWindow::GetInnerWindowWithId(aWindowID);
|
2014-09-20 01:24:28 +04:00
|
|
|
if (window) {
|
2016-01-30 20:05:36 +03:00
|
|
|
IterateWindowListeners(window->AsInner(), StopSharingCallback, nullptr);
|
2014-09-20 01:24:28 +04:00
|
|
|
} else {
|
|
|
|
RemoveWindowID(aWindowID);
|
2012-10-25 03:21:15 +04:00
|
|
|
}
|
2013-01-07 06:31:32 +04:00
|
|
|
}
|
|
|
|
|
2015-03-03 17:51:05 +03:00
|
|
|
StreamListeners*
|
|
|
|
MediaManager::AddWindowID(uint64_t aWindowId)
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-03 17:51:05 +03:00
|
|
|
// Store the WindowID in a hash table and mark as active. The entry is removed
|
|
|
|
// when this window is closed or navigated away from.
|
|
|
|
// This is safe since we're on main-thread, and the windowlist can only
|
|
|
|
// be invalidated from the main-thread (see OnNavigation)
|
|
|
|
StreamListeners* listeners = GetActiveWindows()->Get(aWindowId);
|
|
|
|
if (!listeners) {
|
|
|
|
listeners = new StreamListeners;
|
|
|
|
GetActiveWindows()->Put(aWindowId, listeners);
|
|
|
|
}
|
|
|
|
return listeners;
|
|
|
|
}
|
|
|
|
|
2014-08-22 14:21:48 +04:00
|
|
|
void
|
|
|
|
MediaManager::RemoveWindowID(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
mActiveWindows.Remove(aWindowId);
|
|
|
|
|
|
|
|
// get outer windowID
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* window = nsGlobalWindow::GetInnerWindowWithId(aWindowId);
|
2014-08-22 14:21:48 +04:00
|
|
|
if (!window) {
|
|
|
|
LOG(("No inner window for %llu", aWindowId));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter* outer = window->AsInner()->GetOuterWindow();
|
2014-08-22 14:21:48 +04:00
|
|
|
if (!outer) {
|
|
|
|
LOG(("No outer window for inner %llu", aWindowId));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t outerID = outer->WindowID();
|
|
|
|
|
|
|
|
// Notify the UI that this window no longer has gUM active
|
|
|
|
char windowBuffer[32];
|
2016-02-17 22:23:39 +03:00
|
|
|
snprintf_literal(windowBuffer, "%" PRIu64, outerID);
|
2014-08-22 14:21:48 +04:00
|
|
|
nsString data = NS_ConvertUTF8toUTF16(windowBuffer);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
obs->NotifyObservers(nullptr, "recording-window-ended", data.get());
|
|
|
|
LOG(("Sent recording-window-ended for window %llu (outer %llu)",
|
|
|
|
aWindowId, outerID));
|
|
|
|
}
|
|
|
|
|
2013-01-07 06:31:32 +04:00
|
|
|
void
|
|
|
|
MediaManager::RemoveFromWindowList(uint64_t aWindowID,
|
|
|
|
GetUserMediaCallbackMediaStreamListener *aListener)
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-01-07 06:31:32 +04:00
|
|
|
|
|
|
|
// This is defined as safe on an inactive GUMCMSListener
|
|
|
|
aListener->Remove(); // really queues the remove
|
|
|
|
|
|
|
|
StreamListeners* listeners = GetWindowListeners(aWindowID);
|
|
|
|
if (!listeners) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners->RemoveElement(aListener);
|
|
|
|
if (listeners->Length() == 0) {
|
|
|
|
RemoveWindowID(aWindowID);
|
|
|
|
// listeners has been deleted here
|
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
}
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
void
|
|
|
|
MediaManager::GetPref(nsIPrefBranch *aBranch, const char *aPref,
|
|
|
|
const char *aData, int32_t *aVal)
|
|
|
|
{
|
|
|
|
int32_t temp;
|
|
|
|
if (aData == nullptr || strcmp(aPref,aData) == 0) {
|
|
|
|
if (NS_SUCCEEDED(aBranch->GetIntPref(aPref, &temp))) {
|
|
|
|
*aVal = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 23:34:40 +04:00
|
|
|
void
|
|
|
|
MediaManager::GetPrefBool(nsIPrefBranch *aBranch, const char *aPref,
|
|
|
|
const char *aData, bool *aVal)
|
|
|
|
{
|
|
|
|
bool temp;
|
|
|
|
if (aData == nullptr || strcmp(aPref,aData) == 0) {
|
|
|
|
if (NS_SUCCEEDED(aBranch->GetBoolPref(aPref, &temp))) {
|
|
|
|
*aVal = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
void
|
|
|
|
MediaManager::GetPrefs(nsIPrefBranch *aBranch, const char *aData)
|
|
|
|
{
|
|
|
|
GetPref(aBranch, "media.navigator.video.default_width", aData, &mPrefs.mWidth);
|
|
|
|
GetPref(aBranch, "media.navigator.video.default_height", aData, &mPrefs.mHeight);
|
|
|
|
GetPref(aBranch, "media.navigator.video.default_fps", aData, &mPrefs.mFPS);
|
|
|
|
GetPref(aBranch, "media.navigator.video.default_minfps", aData, &mPrefs.mMinFPS);
|
2015-09-24 16:23:37 +03:00
|
|
|
GetPref(aBranch, "media.navigator.audio.fake_frequency", aData, &mPrefs.mFreq);
|
2016-01-23 00:46:38 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
GetPrefBool(aBranch, "media.getusermedia.aec_enabled", aData, &mPrefs.mAecOn);
|
|
|
|
GetPrefBool(aBranch, "media.getusermedia.agc_enabled", aData, &mPrefs.mAgcOn);
|
|
|
|
GetPrefBool(aBranch, "media.getusermedia.noise_enabled", aData, &mPrefs.mNoiseOn);
|
|
|
|
GetPref(aBranch, "media.getusermedia.aec", aData, &mPrefs.mAec);
|
|
|
|
GetPref(aBranch, "media.getusermedia.agc", aData, &mPrefs.mAgc);
|
|
|
|
GetPref(aBranch, "media.getusermedia.noise", aData, &mPrefs.mNoise);
|
|
|
|
GetPref(aBranch, "media.getusermedia.playout_delay", aData, &mPrefs.mPlayoutDelay);
|
2016-04-20 03:20:45 +03:00
|
|
|
GetPrefBool(aBranch, "media.getusermedia.aec_extended_filter", aData, &mPrefs.mExtendedFilter);
|
|
|
|
GetPrefBool(aBranch, "media.getusermedia.aec_aec_delay_agnostic", aData, &mPrefs.mDelayAgnostic);
|
2016-01-23 00:46:38 +03:00
|
|
|
#endif
|
2016-01-21 19:51:36 +03:00
|
|
|
GetPrefBool(aBranch, "media.navigator.audio.full_duplex", aData, &mPrefs.mFullDuplex);
|
2013-03-05 01:02:17 +04:00
|
|
|
}
|
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
void
|
|
|
|
MediaManager::Shutdown()
|
2012-06-03 11:35:15 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-15 06:54:39 +03:00
|
|
|
if (sInShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sInShutdown = true;
|
|
|
|
|
2013-08-28 08:14:57 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
obs->RemoveObserver(this, "last-pb-context-exited");
|
|
|
|
obs->RemoveObserver(this, "getUserMedia:privileged:allow");
|
|
|
|
obs->RemoveObserver(this, "getUserMedia:response:allow");
|
|
|
|
obs->RemoveObserver(this, "getUserMedia:response:deny");
|
|
|
|
obs->RemoveObserver(this, "getUserMedia:revoke");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefs) {
|
|
|
|
prefs->RemoveObserver("media.navigator.video.default_width", this);
|
|
|
|
prefs->RemoveObserver("media.navigator.video.default_height", this);
|
|
|
|
prefs->RemoveObserver("media.navigator.video.default_fps", this);
|
|
|
|
prefs->RemoveObserver("media.navigator.video.default_minfps", this);
|
|
|
|
prefs->RemoveObserver("media.navigator.audio.fake_frequency", this);
|
2016-01-23 00:46:38 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
prefs->RemoveObserver("media.getusermedia.aec_enabled", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.aec", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.agc_enabled", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.agc", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.noise_enabled", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.noise", this);
|
|
|
|
prefs->RemoveObserver("media.getusermedia.playout_delay", this);
|
|
|
|
#endif
|
2016-01-21 19:51:36 +03:00
|
|
|
prefs->RemoveObserver("media.navigator.audio.full_duplex", this);
|
2015-10-15 06:54:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close off any remaining active windows.
|
|
|
|
GetActiveWindows()->Clear();
|
|
|
|
mActiveCallbacks.Clear();
|
|
|
|
mCallIds.Clear();
|
2016-02-11 00:10:46 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
|
|
|
StopWebRtcLog();
|
|
|
|
#endif
|
2012-10-06 04:20:47 +04:00
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
// Because mMediaThread is not an nsThread, we must dispatch to it so it can
|
|
|
|
// clean up BackgroundChild. Continue stopping thread once this is done.
|
2013-03-05 01:02:17 +04:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
class ShutdownTask : public Runnable
|
2015-10-15 06:54:39 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-11-18 23:03:13 +03:00
|
|
|
ShutdownTask(MediaManager* aManager,
|
2016-04-28 03:06:05 +03:00
|
|
|
already_AddRefed<Runnable> aReply)
|
2015-11-18 23:03:13 +03:00
|
|
|
: mManager(aManager)
|
|
|
|
, mReply(aReply) {}
|
2015-10-15 06:54:39 +03:00
|
|
|
private:
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD
|
2016-01-18 06:50:29 +03:00
|
|
|
Run() override
|
2015-05-29 21:28:03 +03:00
|
|
|
{
|
2015-10-15 06:54:39 +03:00
|
|
|
LOG(("MediaManager Thread Shutdown"));
|
|
|
|
MOZ_ASSERT(MediaManager::IsInMediaThread());
|
2015-11-18 23:03:13 +03:00
|
|
|
// Must shutdown backend on MediaManager thread, since that's where we started it from!
|
|
|
|
{
|
|
|
|
if (mManager->mBackend) {
|
|
|
|
mManager->mBackend->Shutdown(); // ok to invoke multiple times
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 06:54:39 +03:00
|
|
|
mozilla::ipc::BackgroundChild::CloseForCurrentThread();
|
|
|
|
// must explicitly do this before dispatching the reply, since the reply may kill us with Stop()
|
2015-11-18 23:03:13 +03:00
|
|
|
mManager->mBackend = nullptr; // last reference, will invoke Shutdown() again
|
2015-10-15 06:54:39 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mReply.forget()))) {
|
|
|
|
LOG(("Will leak thread: DispatchToMainthread of reply runnable failed in MediaManager shutdown"));
|
2015-05-29 21:28:03 +03:00
|
|
|
}
|
2016-04-28 03:06:05 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-05-29 21:28:03 +03:00
|
|
|
}
|
2015-11-18 23:03:13 +03:00
|
|
|
RefPtr<MediaManager> mManager;
|
2016-04-26 03:23:21 +03:00
|
|
|
RefPtr<Runnable> mReply;
|
2015-10-15 06:54:39 +03:00
|
|
|
};
|
2015-05-29 21:28:03 +03:00
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
// Post ShutdownTask to execute on mMediaThread and pass in a lambda
|
|
|
|
// callback to be executed back on this thread once it is done.
|
|
|
|
//
|
|
|
|
// The lambda callback "captures" the 'this' pointer for member access.
|
|
|
|
// This is safe since this is guaranteed to be here since sSingleton isn't
|
|
|
|
// cleared until the lambda function clears it.
|
|
|
|
|
|
|
|
// note that this == sSingleton
|
|
|
|
RefPtr<MediaManager> that(sSingleton);
|
|
|
|
// Release the backend (and call Shutdown()) from within the MediaManager thread
|
|
|
|
// Don't use MediaManager::PostTask() because we're sInShutdown=true here!
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<ShutdownTask> shutdown = new ShutdownTask(this,
|
2015-10-15 06:54:39 +03:00
|
|
|
media::NewRunnableFrom([this, that]() mutable {
|
|
|
|
LOG(("MediaManager shutdown lambda running, releasing MediaManager singleton and thread"));
|
|
|
|
if (mMediaThread) {
|
|
|
|
mMediaThread->Stop();
|
|
|
|
}
|
2015-03-29 20:43:43 +03:00
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
// Remove async shutdown blocker
|
2015-03-29 20:43:43 +03:00
|
|
|
|
2015-11-18 23:42:16 +03:00
|
|
|
nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase = GetShutdownPhase();
|
|
|
|
shutdownPhase->RemoveBlocker(sSingleton->mShutdownBlocker);
|
2015-10-15 06:54:39 +03:00
|
|
|
|
|
|
|
// we hold a ref to 'that' which is the same as sSingleton
|
|
|
|
sSingleton = nullptr;
|
2015-05-29 21:28:03 +03:00
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
return NS_OK;
|
2016-04-28 03:06:05 +03:00
|
|
|
}));
|
|
|
|
mMediaThread->message_loop()->PostTask(shutdown.forget());
|
2015-10-15 06:54:39 +03:00
|
|
|
}
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2015-10-15 06:54:39 +03:00
|
|
|
nsresult
|
|
|
|
MediaManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> branch( do_QueryInterface(aSubject) );
|
|
|
|
if (branch) {
|
|
|
|
GetPrefs(branch,NS_ConvertUTF16toUTF8(aData).get());
|
|
|
|
LOG(("%s: %dx%d @%dfps (min %d)", __FUNCTION__,
|
|
|
|
mPrefs.mWidth, mPrefs.mHeight, mPrefs.mFPS, mPrefs.mMinFPS));
|
|
|
|
}
|
2015-07-13 06:55:02 +03:00
|
|
|
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
|
|
|
|
// Clear memory of private-browsing-specific deviceIds. Fire and forget.
|
|
|
|
media::SanitizeOriginKeys(0, true);
|
|
|
|
return NS_OK;
|
2015-07-03 01:01:52 +03:00
|
|
|
} else if (!strcmp(aTopic, "getUserMedia:privileged:allow") ||
|
|
|
|
!strcmp(aTopic, "getUserMedia:response:allow")) {
|
2012-10-06 04:20:47 +04:00
|
|
|
nsString key(aData);
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<GetUserMediaTask> task;
|
|
|
|
mActiveCallbacks.Remove(key, getter_AddRefs(task));
|
2014-09-12 18:49:39 +04:00
|
|
|
if (!task) {
|
2012-10-26 04:14:47 +04:00
|
|
|
return NS_OK;
|
2012-10-06 04:20:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aSubject) {
|
2012-12-20 21:03:07 +04:00
|
|
|
// A particular device or devices were chosen by the user.
|
2012-10-16 00:41:46 +04:00
|
|
|
// NOTE: does not allow setting a device to null; assumes nullptr
|
2012-12-20 21:03:07 +04:00
|
|
|
nsCOMPtr<nsISupportsArray> array(do_QueryInterface(aSubject));
|
|
|
|
MOZ_ASSERT(array);
|
|
|
|
uint32_t len = 0;
|
|
|
|
array->Count(&len);
|
2015-07-03 01:01:52 +03:00
|
|
|
bool videoFound = false, audioFound = false;
|
2012-12-20 21:03:07 +04:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
array->GetElementAt(i,getter_AddRefs(supports));
|
|
|
|
nsCOMPtr<nsIMediaDevice> device(do_QueryInterface(supports));
|
|
|
|
MOZ_ASSERT(device); // shouldn't be returning anything else...
|
|
|
|
if (device) {
|
|
|
|
nsString type;
|
|
|
|
device->GetType(type);
|
|
|
|
if (type.EqualsLiteral("video")) {
|
2015-07-03 01:01:52 +03:00
|
|
|
if (!videoFound) {
|
|
|
|
task->SetVideoDevice(static_cast<VideoDevice*>(device.get()));
|
|
|
|
videoFound = true;
|
|
|
|
}
|
2012-12-20 21:03:07 +04:00
|
|
|
} else if (type.EqualsLiteral("audio")) {
|
2015-07-03 01:01:52 +03:00
|
|
|
if (!audioFound) {
|
|
|
|
task->SetAudioDevice(static_cast<AudioDevice*>(device.get()));
|
|
|
|
audioFound = true;
|
|
|
|
}
|
2012-12-20 21:03:07 +04:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Unknown device type in getUserMedia");
|
|
|
|
}
|
2012-10-16 00:41:46 +04:00
|
|
|
}
|
2012-10-06 04:20:47 +04:00
|
|
|
}
|
2016-05-06 04:54:04 +03:00
|
|
|
bool needVideo = IsOn(task->GetConstraints().mVideo);
|
|
|
|
bool needAudio = IsOn(task->GetConstraints().mAudio);
|
|
|
|
MOZ_ASSERT(needVideo || needAudio);
|
|
|
|
|
|
|
|
if ((needVideo && !videoFound) || (needAudio && !audioFound)) {
|
2016-05-09 05:29:46 +03:00
|
|
|
task->Denied(NS_LITERAL_STRING("NotAllowedError"));
|
2016-05-06 04:54:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-10-06 04:20:47 +04:00
|
|
|
}
|
|
|
|
|
2015-05-29 21:28:03 +03:00
|
|
|
if (sInShutdown) {
|
|
|
|
return task->Denied(NS_LITERAL_STRING("In shutdown"));
|
|
|
|
}
|
2012-10-26 04:14:47 +04:00
|
|
|
// Reuse the same thread to save memory.
|
2016-04-28 03:06:05 +03:00
|
|
|
MediaManager::PostTask(task.forget());
|
2012-10-06 04:20:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
} else if (!strcmp(aTopic, "getUserMedia:response:deny")) {
|
2016-05-09 05:29:46 +03:00
|
|
|
nsString errorMessage(NS_LITERAL_STRING("NotAllowedError"));
|
2013-02-16 07:22:27 +04:00
|
|
|
|
|
|
|
if (aSubject) {
|
|
|
|
nsCOMPtr<nsISupportsString> msg(do_QueryInterface(aSubject));
|
|
|
|
MOZ_ASSERT(msg);
|
|
|
|
msg->GetData(errorMessage);
|
|
|
|
if (errorMessage.IsEmpty())
|
2014-10-27 22:42:56 +03:00
|
|
|
errorMessage.AssignLiteral(MOZ_UTF16("InternalError"));
|
2013-02-16 07:22:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-06 04:20:47 +04:00
|
|
|
nsString key(aData);
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<GetUserMediaTask> task;
|
|
|
|
mActiveCallbacks.Remove(key, getter_AddRefs(task));
|
2014-09-12 18:49:39 +04:00
|
|
|
if (task) {
|
|
|
|
task->Denied(errorMessage);
|
2012-10-06 04:20:47 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2012-06-03 11:35:15 +04:00
|
|
|
|
2013-03-05 01:02:17 +04:00
|
|
|
} else if (!strcmp(aTopic, "getUserMedia:revoke")) {
|
2013-01-04 22:11:12 +04:00
|
|
|
nsresult rv;
|
2014-08-22 13:27:16 +04:00
|
|
|
// may be windowid or screen:windowid
|
|
|
|
nsDependentString data(aData);
|
|
|
|
if (Substring(data, 0, strlen("screen:")).EqualsLiteral("screen:")) {
|
|
|
|
uint64_t windowID = PromiseFlatString(Substring(data, strlen("screen:"))).ToInteger64(&rv);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2015-07-24 15:28:16 +03:00
|
|
|
LOG(("Revoking Screen/windowCapture access for window %llu", windowID));
|
2014-08-22 13:27:16 +04:00
|
|
|
StopScreensharing(windowID);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint64_t windowID = nsString(aData).ToInteger64(&rv);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
LOG(("Revoking MediaCapture access for window %llu", windowID));
|
|
|
|
OnNavigation(windowID);
|
|
|
|
}
|
2013-01-04 22:11:12 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-13 12:51:48 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
else if (!strcmp(aTopic, "phone-state-changed")) {
|
|
|
|
nsString state(aData);
|
2014-05-26 01:38:00 +04:00
|
|
|
nsresult rv;
|
|
|
|
uint32_t phoneState = state.ToInteger(&rv);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && phoneState == nsIAudioManager::PHONE_STATE_IN_CALL) {
|
2013-09-13 12:51:48 +04:00
|
|
|
StopMediaStreams();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-04 22:11:12 +04:00
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-30 16:40:54 +03:00
|
|
|
nsresult
|
|
|
|
MediaManager::GetActiveMediaCaptureWindows(nsISupportsArray** aArray)
|
2012-12-22 12:09:36 +04:00
|
|
|
{
|
2015-09-30 16:40:54 +03:00
|
|
|
MOZ_ASSERT(aArray);
|
|
|
|
nsISupportsArray* array;
|
|
|
|
nsresult rv = NS_NewISupportsArray(&array); // AddRefs
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-02-24 19:33:32 +04:00
|
|
|
|
2015-09-30 16:40:54 +03:00
|
|
|
for (auto iter = mActiveWindows.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
const uint64_t& id = iter.Key();
|
|
|
|
StreamListeners* listeners = iter.UserData();
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window =
|
|
|
|
nsGlobalWindow::GetInnerWindowWithId(id)->AsInner();
|
2015-09-30 16:40:54 +03:00
|
|
|
MOZ_ASSERT(window);
|
2016-01-30 20:05:36 +03:00
|
|
|
// XXXkhuey ...
|
2015-09-30 16:40:54 +03:00
|
|
|
if (!window) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-24 19:33:32 +04:00
|
|
|
// mActiveWindows contains both windows that have requested device
|
|
|
|
// access and windows that are currently capturing media. We want
|
|
|
|
// to return only the latter. See bug 975177.
|
|
|
|
bool capturing = false;
|
2015-09-30 16:40:54 +03:00
|
|
|
if (listeners) {
|
|
|
|
uint32_t length = listeners->Length();
|
2014-02-24 19:33:32 +04:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetUserMediaCallbackMediaStreamListener> listener =
|
2015-09-30 16:40:54 +03:00
|
|
|
listeners->ElementAt(i);
|
2014-07-18 06:23:00 +04:00
|
|
|
if (listener->CapturingVideo() || listener->CapturingAudio() ||
|
2014-08-21 02:05:23 +04:00
|
|
|
listener->CapturingScreen() || listener->CapturingWindow() ||
|
|
|
|
listener->CapturingApplication()) {
|
2014-02-24 19:33:32 +04:00
|
|
|
capturing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 16:40:54 +03:00
|
|
|
if (capturing) {
|
2012-12-22 12:09:36 +04:00
|
|
|
array->AppendElement(window);
|
2015-09-30 16:40:54 +03:00
|
|
|
}
|
2014-02-24 19:33:32 +04:00
|
|
|
}
|
2012-12-22 12:09:36 +04:00
|
|
|
|
|
|
|
*aArray = array;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-20 01:24:28 +04:00
|
|
|
// XXX flags might be better...
|
|
|
|
struct CaptureWindowStateData {
|
|
|
|
bool *mVideo;
|
|
|
|
bool *mAudio;
|
|
|
|
bool *mScreenShare;
|
|
|
|
bool *mWindowShare;
|
|
|
|
bool *mAppShare;
|
2015-03-11 18:34:31 +03:00
|
|
|
bool *mBrowserShare;
|
2014-09-20 01:24:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
CaptureWindowStateCallback(MediaManager *aThis,
|
|
|
|
uint64_t aWindowID,
|
|
|
|
StreamListeners *aListeners,
|
|
|
|
void *aData)
|
|
|
|
{
|
|
|
|
struct CaptureWindowStateData *data = (struct CaptureWindowStateData *) aData;
|
|
|
|
|
|
|
|
if (aListeners) {
|
|
|
|
auto length = aListeners->Length();
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
GetUserMediaCallbackMediaStreamListener *listener = aListeners->ElementAt(i);
|
|
|
|
|
|
|
|
if (listener->CapturingVideo()) {
|
|
|
|
*data->mVideo = true;
|
|
|
|
}
|
|
|
|
if (listener->CapturingAudio()) {
|
|
|
|
*data->mAudio = true;
|
|
|
|
}
|
|
|
|
if (listener->CapturingScreen()) {
|
|
|
|
*data->mScreenShare = true;
|
|
|
|
}
|
|
|
|
if (listener->CapturingWindow()) {
|
|
|
|
*data->mWindowShare = true;
|
|
|
|
}
|
|
|
|
if (listener->CapturingApplication()) {
|
|
|
|
*data->mAppShare = true;
|
|
|
|
}
|
2015-03-11 18:34:31 +03:00
|
|
|
if (listener->CapturingBrowser()) {
|
|
|
|
*data->mBrowserShare = true;
|
|
|
|
}
|
2014-09-20 01:24:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-28 00:36:06 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaManager::MediaCaptureWindowState(nsIDOMWindow* aWindow, bool* aVideo,
|
2014-07-18 06:23:00 +04:00
|
|
|
bool* aAudio, bool *aScreenShare,
|
2015-03-11 18:34:31 +03:00
|
|
|
bool* aWindowShare, bool *aAppShare,
|
|
|
|
bool *aBrowserShare)
|
2013-02-28 00:36:06 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-09-20 01:24:28 +04:00
|
|
|
struct CaptureWindowStateData data;
|
|
|
|
data.mVideo = aVideo;
|
|
|
|
data.mAudio = aAudio;
|
|
|
|
data.mScreenShare = aScreenShare;
|
|
|
|
data.mWindowShare = aWindowShare;
|
|
|
|
data.mAppShare = aAppShare;
|
2015-03-11 18:34:31 +03:00
|
|
|
data.mBrowserShare = aBrowserShare;
|
2014-09-20 01:24:28 +04:00
|
|
|
|
2013-02-28 00:36:06 +04:00
|
|
|
*aVideo = false;
|
|
|
|
*aAudio = false;
|
2014-07-18 06:23:00 +04:00
|
|
|
*aScreenShare = false;
|
|
|
|
*aWindowShare = false;
|
2014-08-25 19:01:19 +04:00
|
|
|
*aAppShare = false;
|
2015-03-11 18:34:31 +03:00
|
|
|
*aBrowserShare = false;
|
2013-02-28 00:36:06 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> piWin = do_QueryInterface(aWindow);
|
2014-09-20 01:24:28 +04:00
|
|
|
if (piWin) {
|
|
|
|
IterateWindowListeners(piWin, CaptureWindowStateCallback, &data);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2015-03-11 18:34:31 +03:00
|
|
|
LOG(("%s: window %lld capturing %s %s %s %s %s %s", __FUNCTION__, piWin ? piWin->WindowID() : -1,
|
2014-07-18 06:23:00 +04:00
|
|
|
*aVideo ? "video" : "", *aAudio ? "audio" : "",
|
2014-08-25 19:01:19 +04:00
|
|
|
*aScreenShare ? "screenshare" : "", *aWindowShare ? "windowshare" : "",
|
2015-03-11 18:34:31 +03:00
|
|
|
*aAppShare ? "appshare" : "", *aBrowserShare ? "browsershare" : ""));
|
2013-02-28 00:36:06 +04:00
|
|
|
#endif
|
2014-09-20 01:24:28 +04:00
|
|
|
return NS_OK;
|
2015-03-03 17:51:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaManager::SanitizeDeviceIds(int64_t aSinceWhen)
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-03 17:51:05 +03:00
|
|
|
LOG(("%s: sinceWhen = %llu", __FUNCTION__, aSinceWhen));
|
|
|
|
|
2015-07-13 06:55:02 +03:00
|
|
|
media::SanitizeOriginKeys(aSinceWhen, false); // we fire and forget
|
2015-03-03 17:51:05 +03:00
|
|
|
return NS_OK;
|
2015-03-03 17:51:05 +03:00
|
|
|
}
|
|
|
|
|
2014-09-20 01:24:28 +04:00
|
|
|
static void
|
|
|
|
StopScreensharingCallback(MediaManager *aThis,
|
|
|
|
uint64_t aWindowID,
|
|
|
|
StreamListeners *aListeners,
|
|
|
|
void *aData)
|
2013-02-28 00:36:06 +04:00
|
|
|
{
|
2014-09-20 01:24:28 +04:00
|
|
|
if (aListeners) {
|
|
|
|
auto length = aListeners->Length();
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2015-07-24 15:28:16 +03:00
|
|
|
aListeners->ElementAt(i)->StopSharing();
|
2013-02-28 00:36:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:27:16 +04:00
|
|
|
void
|
|
|
|
MediaManager::StopScreensharing(uint64_t aWindowID)
|
|
|
|
{
|
|
|
|
// We need to stop window/screensharing for all streams in all innerwindows that
|
|
|
|
// correspond to that outerwindow.
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* window = nsGlobalWindow::GetInnerWindowWithId(aWindowID);
|
2014-08-22 13:27:16 +04:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-30 20:05:36 +03:00
|
|
|
IterateWindowListeners(window->AsInner(), &StopScreensharingCallback, nullptr);
|
2014-08-22 13:27:16 +04:00
|
|
|
}
|
|
|
|
|
2014-09-20 01:24:28 +04:00
|
|
|
// lets us do all sorts of things to the listeners
|
2014-08-22 13:27:16 +04:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaManager::IterateWindowListeners(nsPIDOMWindowInner* aWindow,
|
2014-09-20 01:24:28 +04:00
|
|
|
WindowListenerCallback aCallback,
|
|
|
|
void *aData)
|
2014-08-22 13:27:16 +04:00
|
|
|
{
|
2014-09-20 01:24:28 +04:00
|
|
|
// Iterate the docshell tree to find all the child windows, and for each
|
|
|
|
// invoke the callback
|
2016-01-30 20:05:36 +03:00
|
|
|
if (aWindow) {
|
|
|
|
uint64_t windowID = aWindow->WindowID();
|
|
|
|
StreamListeners* listeners = GetActiveWindows()->Get(windowID);
|
|
|
|
// pass listeners so it can modify/delete the list
|
|
|
|
(*aCallback)(this, windowID, listeners, aData);
|
2014-08-22 13:27:16 +04:00
|
|
|
|
|
|
|
// iterate any children of *this* window (iframes, etc)
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell();
|
2014-08-22 13:27:16 +04:00
|
|
|
if (docShell) {
|
|
|
|
int32_t i, count;
|
|
|
|
docShell->GetChildCount(&count);
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item;
|
|
|
|
docShell->GetChildAt(i, getter_AddRefs(item));
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> winOuter = item ? item->GetWindow() : nullptr;
|
2014-08-22 13:27:16 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
if (winOuter) {
|
|
|
|
IterateWindowListeners(winOuter->GetCurrentInnerWindow(),
|
|
|
|
aCallback, aData);
|
2014-08-22 13:27:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 01:24:28 +04:00
|
|
|
|
2013-09-13 12:51:48 +04:00
|
|
|
void
|
|
|
|
MediaManager::StopMediaStreams()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupportsArray> array;
|
|
|
|
GetActiveMediaCaptureWindows(getter_AddRefs(array));
|
|
|
|
uint32_t len;
|
|
|
|
array->Count(&len);
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsCOMPtr<nsISupports> window;
|
|
|
|
array->GetElementAt(i, getter_AddRefs(window));
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win(do_QueryInterface(window));
|
2013-09-13 12:51:48 +04:00
|
|
|
if (win) {
|
|
|
|
OnNavigation(win->WindowID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 17:51:05 +03:00
|
|
|
bool
|
2015-07-10 06:07:42 +03:00
|
|
|
MediaManager::IsActivelyCapturingOrHasAPermission(uint64_t aWindowId)
|
2015-03-03 17:51:05 +03:00
|
|
|
{
|
2015-07-10 06:07:42 +03:00
|
|
|
// Does page currently have a gUM stream active?
|
|
|
|
|
2015-03-03 17:51:05 +03:00
|
|
|
nsCOMPtr<nsISupportsArray> array;
|
|
|
|
GetActiveMediaCaptureWindows(getter_AddRefs(array));
|
|
|
|
uint32_t len;
|
|
|
|
array->Count(&len);
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
nsCOMPtr<nsISupports> window;
|
|
|
|
array->GetElementAt(i, getter_AddRefs(window));
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win(do_QueryInterface(window));
|
2015-03-03 17:51:05 +03:00
|
|
|
if (win && win->WindowID() == aWindowId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 06:07:42 +03:00
|
|
|
|
|
|
|
// Or are persistent permissions (audio or video) granted?
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* window = nsGlobalWindow::GetInnerWindowWithId(aWindowId);
|
2015-07-10 06:07:42 +03:00
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if this site has persistent permissions.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPermissionManager> mgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false; // no permission manager no permissions!
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t audio = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
uint32_t video = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
{
|
|
|
|
auto* principal = window->GetExtantDoc()->NodePrincipal();
|
|
|
|
rv = mgr->TestExactPermissionFromPrincipal(principal, "microphone", &audio);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rv = mgr->TestExactPermissionFromPrincipal(principal, "camera", &video);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return audio == nsIPermissionManager::ALLOW_ACTION ||
|
|
|
|
video == nsIPermissionManager::ALLOW_ACTION;
|
2015-03-03 17:51:05 +03:00
|
|
|
}
|
|
|
|
|
2013-01-01 03:12:15 +04:00
|
|
|
void
|
2016-02-05 15:20:20 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::Stop()
|
2013-01-01 03:12:15 +04:00
|
|
|
{
|
2015-09-30 09:08:26 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
if (mStopped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-01 03:12:15 +04:00
|
|
|
// We can't take a chance on blocking here, so proxy this to another
|
|
|
|
// thread.
|
|
|
|
// Pass a ref to us (which is threadsafe) so it can query us for the
|
|
|
|
// source stream info.
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<MediaOperationTask> mediaOperation =
|
2014-09-12 18:49:39 +04:00
|
|
|
new MediaOperationTask(MEDIA_STOP,
|
|
|
|
this, nullptr, nullptr,
|
2015-09-30 09:08:33 +03:00
|
|
|
!mAudioStopped ? mAudioDevice.get() : nullptr,
|
|
|
|
!mVideoStopped ? mVideoDevice.get() : nullptr,
|
2016-04-28 03:06:05 +03:00
|
|
|
false, mWindowID, nullptr);
|
|
|
|
MediaManager::PostTask(mediaOperation.forget());
|
2015-09-30 09:08:33 +03:00
|
|
|
mStopped = mAudioStopped = mVideoStopped = true;
|
2013-01-01 03:12:15 +04:00
|
|
|
}
|
|
|
|
|
2014-08-22 13:27:16 +04:00
|
|
|
// Doesn't kill audio
|
|
|
|
void
|
2015-07-24 15:28:16 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::StopSharing()
|
2014-08-22 13:27:16 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-02-05 15:20:20 +03:00
|
|
|
if (mVideoDevice &&
|
2016-02-17 00:55:33 +03:00
|
|
|
(mVideoDevice->GetMediaSource() == MediaSourceEnum::Screen ||
|
|
|
|
mVideoDevice->GetMediaSource() == MediaSourceEnum::Application ||
|
|
|
|
mVideoDevice->GetMediaSource() == MediaSourceEnum::Window)) {
|
2016-02-05 15:20:20 +03:00
|
|
|
// We want to stop the whole stream if there's no audio;
|
|
|
|
// just the video track if we have both.
|
|
|
|
// StopTrack figures this out for us.
|
|
|
|
StopTrack(kVideoTrack);
|
2015-09-19 07:49:07 +03:00
|
|
|
} else if (mAudioDevice &&
|
2016-02-17 00:55:33 +03:00
|
|
|
mAudioDevice->GetMediaSource() == MediaSourceEnum::AudioCapture) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = nsGlobalWindow::GetInnerWindowWithId(mWindowID)->AsInner();
|
2015-07-24 15:28:16 +03:00
|
|
|
MOZ_ASSERT(window);
|
|
|
|
window->SetAudioCapture(false);
|
2015-08-25 11:29:51 +03:00
|
|
|
MediaStreamGraph* graph =
|
|
|
|
MediaStreamGraph::GetInstance(MediaStreamGraph::AUDIO_THREAD_DRIVER,
|
|
|
|
dom::AudioChannel::Normal);
|
|
|
|
graph->UnregisterCaptureStreamForWindow(mWindowID);
|
2015-07-24 15:28:16 +03:00
|
|
|
mStream->Destroy();
|
2014-08-22 13:27:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 01:45:57 +03:00
|
|
|
// ApplyConstraints for track
|
|
|
|
|
2015-11-19 06:17:09 +03:00
|
|
|
auto
|
2015-09-21 01:45:57 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::ApplyConstraintsToTrack(
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aWindow,
|
2015-09-20 17:04:51 +03:00
|
|
|
TrackID aTrackID,
|
2015-11-19 06:17:09 +03:00
|
|
|
const MediaTrackConstraints& aConstraints) -> already_AddRefed<PledgeVoid>
|
2015-09-21 01:45:57 +03:00
|
|
|
{
|
2015-09-20 17:04:51 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-19 06:17:09 +03:00
|
|
|
RefPtr<PledgeVoid> p = new PledgeVoid();
|
2015-09-20 17:04:51 +03:00
|
|
|
|
2016-02-01 17:43:38 +03:00
|
|
|
// XXX to support multiple tracks of a type in a stream, this should key off
|
|
|
|
// the TrackID and not just the type
|
|
|
|
RefPtr<AudioDevice> audioDevice =
|
|
|
|
aTrackID == kAudioTrack ? mAudioDevice.get() : nullptr;
|
|
|
|
RefPtr<VideoDevice> videoDevice =
|
|
|
|
aTrackID == kVideoTrack ? mVideoDevice.get() : nullptr;
|
|
|
|
|
|
|
|
if (mStopped || (!audioDevice && !videoDevice))
|
2015-09-21 01:45:57 +03:00
|
|
|
{
|
|
|
|
LOG(("gUM track %d applyConstraints, but we don't have type %s",
|
2016-02-01 17:43:38 +03:00
|
|
|
aTrackID, aTrackID == kAudioTrack ? "audio" : "video"));
|
2015-09-20 17:04:51 +03:00
|
|
|
p->Resolve(false);
|
|
|
|
return p.forget();
|
2015-09-21 01:45:57 +03:00
|
|
|
}
|
2015-09-20 17:04:51 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager::GetInstance();
|
2015-09-20 17:04:51 +03:00
|
|
|
uint32_t id = mgr->mOutstandingVoidPledges.Append(*p);
|
|
|
|
uint64_t windowId = aWindow->WindowID();
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
MediaManager::PostTask(NewTaskFrom([id, windowId,
|
|
|
|
audioDevice, videoDevice,
|
|
|
|
aConstraints]() mutable {
|
2015-09-20 17:04:51 +03:00
|
|
|
MOZ_ASSERT(MediaManager::IsInMediaThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager::GetInstance();
|
2015-09-20 17:04:51 +03:00
|
|
|
const char* badConstraint = nullptr;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (audioDevice) {
|
2016-06-16 02:25:07 +03:00
|
|
|
rv = audioDevice->Restart(aConstraints, mgr->mPrefs, &badConstraint);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE && !badConstraint) {
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<AudioDevice>> audios;
|
2015-09-20 17:04:51 +03:00
|
|
|
audios.AppendElement(audioDevice);
|
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(aConstraints,
|
|
|
|
audios);
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-16 02:25:07 +03:00
|
|
|
rv = videoDevice->Restart(aConstraints, mgr->mPrefs, &badConstraint);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE && !badConstraint) {
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<VideoDevice>> videos;
|
2015-09-20 17:04:51 +03:00
|
|
|
videos.AppendElement(videoDevice);
|
|
|
|
badConstraint = MediaConstraintsHelper::SelectSettings(aConstraints,
|
|
|
|
videos);
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableFrom([id, windowId, rv,
|
|
|
|
badConstraint]() mutable {
|
2015-09-20 17:04:51 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaManager> mgr = MediaManager_GetInstance();
|
2015-09-20 17:04:51 +03:00
|
|
|
if (!mgr) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-11-19 06:17:09 +03:00
|
|
|
RefPtr<PledgeVoid> p = mgr->mOutstandingVoidPledges.Remove(id);
|
2015-09-20 17:04:51 +03:00
|
|
|
if (p) {
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
p->Resolve(false);
|
|
|
|
} else {
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* window = nsGlobalWindow::GetInnerWindowWithId(windowId);
|
2015-09-20 17:04:51 +03:00
|
|
|
if (window) {
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
nsString constraint;
|
|
|
|
constraint.AssignASCII(badConstraint);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2016-01-30 20:05:36 +03:00
|
|
|
new MediaStreamError(window->AsInner(),
|
2015-09-20 17:04:51 +03:00
|
|
|
NS_LITERAL_STRING("OverconstrainedError"),
|
|
|
|
NS_LITERAL_STRING(""),
|
|
|
|
constraint);
|
|
|
|
p->Reject(error);
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamError> error =
|
2016-01-30 20:05:36 +03:00
|
|
|
new MediaStreamError(window->AsInner(),
|
2015-09-20 17:04:51 +03:00
|
|
|
NS_LITERAL_STRING("InternalError"));
|
|
|
|
p->Reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2016-04-28 03:06:05 +03:00
|
|
|
}));
|
2015-09-20 17:04:51 +03:00
|
|
|
}));
|
|
|
|
return p.forget();
|
2015-09-21 01:45:57 +03:00
|
|
|
}
|
|
|
|
|
2014-08-27 09:03:50 +04:00
|
|
|
// Stop backend for track
|
|
|
|
|
|
|
|
void
|
2016-02-05 15:20:20 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::StopTrack(TrackID aTrackID)
|
2014-08-27 09:03:50 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-02-05 15:20:20 +03:00
|
|
|
MOZ_ASSERT(aTrackID == kAudioTrack || aTrackID == kVideoTrack);
|
|
|
|
|
|
|
|
// XXX to support multiple tracks of a type in a stream, this should key off
|
|
|
|
// the TrackID and not just hard coded values.
|
|
|
|
|
|
|
|
bool stopAudio = aTrackID == kAudioTrack;
|
|
|
|
bool stopVideo = aTrackID == kVideoTrack;
|
|
|
|
|
|
|
|
if (mStopped ||
|
|
|
|
(stopAudio && (mAudioStopped || !mAudioDevice)) ||
|
|
|
|
(stopVideo && (mVideoStopped || !mVideoDevice)))
|
2014-08-27 09:03:50 +04:00
|
|
|
{
|
2016-02-05 15:20:20 +03:00
|
|
|
LOG(("Can't stop gUM track %d (%s), exists=%d, stopped=%d",
|
|
|
|
aTrackID,
|
|
|
|
aTrackID == kAudioTrack ? "audio" : "video",
|
|
|
|
aTrackID == kAudioTrack ? !!mAudioDevice : !!mVideoDevice,
|
|
|
|
aTrackID == kAudioTrack ? mAudioStopped : mVideoStopped));
|
|
|
|
return;
|
2014-08-27 09:03:50 +04:00
|
|
|
}
|
2016-02-05 15:20:20 +03:00
|
|
|
|
|
|
|
if ((stopAudio || mAudioStopped || !mAudioDevice) &&
|
|
|
|
(stopVideo || mVideoStopped || !mVideoDevice)) {
|
|
|
|
Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<MediaOperationTask> mediaOperation =
|
2016-02-05 15:20:20 +03:00
|
|
|
new MediaOperationTask(MEDIA_STOP_TRACK,
|
|
|
|
this, nullptr, nullptr,
|
|
|
|
stopAudio ? mAudioDevice.get() : nullptr,
|
|
|
|
stopVideo ? mVideoDevice.get() : nullptr,
|
2016-04-28 03:06:05 +03:00
|
|
|
false , mWindowID, nullptr);
|
|
|
|
MediaManager::PostTask(mediaOperation.forget());
|
2016-02-05 15:20:20 +03:00
|
|
|
mAudioStopped |= stopAudio;
|
|
|
|
mVideoStopped |= stopVideo;
|
2014-08-27 09:03:50 +04:00
|
|
|
}
|
2014-08-22 13:27:16 +04:00
|
|
|
|
2013-01-07 06:31:32 +04:00
|
|
|
void
|
2015-09-30 09:08:22 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::NotifyFinished()
|
2013-01-07 06:31:32 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-01-08 06:44:43 +04:00
|
|
|
mFinished = true;
|
2016-02-05 15:20:20 +03:00
|
|
|
Stop(); // we know it's been activated
|
2015-09-30 09:08:22 +03:00
|
|
|
|
2016-03-08 00:44:25 +03:00
|
|
|
RefPtr<MediaManager> manager(MediaManager::GetIfExists());
|
|
|
|
if (manager) {
|
|
|
|
manager->RemoveFromWindowList(mWindowID, this);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Late NotifyFinished after MediaManager shutdown");
|
|
|
|
}
|
2013-01-07 06:31:32 +04:00
|
|
|
}
|
|
|
|
|
2014-07-14 09:48:02 +04:00
|
|
|
// Called from the MediaStreamGraph thread
|
|
|
|
void
|
|
|
|
GetUserMediaCallbackMediaStreamListener::NotifyDirectListeners(MediaStreamGraph* aGraph,
|
|
|
|
bool aHasListeners)
|
|
|
|
{
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<MediaOperationTask> mediaOperation =
|
2014-09-12 18:49:39 +04:00
|
|
|
new MediaOperationTask(MEDIA_DIRECT_LISTENERS,
|
|
|
|
this, nullptr, nullptr,
|
2015-09-19 07:49:07 +03:00
|
|
|
mAudioDevice, mVideoDevice,
|
2016-04-28 03:06:05 +03:00
|
|
|
aHasListeners, mWindowID, nullptr);
|
|
|
|
MediaManager::PostTask(mediaOperation.forget());
|
2014-07-14 09:48:02 +04:00
|
|
|
}
|
|
|
|
|
2013-01-08 06:44:43 +04:00
|
|
|
// this can be in response to our own RemoveListener() (via ::Remove()), or
|
|
|
|
// because the DOM GC'd the DOMLocalMediaStream/etc we're attached to.
|
|
|
|
void
|
2015-09-30 09:08:22 +03:00
|
|
|
GetUserMediaCallbackMediaStreamListener::NotifyRemoved()
|
2013-01-08 06:44:43 +04:00
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MM_LOG(("Listener removed by DOM Destroy(), mFinished = %d", (int) mFinished));
|
|
|
|
mRemoved = true;
|
|
|
|
|
2013-01-08 06:44:43 +04:00
|
|
|
if (!mFinished) {
|
2015-09-30 09:08:22 +03:00
|
|
|
NotifyFinished();
|
2013-01-08 06:44:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 10:07:48 +03:00
|
|
|
GetUserMediaNotificationEvent::GetUserMediaNotificationEvent(
|
|
|
|
GetUserMediaCallbackMediaStreamListener* aListener,
|
|
|
|
GetUserMediaStatus aStatus,
|
|
|
|
bool aIsAudio, bool aIsVideo, uint64_t aWindowID)
|
|
|
|
: mListener(aListener) , mStatus(aStatus) , mIsAudio(aIsAudio)
|
|
|
|
, mIsVideo(aIsVideo), mWindowID(aWindowID) {}
|
|
|
|
|
|
|
|
GetUserMediaNotificationEvent::GetUserMediaNotificationEvent(
|
|
|
|
GetUserMediaStatus aStatus,
|
|
|
|
already_AddRefed<DOMMediaStream> aStream,
|
|
|
|
OnTracksAvailableCallback* aOnTracksAvailableCallback,
|
|
|
|
bool aIsAudio, bool aIsVideo, uint64_t aWindowID,
|
|
|
|
already_AddRefed<nsIDOMGetUserMediaErrorCallback> aError)
|
|
|
|
: mStream(aStream), mOnTracksAvailableCallback(aOnTracksAvailableCallback),
|
|
|
|
mStatus(aStatus), mIsAudio(aIsAudio), mIsVideo(aIsVideo), mWindowID(aWindowID),
|
|
|
|
mOnFailure(aError) {}
|
|
|
|
GetUserMediaNotificationEvent::~GetUserMediaNotificationEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-28 08:14:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetUserMediaNotificationEvent::Run()
|
|
|
|
{
|
2015-09-30 09:08:22 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-08-28 08:14:57 +04:00
|
|
|
// Make sure mStream is cleared and our reference to the DOMMediaStream
|
|
|
|
// is dropped on the main thread, no matter what happens in this method.
|
|
|
|
// Otherwise this object might be destroyed off the main thread,
|
|
|
|
// releasing DOMMediaStream off the main thread, which is not allowed.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMMediaStream> stream = mStream.forget();
|
2013-08-28 08:14:57 +04:00
|
|
|
|
|
|
|
nsString msg;
|
|
|
|
switch (mStatus) {
|
|
|
|
case STARTING:
|
|
|
|
msg = NS_LITERAL_STRING("starting");
|
|
|
|
stream->OnTracksAvailable(mOnTracksAvailableCallback.forget());
|
|
|
|
break;
|
|
|
|
case STOPPING:
|
2014-08-22 13:27:16 +04:00
|
|
|
case STOPPED_TRACK:
|
|
|
|
msg = NS_LITERAL_STRING("shutdown");
|
|
|
|
break;
|
2013-08-28 08:14:57 +04:00
|
|
|
}
|
2013-10-23 15:10:42 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<nsGlobalWindow> window = nsGlobalWindow::GetInnerWindowWithId(mWindowID);
|
2013-11-26 10:22:16 +04:00
|
|
|
NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
|
2013-10-23 15:10:42 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
return MediaManager::NotifyRecordingStatusChange(window->AsInner(), msg, mIsAudio, mIsVideo);
|
2013-08-28 08:14:57 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 11:35:15 +04:00
|
|
|
} // namespace mozilla
|