Release 4.0.0-preview1-00183
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / StreamRecorder / StreamRecorderOptions.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Diagnostics;
19 using Native = Interop.StreamRecorder;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// Specifies the options associated with <see cref="StreamRecorder"/>.
25     /// </summary>
26     /// <seealso cref="StreamRecorder"/>
27     /// <seealso cref="StreamRecorder.Prepare(StreamRecorderOptions)"/>
28     /// <seealso cref="StreamRecorderAudioOptions"/>
29     /// <seealso cref="StreamRecorderVideoOptions"/>
30     public class StreamRecorderOptions
31     {
32         /// <summary>
33         /// Initialize a new instance of the <see cref="StreamRecorderOptions"/> class with the specified
34         /// save path and file format.
35         /// </summary>
36         /// <param name="savePath">The path that the recording result is saved.</param>
37         /// <param name="fileFormat">The file format of output file.</param>
38         /// <exception cref="ArgumentNullException"><paramref name="savePath"/>is null.</exception>
39         /// <exception cref="ArgumentException">
40         ///     <paramref name="savePath"/>is an empty string.\n
41         ///     -or-\n
42         ///     <paramref name="fileFormat"/> is not valid.
43         /// </exception>
44         public StreamRecorderOptions(string savePath, RecorderFileFormat fileFormat)
45         {
46             SavePath = savePath;
47             FileFormat = fileFormat;
48         }
49
50         private string _savePath;
51
52         /// <summary>
53         /// Gets or sets the file path to record.
54         /// </summary>
55         /// <remarks>
56         /// If the same file already exists in the file system, then old file will be overwritten.
57         /// </remarks>
58         /// <exception cref="ArgumentNullException"><paramref name="value"/>is null.</exception>
59         /// <exception cref="ArgumentException"><paramref name="value"/>is an empty string.</exception>
60         public string SavePath
61         {
62             get => _savePath;
63             set
64             {
65                 if (value == null)
66                 {
67                     throw new ArgumentNullException(nameof(value));
68                 }
69
70                 if (string.IsNullOrWhiteSpace(value))
71                 {
72                     throw new ArgumentException("Path can't be an empty string.", nameof(value));
73                 }
74
75                 _savePath = value;
76             }
77         }
78
79         private RecorderFileFormat _fileFormat;
80
81         /// <summary>
82         /// Gets or sets the file format for recording media stream.
83         /// </summary>
84         /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
85         /// <seealso cref="StreamRecorder.GetSupportedFileFormats"/>
86         public RecorderFileFormat FileFormat
87         {
88             get => _fileFormat;
89             set
90             {
91                 ValidationUtil.ValidateEnum(typeof(RecorderFileFormat), value, nameof(value));
92
93                 _fileFormat = value;
94             }
95         }
96
97         private int _timeLimit;
98
99         /// <summary>
100         /// Gets or sets the time limit of recording.
101         /// </summary>
102         /// <value>
103         /// The maximum time of recording in seconds, or 0 for unlimited time.
104         /// </value>
105         /// <remarks>
106         /// After reaching the limitation, the data which is being recorded will
107         /// be discarded and not written to the file.
108         /// The recorder state must be <see cref="RecorderState.Idle"/> state.
109         /// </remarks>
110         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
111         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
112         /// <seealso cref="SizeLimit"/>
113         public int TimeLimit
114         {
115             get => _timeLimit;
116             set
117             {
118                 if (value < 0)
119                 {
120                     throw new ArgumentOutOfRangeException(nameof(value), value,
121                         "Time limit can't be less than zero.");
122                 }
123
124                 _timeLimit = value;
125             }
126         }
127
128         private int _sizeLimit;
129
130         /// <summary>
131         /// Gets or sets the maximum size of a recording file.
132         /// </summary>
133         /// <value>
134         /// The maximum size of a recording file in kilobytes, or 0 for unlimited size.
135         /// </value>
136         /// <remarks>
137         /// After reaching the limitation, the data which is being recorded will
138         /// be discarded and not written to the file.
139         /// </remarks>
140         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
141         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
142         /// <seealso cref="TimeLimit"/>
143         public int SizeLimit
144         {
145             get => _sizeLimit;
146             set
147             {
148                 if (value < 0)
149                 {
150                     throw new ArgumentOutOfRangeException(nameof(value), value,
151                         "Size limit can't be less than zero.");
152                 }
153
154                 _sizeLimit = value;
155             }
156         }
157
158         /// <summary>
159         /// Gets or sets the options for audio recording.
160         /// </summary>
161         /// <remarks>
162         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
163         /// </remarks>
164         /// <seealso cref="Video"/>
165         public StreamRecorderAudioOptions Audio { get; set; }
166
167         /// <summary>
168         /// Gets or sets the options for video recording.
169         /// </summary>
170         /// <remarks>
171         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
172         /// </remarks>
173         /// <seealso cref="Audio"/>
174         public StreamRecorderVideoOptions Video { get; set; }
175
176         private StreamRecorderSourceType GetSourceType()
177         {
178             Debug.Assert(Audio != null || Video != null);
179
180             if (Audio != null && Video != null)
181             {
182                 return StreamRecorderSourceType.VideoAudio;
183             }
184
185             return Audio != null ? StreamRecorderSourceType.Audio : StreamRecorderSourceType.Video;
186         }
187
188         internal void Apply(StreamRecorder recorder)
189         {
190             if (Audio == null && Video == null)
191             {
192                 throw new ArgumentException("Both Audio and Video are not set.");
193             }
194
195             Native.EnableSourceBuffer(recorder.Handle, GetSourceType()).ThrowIfError("Failed to apply options.");
196
197             Native.SetFileName(recorder.Handle, SavePath).ThrowIfError("Failed to set save path.");
198
199             recorder.ValidateFileFormat(FileFormat);
200             Native.SetFileFormat(recorder.Handle, FileFormat.ToStreamRecorderEnum())
201                 .ThrowIfError("Failed to set file format.");
202
203             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Size, SizeLimit).
204                 ThrowIfError("Failed to set size limit.");
205
206             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Time, TimeLimit).
207                 ThrowIfError("Failed to set time limit.");
208
209             Audio?.Apply(recorder);
210
211             Video?.Apply(recorder);
212         }
213     }
214 }