/
StreamRecorder.cs
374 lines (330 loc) · 14.8 KB
/
StreamRecorder.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Diagnostics;
using System.Linq;
using NativeHandle = Interop.StreamRecorderHandle;
using Native = Interop.StreamRecorder;
namespace Tizen.Multimedia
{
/// <summary>
/// Provides the ability to record user buffer from application.
/// </summary>
/// <seealso cref="Recorder"/>
public partial class StreamRecorder : IDisposable
{
private NativeHandle _handle;
private bool _disposed = false;
private bool _audioEnabled;
private bool _videoEnabled;
private StreamRecorderVideoFormat _sourceFormat;
/// <summary>
/// Initialize a new instance of the <see cref="StreamRecorder"/> class.
/// </summary>
/// <exception cref="NotSupportedException">The feature is not supported.</exception>
/// <since_tizen> 3 </since_tizen>
public StreamRecorder()
{
try
{
Native.Create(out _handle).ThrowIfError("Failed to create stream recorder.");
}
catch (TypeLoadException)
{
throw new NotSupportedException("StreamRecorder is not supported.");
}
LoadCapabilities();
RegisterStreamRecorderNotifiedEvent();
RegisterBufferConsumedEvent();
RegisterRecordingStatusChangedEvent();
RegisterRecordingErrorOccurredEvent();
RegisterRecordingLimitReachedEvent();
}
internal NativeHandle Handle
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(StreamRecorder));
}
return _handle;
}
}
/// <summary>
/// Gets the current state of the stream recorder.
/// </summary>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <since_tizen> 3 </since_tizen>
public RecorderState State
{
get
{
Native.GetState(Handle, out var val).ThrowIfError("Failed to get the stream recorder state.");
return val;
}
}
private void ValidateState(params RecorderState[] required)
{
Debug.Assert(required.Length > 0);
var curState = State;
if (!required.Contains(curState))
{
throw new InvalidOperationException($"The stream recorder is not in a valid state. " +
$"Current State : { curState }, Valid State : { string.Join(", ", required) }.");
}
}
#region Operation methods
/// <summary>
/// Prepares the stream recorder with the specified options.
/// </summary>
/// <remarks>The recorder must be <see cref="RecorderState.Idle"/>.</remarks>
/// <param name="options">The options for recording.</param>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="ArgumentException">Both <see cref="StreamRecorderOptions.Audio"/> and
/// <see cref="StreamRecorderOptions.Video"/> are null.
/// </exception>
/// <exception cref="NotSupportedException"><paramref name="options"/> contains a value which is not supported.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Unprepare"/>
/// <seealso cref="Start"/>
/// <seealso cref="StreamRecorderOptions"/>
/// <seealso cref="StreamRecorderAudioOptions"/>
/// <seealso cref="StreamRecorderVideoOptions"/>
/// <since_tizen> 4 </since_tizen>
public void Prepare(StreamRecorderOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ValidateState(RecorderState.Idle);
options.Apply(this);
Native.Prepare(Handle).ThrowIfError("Failed to prepare stream recorder.");
_audioEnabled = options.Audio != null;
_videoEnabled = options.Video != null;
if (options.Video != null)
{
_sourceFormat = options.Video.SourceFormat;
}
}
/// <summary>
/// Unprepares the stream recorder.
/// </summary>
/// <remarks>
/// The recorder state must be <see cref="RecorderState.Ready"/> state by
/// <see cref="Prepare(StreamRecorderOptions)"/>, <see cref="Cancel"/> and <see cref="Commit"/>.<br/>
/// The recorder state will be <see cref="RecorderState.Idle"/>.<br/>
/// <br/>
/// It has no effect if the recorder is already in the <see cref="RecorderState.Idle"/> state.
/// </remarks>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Prepare"/>
/// <since_tizen> 3 </since_tizen>
public void Unprepare()
{
if (State == RecorderState.Idle)
{
return;
}
ValidateState(RecorderState.Ready);
Native.Unprepare(Handle).ThrowIfError("Failed to reset the stream recorder.");
}
/// <summary>
/// Starts recording.
/// </summary>
/// <remarks>
/// The recorder state must be <see cref="RecorderState.Ready"/> state by
/// <see cref="Prepare(StreamRecorderOptions)"/> or
/// <see cref="RecorderState.Paused"/> state by <see cref="Pause"/>.<br/>
/// <br/>
/// It has no effect if the recorder is already in the <see cref="RecorderState.Recording"/> state.
/// </remarks>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="UnauthorizedAccessException">The access of the resources can not be granted.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Pause"/>
/// <seealso cref="Commit"/>
/// <seealso cref="Cancel"/>
/// <since_tizen> 3 </since_tizen>
public void Start()
{
if (State == RecorderState.Recording)
{
return;
}
ValidateState(RecorderState.Ready, RecorderState.Paused);
Native.Start(Handle).ThrowIfError("Failed to start the stream recorder.");
}
/// <summary>
/// Pauses recording.
/// </summary>
/// <remarks>
/// Recording can be resumed with <see cref="Start"/>.<br/>
/// <br/>
/// The recorder state must be <see cref="RecorderState.Recording"/> state by <see cref="Start"/>.<br/>
/// <br/>
/// It has no effect if the recorder is already in the <see cref="RecorderState.Paused"/> state.
/// </remarks>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Start"/>
/// <seealso cref="Commit"/>
/// <seealso cref="Cancel"/>
/// <since_tizen> 3 </since_tizen>
public void Pause()
{
if (State == RecorderState.Paused)
{
return;
}
ValidateState(RecorderState.Recording);
Native.Pause(Handle).ThrowIfError("Failed to pause the stream recorder.");
}
/// <summary>
/// Stops recording and saves the result.
/// </summary>
/// <remarks>
/// The recorder state must be <see cref="RecorderState.Recording"/> state by <see cref="Start"/> or
/// <see cref="RecorderState.Paused"/> state by <see cref="Pause"/>.<br/>
/// <br/>
/// The recorder state will be <see cref="RecorderState.Ready"/> after commit.
/// <para>
/// http://tizen.org/privilege/mediastorage is needed if the save path are relevant to media storage.
/// http://tizen.org/privilege/externalstorage is needed if the save path are relevant to external storage.
/// </para>
/// </remarks>
/// <privilege>http://tizen.org/privilege/mediastorage</privilege>
/// <privilege>http://tizen.org/privilege/externalstorage</privilege>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="UnauthorizedAccessException">The access to the resources can not be granted.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Start"/>
/// <seealso cref="Pause"/>
/// <since_tizen> 3 </since_tizen>
public void Commit()
{
ValidateState(RecorderState.Paused, RecorderState.Recording);
Native.Commit(Handle).ThrowIfError("Failed to commit.");
}
/// <summary>
/// Cancels recording.
/// The recording data is discarded and not written.
/// </summary>
/// <remarks>
/// The recorder state must be <see cref="RecorderState.Recording"/> state by <see cref="Start"/> or
/// <see cref="RecorderState.Paused"/> state by <see cref="Pause"/>.
/// </remarks>
/// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <seealso cref="Start"/>
/// <seealso cref="Pause"/>
/// <since_tizen> 3 </since_tizen>
public void Cancel()
{
ValidateState(RecorderState.Paused, RecorderState.Recording);
Native.Cancel(Handle).ThrowIfError("Failed to cancel recording.");
}
private static bool AreVideoTypesMatched(StreamRecorderVideoFormat videoFormat, MediaFormatVideoMimeType mimeType)
{
return (videoFormat == StreamRecorderVideoFormat.Nv12 && mimeType == MediaFormatVideoMimeType.NV12) ||
(videoFormat == StreamRecorderVideoFormat.Nv21 && mimeType == MediaFormatVideoMimeType.NV21) ||
(videoFormat == StreamRecorderVideoFormat.I420 && mimeType == MediaFormatVideoMimeType.I420);
}
/// <summary>
/// Pushes a packet as recording raw data.
/// </summary>
/// <param name="packet">An audio or video packet to record.</param>
/// <remarks>
/// The recorder state must be <see cref="RecorderState.Recording"/> state by <see cref="Start"/>.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// The recorder is not in the valid state.<br/>
/// -or-<br/>
/// <paramref name="packet"/> is an audio packet but audio recording is not enabled(See <see cref="StreamRecorderOptions.Audio"/>).<br/>
/// -or-<br/>
/// <paramref name="packet"/> is a video packet but video recording is not enabled(See <see cref="StreamRecorderOptions.Video"/>).<br/>
/// -or-<br/>
/// <paramref name="packet"/> is a video packet but the <see cref="VideoMediaFormat.MimeType"/> does not match the video source format.<br/>
/// -or-<br/>
/// An internal error occurs.
/// </exception>
/// <exception cref="ObjectDisposedException">The <see cref="StreamRecorder"/> has already been disposed.</exception>
/// <see cref="Prepare(StreamRecorderOptions)"/>
/// <seealso cref="StreamRecorderOptions.Audio"/>
/// <seealso cref="StreamRecorderOptions.Video"/>
/// <seealso cref="StreamRecorderVideoOptions.SourceFormat"/>
/// <since_tizen> 3 </since_tizen>
public void PushBuffer(MediaPacket packet)
{
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
ValidateState(RecorderState.Recording);
switch (packet.Format.Type)
{
case MediaFormatType.Audio:
if (_audioEnabled == false)
{
throw new InvalidOperationException("Audio option is not set.");
}
break;
case MediaFormatType.Video:
if (_videoEnabled == false)
{
throw new InvalidOperationException("Video option is not set.");
}
if (AreVideoTypesMatched(_sourceFormat, (packet.Format as VideoMediaFormat).MimeType) == false)
{
throw new InvalidOperationException("Video format does not match.");
}
break;
default:
throw new ArgumentException("Packet is not valid.");
}
Native.PushStreamBuffer(Handle, MediaPacket.Lock.Get(packet).GetHandle())
.ThrowIfError("Failed to push buffer.");
}
#endregion
#region Dispose support
/// <summary>
/// Release any unmanaged resources used by this object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Releases the resources used by the StreamRecorder.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
/// </param>
/// <since_tizen> 3 </since_tizen>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_handle?.Dispose();
_disposed = true;
}
}
#endregion
}
}