Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef MediaRecorder_h
8
#define MediaRecorder_h
9
10
#include "mozilla/dom/MediaRecorderBinding.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/MozPromise.h"
13
#include "nsIDocumentActivity.h"
14
15
// Max size for allowing queue encoded data in memory
16
#define MAX_ALLOW_MEMORY_BUFFER 1024000
17
namespace mozilla {
18
19
class AudioNodeTrack;
20
class DOMMediaStream;
21
class ErrorResult;
22
struct MediaRecorderOptions;
23
class GlobalObject;
24
25
namespace dom {
26
27
class AudioNode;
28
class Blob;
29
class Document;
30
class DOMException;
31
32
/**
33
* Implementation of
35
* The MediaRecorder accepts a mediaStream as input source passed from UA. When
36
* recorder starts, a MediaEncoder will be created and accept the mediaStream as
37
* input source. Encoder will get the raw data by track data changes, encode it
38
* by selected MIME Type, then store the encoded in a MutableBlobStorage object.
39
* The encoded data will be extracted on every timeslice passed from Start
40
* function call or by RequestData function. Thread model: When the recorder
41
* starts, it creates a "Media Encoder" thread to read data from MediaEncoder
42
* object and store buffer in MutableBlobStorage object. Also extract the
43
* encoded data and create blobs on every timeslice passed from start function
44
* or RequestData function called by UA.
45
*/
46
47
class MediaRecorder final : public DOMEventTargetHelper,
48
public nsIDocumentActivity {
49
public:
50
class Session;
51
52
explicit MediaRecorder(nsPIDOMWindowInner* aOwnerWindow);
53
54
static nsTArray<RefPtr<Session>> GetSessions();
55
56
// nsWrapperCache
57
JSObject* WrapObject(JSContext* aCx,
58
JS::Handle<JSObject*> aGivenProto) override;
59
60
NS_DECL_ISUPPORTS_INHERITED
61
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaRecorder, DOMEventTargetHelper)
62
63
// WebIDL
64
// Start recording. If timeSlice has been provided, mediaRecorder will
65
// raise a dataavailable event containing the Blob of collected data on every
66
// timeSlice milliseconds. If timeSlice isn't provided, UA should call the
67
// RequestData to obtain the Blob data, also set the mTimeSlice to zero.
68
void Start(const Optional<uint32_t>& timeSlice, ErrorResult& aResult);
69
// Stop the recording activiy. Including stop the Media Encoder thread,
70
// un-hook the mediaStreamListener to encoder.
71
void Stop(ErrorResult& aResult);
72
// Pause a recording.
73
void Pause(ErrorResult& aResult);
74
// Resume a paused recording.
75
void Resume(ErrorResult& aResult);
76
// Extract encoded data Blob from MutableBlobStorage.
77
void RequestData(ErrorResult& aResult);
78
// Return the The DOMMediaStream passed from UA.
79
DOMMediaStream* Stream() const { return mStream; }
80
// Return the current encoding MIME type selected by the MediaEncoder.
81
void GetMimeType(nsString& aMimeType);
82
// The current state of the MediaRecorder object.
83
RecordingState State() const { return mState; }
84
85
static bool IsTypeSupported(GlobalObject& aGlobal,
86
const nsAString& aMIMEType);
87
static bool IsTypeSupported(const nsAString& aMIMEType);
88
89
// Construct a recorder with a DOM media stream object as its source.
90
static already_AddRefed<MediaRecorder> Constructor(
91
const GlobalObject& aGlobal, DOMMediaStream& aStream,
92
const MediaRecorderOptions& aOptions, ErrorResult& aRv);
93
// Construct a recorder with a Web Audio destination node as its source.
94
static already_AddRefed<MediaRecorder> Constructor(
95
const GlobalObject& aGlobal, AudioNode& aAudioNode,
96
uint32_t aAudioNodeOutput, const MediaRecorderOptions& aOptions,
97
ErrorResult& aRv);
98
99
/*
100
* Measure the size of the buffer, and heap memory in bytes occupied by
101
* mAudioEncoder and mVideoEncoder.
102
*/
103
typedef MozPromise<size_t, size_t, true> SizeOfPromise;
104
RefPtr<SizeOfPromise> SizeOfExcludingThis(
105
mozilla::MallocSizeOf aMallocSizeOf);
106
// EventHandler
107
IMPL_EVENT_HANDLER(start)
108
IMPL_EVENT_HANDLER(stop)
109
IMPL_EVENT_HANDLER(dataavailable)
110
IMPL_EVENT_HANDLER(pause)
111
IMPL_EVENT_HANDLER(resume)
112
IMPL_EVENT_HANDLER(error)
113
114
NS_DECL_NSIDOCUMENTACTIVITY
115
116
uint32_t AudioBitsPerSecond() const { return mAudioBitsPerSecond; }
117
uint32_t VideoBitsPerSecond() const { return mVideoBitsPerSecond; }
118
119
protected:
120
virtual ~MediaRecorder();
121
122
MediaRecorder& operator=(const MediaRecorder& x) = delete;
123
// Create dataavailable event with Blob data and it runs in main thread
124
nsresult CreateAndDispatchBlobEvent(BlobImpl* aBlobImpl);
125
// Creating a simple event to notify UA simple event.
126
void DispatchSimpleEvent(const nsAString& aStr);
127
// Creating a error event with message.
128
void NotifyError(nsresult aRv);
129
130
MediaRecorder(const MediaRecorder& x) = delete; // prevent bad usage
131
// Remove session pointer.
132
void RemoveSession(Session* aSession);
133
// Create DOMExceptions capturing the JS stack for async errors. These are
134
// created ahead of time rather than on demand when firing an error as the JS
135
// stack of the operation that started the async behavior will not be
136
// available at the time the error event is fired. Note, depending on when
137
// this is called there may not be a JS stack to capture.
138
void InitializeDomExceptions();
139
// Runs the "Inactivate the recorder" algorithm.
140
void Inactivate();
141
// Stop the recorder and its internal session. This should be used by
142
// sessions that are in the process of being destroyed.
143
void StopForSessionDestruction();
144
// DOM wrapper for source media stream. Will be null when input is audio node.
145
RefPtr<DOMMediaStream> mStream;
146
// Source audio node. Will be null when input is a media stream.
147
RefPtr<AudioNode> mAudioNode;
148
// Source audio node's output index. Will be zero when input is a media
149
// stream.
150
uint32_t mAudioNodeOutput = 0;
151
152
// The current state of the MediaRecorder object.
153
RecordingState mState = RecordingState::Inactive;
154
// Hold the sessions reference and clean it when the DestroyRunnable for a
155
// session is running.
156
nsTArray<RefPtr<Session>> mSessions;
157
158
RefPtr<Document> mDocument;
159
160
nsString mMimeType;
161
nsString mConstrainedMimeType;
162
163
uint32_t mAudioBitsPerSecond = 0;
164
uint32_t mVideoBitsPerSecond = 0;
165
Maybe<uint32_t> mConstrainedBitsPerSecond;
166
167
// DOMExceptions that are created early and possibly thrown in NotifyError.
168
// Creating them early allows us to capture the JS stack for which cannot be
169
// done at the time the error event is fired.
170
RefPtr<DOMException> mSecurityDomException;
171
RefPtr<DOMException> mUnknownDomException;
172
173
private:
174
// Register MediaRecorder into Document to listen the activity changes.
175
void RegisterActivityObserver();
176
void UnRegisterActivityObserver();
177
178
bool CheckPermission(const nsString& aType);
179
};
180
181
} // namespace dom
182
} // namespace mozilla
183
184
#endif