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/. */
|
|
|
|
|
|
|
|
#ifndef MediaRecorder_h
|
|
|
|
#define MediaRecorder_h
|
|
|
|
|
|
|
|
#include "mozilla/dom/MediaRecorderBinding.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2017-09-28 01:34:05 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2014-06-19 06:11:34 +04:00
|
|
|
#include "nsIDocumentActivity.h"
|
2013-07-05 05:50:25 +04:00
|
|
|
|
|
|
|
// Max size for allowing queue encoded data in memory
|
|
|
|
#define MAX_ALLOW_MEMORY_BUFFER 1024000
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
class AudioNodeStream;
|
2013-09-06 00:25:17 +04:00
|
|
|
class DOMMediaStream;
|
2014-08-28 01:40:00 +04:00
|
|
|
class ErrorResult;
|
2014-03-19 10:52:45 +04:00
|
|
|
struct MediaRecorderOptions;
|
2014-08-28 01:40:00 +04:00
|
|
|
class MediaStream;
|
2016-03-07 06:48:16 +03:00
|
|
|
class GlobalObject;
|
2013-07-05 05:50:25 +04:00
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
class AudioNode;
|
2017-10-05 08:41:41 +03:00
|
|
|
class Blob;
|
2019-01-02 16:05:23 +03:00
|
|
|
class Document;
|
2017-08-07 00:49:24 +03:00
|
|
|
class DOMException;
|
2014-08-28 01:40:00 +04:00
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
/**
|
|
|
|
* Implementation of
|
|
|
|
* https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-capture/MediaRecorder.html
|
|
|
|
* The MediaRecorder accepts a mediaStream as input source passed from UA. When
|
|
|
|
* recorder starts, a MediaEncoder will be created and accept the mediaStream as
|
2017-10-05 08:41:41 +03:00
|
|
|
* input source. Encoder will get the raw data by track data changes, encode it
|
|
|
|
* by selected MIME Type, then store the encoded in a MutableBlobStorage object.
|
2013-07-05 05:50:25 +04:00
|
|
|
* The encoded data will be extracted on every timeslice passed from Start
|
|
|
|
* function call or by RequestData function. Thread model: When the recorder
|
2017-10-05 08:41:41 +03:00
|
|
|
* starts, it creates a "Media Encoder" thread to read data from MediaEncoder
|
|
|
|
* object and store buffer in MutableBlobStorage object. Also extract the
|
2013-07-05 05:50:25 +04:00
|
|
|
* encoded data and create blobs on every timeslice passed from start function
|
|
|
|
* or RequestData function called by UA.
|
|
|
|
*/
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MediaRecorder final : public DOMEventTargetHelper,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIDocumentActivity {
|
2017-05-24 19:51:47 +03:00
|
|
|
public:
|
2013-09-26 17:40:40 +04:00
|
|
|
class Session;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaRecorder(DOMMediaStream& aSourceMediaStream,
|
|
|
|
nsPIDOMWindowInner* aOwnerWindow);
|
|
|
|
MediaRecorder(AudioNode& aSrcAudioNode, uint32_t aSrcOutput,
|
|
|
|
nsPIDOMWindowInner* aOwnerWindow);
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2017-05-24 19:51:47 +03:00
|
|
|
static nsTArray<RefPtr<Session>> GetSessions();
|
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
// nsWrapperCache
|
2016-01-18 06:50:29 +03:00
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* GetParentObject() { return GetOwner(); }
|
2013-07-05 05:50:25 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaRecorder, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
// WebIDL
|
|
|
|
// Start recording. If timeSlice has been provided, mediaRecorder will
|
|
|
|
// raise a dataavailable event containing the Blob of collected data on every
|
|
|
|
// timeSlice milliseconds. If timeSlice isn't provided, UA should call the
|
|
|
|
// RequestData to obtain the Blob data, also set the mTimeSlice to zero.
|
|
|
|
void Start(const Optional<int32_t>& timeSlice, ErrorResult& aResult);
|
|
|
|
// Stop the recording activiy. Including stop the Media Encoder thread,
|
|
|
|
// un-hook the mediaStreamListener to encoder.
|
|
|
|
void Stop(ErrorResult& aResult);
|
2017-05-24 19:51:47 +03:00
|
|
|
// Pause a recording.
|
2013-07-14 11:31:59 +04:00
|
|
|
void Pause(ErrorResult& aResult);
|
2017-05-24 19:51:47 +03:00
|
|
|
// Resume a paused recording.
|
2013-07-14 11:31:59 +04:00
|
|
|
void Resume(ErrorResult& aResult);
|
2017-10-05 08:41:41 +03:00
|
|
|
// Extract encoded data Blob from MutableBlobStorage.
|
2013-07-05 05:50:25 +04:00
|
|
|
void RequestData(ErrorResult& aResult);
|
|
|
|
// Return the The DOMMediaStream passed from UA.
|
2014-08-28 01:40:00 +04:00
|
|
|
DOMMediaStream* Stream() const { return mDOMStream; }
|
2013-07-05 05:50:25 +04:00
|
|
|
// Return the current encoding MIME type selected by the MediaEncoder.
|
2013-09-26 17:40:40 +04:00
|
|
|
void GetMimeType(nsString& aMimeType);
|
2018-11-13 15:50:04 +03:00
|
|
|
// The current state of the MediaRecorder object.
|
|
|
|
RecordingState State() const { return mState; }
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2016-03-07 06:48:16 +03:00
|
|
|
static bool IsTypeSupported(GlobalObject& aGlobal, const nsAString& aType);
|
|
|
|
static bool IsTypeSupported(const nsAString& aType);
|
|
|
|
|
2014-08-28 01:40:00 +04:00
|
|
|
// Construct a recorder with a DOM media stream object as its source.
|
2013-07-05 05:50:25 +04:00
|
|
|
static already_AddRefed<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);
|
2014-08-28 01:40:00 +04:00
|
|
|
// Construct a recorder with a Web Audio destination node as its source.
|
|
|
|
static already_AddRefed<MediaRecorder> Constructor(
|
|
|
|
const GlobalObject& aGlobal, AudioNode& aSrcAudioNode,
|
|
|
|
uint32_t aSrcOutput, const MediaRecorderOptions& aInitDict,
|
|
|
|
ErrorResult& aRv);
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2014-07-01 09:25:29 +04:00
|
|
|
/*
|
2017-05-24 19:51:47 +03:00
|
|
|
* Measure the size of the buffer, and heap memory in bytes occupied by
|
|
|
|
* mAudioEncoder and mVideoEncoder.
|
2014-07-01 09:25:29 +04:00
|
|
|
*/
|
2017-05-24 19:51:47 +03:00
|
|
|
typedef MozPromise<size_t, size_t, true> SizeOfPromise;
|
|
|
|
RefPtr<SizeOfPromise> SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf);
|
2013-07-05 05:50:25 +04:00
|
|
|
// EventHandler
|
2014-06-27 07:07:45 +04:00
|
|
|
IMPL_EVENT_HANDLER(start)
|
2013-07-05 05:50:25 +04:00
|
|
|
IMPL_EVENT_HANDLER(stop)
|
2018-11-13 15:50:04 +03:00
|
|
|
IMPL_EVENT_HANDLER(dataavailable)
|
|
|
|
IMPL_EVENT_HANDLER(pause)
|
|
|
|
IMPL_EVENT_HANDLER(resume)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
2013-07-05 05:50:25 +04:00
|
|
|
IMPL_EVENT_HANDLER(warning)
|
|
|
|
|
2014-06-19 06:11:34 +04:00
|
|
|
NS_DECL_NSIDOCUMENTACTIVITY
|
|
|
|
|
2015-06-08 06:47:28 +03:00
|
|
|
uint32_t GetAudioBitrate() { return mAudioBitsPerSecond; }
|
|
|
|
uint32_t GetVideoBitrate() { return mVideoBitsPerSecond; }
|
|
|
|
uint32_t GetBitrate() { return mBitsPerSecond; }
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
protected:
|
2014-07-09 01:23:16 +04:00
|
|
|
virtual ~MediaRecorder();
|
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
MediaRecorder& operator=(const MediaRecorder& x) = delete;
|
2013-07-05 05:50:25 +04:00
|
|
|
// Create dataavailable event with Blob data and it runs in main thread
|
2017-10-05 08:41:41 +03:00
|
|
|
nsresult CreateAndDispatchBlobEvent(Blob* aBlob);
|
2013-07-05 05:50:25 +04:00
|
|
|
// Creating a simple event to notify UA simple event.
|
|
|
|
void DispatchSimpleEvent(const nsAString& aStr);
|
|
|
|
// Creating a error event with message.
|
|
|
|
void NotifyError(nsresult aRv);
|
2013-09-26 17:40:40 +04:00
|
|
|
// Set encoded MIME type.
|
|
|
|
void SetMimeType(const nsString& aMimeType);
|
2015-05-25 18:49:03 +03:00
|
|
|
void SetOptions(const MediaRecorderOptions& aInitDict);
|
2013-07-05 05:50:25 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
MediaRecorder(const MediaRecorder& x) = delete; // prevent bad usage
|
2014-03-25 21:11:58 +04:00
|
|
|
// Remove session pointer.
|
|
|
|
void RemoveSession(Session* aSession);
|
2017-08-07 00:49:24 +03:00
|
|
|
// Create DOMExceptions capturing the JS stack for async errors. These are
|
|
|
|
// created ahead of time rather than on demand when firing an error as the JS
|
|
|
|
// stack of the operation that started the async behavior will not be
|
|
|
|
// available at the time the error event is fired. Note, depending on when
|
|
|
|
// this is called there may not be a JS stack to capture.
|
|
|
|
void InitializeDomExceptions();
|
2017-09-01 02:38:52 +03:00
|
|
|
// Set the recorder state to inactive. This is needed to handle error states
|
|
|
|
// in the recorder where state must transition to inactive before full
|
|
|
|
// stoppage can be reached.
|
|
|
|
void ForceInactive();
|
|
|
|
// Stop the recorder and its internal session. This should be used by
|
|
|
|
// sessions that are in the process of being destroyed.
|
|
|
|
void StopForSessionDestruction();
|
2014-08-28 01:40:00 +04:00
|
|
|
// DOM wrapper for source media stream. Will be null when input is audio node.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMMediaStream> mDOMStream;
|
2014-08-28 01:40:00 +04:00
|
|
|
// Source audio node. Will be null when input is a media stream.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioNode> mAudioNode;
|
2017-05-24 19:51:47 +03:00
|
|
|
// Source audio node's output index. Will be zero when input is a media
|
|
|
|
// stream.
|
|
|
|
const uint32_t mAudioNodeOutput;
|
2014-08-28 01:40:00 +04:00
|
|
|
|
2013-07-05 05:50:25 +04:00
|
|
|
// The current state of the MediaRecorder object.
|
|
|
|
RecordingState mState;
|
2014-08-18 07:42:49 +04:00
|
|
|
// Hold the sessions reference and clean it when the DestroyRunnable for a
|
2014-06-19 06:11:34 +04:00
|
|
|
// session is running.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<Session>> mSessions;
|
2016-11-18 14:29:13 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> mDocument;
|
2016-11-18 14:29:13 +03:00
|
|
|
|
2013-09-26 17:40:40 +04:00
|
|
|
// It specifies the container format as well as the audio and video capture
|
|
|
|
// formats.
|
|
|
|
nsString mMimeType;
|
2014-06-19 06:11:34 +04:00
|
|
|
|
2015-05-25 18:49:03 +03:00
|
|
|
uint32_t mAudioBitsPerSecond;
|
|
|
|
uint32_t mVideoBitsPerSecond;
|
|
|
|
uint32_t mBitsPerSecond;
|
2016-12-08 11:00:12 +03:00
|
|
|
|
2017-09-18 22:34:02 +03:00
|
|
|
TimeStamp mStartTime;
|
|
|
|
|
2017-08-07 00:49:24 +03:00
|
|
|
// DOMExceptions that are created early and possibly thrown in NotifyError.
|
|
|
|
// Creating them early allows us to capture the JS stack for which cannot be
|
|
|
|
// done at the time the error event is fired.
|
|
|
|
RefPtr<DOMException> mSecurityDomException;
|
|
|
|
RefPtr<DOMException> mUnknownDomException;
|
|
|
|
|
2014-06-19 06:11:34 +04:00
|
|
|
private:
|
|
|
|
// Register MediaRecorder into Document to listen the activity changes.
|
|
|
|
void RegisterActivityObserver();
|
|
|
|
void UnRegisterActivityObserver();
|
2015-01-06 04:54:27 +03:00
|
|
|
|
2015-10-27 10:14:12 +03:00
|
|
|
bool CheckPermission(const nsString& aType);
|
2013-07-05 05:50:25 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-07-05 05:50:25 +04:00
|
|
|
|
|
|
|
#endif
|