2013-07-04 18:50:25 -07: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"
|
|
|
|
#include "GeneratedEvents.h"
|
|
|
|
#include "MediaEncoder.h"
|
|
|
|
#include "nsDOMEventTargetHelper.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMRecordErrorEvent.h"
|
|
|
|
#include "nsTArray.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "DOMMediaStream.h"
|
|
|
|
#include "EncodedBufferCache.h"
|
|
|
|
#include "nsIDOMFile.h"
|
2013-09-10 11:27:39 -07:00
|
|
|
#include "mozilla/dom/BlobEvent.h"
|
2013-07-04 18:50:25 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-06 22:55:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(MediaRecorder, nsDOMEventTargetHelper,
|
|
|
|
mStream)
|
2013-07-04 18:50:25 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaRecorder)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaRecorder, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaRecorder, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
// This task is used for triggering the creating blob object and it runs at main thread.
|
|
|
|
class MediaRecorder::PushBlobTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PushBlobTask(MediaRecorder* recorder)
|
|
|
|
: mRecorder(recorder) {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsresult rv = mRecorder->CreateAndDispatchBlobEvent();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mRecorder->NotifyError(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
MediaRecorder* mRecorder;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This task is for firing the error message from encoder and it runs in main thread
|
|
|
|
class MediaRecorder::PushErrorMessageTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PushErrorMessageTask(MediaRecorder* recorder, nsresult aError)
|
|
|
|
: mRecorder(recorder),
|
|
|
|
mError(aError) { }
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mRecorder->NotifyError(mError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MediaRecorder* mRecorder;
|
|
|
|
nsresult mError;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class is used for avoiding abort by directly use the NS_NewRunnableMethod and invoke ExtractEncodedData function
|
|
|
|
// The abort is checking if the destructor runs at main thread during the cycle-collect step in nsDOMEventTargetHelper
|
|
|
|
class MediaRecorder::ExtractEncodedDataTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExtractEncodedDataTask(MediaRecorder* aRecorder, MediaEncoder* aEncoder)
|
|
|
|
: mRecorder(aRecorder),
|
|
|
|
mEncoder(aEncoder) {}
|
|
|
|
|
|
|
|
class ReleaseEncoderThreadTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ReleaseEncoderThreadTask(already_AddRefed<MediaRecorder> recorder)
|
|
|
|
: mRecorder(recorder) {}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mRecorder->mReadThread->Shutdown();
|
|
|
|
mRecorder->mReadThread = nullptr;
|
2013-08-29 04:59:08 -07:00
|
|
|
|
|
|
|
// Setting mState to Inactive here is for the case where SourceStream
|
|
|
|
// ends itself, thus the recorder should stop itself too.
|
|
|
|
mRecorder->mState = RecordingState::Inactive;
|
|
|
|
mRecorder->DispatchSimpleEvent(NS_LITERAL_STRING("stop"));
|
|
|
|
|
2013-07-04 18:50:25 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<MediaRecorder> mRecorder;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
mRecorder->ExtractEncodedData();
|
|
|
|
NS_DispatchToMainThread(new ReleaseEncoderThreadTask(mRecorder.forget()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<MediaRecorder> mRecorder;
|
|
|
|
nsRefPtr<MediaEncoder> mEncoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
MediaRecorder::~MediaRecorder()
|
|
|
|
{
|
2013-07-26 05:29:41 -07:00
|
|
|
if (mStreamPort) {
|
|
|
|
mStreamPort->Destroy();
|
|
|
|
}
|
2013-07-21 07:33:01 -07:00
|
|
|
if (mTrackUnionStream) {
|
|
|
|
mTrackUnionStream->Destroy();
|
|
|
|
}
|
2013-07-04 18:50:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-22 22:17:08 -07:00
|
|
|
MediaRecorder::Init(nsPIDOMWindow* aOwnerWindow)
|
2013-07-04 18:50:25 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aOwnerWindow);
|
|
|
|
MOZ_ASSERT(aOwnerWindow->IsInnerWindow());
|
|
|
|
BindToOwner(aOwnerWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaRecorder::MediaRecorder(DOMMediaStream& aStream)
|
|
|
|
: mTimeSlice(0),
|
|
|
|
mState(RecordingState::Inactive)
|
|
|
|
{
|
|
|
|
mStream = &aStream;
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::ExtractEncodedData()
|
|
|
|
{
|
|
|
|
TimeStamp lastBlobTimeStamp = TimeStamp::Now();
|
|
|
|
do {
|
|
|
|
nsTArray<nsTArray<uint8_t> > outputBufs;
|
|
|
|
mEncoder->GetEncodedData(&outputBufs, mMimeType);
|
2013-09-05 13:25:17 -07:00
|
|
|
for (uint32_t i = 0; i < outputBufs.Length(); i++) {
|
2013-07-04 18:50:25 -07:00
|
|
|
mEncodedBufferCache->AppendBuffer(outputBufs[i]);
|
|
|
|
}
|
|
|
|
|
2013-07-24 02:26:17 -07:00
|
|
|
if (mTimeSlice > 0 && (TimeStamp::Now() - lastBlobTimeStamp).ToMilliseconds() > mTimeSlice) {
|
2013-07-04 18:50:25 -07:00
|
|
|
NS_DispatchToMainThread(new PushBlobTask(this));
|
|
|
|
lastBlobTimeStamp = TimeStamp::Now();
|
|
|
|
}
|
2013-08-29 04:59:08 -07:00
|
|
|
} while (!mEncoder->IsShutdown());
|
2013-07-04 18:50:25 -07:00
|
|
|
|
|
|
|
NS_DispatchToMainThread(new PushBlobTask(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::Start(const Optional<int32_t>& aTimeSlice, ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
if (mState != RecordingState::Inactive) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:20:42 -07:00
|
|
|
if (mStream->GetStream()->IsFinished() || mStream->GetStream()->IsDestroyed()) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:50:25 -07:00
|
|
|
if (aTimeSlice.WasPassed()) {
|
|
|
|
if (aTimeSlice.Value() < 0) {
|
|
|
|
aResult.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mTimeSlice = aTimeSlice.Value();
|
|
|
|
} else {
|
|
|
|
mTimeSlice = 0;
|
|
|
|
}
|
2013-07-14 00:31:59 -07:00
|
|
|
|
|
|
|
// Create a TrackUnionStream to support Pause/Resume by using ChangeExplicitBlockerCount
|
|
|
|
MediaStreamGraph* gm = mStream->GetStream()->Graph();
|
2013-08-06 15:14:35 -07:00
|
|
|
mTrackUnionStream = gm->CreateTrackUnionStream(nullptr);
|
2013-07-14 00:31:59 -07:00
|
|
|
MOZ_ASSERT(mTrackUnionStream, "CreateTrackUnionStream failed");
|
|
|
|
|
|
|
|
if (!CheckPrincipal()) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:50:25 -07:00
|
|
|
if (mEncodedBufferCache == nullptr) {
|
|
|
|
mEncodedBufferCache = new EncodedBufferCache(MAX_ALLOW_MEMORY_BUFFER);
|
|
|
|
}
|
|
|
|
|
2013-07-14 00:04:10 -07:00
|
|
|
mEncoder = MediaEncoder::CreateEncoder(NS_LITERAL_STRING(""));
|
2013-07-04 18:50:25 -07:00
|
|
|
MOZ_ASSERT(mEncoder, "CreateEncoder failed");
|
|
|
|
|
2013-07-14 00:31:59 -07:00
|
|
|
mTrackUnionStream->SetAutofinish(true);
|
2013-07-26 05:29:41 -07:00
|
|
|
mStreamPort = mTrackUnionStream->AllocateInputPort(mStream->GetStream(), MediaInputPort::FLAG_BLOCK_OUTPUT);
|
2013-07-14 00:31:59 -07:00
|
|
|
|
2013-07-04 18:50:25 -07:00
|
|
|
if (mEncoder) {
|
2013-07-14 00:31:59 -07:00
|
|
|
mTrackUnionStream->AddListener(mEncoder);
|
2013-07-04 18:50:25 -07:00
|
|
|
} else {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mReadThread) {
|
|
|
|
nsresult rv = NS_NewNamedThread("Media Encoder",
|
|
|
|
getter_AddRefs(mReadThread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aResult.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsRefPtr<ExtractEncodedDataTask> event = new ExtractEncodedDataTask(this, mEncoder);
|
|
|
|
mReadThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
mState = RecordingState::Recording;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::Stop(ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
if (mState == RecordingState::Inactive) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-10 05:29:35 -07:00
|
|
|
mState = RecordingState::Inactive;
|
2013-08-29 04:59:08 -07:00
|
|
|
|
|
|
|
mStreamPort->Destroy();
|
|
|
|
mStreamPort = nullptr;
|
|
|
|
|
|
|
|
mTrackUnionStream->Destroy();
|
|
|
|
mTrackUnionStream = nullptr;
|
2013-07-04 18:50:25 -07:00
|
|
|
}
|
|
|
|
|
2013-07-14 00:31:59 -07:00
|
|
|
void
|
|
|
|
MediaRecorder::Pause(ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
if (mState != RecordingState::Recording) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mTrackUnionStream->ChangeExplicitBlockerCount(-1);
|
|
|
|
|
|
|
|
mState = RecordingState::Paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::Resume(ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
if (mState != RecordingState::Paused) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mTrackUnionStream->ChangeExplicitBlockerCount(1);
|
|
|
|
mState = RecordingState::Recording;
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:50:25 -07:00
|
|
|
void
|
|
|
|
MediaRecorder::RequestData(ErrorResult& aResult)
|
|
|
|
{
|
|
|
|
if (mState != RecordingState::Recording) {
|
|
|
|
aResult.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-07 03:31:51 -07:00
|
|
|
NS_DispatchToMainThread(NS_NewRunnableMethod(this, &MediaRecorder::CreateAndDispatchBlobEvent),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2013-07-04 18:50:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaRecorder::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return MediaRecorderBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<MediaRecorder>
|
2013-08-22 22:17:08 -07:00
|
|
|
MediaRecorder::Constructor(const GlobalObject& aGlobal,
|
2013-07-04 18:50:25 -07:00
|
|
|
DOMMediaStream& aStream, ErrorResult& aRv)
|
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-07-04 18:50:25 -07:00
|
|
|
if (!sgo) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-07-04 18:50:25 -07:00
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MediaRecorder> object = new MediaRecorder(aStream);
|
2013-08-22 22:17:08 -07:00
|
|
|
object->Init(ownerWindow);
|
2013-07-04 18:50:25 -07:00
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaRecorder::CreateAndDispatchBlobEvent()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
|
|
|
|
if (!CheckPrincipal()) {
|
|
|
|
// Media is not same-origin, don't allow the data out.
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:27:39 -07:00
|
|
|
BlobEventInitInitializer init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mData = mEncodedBufferCache->ExtractBlob(mMimeType);
|
|
|
|
nsRefPtr<BlobEvent> event =
|
|
|
|
BlobEvent::Constructor(this,
|
|
|
|
NS_LITERAL_STRING("dataavailable"),
|
|
|
|
init);
|
2013-07-04 18:50:25 -07:00
|
|
|
event->SetTrusted(true);
|
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::DispatchSimpleEvent(const nsAString & aStr)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rv = event->InitEvent(aStr, false, false);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Failed to dispatch the event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaRecorder::NotifyError(nsresult aRv)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsString errorMsg;
|
|
|
|
switch (aRv) {
|
|
|
|
case NS_ERROR_DOM_SECURITY_ERR:
|
|
|
|
errorMsg = NS_LITERAL_STRING("SecurityError");
|
|
|
|
break;
|
|
|
|
case NS_ERROR_OUT_OF_MEMORY:
|
|
|
|
errorMsg = NS_LITERAL_STRING("OutOfMemoryError");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorMsg = NS_LITERAL_STRING("GenericError");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
rv = NS_NewDOMRecordErrorEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRecordErrorEvent> errorEvent = do_QueryInterface(event);
|
|
|
|
rv = errorEvent->InitRecordErrorEvent(NS_LITERAL_STRING("error"),
|
|
|
|
false, false, errorMsg);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Failed to dispatch the error event!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaRecorder::CheckPrincipal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mStream->GetPrincipal();
|
2013-07-09 21:52:29 -07:00
|
|
|
if (!GetOwner())
|
|
|
|
return false;
|
2013-07-04 18:50:25 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetOwner()->GetExtantDoc();
|
|
|
|
if (!doc || !principal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool subsumes;
|
|
|
|
if (NS_FAILED(doc->NodePrincipal()->Subsumes(principal, &subsumes)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return subsumes;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|