[NUI] TCSACR-226 code change (#1032)
[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     /// <since_tizen> 4 </since_tizen>
31     public class StreamRecorderOptions
32     {
33         /// <summary>
34         /// Initialize a new instance of the <see cref="StreamRecorderOptions"/> class with the specified
35         /// save path and file format.
36         /// </summary>
37         /// <param name="savePath">The path that the recording result is saved.</param>
38         /// <param name="fileFormat">The file format of output file.</param>
39         /// <exception cref="ArgumentNullException"><paramref name="savePath"/>is null.</exception>
40         /// <exception cref="ArgumentException">
41         ///     <paramref name="savePath"/>is an empty string.<br/>
42         ///     -or-<br/>
43         ///     <paramref name="fileFormat"/> is not valid.
44         /// </exception>
45         /// <since_tizen> 4 </since_tizen>
46         public StreamRecorderOptions(string savePath, RecorderFileFormat fileFormat)
47         {
48             SavePath = savePath;
49             FileFormat = fileFormat;
50         }
51
52         private string _savePath;
53
54         /// <summary>
55         /// Gets or sets the file path to record.
56         /// </summary>
57         /// <remarks>
58         /// If the same file already exists in the file system, then old file will be overwritten.
59         /// </remarks>
60         /// <exception cref="ArgumentNullException"><paramref name="value"/>is null.</exception>
61         /// <exception cref="ArgumentException"><paramref name="value"/>is an empty string.</exception>
62         /// <since_tizen> 4 </since_tizen>
63         public string SavePath
64         {
65             get => _savePath;
66             set
67             {
68                 if (value == null)
69                 {
70                     throw new ArgumentNullException(nameof(value));
71                 }
72
73                 if (string.IsNullOrWhiteSpace(value))
74                 {
75                     throw new ArgumentException("Path can't be an empty string.", nameof(value));
76                 }
77
78                 _savePath = value;
79             }
80         }
81
82         private RecorderFileFormat _fileFormat;
83
84         /// <summary>
85         /// Gets or sets the file format for recording media stream.
86         /// </summary>
87         /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
88         /// <seealso cref="StreamRecorder.GetSupportedFileFormats"/>
89         /// <since_tizen> 4 </since_tizen>
90         public RecorderFileFormat FileFormat
91         {
92             get => _fileFormat;
93             set
94             {
95                 ValidationUtil.ValidateEnum(typeof(RecorderFileFormat), value, nameof(value));
96
97                 _fileFormat = value;
98             }
99         }
100
101         private int _timeLimit;
102
103         /// <summary>
104         /// Gets or sets the time limit of recording.
105         /// </summary>
106         /// <value>
107         /// The maximum time of recording in seconds, or 0 for unlimited time.
108         /// </value>
109         /// <remarks>
110         /// After reaching the limitation, the data which is being recorded will
111         /// be discarded and not written to the file.
112         /// </remarks>
113         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
114         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
115         /// <seealso cref="SizeLimit"/>
116         /// <since_tizen> 4 </since_tizen>
117         public int TimeLimit
118         {
119             get => _timeLimit;
120             set
121             {
122                 if (value < 0)
123                 {
124                     throw new ArgumentOutOfRangeException(nameof(value), value,
125                         "Time limit can't be less than zero.");
126                 }
127
128                 _timeLimit = value;
129             }
130         }
131
132         private int _sizeLimit;
133
134         /// <summary>
135         /// Gets or sets the maximum size of a recording file.
136         /// </summary>
137         /// <value>
138         /// The maximum size of a recording file in kilobytes, or 0 for unlimited size.
139         /// </value>
140         /// <remarks>
141         /// After reaching the limitation, the data which is being recorded will
142         /// be discarded and not written to the file.
143         /// </remarks>
144         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
145         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
146         /// <seealso cref="TimeLimit"/>
147         /// <since_tizen> 4 </since_tizen>
148         public int SizeLimit
149         {
150             get => _sizeLimit;
151             set
152             {
153                 if (value < 0)
154                 {
155                     throw new ArgumentOutOfRangeException(nameof(value), value,
156                         "Size limit can't be less than zero.");
157                 }
158
159                 _sizeLimit = value;
160             }
161         }
162
163         /// <summary>
164         /// Gets or sets the options for audio recording.
165         /// </summary>
166         /// <remarks>
167         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
168         /// </remarks>
169         /// <seealso cref="Video"/>
170         /// <since_tizen> 4 </since_tizen>
171         public StreamRecorderAudioOptions Audio { get; set; }
172
173         /// <summary>
174         /// Gets or sets the options for video recording.
175         /// </summary>
176         /// <remarks>
177         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
178         /// </remarks>
179         /// <seealso cref="Audio"/>
180         /// <since_tizen> 4 </since_tizen>
181         public StreamRecorderVideoOptions Video { get; set; }
182
183         private StreamRecorderSourceType GetSourceType()
184         {
185             Debug.Assert(Audio != null || Video != null);
186
187             if (Audio != null && Video != null)
188             {
189                 return StreamRecorderSourceType.VideoAudio;
190             }
191
192             return Audio != null ? StreamRecorderSourceType.Audio : StreamRecorderSourceType.Video;
193         }
194
195         internal void Apply(StreamRecorder recorder)
196         {
197             if (Audio == null && Video == null)
198             {
199                 throw new ArgumentException("Both Audio and Video are not set.");
200             }
201
202             Native.EnableSourceBuffer(recorder.Handle, GetSourceType()).ThrowIfError("Failed to apply options.");
203
204             Native.SetFileName(recorder.Handle, SavePath).ThrowIfError("Failed to set save path.");
205
206             recorder.ValidateFileFormat(FileFormat);
207             Native.SetFileFormat(recorder.Handle, FileFormat.ToStreamRecorderEnum())
208                 .ThrowIfError("Failed to set file format.");
209
210             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Size, SizeLimit).
211                 ThrowIfError("Failed to set size limit.");
212
213             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Time, TimeLimit).
214                 ThrowIfError("Failed to set time limit.");
215
216             Audio?.Apply(recorder);
217
218             Video?.Apply(recorder);
219         }
220     }
221 }