forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediaEncoder.h
298 lines (256 loc) · 9.39 KB
/
MediaEncoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* 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 MediaEncoder_h_
#define MediaEncoder_h_
#include "ContainerWriter.h"
#include "CubebUtils.h"
#include "MediaQueue.h"
#include "MediaTrackGraph.h"
#include "MediaTrackListener.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
#include "nsIMemoryReporter.h"
#include "TrackEncoder.h"
namespace mozilla {
class DriftCompensator;
class Muxer;
class Runnable;
class TaskQueue;
namespace dom {
class AudioNode;
class AudioStreamTrack;
class MediaStreamTrack;
class VideoStreamTrack;
} // namespace dom
class DriftCompensator;
class MediaEncoder;
class MediaEncoderListener {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEncoderListener)
virtual void Initialized() = 0;
virtual void DataAvailable() = 0;
virtual void Error() = 0;
virtual void Shutdown() = 0;
protected:
virtual ~MediaEncoderListener() {}
};
/**
* MediaEncoder is the framework of encoding module, it controls and manages
* procedures between ContainerWriter and TrackEncoder. ContainerWriter packs
* the encoded track data with a specific container (e.g. ogg, webm).
* AudioTrackEncoder and VideoTrackEncoder are subclasses of TrackEncoder, and
* are responsible for encoding raw data coming from MediaTrackGraph.
*
* MediaEncoder solves threading issues by doing message passing to a TaskQueue
* (the "encoder thread") as passed in to the constructor. Each
* MediaStreamTrack to be recorded is set up with a MediaTrackListener.
* Typically there are a non-direct track listeners for audio, direct listeners
* for video, and there is always a non-direct listener on each track for
* time-keeping. The listeners forward data to their corresponding TrackEncoders
* on the encoder thread.
*
* The MediaEncoder listens to events from all TrackEncoders, and in turn
* signals events to interested parties. Typically a MediaRecorder::Session.
* The event that there's data available in the TrackEncoders is what typically
* drives the extraction and muxing of data.
*
* MediaEncoder is designed to be a passive component, neither does it own or is
* in charge of managing threads. Instead this is done by its owner.
*
* For example, usage from MediaRecorder of this component would be:
* 1) Create an encoder with a valid MIME type.
* => encoder = MediaEncoder::CreateEncoder(aMIMEType);
* It then creates a ContainerWriter according to the MIME type
*
* 2) Connect a MediaEncoderListener to be notified when the MediaEncoder has
* been initialized and when there's data available.
* => encoder->RegisterListener(listener);
*
* 3) Connect the sources to be recorded. Either through:
* => encoder->ConnectAudioNode(node);
* or
* => encoder->ConnectMediaStreamTrack(track);
* These should not be mixed. When connecting MediaStreamTracks there is
* support for at most one of each kind.
*
* 4) When the MediaEncoderListener is notified that the MediaEncoder has
* data available, we can encode data. This also encodes metadata on its
* first invocation.
* => encoder->GetEncodedData(...);
*
* 5) To stop encoding, there are multiple options:
*
* 5.1) Stop() for a graceful stop.
* => encoder->Stop();
*
* 5.2) Cancel() for an immediate stop, if you don't need the data currently
* buffered.
* => encoder->Cancel();
*
* 5.3) When all input tracks end, the MediaEncoder will automatically stop
* and shut down.
*/
class MediaEncoder {
private:
class AudioTrackListener;
class VideoTrackListener;
class EncoderListener;
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEncoder)
MediaEncoder(TaskQueue* aEncoderThread,
RefPtr<DriftCompensator> aDriftCompensator,
UniquePtr<ContainerWriter> aWriter,
AudioTrackEncoder* aAudioEncoder,
VideoTrackEncoder* aVideoEncoder, TrackRate aTrackRate,
const nsAString& aMIMEType);
/**
* Called on main thread from MediaRecorder::Pause.
*/
void Suspend();
/**
* Called on main thread from MediaRecorder::Resume.
*/
void Resume();
/**
* Stops the current encoding, and disconnects the input tracks.
*/
void Stop();
/**
* Connects an AudioNode with the appropriate encoder.
*/
void ConnectAudioNode(dom::AudioNode* aNode, uint32_t aOutput);
/**
* Connects a MediaStreamTrack with the appropriate encoder.
*/
void ConnectMediaStreamTrack(dom::MediaStreamTrack* aTrack);
/**
* Removes a connected MediaStreamTrack.
*/
void RemoveMediaStreamTrack(dom::MediaStreamTrack* aTrack);
/**
* Creates an encoder with a given MIME type. Returns null if we are unable
* to create the encoder. For now, default aMIMEType to "audio/ogg" and use
* Ogg+Opus if it is empty.
*/
static already_AddRefed<MediaEncoder> CreateEncoder(
TaskQueue* aEncoderThread, const nsAString& aMIMEType,
uint32_t aAudioBitrate, uint32_t aVideoBitrate, uint8_t aTrackTypes,
TrackRate aTrackRate);
/**
* Encodes raw data for all tracks to aOutputBufs. The buffer of container
* data is allocated in ContainerWriter::GetContainerData().
*
* On its first call, metadata is also encoded. TrackEncoders must have been
* initialized before this is called.
*/
nsresult GetEncodedData(nsTArray<nsTArray<uint8_t>>* aOutputBufs);
/**
* Return true if MediaEncoder has been shutdown. Reasons are encoding
* complete, encounter an error, or being canceled by its caller.
*/
bool IsShutdown();
/**
* Cancels the encoding and shuts down the encoder using Shutdown().
*/
RefPtr<GenericNonExclusivePromise> Cancel();
bool HasError();
static bool IsWebMEncoderEnabled();
const nsString& MimeType() const;
/**
* Notifies listeners that this MediaEncoder has been initialized.
*/
void NotifyInitialized();
/**
* Notifies listeners that this MediaEncoder has data available in some
* TrackEncoders.
*/
void NotifyDataAvailable();
/**
* Registers a listener to events from this MediaEncoder.
* We hold a strong reference to the listener.
*/
void RegisterListener(MediaEncoderListener* aListener);
/**
* Unregisters a listener from events from this MediaEncoder.
* The listener will stop receiving events synchronously.
*/
bool UnregisterListener(MediaEncoderListener* aListener);
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
/*
* Measure the size of the buffer, and heap memory in bytes occupied by
* mAudioEncoder and mVideoEncoder.
*/
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf);
/**
* Set desired video keyframe interval defined in milliseconds.
*/
void SetVideoKeyFrameInterval(uint32_t aVideoKeyFrameInterval);
protected:
~MediaEncoder();
private:
/**
* Sets mGraphTrack if not already set, using a new stream from aTrack's
* graph.
*/
void EnsureGraphTrackFrom(MediaTrack* aTrack);
/**
* Takes a regular runnable and dispatches it to the graph wrapped in a
* ControlMessage.
*/
void RunOnGraph(already_AddRefed<Runnable> aRunnable);
/**
* Shuts down the MediaEncoder and cleans up track encoders.
* Listeners will be notified of the shutdown unless we were Cancel()ed first.
*/
RefPtr<GenericNonExclusivePromise> Shutdown();
/**
* Sets mError to true, notifies listeners of the error if mError changed,
* and stops encoding.
*/
void SetError();
const RefPtr<TaskQueue> mEncoderThread;
const RefPtr<DriftCompensator> mDriftCompensator;
UniquePtr<Muxer> mMuxer;
RefPtr<AudioTrackEncoder> mAudioEncoder;
RefPtr<AudioTrackListener> mAudioListener;
RefPtr<VideoTrackEncoder> mVideoEncoder;
RefPtr<VideoTrackListener> mVideoListener;
RefPtr<EncoderListener> mEncoderListener;
nsTArray<RefPtr<MediaEncoderListener>> mListeners;
// The AudioNode we are encoding.
// Will be null when input is media stream or destination node.
RefPtr<dom::AudioNode> mAudioNode;
// Pipe-track for allowing a track listener on a non-destination AudioNode.
// Will be null when input is media stream or destination node.
RefPtr<AudioNodeTrack> mPipeTrack;
// Input port that connect mAudioNode to mPipeTrack.
// Will be null when input is media stream or destination node.
RefPtr<MediaInputPort> mInputPort;
// An audio track that we are encoding. Will be null if the input stream
// doesn't contain audio on start() or if the input is an AudioNode.
RefPtr<dom::AudioStreamTrack> mAudioTrack;
// A video track that we are encoding. Will be null if the input stream
// doesn't contain video on start() or if the input is an AudioNode.
RefPtr<dom::VideoStreamTrack> mVideoTrack;
// A stream to keep the MediaTrackGraph alive while we're recording.
RefPtr<SharedDummyTrack> mGraphTrack;
TimeStamp mStartTime;
const nsString mMIMEType;
bool mInitialized;
bool mCompleted;
bool mError;
bool mShutdown;
// Set when shutdown starts.
RefPtr<GenericNonExclusivePromise> mShutdownPromise;
// Get duration from create encoder, for logging purpose
double GetEncodeTimeStamp() {
TimeDuration decodeTime;
decodeTime = TimeStamp::Now() - mStartTime;
return decodeTime.ToMilliseconds();
}
};
} // namespace mozilla
#endif