2013-07-05 05:50:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "MediaRecorder.h"
|
2017-05-24 19:51:47 +03:00
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "DOMMediaStream.h"
|
2017-05-24 19:51:47 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2016-03-09 05:32:02 +03:00
|
|
|
#include "MediaDecoder.h"
|
2013-07-05 05:50:25 +04:00
|
|
|
#include "MediaEncoder.h"
|
2017-05-24 19:51:47 +03:00
|
|
|
#include "MediaStreamGraphImpl.h"
|
2017-12-15 23:45:35 +03:00
|
|
|
#include "VideoUtils.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-08-28 01:40:00 +04:00
|
|
|
#include "mozilla/dom/AudioStreamTrack.h"
|
|
|
|
#include "mozilla/dom/BlobEvent.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2017-08-07 00:48:42 +03:00
|
|
|
#include "mozilla/dom/MediaRecorderErrorEvent.h"
|
2017-10-05 08:41:41 +03:00
|
|
|
#include "mozilla/dom/MutableBlobStorage.h"
|
2014-08-28 01:40:00 +04:00
|
|
|
#include "mozilla/dom/VideoStreamTrack.h"
|
2017-05-24 19:51:47 +03:00
|
|
|
#include "mozilla/media/MediaUtils.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/TaskQueue.h"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2017-05-24 19:51:47 +03:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
|
|
|
#include "nsContentTypeParser.h"
|
2016-02-17 00:55:33 +03:00
|
|
|
#include "nsContentUtils.h"
|
2018-03-19 17:52:36 +03:00
|
|
|
#include "nsDocShell.h"
|
2013-07-05 05:50:25 +04:00
|
|
|
#include "nsError.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-01-06 04:54:27 +03:00
|
|
|
#include "nsIPermissionManager.h"
|
2014-03-19 10:52:45 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2016-02-01 17:47:17 +03:00
|
|
|
#include "nsIScriptError.h"
|
2014-03-19 10:52:45 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2014-06-26 13:22:05 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2014-08-28 01:40:00 +04:00
|
|
|
#include "nsTArray.h"
|
2013-12-18 14:39:45 +04:00
|
|
|
|
2014-10-25 21:24:36 +04:00
|
|
|
#ifdef LOG
|
|
|
|
# undef LOG
|
|
|
|
#endif
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
mozilla::LazyLogModule gMediaRecorderLog("MediaRecorder");
|
2015-05-21 23:22:04 +03:00
|
|
|
#define LOG(type, msg) MOZ_LOG(gMediaRecorderLog, type, msg)
|
2014-03-25 21:11:58 +04:00
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
using namespace mozilla::media;
|
|
|
|
|
|
|
|
/* static */ StaticRefPtr<nsIAsyncShutdownBlocker>
|
|
|
|
gMediaRecorderShutdownBlocker;
|
|
|
|
static nsTHashtable<nsRefPtrHashKey<MediaRecorder::Session>> gSessions;
|
|
|
|
|
2014-07-01 09:25:29 +04:00
|
|
|
/**
|
2017-05-24 19:51:47 +03:00
|
|
|
* MediaRecorderReporter measures memory being used by the Media Recorder.
|
|
|
|
*
|
|
|
|
* It is a singleton reporter and the single class object lives as long as at
|
|
|
|
* least one Recorder is registered. In MediaRecorder, the reporter is
|
|
|
|
* unregistered when it is destroyed.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class MediaRecorderReporter final : public nsIMemoryReporter {
|
2014-07-01 09:25:29 +04:00
|
|
|
public:
|
|
|
|
static void AddMediaRecorder(MediaRecorder* aRecorder) {
|
2017-11-10 13:58:33 +03:00
|
|
|
if (!sUniqueInstance) {
|
|
|
|
sUniqueInstance = MakeAndAddRef<MediaRecorderReporter>();
|
|
|
|
RegisterWeakAsyncMemoryReporter(sUniqueInstance);
|
|
|
|
}
|
|
|
|
sUniqueInstance->mRecorders.AppendElement(aRecorder);
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RemoveMediaRecorder(MediaRecorder* aRecorder) {
|
2017-11-10 13:58:33 +03:00
|
|
|
if (!sUniqueInstance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sUniqueInstance->mRecorders.RemoveElement(aRecorder);
|
|
|
|
if (sUniqueInstance->mRecorders.IsEmpty()) {
|
|
|
|
UnregisterWeakMemoryReporter(sUniqueInstance);
|
2014-07-01 09:25:29 +04:00
|
|
|
sUniqueInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 13:58:33 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
MediaRecorderReporter() = default;
|
|
|
|
|
2016-08-08 03:54:50 +03:00
|
|
|
NS_IMETHOD
|
2014-07-01 09:25:29 +04:00
|
|
|
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aAnonymize) override {
|
2017-05-24 19:51:47 +03:00
|
|
|
nsTArray<RefPtr<MediaRecorder::SizeOfPromise>> promises;
|
2017-11-10 13:58:33 +03:00
|
|
|
for (const RefPtr<MediaRecorder>& recorder : mRecorders) {
|
2017-05-24 19:51:47 +03:00
|
|
|
promises.AppendElement(recorder->SizeOfExcludingThis(MallocSizeOf));
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
nsCOMPtr<nsIHandleReportCallback> handleReport = aHandleReport;
|
|
|
|
nsCOMPtr<nsISupports> data = aData;
|
|
|
|
MediaRecorder::SizeOfPromise::All(GetCurrentThreadSerialEventTarget(),
|
|
|
|
promises)
|
|
|
|
->Then(
|
|
|
|
GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[handleReport, data](const nsTArray<size_t>& sizes) {
|
2017-11-10 13:59:43 +03:00
|
|
|
nsCOMPtr<nsIMemoryReporterManager> manager =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
if (!manager) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
size_t sum = 0;
|
|
|
|
for (const size_t& size : sizes) {
|
|
|
|
sum += size;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
handleReport->Callback(
|
|
|
|
EmptyCString(), NS_LITERAL_CSTRING("explicit/media/recorder"),
|
|
|
|
KIND_HEAP, UNITS_BYTES, sum,
|
|
|
|
NS_LITERAL_CSTRING("Memory used by media recorder."), data);
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-11-10 13:59:43 +03:00
|
|
|
manager->EndReport();
|
2017-05-24 19:51:47 +03:00
|
|
|
},
|
|
|
|
[](size_t) { MOZ_CRASH("Unexpected reject"); });
|
2014-07-01 09:25:29 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
|
2017-11-10 13:58:33 +03:00
|
|
|
|
|
|
|
virtual ~MediaRecorderReporter() {
|
|
|
|
MOZ_ASSERT(mRecorders.IsEmpty(), "All recorders must have been removed");
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
2017-11-10 13:58:33 +03:00
|
|
|
|
|
|
|
static StaticRefPtr<MediaRecorderReporter> sUniqueInstance;
|
|
|
|
|
|
|
|
nsTArray<RefPtr<MediaRecorder>> mRecorders;
|
2014-07-01 09:25:29 +04:00
|
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(MediaRecorderReporter, nsIMemoryReporter);
|
|
|
|
|
2016-11-18 14:29:13 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MediaRecorder)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaRecorder,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAudioNode)
|
2017-08-07 00:49:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSecurityDomException)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnknownDomException)
|
2016-11-18 14:29:13 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaRecorder,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDOMStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAudioNode)
|
2017-08-07 00:49:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSecurityDomException)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mUnknownDomException)
|
2016-11-18 14:29:13 +03:00
|
|
|
tmp->UnRegisterActivityObserver();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaRecorder)
|
2014-06-19 06:11:34 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDocumentActivity)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaRecorder, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaRecorder, DOMEventTargetHelper)
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
/**
|
|
|
|
* Session is an object to represent a single recording event.
|
|
|
|
* In original design, all recording context is stored in MediaRecorder, which
|
2017-09-29 17:11:47 +03:00
|
|
|
* causes a problem if someone calls MediaRecorder::Stop and
|
2013-09-26 17:40:40 +04:00
|
|
|
* MediaRecorder::Start quickly. To prevent blocking main thread, media encoding
|
|
|
|
* is executed in a second thread, named as Read Thread. For the same reason, we
|
2017-09-29 17:11:47 +03:00
|
|
|
* do not wait Read Thread shutdown in MediaRecorder::Stop. If someone call
|
|
|
|
* MediaRecorder::Start before Read Thread shutdown, the same recording context
|
|
|
|
* in MediaRecorder might be access by two Reading Threads, which cause a
|
2013-09-26 17:40:40 +04:00
|
|
|
* problem. In the new design, we put recording context into Session object,
|
|
|
|
* including Read Thread. Each Session has its own recording context and Read
|
|
|
|
* Thread, problem is been resolved.
|
|
|
|
*
|
|
|
|
* Life cycle of a Session object.
|
|
|
|
* 1) Initialization Stage (in main thread)
|
2013-12-18 14:39:45 +04:00
|
|
|
* Setup media streams in MSG, and bind MediaEncoder with Source Stream when
|
2013-09-26 17:40:40 +04:00
|
|
|
* mStream is available. Resource allocation, such as encoded data cache buffer
|
|
|
|
* and MediaEncoder. Create read thread. Automatically switch to Extract stage
|
|
|
|
* in the end of this stage. 2) Extract Stage (in Read Thread) Pull encoded A/V
|
|
|
|
* frames from MediaEncoder, dispatch to OnDataAvailable handler. Unless a
|
|
|
|
* client calls Session::Stop, Session object keeps stay in this stage. 3)
|
|
|
|
* Destroy Stage (in main thread) Switch from Extract stage to Destroy stage by
|
|
|
|
* calling Session::Stop. Release session resource and remove associated streams
|
|
|
|
* from MSG.
|
|
|
|
*
|
2014-06-19 06:11:34 +04:00
|
|
|
* Lifetime of MediaRecorder and Session objects.
|
2014-08-18 07:42:49 +04:00
|
|
|
* 1) MediaRecorder creates a Session in MediaRecorder::Start function and holds
|
2017-05-24 19:51:47 +03:00
|
|
|
* a reference to Session. Then the Session registers itself to a
|
|
|
|
* ShutdownBlocker and also holds a reference to MediaRecorder.
|
2014-08-18 07:42:49 +04:00
|
|
|
* Therefore, the reference dependency in gecko is:
|
2017-05-24 19:51:47 +03:00
|
|
|
* ShutdownBlocker -> Session <-> MediaRecorder, note that there is a cycle
|
2014-08-18 07:42:49 +04:00
|
|
|
* reference between Session and MediaRecorder.
|
2013-09-26 17:40:40 +04:00
|
|
|
* 2) A Session is destroyed in DestroyRunnable after MediaRecorder::Stop being
|
|
|
|
* called _and_ all encoded media data been passed to OnDataAvailable handler.
|
2014-06-19 06:11:34 +04:00
|
|
|
* 3) MediaRecorder::Stop is called by user or the document is going to
|
|
|
|
* inactive or invisible.
|
2013-09-26 17:40:40 +04:00
|
|
|
*/
|
2017-05-24 19:51:47 +03:00
|
|
|
class MediaRecorder::Session : public PrincipalChangeObserver<MediaStreamTrack>,
|
2016-02-01 17:47:17 +03:00
|
|
|
public DOMMediaStream::TrackListener {
|
2017-05-24 19:51:47 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Session)
|
2013-11-05 02:27:39 +04:00
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
// Main thread task.
|
|
|
|
// Create a blob event and send back to client.
|
2016-04-26 03:23:21 +03:00
|
|
|
class PushBlobRunnable : public Runnable, public MutableBlobStorageCallback {
|
2013-09-26 17:40:40 +04:00
|
|
|
public:
|
2018-02-12 23:44:40 +03:00
|
|
|
// We need to always declare refcounting because
|
|
|
|
// MutableBlobStorageCallback has pure-virtual refcounting.
|
2017-10-05 08:41:41 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
// aDestroyRunnable can be null. If it's not, it will be dispatched after
|
|
|
|
// the PushBlobRunnable::Run().
|
|
|
|
PushBlobRunnable(Session* aSession, Runnable* aDestroyRunnable)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MediaRecorder::Session::PushBlobRunnable"),
|
|
|
|
mSession(aSession),
|
2017-10-05 08:41:41 +03:00
|
|
|
mDestroyRunnable(aDestroyRunnable) {}
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.PushBlobRunnable s=(%p)", mSession.get()));
|
2013-09-26 17:40:40 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
mSession->GetBlobWhenReady(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlobStoreCompleted(MutableBlobStorage* aBlobStorage, Blob* aBlob,
|
|
|
|
nsresult aRv) override {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> recorder = mSession->mRecorder;
|
2014-03-25 21:11:58 +04:00
|
|
|
if (!recorder) {
|
2017-10-05 08:41:41 +03:00
|
|
|
return;
|
2014-03-25 21:11:58 +04:00
|
|
|
}
|
2014-06-27 07:07:45 +04:00
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
if (NS_FAILED(aRv)) {
|
2017-10-31 20:34:23 +03:00
|
|
|
mSession->DoSessionEndTask(aRv);
|
2017-10-05 08:41:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = recorder->CreateAndDispatchBlobEvent(aBlob);
|
2013-09-26 17:40:40 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-10-31 20:34:23 +03:00
|
|
|
mSession->DoSessionEndTask(aRv);
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
2014-03-25 21:11:58 +04:00
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
if (mDestroyRunnable &&
|
|
|
|
NS_FAILED(NS_DispatchToMainThread(mDestroyRunnable.forget()))) {
|
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread failed");
|
|
|
|
}
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
private:
|
2017-10-05 08:41:41 +03:00
|
|
|
~PushBlobRunnable() = default;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Session> mSession;
|
2017-10-05 08:41:41 +03:00
|
|
|
|
|
|
|
// The generation of the blob is async. In order to avoid dispatching the
|
|
|
|
// DestroyRunnable before pushing the blob event, we store the runnable
|
|
|
|
// here.
|
|
|
|
RefPtr<Runnable> mDestroyRunnable;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StoreEncodedBufferRunnable final : public Runnable {
|
|
|
|
RefPtr<Session> mSession;
|
|
|
|
nsTArray<nsTArray<uint8_t>> mBuffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StoreEncodedBufferRunnable(Session* aSession,
|
|
|
|
nsTArray<nsTArray<uint8_t>>&& aBuffer)
|
|
|
|
: Runnable("StoreEncodedBufferRunnable"),
|
|
|
|
mSession(aSession),
|
2018-05-30 22:15:35 +03:00
|
|
|
mBuffer(std::move(aBuffer)) {}
|
2017-10-05 08:41:41 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2017-10-31 11:48:54 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-05 08:41:41 +03:00
|
|
|
mSession->MaybeCreateMutableBlobStorage();
|
|
|
|
for (uint32_t i = 0; i < mBuffer.Length(); i++) {
|
2017-10-31 11:48:54 +03:00
|
|
|
if (mBuffer[i].IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mSession->mMutableBlobStorage->Append(
|
|
|
|
mBuffer[i].Elements(), mBuffer[i].Length());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mSession->DoSessionEndTask(rv);
|
|
|
|
break;
|
2017-10-05 08:41:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
};
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2014-11-18 09:40:48 +03:00
|
|
|
// Notify encoder error, run in main thread task. (Bug 1095381)
|
2016-04-26 03:23:21 +03:00
|
|
|
class EncoderErrorNotifierRunnable : public Runnable {
|
2014-11-18 09:40:48 +03:00
|
|
|
public:
|
|
|
|
explicit EncoderErrorNotifierRunnable(Session* aSession)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MediaRecorder::Session::EncoderErrorNotifierRunnable"),
|
|
|
|
mSession(aSession) {}
|
2014-11-18 09:40:48 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Session.ErrorNotifyRunnable s=(%p)", mSession.get()));
|
2014-11-18 09:40:48 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> recorder = mSession->mRecorder;
|
2014-11-18 09:40:48 +03:00
|
|
|
if (!recorder) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
recorder->NotifyError(NS_ERROR_UNEXPECTED);
|
2014-11-18 09:40:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Session> mSession;
|
2014-11-18 09:40:48 +03:00
|
|
|
};
|
|
|
|
|
2019-01-14 21:32:52 +03:00
|
|
|
// Fire a named event, run in main thread task.
|
|
|
|
class DispatchEventRunnable : public Runnable {
|
|
|
|
public:
|
|
|
|
explicit DispatchEventRunnable(Session* aSession,
|
|
|
|
const nsAString& aEventName)
|
|
|
|
: Runnable("dom::MediaRecorder::Session::DispatchEventRunnable"),
|
|
|
|
mSession(aSession),
|
|
|
|
mEventName(aEventName) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Session.DispatchEventRunnable s=(%p) e=(%s)", mSession.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(mEventName).get()));
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(mSession->mRecorder, NS_OK);
|
|
|
|
mSession->mRecorder->DispatchSimpleEvent(mEventName);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<Session> mSession;
|
|
|
|
nsString mEventName;
|
|
|
|
};
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
// Main thread task.
|
|
|
|
// To delete RecordingSession object.
|
2016-04-26 03:23:21 +03:00
|
|
|
class DestroyRunnable : public Runnable {
|
2013-07-05 05:50:25 +04:00
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit DestroyRunnable(Session* aSession)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MediaRecorder::Session::DestroyRunnable"),
|
|
|
|
mSession(aSession) {}
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2017-01-09 06:08:09 +03:00
|
|
|
explicit DestroyRunnable(already_AddRefed<Session> aSession)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::MediaRecorder::Session::DestroyRunnable"),
|
|
|
|
mSession(aSession) {}
|
2017-01-09 06:08:09 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2017-10-31 20:34:23 +03:00
|
|
|
LOG(LogLevel::Debug,
|
|
|
|
("Session.DestroyRunnable session refcnt = (%d) s=(%p)",
|
|
|
|
static_cast<int>(mSession->mRefCnt), mSession.get()));
|
2016-03-19 23:53:51 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() && mSession);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> recorder = mSession->mRecorder;
|
2014-03-25 21:11:58 +04:00
|
|
|
if (!recorder) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-05 02:27:39 +04:00
|
|
|
// SourceMediaStream is ended, and send out TRACK_EVENT_END notification.
|
|
|
|
// Read Thread will be terminate soon.
|
|
|
|
// We need to switch MediaRecorder to "Stop" state first to make sure
|
|
|
|
// MediaRecorder is not associated with this Session anymore, then, it's
|
|
|
|
// safe to delete this Session.
|
2014-01-29 19:09:43 +04:00
|
|
|
// Also avoid to run if this session already call stop before
|
2017-10-31 20:34:23 +03:00
|
|
|
if (mSession->mRunningState.isOk() &&
|
|
|
|
mSession->mRunningState.unwrap() != RunningState::Stopping &&
|
|
|
|
mSession->mRunningState.unwrap() != RunningState::Stopped) {
|
2017-09-01 02:38:52 +03:00
|
|
|
recorder->StopForSessionDestruction();
|
2017-01-09 06:08:09 +03:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(
|
|
|
|
new DestroyRunnable(mSession.forget())))) {
|
2014-06-26 13:22:05 +04:00
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread failed");
|
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-08-29 15:59:08 +04:00
|
|
|
|
2017-10-31 20:34:23 +03:00
|
|
|
if (mSession->mRunningState.isOk()) {
|
|
|
|
mSession->mRunningState = RunningState::Stopped;
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
// Dispatch stop event and clear MIME type.
|
2014-01-29 19:09:43 +04:00
|
|
|
mSession->mMimeType = NS_LITERAL_STRING("");
|
|
|
|
recorder->SetMimeType(mSession->mMimeType);
|
2014-03-25 21:11:58 +04:00
|
|
|
recorder->DispatchSimpleEvent(NS_LITERAL_STRING("stop"));
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
RefPtr<Session> session = mSession.forget();
|
|
|
|
session->Shutdown()->Then(
|
|
|
|
GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[session]() {
|
|
|
|
gSessions.RemoveEntry(session);
|
|
|
|
if (gSessions.Count() == 0 && gMediaRecorderShutdownBlocker) {
|
|
|
|
// All sessions finished before shutdown, no need to keep the
|
|
|
|
// blocker.
|
|
|
|
RefPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
|
|
|
|
barrier->RemoveBlocker(gMediaRecorderShutdownBlocker);
|
|
|
|
gMediaRecorderShutdownBlocker = nullptr;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[]() { MOZ_CRASH("Not reached"); });
|
2013-07-05 05:50:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-05 02:27:39 +04:00
|
|
|
// Call mSession::Release automatically while DestroyRunnable be destroy.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Session> mSession;
|
2013-07-05 05:50:25 +04:00
|
|
|
};
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
class EncoderListener : public MediaEncoderListener {
|
|
|
|
public:
|
|
|
|
EncoderListener(TaskQueue* aEncoderThread, Session* aSession)
|
|
|
|
: mEncoderThread(aEncoderThread), mSession(aSession) {}
|
|
|
|
|
|
|
|
void Forget() {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
mSession = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialized() override {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
if (mSession) {
|
|
|
|
mSession->MediaEncoderInitialized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataAvailable() override {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
if (mSession) {
|
|
|
|
mSession->MediaEncoderDataAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Error() override {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
if (mSession) {
|
|
|
|
mSession->MediaEncoderError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() override {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
if (mSession) {
|
|
|
|
mSession->MediaEncoderShutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RefPtr<TaskQueue> mEncoderThread;
|
|
|
|
RefPtr<Session> mSession;
|
|
|
|
};
|
|
|
|
|
2014-11-18 09:40:48 +03:00
|
|
|
friend class EncoderErrorNotifierRunnable;
|
2013-09-26 17:40:40 +04:00
|
|
|
friend class PushBlobRunnable;
|
|
|
|
friend class DestroyRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Session(MediaRecorder* aRecorder, int32_t aTimeSlice)
|
2015-04-22 10:09:49 +03:00
|
|
|
: mRecorder(aRecorder),
|
2018-11-23 18:02:35 +03:00
|
|
|
mMediaStreamReady(false),
|
2015-04-22 10:09:49 +03:00
|
|
|
mTimeSlice(aTimeSlice),
|
2017-10-31 20:34:23 +03:00
|
|
|
mRunningState(RunningState::Idling) {
|
2013-09-26 17:40:40 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
mMaxMemory = Preferences::GetUint("media.recorder.max_memory",
|
|
|
|
MAX_ALLOW_MEMORY_BUFFER);
|
2014-01-04 21:20:21 +04:00
|
|
|
mLastBlobTimeStamp = TimeStamp::Now();
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:47:17 +03:00
|
|
|
void PrincipalChanged(MediaStreamTrack* aTrack) override {
|
|
|
|
NS_ASSERTION(mMediaStreamTracks.Contains(aTrack),
|
|
|
|
"Principal changed for unrecorded track");
|
|
|
|
if (!MediaStreamTracksPrincipalSubsumes()) {
|
|
|
|
DoSessionEndTask(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack) override {
|
|
|
|
LOG(LogLevel::Warning,
|
|
|
|
("Session.NotifyTrackAdded %p Raising error due to track set change",
|
|
|
|
this));
|
2018-11-23 18:02:35 +03:00
|
|
|
if (mMediaStreamReady) {
|
|
|
|
DoSessionEndTask(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NewRunnableMethod("MediaRecorder::Session::MediaStreamReady", this,
|
|
|
|
&Session::MediaStreamReady));
|
|
|
|
return;
|
2016-02-01 17:47:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack) override {
|
2018-11-23 18:02:35 +03:00
|
|
|
if (!mMediaStreamReady) {
|
|
|
|
// We haven't chosen the track set to record yet.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
if (aTrack->Ended()) {
|
|
|
|
// TrackEncoder will pickup tracks that end itself.
|
|
|
|
return;
|
2016-02-01 17:47:17 +03:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
MOZ_ASSERT(mEncoder);
|
|
|
|
if (mEncoder) {
|
|
|
|
mEncoder->RemoveMediaStreamTrack(aTrack);
|
2016-02-01 17:47:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogLevel::Warning,
|
|
|
|
("Session.NotifyTrackRemoved %p Raising error due to track set change",
|
|
|
|
this));
|
|
|
|
DoSessionEndTask(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
void Start() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.Start %p", this));
|
2013-09-26 17:40:40 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-05 05:16:29 +03:00
|
|
|
DOMMediaStream* domStream = mRecorder->Stream();
|
|
|
|
if (domStream) {
|
2017-05-24 19:51:47 +03:00
|
|
|
// The callback reports back when tracks are available and can be
|
|
|
|
// attached to MediaEncoder. This allows `recorder.start()` before any
|
2017-02-24 16:34:25 +03:00
|
|
|
// tracks are available. We have supported this historically and have
|
|
|
|
// mochitests assuming this behavior.
|
2018-11-23 18:02:35 +03:00
|
|
|
mMediaStream = domStream;
|
|
|
|
mMediaStream->RegisterTrackListener(this);
|
|
|
|
nsTArray<RefPtr<MediaStreamTrack>> tracks(2);
|
|
|
|
mMediaStream->GetTracks(tracks);
|
|
|
|
for (const auto& track : tracks) {
|
|
|
|
// Notify of existing tracks, as the stream doesn't do this by itself.
|
|
|
|
NotifyTrackAdded(track);
|
|
|
|
}
|
2017-02-24 16:34:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRecorder->mAudioNode) {
|
2016-02-01 17:47:17 +03:00
|
|
|
// Check that we may access the audio node's content.
|
|
|
|
if (!AudioNodePrincipalSubsumes()) {
|
|
|
|
LOG(LogLevel::Warning,
|
|
|
|
("Session.Start AudioNode principal check failed"));
|
|
|
|
DoSessionEndTask(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
TrackRate trackRate =
|
|
|
|
mRecorder->mAudioNode->Context()->Graph()->GraphRate();
|
2016-02-01 17:47:17 +03:00
|
|
|
|
2016-01-05 05:16:29 +03:00
|
|
|
// Web Audio node has only audio.
|
2016-06-15 18:48:44 +03:00
|
|
|
InitEncoder(ContainerWriter::CREATE_AUDIO_TRACK, trackRate);
|
2017-02-24 16:34:25 +03:00
|
|
|
return;
|
2016-01-05 05:16:29 +03:00
|
|
|
}
|
2017-02-24 16:34:25 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(false, "Unknown source");
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.Stop %p", this));
|
2013-09-26 17:40:40 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
if (mEncoder) {
|
|
|
|
mEncoder->Stop();
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:34:23 +03:00
|
|
|
if (mRunningState.isOk() &&
|
|
|
|
mRunningState.unwrap() == RunningState::Idling) {
|
|
|
|
LOG(LogLevel::Debug, ("Session.Stop Explicit end task %p", this));
|
2015-04-22 10:09:49 +03:00
|
|
|
// End the Session directly if there is no ExtractRunnable.
|
|
|
|
DoSessionEndTask(NS_OK);
|
2017-10-31 20:34:23 +03:00
|
|
|
} else if (mRunningState.isOk() &&
|
|
|
|
(mRunningState.unwrap() == RunningState::Starting ||
|
|
|
|
mRunningState.unwrap() == RunningState::Running)) {
|
|
|
|
mRunningState = RunningState::Stopping;
|
2015-04-22 10:09:49 +03:00
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:33:00 +04:00
|
|
|
nsresult Pause() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.Pause"));
|
2014-02-19 08:15:53 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
if (!mEncoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2016-03-19 08:04:50 +03:00
|
|
|
}
|
2014-01-27 06:33:00 +04:00
|
|
|
|
2019-03-22 14:42:55 +03:00
|
|
|
mEncoder->Suspend();
|
2019-01-14 21:32:53 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new DispatchEventRunnable(this, NS_LITERAL_STRING("pause")));
|
2014-01-27 06:33:00 +04:00
|
|
|
return NS_OK;
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:33:00 +04:00
|
|
|
nsresult Resume() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.Resume"));
|
2014-02-19 08:15:53 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
if (!mEncoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2016-03-19 08:04:50 +03:00
|
|
|
}
|
2014-01-27 06:33:00 +04:00
|
|
|
|
2019-03-22 14:42:55 +03:00
|
|
|
mEncoder->Resume();
|
2019-01-14 21:32:53 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new DispatchEventRunnable(this, NS_LITERAL_STRING("resume")));
|
2014-01-27 06:33:00 +04:00
|
|
|
return NS_OK;
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 13:26:11 +04:00
|
|
|
nsresult RequestData() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.RequestData"));
|
2014-10-17 13:26:11 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
if (NS_FAILED(
|
|
|
|
NS_DispatchToMainThread(new PushBlobRunnable(this, nullptr)))) {
|
2014-10-17 13:26:11 +04:00
|
|
|
MOZ_ASSERT(false, "RequestData NS_DispatchToMainThread failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
void MaybeCreateMutableBlobStorage() {
|
|
|
|
if (!mMutableBlobStorage) {
|
|
|
|
mMutableBlobStorage = new MutableBlobStorage(
|
|
|
|
MutableBlobStorage::eCouldBeInTemporaryFile, nullptr, mMaxMemory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetBlobWhenReady(MutableBlobStorageCallback* aCallback) {
|
2014-03-25 21:11:58 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-05 08:41:41 +03:00
|
|
|
|
|
|
|
MaybeCreateMutableBlobStorage();
|
|
|
|
mMutableBlobStorage->GetBlobWhenReady(mRecorder->GetParentObject(),
|
|
|
|
NS_ConvertUTF16toUTF8(mMimeType),
|
|
|
|
aCallback);
|
|
|
|
mMutableBlobStorage = nullptr;
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
RefPtr<SizeOfPromise> SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-05 08:41:41 +03:00
|
|
|
size_t encodedBufferSize =
|
|
|
|
mMutableBlobStorage ? mMutableBlobStorage->SizeOfCurrentMemoryBuffer()
|
|
|
|
: 0;
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
if (!mEncoder) {
|
|
|
|
return SizeOfPromise::CreateAndResolve(encodedBufferSize, __func__);
|
2013-11-25 03:57:52 +04:00
|
|
|
}
|
2014-06-19 06:11:34 +04:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
auto& encoder = mEncoder;
|
|
|
|
return InvokeAsync(
|
|
|
|
mEncoderThread, __func__,
|
|
|
|
[encoder, encodedBufferSize, aMallocSizeOf]() {
|
|
|
|
return SizeOfPromise::CreateAndResolve(
|
|
|
|
encodedBufferSize + encoder->SizeOfExcludingThis(aMallocSizeOf),
|
|
|
|
__func__);
|
|
|
|
});
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
private:
|
2017-05-24 19:51:47 +03:00
|
|
|
// Only DestroyRunnable is allowed to delete Session object on main thread.
|
2014-06-24 20:36:43 +04:00
|
|
|
virtual ~Session() {
|
2017-05-24 19:51:47 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mShutdownPromise);
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.~Session (%p)", this));
|
2014-06-24 20:36:43 +04:00
|
|
|
}
|
2017-10-05 08:41:41 +03:00
|
|
|
// Pull encoded media data from MediaEncoder and put into MutableBlobStorage.
|
2013-09-26 17:40:40 +04:00
|
|
|
// Destroy this session object in the end of this function.
|
2014-06-24 10:05:49 +04:00
|
|
|
// If the bool aForceFlush is true, we will force to dispatch a
|
|
|
|
// PushBlobRunnable to main thread.
|
2017-10-05 08:41:41 +03:00
|
|
|
void Extract(bool aForceFlush, Runnable* aDestroyRunnable) {
|
2017-05-24 19:51:47 +03:00
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.Extract %p", this));
|
2013-09-26 17:40:40 +04:00
|
|
|
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("MediaRecorder::Session::Extract", OTHER);
|
2014-10-20 12:17:26 +04:00
|
|
|
|
2014-01-04 21:20:21 +04:00
|
|
|
// Pull encoded media data from MediaEncoder
|
|
|
|
nsTArray<nsTArray<uint8_t>> encodedBuf;
|
2017-05-24 19:51:47 +03:00
|
|
|
nsresult rv = mEncoder->GetEncodedData(&encodedBuf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_RELEASE_ASSERT(encodedBuf.IsEmpty());
|
|
|
|
// Even if we failed to encode more data, it might be time to push a blob
|
|
|
|
// with already encoded data.
|
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2014-01-04 21:20:21 +04:00
|
|
|
// Append pulled data into cache buffer.
|
2017-10-05 08:41:41 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new StoreEncodedBufferRunnable(this, std::move(encodedBuf)));
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2014-06-24 10:05:49 +04:00
|
|
|
// Whether push encoded data back to onDataAvailable automatically or we
|
|
|
|
// need a flush.
|
2017-05-24 19:51:47 +03:00
|
|
|
bool pushBlob = aForceFlush;
|
|
|
|
if (!pushBlob && mTimeSlice > 0 &&
|
|
|
|
(TimeStamp::Now() - mLastBlobTimeStamp).ToMilliseconds() > mTimeSlice) {
|
2014-06-24 10:05:49 +04:00
|
|
|
pushBlob = true;
|
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
if (pushBlob) {
|
2017-10-05 08:41:41 +03:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(
|
|
|
|
new PushBlobRunnable(this, aDestroyRunnable)))) {
|
2014-06-26 13:22:05 +04:00
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread PushBlobRunnable failed");
|
|
|
|
} else {
|
|
|
|
mLastBlobTimeStamp = TimeStamp::Now();
|
|
|
|
}
|
2017-10-05 08:41:41 +03:00
|
|
|
} else if (aDestroyRunnable) {
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(aDestroyRunnable))) {
|
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread DestroyRunnable failed");
|
|
|
|
}
|
2014-01-04 21:20:21 +04:00
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
|
|
|
|
2018-11-23 18:02:35 +03:00
|
|
|
void MediaStreamReady() {
|
|
|
|
if (!mMediaStream) {
|
|
|
|
// Already shut down. This can happen because MediaStreamReady is async.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMediaStreamReady) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-24 16:23:35 +03:00
|
|
|
|
2017-10-31 20:34:23 +03:00
|
|
|
if (!mRunningState.isOk() ||
|
|
|
|
mRunningState.unwrap() != RunningState::Idling) {
|
2017-02-24 16:23:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<RefPtr<mozilla::dom::MediaStreamTrack>> tracks;
|
2018-11-23 18:02:35 +03:00
|
|
|
mMediaStream->GetTracks(tracks);
|
2018-06-08 13:28:55 +03:00
|
|
|
uint8_t trackTypes = 0;
|
|
|
|
int32_t audioTracks = 0;
|
|
|
|
int32_t videoTracks = 0;
|
2017-02-24 16:23:35 +03:00
|
|
|
for (auto& track : tracks) {
|
|
|
|
if (track->Ended()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectMediaStreamTrack(*track);
|
2018-06-08 13:28:55 +03:00
|
|
|
|
|
|
|
if (track->AsAudioStreamTrack()) {
|
|
|
|
++audioTracks;
|
|
|
|
trackTypes |= ContainerWriter::CREATE_AUDIO_TRACK;
|
|
|
|
} else if (track->AsVideoStreamTrack()) {
|
|
|
|
++videoTracks;
|
|
|
|
trackTypes |= ContainerWriter::CREATE_VIDEO_TRACK;
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexpected track type");
|
|
|
|
}
|
2017-02-24 16:23:35 +03:00
|
|
|
}
|
|
|
|
|
2018-11-23 18:02:35 +03:00
|
|
|
if (trackTypes == 0) {
|
|
|
|
MOZ_ASSERT(audioTracks == 0);
|
|
|
|
MOZ_ASSERT(videoTracks == 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMediaStreamReady = true;
|
|
|
|
|
2018-06-08 13:28:55 +03:00
|
|
|
if (audioTracks > 1 || videoTracks > 1) {
|
2017-02-24 16:23:35 +03:00
|
|
|
// When MediaRecorder supports multiple tracks, we should set up a single
|
|
|
|
// MediaInputPort from the input stream, and let main thread check
|
|
|
|
// track principals async later.
|
|
|
|
nsPIDOMWindowInner* window = mRecorder->GetParentObject();
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* document = window ? window->GetExtantDoc() : nullptr;
|
2017-02-24 16:23:35 +03:00
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::errorFlag,
|
|
|
|
NS_LITERAL_CSTRING("Media"), document,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"MediaRecorderMultiTracksNotSupported");
|
|
|
|
DoSessionEndTask(NS_ERROR_ABORT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we may access the tracks' content.
|
|
|
|
if (!MediaStreamTracksPrincipalSubsumes()) {
|
2018-11-23 18:02:35 +03:00
|
|
|
LOG(LogLevel::Warning, ("Session.MediaTracksReady MediaStreamTracks "
|
2017-02-24 16:23:35 +03:00
|
|
|
"principal check failed"));
|
|
|
|
DoSessionEndTask(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogLevel::Debug,
|
2018-11-23 18:02:35 +03:00
|
|
|
("Session.MediaTracksReady track type = (%d)", trackTypes));
|
|
|
|
InitEncoder(trackTypes, mMediaStream->GraphRate());
|
2016-02-01 17:47:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectMediaStreamTrack(MediaStreamTrack& aTrack) {
|
2017-05-24 19:51:47 +03:00
|
|
|
for (auto& track : mMediaStreamTracks) {
|
|
|
|
if (track->AsAudioStreamTrack() && aTrack.AsAudioStreamTrack()) {
|
|
|
|
// We only allow one audio track. See bug 1276928.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (track->AsVideoStreamTrack() && aTrack.AsVideoStreamTrack()) {
|
|
|
|
// We only allow one video track. See bug 1276928.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 17:47:17 +03:00
|
|
|
mMediaStreamTracks.AppendElement(&aTrack);
|
|
|
|
aTrack.AddPrincipalChangeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrincipalSubsumes(nsIPrincipal* aPrincipal) {
|
|
|
|
if (!mRecorder->GetOwner()) return false;
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = mRecorder->GetOwner()->GetExtantDoc();
|
2016-02-01 17:47:17 +03:00
|
|
|
if (!doc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!aPrincipal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool subsumes;
|
|
|
|
if (NS_FAILED(doc->NodePrincipal()->Subsumes(aPrincipal, &subsumes))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return subsumes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaStreamTracksPrincipalSubsumes() {
|
|
|
|
MOZ_ASSERT(mRecorder->mDOMStream);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = nullptr;
|
|
|
|
for (RefPtr<MediaStreamTrack>& track : mMediaStreamTracks) {
|
|
|
|
nsContentUtils::CombineResourcePrincipals(&principal,
|
|
|
|
track->GetPrincipal());
|
|
|
|
}
|
|
|
|
return PrincipalSubsumes(principal);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioNodePrincipalSubsumes() {
|
2017-05-24 19:51:47 +03:00
|
|
|
MOZ_ASSERT(mRecorder->mAudioNode);
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = mRecorder->mAudioNode->GetOwner()
|
|
|
|
? mRecorder->mAudioNode->GetOwner()->GetExtantDoc()
|
|
|
|
: nullptr;
|
2016-02-01 17:47:17 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = doc ? doc->NodePrincipal() : nullptr;
|
|
|
|
return PrincipalSubsumes(principal);
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:48:44 +03:00
|
|
|
void InitEncoder(uint8_t aTrackTypes, TrackRate aTrackRate) {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session.InitEncoder %p", this));
|
2013-12-18 14:39:45 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2017-10-31 20:34:23 +03:00
|
|
|
if (!mRunningState.isOk() ||
|
|
|
|
mRunningState.unwrap() != RunningState::Idling) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Double-init");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
// Create a TaskQueue to read encode media data from MediaEncoder.
|
|
|
|
MOZ_RELEASE_ASSERT(!mEncoderThread);
|
|
|
|
RefPtr<SharedThreadPool> pool =
|
2017-12-15 23:45:35 +03:00
|
|
|
GetMediaThreadPool(MediaThreadType::WEBRTC_DECODER);
|
2017-05-24 19:51:47 +03:00
|
|
|
if (!pool) {
|
|
|
|
LOG(LogLevel::Debug, ("Session.InitEncoder %p Failed to create "
|
|
|
|
"MediaRecorderReadThread thread pool",
|
|
|
|
this));
|
|
|
|
DoSessionEndTask(NS_ERROR_FAILURE);
|
2015-04-22 10:09:49 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
2017-12-15 23:45:35 +03:00
|
|
|
mEncoderThread =
|
|
|
|
MakeAndAddRef<TaskQueue>(pool.forget(), "MediaRecorderReadThread");
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
if (!gMediaRecorderShutdownBlocker) {
|
|
|
|
// Add a shutdown blocker so mEncoderThread can be shutdown async.
|
|
|
|
class Blocker : public ShutdownBlocker {
|
|
|
|
public:
|
|
|
|
Blocker()
|
|
|
|
: ShutdownBlocker(
|
|
|
|
NS_LITERAL_STRING("MediaRecorder::Session: shutdown")) {}
|
|
|
|
|
|
|
|
NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient*) override {
|
|
|
|
// Distribute the global async shutdown blocker in a ticket. If there
|
|
|
|
// are zero graphs then shutdown is unblocked when we go out of scope.
|
|
|
|
RefPtr<ShutdownTicket> ticket =
|
|
|
|
MakeAndAddRef<ShutdownTicket>(gMediaRecorderShutdownBlocker);
|
|
|
|
gMediaRecorderShutdownBlocker = nullptr;
|
|
|
|
|
|
|
|
nsTArray<RefPtr<ShutdownPromise>> promises(gSessions.Count());
|
|
|
|
for (auto iter = gSessions.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
promises.AppendElement(iter.Get()->GetKey()->Shutdown());
|
|
|
|
}
|
|
|
|
gSessions.Clear();
|
|
|
|
ShutdownPromise::All(GetCurrentThreadSerialEventTarget(), promises)
|
|
|
|
->Then(GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[ticket]() mutable {
|
|
|
|
MOZ_ASSERT(gSessions.Count() == 0);
|
|
|
|
// Unblock shutdown
|
|
|
|
ticket = nullptr;
|
|
|
|
},
|
|
|
|
[]() { MOZ_CRASH("Not reached"); });
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
gMediaRecorderShutdownBlocker = MakeAndAddRef<Blocker>();
|
|
|
|
RefPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
|
|
|
|
nsresult rv = barrier->AddBlocker(
|
|
|
|
gMediaRecorderShutdownBlocker, NS_LITERAL_STRING(__FILE__), __LINE__,
|
|
|
|
NS_LITERAL_STRING("MediaRecorder::Session: shutdown"));
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
gSessions.PutEntry(this);
|
|
|
|
|
|
|
|
uint32_t audioBitrate = mRecorder->GetAudioBitrate();
|
|
|
|
uint32_t videoBitrate = mRecorder->GetVideoBitrate();
|
|
|
|
uint32_t bitrate = mRecorder->GetBitrate();
|
|
|
|
if (bitrate > 0) {
|
|
|
|
// There's a total cap set. We have to make sure the type-specific limits
|
|
|
|
// are within range.
|
|
|
|
if ((aTrackTypes & ContainerWriter::CREATE_AUDIO_TRACK) &&
|
|
|
|
(aTrackTypes & ContainerWriter::CREATE_VIDEO_TRACK) &&
|
|
|
|
audioBitrate + videoBitrate > bitrate) {
|
|
|
|
LOG(LogLevel::Info, ("Session.InitEncoder Bitrates higher than total "
|
|
|
|
"cap. Recalculating."));
|
|
|
|
double factor =
|
|
|
|
bitrate / static_cast<double>(audioBitrate + videoBitrate);
|
|
|
|
audioBitrate = static_cast<uint32_t>(audioBitrate * factor);
|
|
|
|
videoBitrate = static_cast<uint32_t>(videoBitrate * factor);
|
|
|
|
} else if ((aTrackTypes & ContainerWriter::CREATE_AUDIO_TRACK) &&
|
|
|
|
!(aTrackTypes & ContainerWriter::CREATE_VIDEO_TRACK)) {
|
|
|
|
audioBitrate = std::min(audioBitrate, bitrate);
|
|
|
|
videoBitrate = 0;
|
|
|
|
} else if (!(aTrackTypes & ContainerWriter::CREATE_AUDIO_TRACK) &&
|
|
|
|
(aTrackTypes & ContainerWriter::CREATE_VIDEO_TRACK)) {
|
|
|
|
audioBitrate = 0;
|
|
|
|
videoBitrate = std::min(videoBitrate, bitrate);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(audioBitrate + videoBitrate <= bitrate);
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:39:45 +04:00
|
|
|
// Allocate encoder and bind with union stream.
|
|
|
|
// At this stage, the API doesn't allow UA to choose the output mimeType
|
|
|
|
// format.
|
2014-03-19 10:52:45 +04:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
mEncoder = MediaEncoder::CreateEncoder(
|
|
|
|
mEncoderThread, NS_LITERAL_STRING(""), audioBitrate, videoBitrate,
|
2016-11-17 18:35:06 +03:00
|
|
|
aTrackTypes, aTrackRate);
|
2013-12-18 14:39:45 +04:00
|
|
|
|
|
|
|
if (!mEncoder) {
|
2017-05-24 19:51:47 +03:00
|
|
|
LOG(LogLevel::Error, ("Session.InitEncoder !mEncoder %p", this));
|
2013-12-18 14:39:45 +04:00
|
|
|
DoSessionEndTask(NS_ERROR_ABORT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
mEncoderListener = MakeAndAddRef<EncoderListener>(mEncoderThread, this);
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv =
|
|
|
|
mEncoderThread->Dispatch(NewRunnableMethod<RefPtr<EncoderListener>>(
|
|
|
|
"mozilla::MediaEncoder::RegisterListener", mEncoder,
|
|
|
|
&MediaEncoder::RegisterListener, mEncoderListener));
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-21 07:12:42 +03:00
|
|
|
Unused << rv;
|
2016-06-15 18:48:44 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
if (mRecorder->mAudioNode) {
|
|
|
|
mEncoder->ConnectAudioNode(mRecorder->mAudioNode,
|
|
|
|
mRecorder->mAudioNodeOutput);
|
2016-06-15 18:48:44 +03:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
for (auto& track : mMediaStreamTracks) {
|
|
|
|
mEncoder->ConnectMediaStreamTrack(track);
|
2013-12-18 14:39:45 +04:00
|
|
|
}
|
|
|
|
|
2018-01-31 12:19:39 +03:00
|
|
|
// If user defines timeslice interval for video blobs we have to set
|
|
|
|
// appropriate video keyframe interval defined in milliseconds.
|
|
|
|
mEncoder->SetVideoKeyFrameInterval(mTimeSlice);
|
|
|
|
|
2017-10-31 20:34:23 +03:00
|
|
|
// Set mRunningState to Running so that ExtractRunnable/DestroyRunnable will
|
|
|
|
// take the responsibility to end the session.
|
|
|
|
mRunningState = RunningState::Starting;
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
2013-12-18 14:39:45 +04:00
|
|
|
// application should get blob and onstop event
|
|
|
|
void DoSessionEndTask(nsresult rv) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-10-31 20:34:23 +03:00
|
|
|
if (mRunningState.isErr()) {
|
|
|
|
// We have already ended with an error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRunningState.isOk() &&
|
|
|
|
mRunningState.unwrap() == RunningState::Stopped) {
|
|
|
|
// We have already ended gracefully.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRunningState.isOk() &&
|
|
|
|
(mRunningState.unwrap() == RunningState::Idling ||
|
|
|
|
mRunningState.unwrap() == RunningState::Starting)) {
|
2019-01-14 21:32:53 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new DispatchEventRunnable(this, NS_LITERAL_STRING("start")));
|
2017-10-31 20:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == NS_OK) {
|
|
|
|
mRunningState = RunningState::Stopped;
|
|
|
|
} else {
|
|
|
|
mRunningState = Err(rv);
|
2017-09-03 23:28:01 +03:00
|
|
|
}
|
2015-08-27 10:35:30 +03:00
|
|
|
|
2013-12-18 14:39:45 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-09-01 02:38:52 +03:00
|
|
|
mRecorder->ForceInactive();
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod<nsresult>(
|
|
|
|
"dom::MediaRecorder::NotifyError", mRecorder,
|
|
|
|
&MediaRecorder::NotifyError, rv));
|
2013-12-18 14:39:45 +04:00
|
|
|
}
|
2017-10-05 08:41:41 +03:00
|
|
|
|
|
|
|
RefPtr<Runnable> destroyRunnable = new DestroyRunnable(this);
|
|
|
|
|
2016-02-01 17:47:17 +03:00
|
|
|
if (rv != NS_ERROR_DOM_SECURITY_ERR) {
|
|
|
|
// Don't push a blob if there was a security error.
|
2017-10-05 08:41:41 +03:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(
|
|
|
|
new PushBlobRunnable(this, destroyRunnable)))) {
|
2016-02-01 17:47:17 +03:00
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread PushBlobRunnable failed");
|
|
|
|
}
|
2017-10-05 08:41:41 +03:00
|
|
|
} else {
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(destroyRunnable))) {
|
|
|
|
MOZ_ASSERT(false, "NS_DispatchToMainThread DestroyRunnable failed");
|
|
|
|
}
|
2014-06-26 13:22:05 +04:00
|
|
|
}
|
2013-12-18 14:39:45 +04:00
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
void MediaEncoderInitialized() {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
2017-01-09 06:06:28 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
// Pull encoded metadata from MediaEncoder
|
|
|
|
nsTArray<nsTArray<uint8_t>> encodedBuf;
|
2017-10-31 20:34:23 +03:00
|
|
|
nsString mime;
|
|
|
|
nsresult rv = mEncoder->GetEncodedMetadata(&encodedBuf, mime);
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
2013-11-05 02:27:39 +04:00
|
|
|
}
|
2016-02-01 17:47:17 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
// Append pulled data into cache buffer.
|
2017-10-05 08:41:41 +03:00
|
|
|
NS_DispatchToMainThread(
|
|
|
|
new StoreEncodedBufferRunnable(this, std::move(encodedBuf)));
|
2017-10-31 20:34:23 +03:00
|
|
|
|
|
|
|
RefPtr<Session> self = this;
|
|
|
|
NS_DispatchToMainThread(NewRunnableFrom([self, mime]() {
|
|
|
|
if (!self->mRecorder) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Recorder should be live");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (self->mRunningState.isOk()) {
|
|
|
|
auto state = self->mRunningState.unwrap();
|
|
|
|
if (state == RunningState::Starting ||
|
|
|
|
state == RunningState::Stopping) {
|
|
|
|
if (state == RunningState::Starting) {
|
|
|
|
// We set it to Running in the runnable since we can only assign
|
|
|
|
// mRunningState on main thread. We set it before running the start
|
|
|
|
// event runnable since that dispatches synchronously (and may cause
|
|
|
|
// js calls to methods depending on mRunningState).
|
|
|
|
self->mRunningState = RunningState::Running;
|
|
|
|
}
|
|
|
|
self->mMimeType = mime;
|
|
|
|
self->mRecorder->SetMimeType(self->mMimeType);
|
2019-01-14 21:32:53 +03:00
|
|
|
auto startEvent = MakeRefPtr<DispatchEventRunnable>(
|
|
|
|
self, NS_LITERAL_STRING("start"));
|
2017-10-31 20:34:23 +03:00
|
|
|
startEvent->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}));
|
2017-05-24 19:51:47 +03:00
|
|
|
}
|
2017-04-07 17:50:55 +03:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
void MediaEncoderDataAvailable() {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
2016-02-01 17:47:17 +03:00
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
Extract(false, nullptr);
|
2013-11-05 02:27:39 +04:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
void MediaEncoderError() {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
NS_DispatchToMainThread(NewRunnableMethod<nsresult>(
|
|
|
|
"dom::MediaRecorder::Session::DoSessionEndTask", this,
|
|
|
|
&Session::DoSessionEndTask, NS_ERROR_FAILURE));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaEncoderShutdown() {
|
|
|
|
MOZ_ASSERT(mEncoderThread->IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(mEncoder->IsShutdown());
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
// For the stop event. Let's the creation of the blob to dispatch this
|
|
|
|
// runnable.
|
|
|
|
RefPtr<Runnable> destroyRunnable = new DestroyRunnable(this);
|
2017-05-24 19:51:47 +03:00
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
// Forces the last blob even if it's not time for it yet.
|
|
|
|
Extract(true, destroyRunnable);
|
2013-11-05 02:27:39 +04:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
// Clean up.
|
|
|
|
mEncoderListener->Forget();
|
|
|
|
DebugOnly<bool> unregistered =
|
|
|
|
mEncoder->UnregisterListener(mEncoderListener);
|
|
|
|
MOZ_ASSERT(unregistered);
|
2013-11-05 02:27:39 +04:00
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
RefPtr<ShutdownPromise> Shutdown() {
|
2014-08-18 07:42:49 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-24 19:51:47 +03:00
|
|
|
LOG(LogLevel::Debug, ("Session Shutdown %p", this));
|
|
|
|
|
|
|
|
if (mShutdownPromise) {
|
|
|
|
return mShutdownPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
mShutdownPromise = ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
RefPtr<Session> self = this;
|
|
|
|
|
|
|
|
if (mEncoder) {
|
|
|
|
auto& encoder = mEncoder;
|
|
|
|
encoder->Cancel();
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(mEncoderListener);
|
|
|
|
auto& encoderListener = mEncoderListener;
|
|
|
|
mShutdownPromise = mShutdownPromise->Then(
|
|
|
|
mEncoderThread, __func__,
|
|
|
|
[encoder, encoderListener]() {
|
|
|
|
encoder->UnregisterListener(encoderListener);
|
|
|
|
encoderListener->Forget();
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
},
|
|
|
|
[]() {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unexpected reject");
|
|
|
|
return ShutdownPromise::CreateAndReject(false, __func__);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove main thread state.
|
|
|
|
if (mMediaStream) {
|
|
|
|
mMediaStream->UnregisterTrackListener(this);
|
|
|
|
mMediaStream = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
auto tracks(std::move(mMediaStreamTracks));
|
2017-05-24 19:51:47 +03:00
|
|
|
for (RefPtr<MediaStreamTrack>& track : tracks) {
|
|
|
|
track->RemovePrincipalChangeObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break the cycle reference between Session and MediaRecorder.
|
2014-08-18 07:42:49 +04:00
|
|
|
if (mRecorder) {
|
2017-05-24 19:51:47 +03:00
|
|
|
mShutdownPromise = mShutdownPromise->Then(
|
|
|
|
GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[self]() {
|
|
|
|
self->mRecorder->RemoveSession(self);
|
|
|
|
self->mRecorder = nullptr;
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
},
|
|
|
|
[]() {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unexpected reject");
|
|
|
|
return ShutdownPromise::CreateAndReject(false, __func__);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEncoderThread) {
|
|
|
|
RefPtr<TaskQueue>& encoderThread = mEncoderThread;
|
|
|
|
mShutdownPromise = mShutdownPromise->Then(
|
|
|
|
GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[encoderThread]() { return encoderThread->BeginShutdown(); },
|
|
|
|
[]() {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unexpected reject");
|
|
|
|
return ShutdownPromise::CreateAndReject(false, __func__);
|
|
|
|
});
|
2014-08-18 07:42:49 +04:00
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
return mShutdownPromise;
|
2014-08-18 07:42:49 +04:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
private:
|
2017-10-31 20:34:23 +03:00
|
|
|
enum class RunningState {
|
|
|
|
Idling, // Session has been created
|
|
|
|
Starting, // MediaEncoder started, waiting for data
|
|
|
|
Running, // MediaEncoder has produced data
|
|
|
|
Stopping, // Stop() has been called
|
|
|
|
Stopped, // Session has stopped without any error
|
|
|
|
};
|
|
|
|
|
2017-09-29 17:11:47 +03:00
|
|
|
// Hold reference to MediaRecorder that ensure MediaRecorder is alive
|
2014-06-19 06:11:34 +04:00
|
|
|
// if there is an active session. Access ONLY on main thread.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> mRecorder;
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2016-02-01 17:47:17 +03:00
|
|
|
// Stream currently recorded.
|
|
|
|
RefPtr<DOMMediaStream> mMediaStream;
|
|
|
|
|
2018-11-23 18:02:35 +03:00
|
|
|
// True after we have decided on the track set to use for the recording.
|
|
|
|
bool mMediaStreamReady;
|
|
|
|
|
2016-02-01 17:47:17 +03:00
|
|
|
// Tracks currently recorded. This should be a subset of mMediaStream's track
|
|
|
|
// set.
|
|
|
|
nsTArray<RefPtr<MediaStreamTrack>> mMediaStreamTracks;
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
// Runnable thread for reading data from MediaEncoder.
|
|
|
|
RefPtr<TaskQueue> mEncoderThread;
|
2013-09-26 17:40:40 +04:00
|
|
|
// MediaEncoder pipeline.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaEncoder> mEncoder;
|
2017-05-24 19:51:47 +03:00
|
|
|
// Listener through which MediaEncoder signals us.
|
|
|
|
RefPtr<EncoderListener> mEncoderListener;
|
|
|
|
// Set in Shutdown() and resolved when shutdown is complete.
|
|
|
|
RefPtr<ShutdownPromise> mShutdownPromise;
|
|
|
|
// A buffer to cache encoded media data.
|
2017-10-05 08:41:41 +03:00
|
|
|
RefPtr<MutableBlobStorage> mMutableBlobStorage;
|
|
|
|
// Max memory to use for the MutableBlobStorage.
|
|
|
|
uint64_t mMaxMemory;
|
2014-01-29 19:09:43 +04:00
|
|
|
// Current session mimeType
|
|
|
|
nsString mMimeType;
|
2014-01-04 21:20:21 +04:00
|
|
|
// Timestamp of the last fired dataavailable event.
|
|
|
|
TimeStamp mLastBlobTimeStamp;
|
2017-10-05 08:41:41 +03:00
|
|
|
// The interval of passing encoded data from MutableBlobStorage to
|
|
|
|
// onDataAvailable handler. "mTimeSlice < 0" means Session object does not
|
|
|
|
// push encoded data to onDataAvailable, instead, it passive wait the client
|
|
|
|
// side pull encoded data by calling requestData API.
|
2013-09-26 17:40:40 +04:00
|
|
|
const int32_t mTimeSlice;
|
2017-10-31 20:34:23 +03:00
|
|
|
// The session's current main thread state. The error type gets setwhen ending
|
|
|
|
// a recording with an error. An NS_OK error is invalid.
|
2015-04-22 10:09:49 +03:00
|
|
|
// Main thread only.
|
2017-10-31 20:34:23 +03:00
|
|
|
Result<RunningState, nsresult> mRunningState;
|
2013-07-05 05:50:25 +04:00
|
|
|
};
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(MediaRecorder::Session::PushBlobRunnable, Runnable)
|
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
MediaRecorder::~MediaRecorder() {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("~MediaRecorder (%p)", this));
|
2014-06-19 06:11:34 +04:00
|
|
|
UnRegisterActivityObserver();
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
MediaRecorder::MediaRecorder(DOMMediaStream& aSourceMediaStream,
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aOwnerWindow)
|
2014-08-28 01:40:00 +04:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow),
|
2017-05-24 19:51:47 +03:00
|
|
|
mAudioNodeOutput(0),
|
2014-08-28 01:40:00 +04:00
|
|
|
mState(RecordingState::Inactive),
|
2018-06-15 10:25:02 +03:00
|
|
|
mAudioBitsPerSecond(0),
|
|
|
|
mVideoBitsPerSecond(0),
|
|
|
|
mBitsPerSecond(0) {
|
2014-01-07 06:53:23 +04:00
|
|
|
MOZ_ASSERT(aOwnerWindow);
|
2014-08-28 01:40:00 +04:00
|
|
|
mDOMStream = &aSourceMediaStream;
|
2015-11-15 16:49:01 +03:00
|
|
|
|
2014-06-19 06:11:34 +04:00
|
|
|
RegisterActivityObserver();
|
|
|
|
}
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
MediaRecorder::MediaRecorder(AudioNode& aSrcAudioNode, uint32_t aSrcOutput,
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aOwnerWindow)
|
2014-08-28 01:40:00 +04:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow),
|
2017-05-24 19:51:47 +03:00
|
|
|
mAudioNodeOutput(aSrcOutput),
|
2014-08-28 01:40:00 +04:00
|
|
|
mState(RecordingState::Inactive),
|
2018-06-15 10:25:02 +03:00
|
|
|
mAudioBitsPerSecond(0),
|
|
|
|
mVideoBitsPerSecond(0),
|
|
|
|
mBitsPerSecond(0) {
|
2014-08-28 01:40:00 +04:00
|
|
|
MOZ_ASSERT(aOwnerWindow);
|
|
|
|
|
|
|
|
mAudioNode = &aSrcAudioNode;
|
2015-11-15 16:49:01 +03:00
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
RegisterActivityObserver();
|
|
|
|
}
|
|
|
|
|
2014-06-19 06:11:34 +04:00
|
|
|
void MediaRecorder::RegisterActivityObserver() {
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsPIDOMWindowInner* window = GetOwner()) {
|
2016-11-18 14:29:13 +03:00
|
|
|
mDocument = window->GetExtantDoc();
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->RegisterActivityObserver(
|
2014-06-19 06:11:34 +04:00
|
|
|
NS_ISUPPORTS_CAST(nsIDocumentActivity*, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::UnRegisterActivityObserver() {
|
2016-11-18 14:29:13 +03:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->UnregisterActivityObserver(
|
|
|
|
NS_ISUPPORTS_CAST(nsIDocumentActivity*, this));
|
2014-06-19 06:11:34 +04:00
|
|
|
}
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
void MediaRecorder::SetMimeType(const nsString& aMimeType) {
|
|
|
|
mMimeType = aMimeType;
|
|
|
|
}
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
void MediaRecorder::GetMimeType(nsString& aMimeType) { aMimeType = mMimeType; }
|
2013-07-05 05:50:25 +04:00
|
|
|
|
|
|
|
void MediaRecorder::Start(const Optional<int32_t>& aTimeSlice,
|
|
|
|
ErrorResult& aResult) {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.Start %p", this));
|
2017-08-07 00:49:24 +03:00
|
|
|
|
|
|
|
InitializeDomExceptions();
|
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
if (mState != RecordingState::Inactive) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-19 17:37:40 +03:00
|
|
|
nsTArray<RefPtr<MediaStreamTrack>> tracks;
|
|
|
|
if (mDOMStream) {
|
|
|
|
mDOMStream->GetTracks(tracks);
|
|
|
|
}
|
|
|
|
if (!tracks.IsEmpty()) {
|
|
|
|
// If there are tracks already available that we're not allowed
|
|
|
|
// to record, we should throw a security error.
|
|
|
|
bool subsumes = false;
|
|
|
|
nsPIDOMWindowInner* window;
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc;
|
2016-04-19 17:37:40 +03:00
|
|
|
if (!(window = GetOwner()) || !(doc = window->GetExtantDoc()) ||
|
|
|
|
NS_FAILED(doc->NodePrincipal()->Subsumes(mDOMStream->GetPrincipal(),
|
|
|
|
&subsumes)) ||
|
|
|
|
!subsumes) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
int32_t timeSlice = 0;
|
2013-07-05 05:50:25 +04:00
|
|
|
if (aTimeSlice.WasPassed()) {
|
|
|
|
if (aTimeSlice.Value() < 0) {
|
|
|
|
aResult.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-14 11:31:59 +04:00
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
timeSlice = aTimeSlice.Value();
|
|
|
|
}
|
2014-07-01 09:25:29 +04:00
|
|
|
MediaRecorderReporter::AddMediaRecorder(this);
|
2013-09-26 17:40:40 +04:00
|
|
|
mState = RecordingState::Recording;
|
2014-06-26 13:22:05 +04:00
|
|
|
// Start a session.
|
2014-03-25 21:11:58 +04:00
|
|
|
mSessions.AppendElement();
|
2014-08-18 07:42:49 +04:00
|
|
|
mSessions.LastElement() = new Session(this, timeSlice);
|
2014-03-25 21:11:58 +04:00
|
|
|
mSessions.LastElement()->Start();
|
2017-09-18 22:34:02 +03:00
|
|
|
mStartTime = TimeStamp::Now();
|
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::MEDIARECORDER_RECORDING_COUNT, 1);
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::Stop(ErrorResult& aResult) {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.Stop %p", this));
|
2014-07-01 09:25:29 +04:00
|
|
|
MediaRecorderReporter::RemoveMediaRecorder(this);
|
2013-07-05 05:50:25 +04:00
|
|
|
if (mState == RecordingState::Inactive) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-18 14:39:45 +04:00
|
|
|
mState = RecordingState::Inactive;
|
2014-06-19 06:11:34 +04:00
|
|
|
MOZ_ASSERT(mSessions.Length() > 0);
|
|
|
|
mSessions.LastElement()->Stop();
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2013-07-14 11:31:59 +04:00
|
|
|
void MediaRecorder::Pause(ErrorResult& aResult) {
|
2019-01-14 21:32:54 +03:00
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.Pause %p", this));
|
2018-10-08 12:06:07 +03:00
|
|
|
if (mState == RecordingState::Inactive) {
|
2013-07-14 11:31:59 +04:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:32:56 +03:00
|
|
|
if (mState == RecordingState::Paused) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:11:58 +04:00
|
|
|
MOZ_ASSERT(mSessions.Length() > 0);
|
|
|
|
nsresult rv = mSessions.LastElement()->Pause();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NotifyError(rv);
|
|
|
|
return;
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
2018-11-13 15:50:04 +03:00
|
|
|
|
2014-03-25 21:11:58 +04:00
|
|
|
mState = RecordingState::Paused;
|
2013-07-14 11:31:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::Resume(ErrorResult& aResult) {
|
2019-01-14 21:32:54 +03:00
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.Resume %p", this));
|
2018-10-08 12:06:07 +03:00
|
|
|
if (mState == RecordingState::Inactive) {
|
2013-07-14 11:31:59 +04:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-26 17:40:40 +04:00
|
|
|
|
2019-01-14 21:32:56 +03:00
|
|
|
if (mState == RecordingState::Recording) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:11:58 +04:00
|
|
|
MOZ_ASSERT(mSessions.Length() > 0);
|
|
|
|
nsresult rv = mSessions.LastElement()->Resume();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NotifyError(rv);
|
|
|
|
return;
|
2013-09-26 17:40:40 +04:00
|
|
|
}
|
2018-11-13 15:50:04 +03:00
|
|
|
|
2014-03-25 21:11:58 +04:00
|
|
|
mState = RecordingState::Recording;
|
2013-07-14 11:31:59 +04:00
|
|
|
}
|
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
void MediaRecorder::RequestData(ErrorResult& aResult) {
|
2017-05-14 23:53:06 +03:00
|
|
|
if (mState == RecordingState::Inactive) {
|
2013-07-05 05:50:25 +04:00
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-19 06:11:34 +04:00
|
|
|
MOZ_ASSERT(mSessions.Length() > 0);
|
2014-10-17 13:26:11 +04:00
|
|
|
nsresult rv = mSessions.LastElement()->RequestData();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NotifyError(rv);
|
2014-06-26 13:22:05 +04:00
|
|
|
}
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* MediaRecorder::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return MediaRecorder_Binding::Wrap(aCx, this, aGivenProto);
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<MediaRecorder> MediaRecorder::Constructor(
|
2013-08-23 09:17:08 +04:00
|
|
|
const GlobalObject& aGlobal, DOMMediaStream& aStream,
|
2014-03-19 10:52:45 +04:00
|
|
|
const MediaRecorderOptions& aInitDict, ErrorResult& aRv) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
2014-08-28 01:40:00 +04:00
|
|
|
if (!ownerWindow) {
|
2013-07-05 05:50:25 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:48:16 +03:00
|
|
|
if (!IsTypeSupported(aInitDict.mMimeType)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> object = new MediaRecorder(aStream, ownerWindow);
|
2015-05-25 18:49:03 +03:00
|
|
|
object->SetOptions(aInitDict);
|
2014-08-28 01:40:00 +04:00
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<MediaRecorder> MediaRecorder::Constructor(
|
2014-08-28 01:40:00 +04:00
|
|
|
const GlobalObject& aGlobal, AudioNode& aSrcAudioNode, uint32_t aSrcOutput,
|
|
|
|
const MediaRecorderOptions& aInitDict, ErrorResult& aRv) {
|
|
|
|
// Allow recording from audio node only when pref is on.
|
|
|
|
if (!Preferences::GetBool("media.recorder.audio_node.enabled", false)) {
|
|
|
|
// Pretending that this constructor is not defined.
|
|
|
|
NS_NAMED_LITERAL_STRING(argStr, "Argument 1 of MediaRecorder.constructor");
|
|
|
|
NS_NAMED_LITERAL_STRING(typeStr, "MediaStream");
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowTypeError<MSG_DOES_NOT_IMPLEMENT_INTERFACE>(argStr, typeStr);
|
2014-08-28 01:40:00 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
2013-07-05 05:50:25 +04:00
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
// aSrcOutput doesn't matter to destination node because it has no output.
|
|
|
|
if (aSrcAudioNode.NumberOfOutputs() > 0 &&
|
|
|
|
aSrcOutput >= aSrcAudioNode.NumberOfOutputs()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:48:16 +03:00
|
|
|
if (!IsTypeSupported(aInitDict.mMimeType)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRecorder> object =
|
|
|
|
new MediaRecorder(aSrcAudioNode, aSrcOutput, ownerWindow);
|
2015-05-25 18:49:03 +03:00
|
|
|
object->SetOptions(aInitDict);
|
2013-07-05 05:50:25 +04:00
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-25 18:49:03 +03:00
|
|
|
void MediaRecorder::SetOptions(const MediaRecorderOptions& aInitDict) {
|
|
|
|
SetMimeType(aInitDict.mMimeType);
|
|
|
|
mAudioBitsPerSecond = aInitDict.mAudioBitsPerSecond.WasPassed()
|
|
|
|
? aInitDict.mAudioBitsPerSecond.Value()
|
|
|
|
: 0;
|
|
|
|
mVideoBitsPerSecond = aInitDict.mVideoBitsPerSecond.WasPassed()
|
|
|
|
? aInitDict.mVideoBitsPerSecond.Value()
|
|
|
|
: 0;
|
|
|
|
mBitsPerSecond = aInitDict.mBitsPerSecond.WasPassed()
|
|
|
|
? aInitDict.mBitsPerSecond.Value()
|
|
|
|
: 0;
|
2015-06-08 06:47:28 +03:00
|
|
|
// We're not handling dynamic changes yet. Eventually we'll handle
|
|
|
|
// setting audio, video and/or total -- and anything that isn't set,
|
|
|
|
// we'll derive. Calculated versions require querying bitrates after
|
|
|
|
// the encoder is Init()ed. This happens only after data is
|
|
|
|
// available and thus requires dynamic changes.
|
|
|
|
//
|
|
|
|
// Until dynamic changes are supported, I prefer to be safe and err
|
|
|
|
// slightly high
|
|
|
|
if (aInitDict.mBitsPerSecond.WasPassed() &&
|
|
|
|
!aInitDict.mVideoBitsPerSecond.WasPassed()) {
|
|
|
|
mVideoBitsPerSecond = mBitsPerSecond;
|
|
|
|
}
|
2015-05-25 18:49:03 +03:00
|
|
|
}
|
|
|
|
|
2016-04-18 07:06:15 +03:00
|
|
|
static char const* const gWebMVideoEncoderCodecs[4] = {
|
2016-03-07 06:48:16 +03:00
|
|
|
"opus",
|
|
|
|
"vp8",
|
|
|
|
"vp8.0",
|
|
|
|
// no VP9 yet
|
|
|
|
nullptr,
|
|
|
|
};
|
2018-06-03 22:19:12 +03:00
|
|
|
static char const* const gWebMAudioEncoderCodecs[4] = {
|
|
|
|
"opus",
|
|
|
|
nullptr,
|
|
|
|
};
|
2016-03-07 06:48:16 +03:00
|
|
|
static char const* const gOggAudioEncoderCodecs[2] = {
|
|
|
|
"opus",
|
|
|
|
// we could support vorbis here too, but don't
|
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class String>
|
|
|
|
static bool CodecListContains(char const* const* aCodecs,
|
|
|
|
const String& aCodec) {
|
|
|
|
for (int32_t i = 0; aCodecs[i]; ++i) {
|
|
|
|
if (aCodec.EqualsASCII(aCodecs[i])) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool MediaRecorder::IsTypeSupported(GlobalObject& aGlobal,
|
|
|
|
const nsAString& aMIMEType) {
|
|
|
|
return IsTypeSupported(aMIMEType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool MediaRecorder::IsTypeSupported(const nsAString& aMIMEType) {
|
|
|
|
char const* const* codeclist = nullptr;
|
|
|
|
|
|
|
|
if (aMIMEType.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentTypeParser parser(aMIMEType);
|
|
|
|
nsAutoString mimeType;
|
|
|
|
nsresult rv = parser.GetType(mimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// effectively a 'switch (mimeType) {'
|
|
|
|
if (mimeType.EqualsLiteral(AUDIO_OGG)) {
|
|
|
|
if (MediaDecoder::IsOggEnabled() && MediaDecoder::IsOpusEnabled()) {
|
|
|
|
codeclist = gOggAudioEncoderCodecs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef MOZ_WEBM_ENCODER
|
2018-06-03 22:19:12 +03:00
|
|
|
else if ((mimeType.EqualsLiteral(VIDEO_WEBM) ||
|
|
|
|
mimeType.EqualsLiteral(AUDIO_WEBM)) &&
|
2016-03-07 06:48:16 +03:00
|
|
|
MediaEncoder::IsWebMEncoderEnabled()) {
|
2018-06-03 22:19:12 +03:00
|
|
|
if (mimeType.EqualsLiteral(AUDIO_WEBM)) {
|
|
|
|
codeclist = gWebMAudioEncoderCodecs;
|
|
|
|
} else {
|
|
|
|
codeclist = gWebMVideoEncoderCodecs;
|
|
|
|
}
|
2016-03-07 06:48:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// codecs don't matter if we don't support the container
|
|
|
|
if (!codeclist) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// now filter on codecs, and if needed rescind support
|
|
|
|
nsAutoString codecstring;
|
|
|
|
rv = parser.GetParameter("codecs", codecstring);
|
|
|
|
|
|
|
|
nsTArray<nsString> codecs;
|
|
|
|
if (!ParseCodecsString(codecstring, codecs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const nsString& codec : codecs) {
|
|
|
|
if (!CodecListContains(codeclist, codec)) {
|
|
|
|
// Totally unsupported codec
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:41:41 +03:00
|
|
|
nsresult MediaRecorder::CreateAndDispatchBlobEvent(Blob* aBlob) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2016-02-01 17:47:17 +03:00
|
|
|
|
2013-10-09 20:05:22 +04:00
|
|
|
BlobEventInit init;
|
2013-09-10 22:27:39 +04:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
2017-10-05 08:41:41 +03:00
|
|
|
init.mData = aBlob;
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobEvent> event =
|
2013-09-10 22:27:39 +04:00
|
|
|
BlobEvent::Constructor(this, NS_LITERAL_STRING("dataavailable"), init);
|
2013-07-05 05:50:25 +04:00
|
|
|
event->SetTrusted(true);
|
2018-04-05 20:42:41 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
DispatchEvent(*event, rv);
|
|
|
|
return rv.StealNSResult();
|
2013-07-05 05:50:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::DispatchSimpleEvent(const nsAString& aStr) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2013-07-05 05:50:25 +04:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:32:54 +03:00
|
|
|
rv = DOMEventTargetHelper::DispatchTrustedEvent(aStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(LogLevel::Error,
|
|
|
|
("MediaRecorder.DispatchSimpleEvent: DispatchTrustedEvent failed %p",
|
|
|
|
this));
|
2013-07-05 05:50:25 +04:00
|
|
|
NS_ERROR("Failed to dispatch the event!!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::NotifyError(nsresult aRv) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2013-07-05 05:50:25 +04:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-07 00:48:42 +03:00
|
|
|
MediaRecorderErrorEventInit init;
|
2017-08-11 02:32:12 +03:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
2017-08-07 00:49:24 +03:00
|
|
|
// These DOMExceptions have been created earlier so they can contain stack
|
|
|
|
// traces. We attach the appropriate one here to be fired. We should have
|
|
|
|
// exceptions here, but defensively check.
|
2017-08-07 00:48:42 +03:00
|
|
|
switch (aRv) {
|
|
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
2017-08-07 00:49:24 +03:00
|
|
|
if (!mSecurityDomException) {
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.NotifyError: "
|
|
|
|
"mSecurityDomException was not initialized"));
|
|
|
|
mSecurityDomException = DOMException::Create(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
init.mError = mSecurityDomException.forget();
|
2017-08-07 00:48:42 +03:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-07 00:49:24 +03:00
|
|
|
if (!mUnknownDomException) {
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.NotifyError: "
|
|
|
|
"mUnknownDomException was not initialized"));
|
|
|
|
mUnknownDomException = DOMException::Create(NS_ERROR_DOM_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.NotifyError: "
|
|
|
|
"mUnknownDomException being fired for aRv: %X",
|
|
|
|
uint32_t(aRv)));
|
|
|
|
init.mError = mUnknownDomException.forget();
|
2017-08-07 00:48:42 +03:00
|
|
|
}
|
2017-08-11 02:32:12 +03:00
|
|
|
|
2017-08-07 00:48:42 +03:00
|
|
|
RefPtr<MediaRecorderErrorEvent> event = MediaRecorderErrorEvent::Constructor(
|
|
|
|
this, NS_LITERAL_STRING("error"), init);
|
2013-07-05 05:50:25 +04:00
|
|
|
event->SetTrusted(true);
|
2014-07-01 03:02:02 +04:00
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
IgnoredErrorResult res;
|
|
|
|
DispatchEvent(*event, res);
|
|
|
|
if (res.Failed()) {
|
2013-07-05 05:50:25 +04:00
|
|
|
NS_ERROR("Failed to dispatch the error event!!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:11:58 +04:00
|
|
|
void MediaRecorder::RemoveSession(Session* aSession) {
|
2015-06-04 01:25:57 +03:00
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.RemoveSession (%p)", aSession));
|
2014-03-25 21:11:58 +04:00
|
|
|
mSessions.RemoveElement(aSession);
|
|
|
|
}
|
|
|
|
|
2014-06-19 06:11:34 +04:00
|
|
|
void MediaRecorder::NotifyOwnerDocumentActivityChanged() {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = GetOwner();
|
2014-06-19 06:11:34 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(window);
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = window->GetExtantDoc();
|
2014-06-19 06:11:34 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(doc);
|
|
|
|
|
2018-03-19 17:52:36 +03:00
|
|
|
bool inFrameSwap = false;
|
|
|
|
if (nsDocShell* docShell = static_cast<nsDocShell*>(doc->GetDocShell())) {
|
|
|
|
inFrameSwap = docShell->InFrameSwap();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder %p NotifyOwnerDocumentActivityChanged "
|
|
|
|
"IsActive=%d, "
|
|
|
|
"IsVisible=%d, "
|
|
|
|
"InFrameSwap=%d",
|
|
|
|
this, doc->IsActive(), doc->IsVisible(), inFrameSwap));
|
|
|
|
if (!doc->IsActive() || !(inFrameSwap || doc->IsVisible())) {
|
2014-06-19 06:11:34 +04:00
|
|
|
// Stop the session.
|
|
|
|
ErrorResult result;
|
|
|
|
Stop(result);
|
2015-11-21 00:29:41 +03:00
|
|
|
result.SuppressException();
|
2014-06-19 06:11:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 02:38:52 +03:00
|
|
|
void MediaRecorder::ForceInactive() {
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.ForceInactive %p", this));
|
|
|
|
mState = RecordingState::Inactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaRecorder::StopForSessionDestruction() {
|
|
|
|
LOG(LogLevel::Debug, ("MediaRecorder.StopForSessionDestruction %p", this));
|
|
|
|
MediaRecorderReporter::RemoveMediaRecorder(this);
|
|
|
|
// We do not perform a mState != RecordingState::Recording) check here as
|
|
|
|
// we may already be inactive due to ForceInactive().
|
|
|
|
mState = RecordingState::Inactive;
|
|
|
|
MOZ_ASSERT(mSessions.Length() > 0);
|
|
|
|
mSessions.LastElement()->Stop();
|
2017-09-18 22:34:02 +03:00
|
|
|
// This is a coarse calculation and does not reflect the duration of the
|
|
|
|
// final recording for reasons such as pauses. However it allows us an idea
|
|
|
|
// of how long people are running their recorders for.
|
|
|
|
TimeDuration timeDelta = TimeStamp::Now() - mStartTime;
|
|
|
|
Telemetry::Accumulate(Telemetry::MEDIA_RECORDER_RECORDING_DURATION,
|
|
|
|
timeDelta.ToSeconds());
|
2017-09-01 02:38:52 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 00:49:24 +03:00
|
|
|
void MediaRecorder::InitializeDomExceptions() {
|
|
|
|
mSecurityDomException = DOMException::Create(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
mUnknownDomException = DOMException::Create(NS_ERROR_DOM_UNKNOWN_ERR);
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
RefPtr<MediaRecorder::SizeOfPromise> MediaRecorder::SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// The return type of a chained MozPromise cannot be changed, so we create a
|
|
|
|
// holder for our desired return type and resolve that from All()->Then().
|
|
|
|
auto holder = MakeRefPtr<Refcountable<MozPromiseHolder<SizeOfPromise>>>();
|
|
|
|
RefPtr<SizeOfPromise> promise = holder->Ensure(__func__);
|
|
|
|
|
|
|
|
nsTArray<RefPtr<SizeOfPromise>> promises(mSessions.Length());
|
|
|
|
for (const RefPtr<Session>& session : mSessions) {
|
|
|
|
promises.AppendElement(session->SizeOfExcludingThis(aMallocSizeOf));
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
2017-05-24 19:51:47 +03:00
|
|
|
|
|
|
|
SizeOfPromise::All(GetCurrentThreadSerialEventTarget(), promises)
|
|
|
|
->Then(GetCurrentThreadSerialEventTarget(), __func__,
|
|
|
|
[holder](const nsTArray<size_t>& sizes) {
|
|
|
|
size_t total = 0;
|
|
|
|
for (const size_t& size : sizes) {
|
|
|
|
total += size;
|
|
|
|
}
|
|
|
|
holder->Resolve(total, __func__);
|
|
|
|
},
|
|
|
|
[]() { MOZ_CRASH("Unexpected reject"); });
|
|
|
|
|
|
|
|
return promise;
|
2014-07-01 09:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
StaticRefPtr<MediaRecorderReporter> MediaRecorderReporter::sUniqueInstance;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|