Release 4.0.0-preview1-00332
[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.<br/>
41         ///     -or-<br/>
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         /// </remarks>
109         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
110         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
111         /// <seealso cref="SizeLimit"/>
112         public int TimeLimit
113         {
114             get => _timeLimit;
115             set
116             {
117                 if (value < 0)
118                 {
119                     throw new ArgumentOutOfRangeException(nameof(value), value,
120                         "Time limit can't be less than zero.");
121                 }
122
123                 _timeLimit = value;
124             }
125         }
126
127         private int _sizeLimit;
128
129         /// <summary>
130         /// Gets or sets the maximum size of a recording file.
131         /// </summary>
132         /// <value>
133         /// The maximum size of a recording file in kilobytes, or 0 for unlimited size.
134         /// </value>
135         /// <remarks>
136         /// After reaching the limitation, the data which is being recorded will
137         /// be discarded and not written to the file.
138         /// </remarks>
139         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
140         /// <seealso cref="StreamRecorder.RecordingLimitReached"/>
141         /// <seealso cref="TimeLimit"/>
142         public int SizeLimit
143         {
144             get => _sizeLimit;
145             set
146             {
147                 if (value < 0)
148                 {
149                     throw new ArgumentOutOfRangeException(nameof(value), value,
150                         "Size limit can't be less than zero.");
151                 }
152
153                 _sizeLimit = value;
154             }
155         }
156
157         /// <summary>
158         /// Gets or sets the options for audio recording.
159         /// </summary>
160         /// <remarks>
161         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
162         /// </remarks>
163         /// <seealso cref="Video"/>
164         public StreamRecorderAudioOptions Audio { get; set; }
165
166         /// <summary>
167         /// Gets or sets the options for video recording.
168         /// </summary>
169         /// <remarks>
170         /// <see cref="Audio"/> or <see cref="Video"/> must be set for recording.
171         /// </remarks>
172         /// <seealso cref="Audio"/>
173         public StreamRecorderVideoOptions Video { get; set; }
174
175         private StreamRecorderSourceType GetSourceType()
176         {
177             Debug.Assert(Audio != null || Video != null);
178
179             if (Audio != null && Video != null)
180             {
181                 return StreamRecorderSourceType.VideoAudio;
182             }
183
184             return Audio != null ? StreamRecorderSourceType.Audio : StreamRecorderSourceType.Video;
185         }
186
187         internal void Apply(StreamRecorder recorder)
188         {
189             if (Audio == null && Video == null)
190             {
191                 throw new ArgumentException("Both Audio and Video are not set.");
192             }
193
194             Native.EnableSourceBuffer(recorder.Handle, GetSourceType()).ThrowIfError("Failed to apply options.");
195
196             Native.SetFileName(recorder.Handle, SavePath).ThrowIfError("Failed to set save path.");
197
198             recorder.ValidateFileFormat(FileFormat);
199             Native.SetFileFormat(recorder.Handle, FileFormat.ToStreamRecorderEnum())
200                 .ThrowIfError("Failed to set file format.");
201
202             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Size, SizeLimit).
203                 ThrowIfError("Failed to set size limit.");
204
205             Native.SetRecordingLimit(recorder.Handle, RecordingLimitType.Time, TimeLimit).
206                 ThrowIfError("Failed to set time limit.");
207
208             Audio?.Apply(recorder);
209
210             Video?.Apply(recorder);
211         }
212     }
213 }