/
StreamRecorderOptions.cs
221 lines (193 loc) · 7.83 KB
/
StreamRecorderOptions.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
/*
* 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 Native = Interop.StreamRecorder;
namespace Tizen.Multimedia
{
/// <summary>
/// Specifies the options associated with <see cref="StreamRecorder"/>.
/// </summary>
/// <seealso cref="StreamRecorder"/>
/// <seealso cref="StreamRecorder.Prepare(StreamRecorderOptions)"/>
/// <seealso cref="StreamRecorderAudioOptions"/>
/// <seealso cref="StreamRecorderVideoOptions"/>
/// <since_tizen> 4 </since_tizen>
public class StreamRecorderOptions
{
/// <summary>
/// Initialize a new instance of the <see cref="StreamRecorderOptions"/> class with the specified
/// save path and file format.
/// </summary>
/// <param name="savePath">The path that the recording result is saved.</param>
/// <param name="fileFormat">The file format of output file.</param>
/// <exception cref="ArgumentNullException"><paramref name="savePath"/>is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="savePath"/>is an empty string.<br/>
/// -or-<br/>
/// <paramref name="fileFormat"/> is not valid.
/// </exception>
/// <since_tizen> 4 </since_tizen>
public StreamRecorderOptions(string savePath, RecorderFileFormat fileFormat)
{
SavePath = savePath;
FileFormat = fileFormat;
}
private string _savePath;
/// <summary>
/// Gets or sets the file path to record.
/// </summary>
/// <remarks>
/// If the same file already exists in the file system, then old file will be overwritten.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="value"/>is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/>is an empty string.</exception>
/// <since_tizen> 4 </since_tizen>
public string SavePath
{
get => _savePath;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Path can't be an empty string.", nameof(value));
}
_savePath = value;
}
}
private RecorderFileFormat _fileFormat;
/// <summary>
/// Gets or sets the file format for recording media stream.
/// </summary>
/// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
/// <seealso cref="StreamRecorder.GetSupportedFileFormats"/>
/// <since_tizen> 4 </since_tizen>
public RecorderFileFormat FileFormat
{
get => _fileFormat;
set
{
ValidationUtil.ValidateEnum(typeof(RecorderFileFormat), value, nameof(value));
_fileFormat = value;
}
}
private int _timeLimit;
/// <summary>
/// Gets or sets the time limit of recording.
/// </summary>
/// <value>
/// The maximum time of recording in seconds, or 0 for unlimited time.
/// </value>
/// <remarks>
/// After reaching the limitation, the data which is being recorded will
/// be discarded and not written to the file.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
/// <seealso cref="StreamRecorder.RecordingLimitReached"/>
/// <seealso cref="SizeLimit"/>
/// <since_tizen> 4 </since_tizen>
public int TimeLimit
{
get => _timeLimit;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value,
"Time limit can't be less than zero.");
}
_timeLimit = value;
}
}
private int _sizeLimit;
/// <summary>
/// Gets or sets the maximum size of a recording file.
/// </summary>
/// <value>
/// The maximum size of a recording file in kilobytes, or 0 for unlimited size.
/// </value>
/// <remarks>
/// After reaching the limitation, the data which is being recorded will
/// be discarded and not written to the file.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
/// <seealso cref="StreamRecorder.RecordingLimitReached"/>
/// <seealso cref="TimeLimit"/>
/// <since_tizen> 4 </since_tizen>
public int SizeLimit
{
get => _sizeLimit;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value,
"Size limit can't be less than zero.");
}
_sizeLimit = value;
}
}
/// <summary>
/// Gets or sets the options for audio recording.
/// </summary>
/// <remarks>
/// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
/// </remarks>
/// <seealso cref="Video"/>
/// <since_tizen> 4 </since_tizen>
public StreamRecorderAudioOptions Audio { get; set; }
/// <summary>
/// Gets or sets the options for video recording.
/// </summary>
/// <remarks>
/// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
/// </remarks>
/// <seealso cref="Audio"/>
/// <since_tizen> 4 </since_tizen>
public StreamRecorderVideoOptions Video { get; set; }
private StreamRecorderSourceType GetSourceType()
{
Debug.Assert(Audio != null || Video != null);
if (Audio != null && Video != null)
{
return StreamRecorderSourceType.VideoAudio;
}
return Audio != null ? StreamRecorderSourceType.Audio : StreamRecorderSourceType.Video;
}
internal void Apply(StreamRecorder recorder)
{
if (Audio == null && Video == null)
{
throw new ArgumentException("Both Audio and Video are not set.");
}
Native.EnableSourceBuffer(recorder.Handle, GetSourceType()).ThrowIfError("Failed to apply options.");
Native.SetFileName(recorder.Handle, SavePath).ThrowIfError("Failed to set save path.");
recorder.ValidateFileFormat(FileFormat);
Native.SetFileFormat(recorder.Handle, FileFormat.ToStreamRecorderEnum())
.ThrowIfError("Failed to set file format.");
Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Size, SizeLimit).
ThrowIfError("Failed to set size limit.");
Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Time, TimeLimit).
ThrowIfError("Failed to set time limit.");
Audio?.Apply(recorder);
Video?.Apply(recorder);
}
}
}