346053217e06f11f0755a3ae8241ab6158de731e
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / StreamRecorder / StreamRecorderVideoOptions.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 Native = Interop.StreamRecorder;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Specifies the options associated with video recording.
24     /// </summary>
25     /// <seealso cref="StreamRecorder"/>
26     /// <seealso cref="StreamRecorderOptions"/>
27     /// <seealso cref="StreamRecorderAudioOptions"/>
28     public class StreamRecorderVideoOptions
29     {
30         private const int DefaultBitRate = 0;
31
32         /// <summary>
33         /// Initialize a new instance of the <see cref="StreamRecorderVideoOptions"/> class with the specified
34         /// codec, resolution, source format and frame rate.
35         /// </summary>
36         /// <param name="codec">The <see cref="RecorderVideoCodec"/> for encoding video stream.</param>
37         /// <param name="resolution">The resolution of video recording.</param>
38         /// <param name="sourceFormat">The format of source stream.</param>
39         /// <param name="frameRate">The frame rate for encoding video stream.</param>
40         /// <remarks>
41         /// <see cref="BitRate"/> will be set as default.
42         /// </remarks>
43         /// <exception cref="ArgumentException">
44         ///     <paramref name="codec"/> is not valid.\n
45         ///     -or-\n
46         ///     <paramref name="sourceFormat"/> is not valid.\n
47         /// </exception>
48         /// <exception cref="ArgumentOutOfRangeException">
49         ///     Width or height of <paramref name="resolution"/> is less than or equal to zero.\n
50         ///     -or-\n
51         ///     <paramref name="frameRate"/> is less than or equal to zero.\n
52         /// </exception>
53         public StreamRecorderVideoOptions(RecorderVideoCodec codec, Size resolution,
54             StreamRecorderVideoFormat sourceFormat, int frameRate) :
55             this(codec, resolution, sourceFormat, frameRate, DefaultBitRate)
56         {
57         }
58
59         /// <summary>
60         /// Initialize a new instance of the <see cref="StreamRecorderVideoOptions"/> class with the specified
61         /// codec, resolution, source format, frame rate and bit rate.
62         /// </summary>
63         /// <param name="codec">The <see cref="RecorderVideoCodec"/> for encoding video stream.</param>
64         /// <param name="resolution">The resolution of video recording.</param>
65         /// <param name="sourceFormat">The format of source stream.</param>
66         /// <param name="frameRate">The frame rate for encoding video stream.</param>
67         /// <param name="bitRate">The bit rate for encoding video stream.</param>
68         /// <exception cref="ArgumentException">
69         ///     <paramref name="codec"/> is not valid.\n
70         ///     -or-\n
71         ///     <paramref name="sourceFormat"/> is not valid.\n
72         /// </exception>
73         /// <exception cref="ArgumentOutOfRangeException">
74         ///     Width or height of <paramref name="resolution"/> is less than or equal to zero.\n
75         ///     -or-\n
76         ///     <paramref name="frameRate"/> is less than or equal to zero.\n
77         ///     -or-\n
78         ///     <paramref name="bitRate"/> is less than zero.
79         /// </exception>
80         public StreamRecorderVideoOptions(RecorderVideoCodec codec, Size resolution,
81             StreamRecorderVideoFormat sourceFormat, int frameRate, int bitRate)
82         {
83             Codec = codec;
84             Resolution = resolution;
85             SourceFormat = sourceFormat;
86             FrameRate = frameRate;
87             BitRate = bitRate;
88         }
89
90         private RecorderVideoCodec _codec;
91
92         /// <summary>
93         /// Gets or sets the video codec for encoding video stream.
94         /// </summary>
95         /// <value>The codec for video stream recording.</value>
96         /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
97         /// <seealso cref="StreamRecorder.GetSupportedVideoCodecs"/>
98         public RecorderVideoCodec Codec
99         {
100             get => _codec;
101             set
102             {
103                 ValidationUtil.ValidateEnum(typeof(RecorderVideoCodec), value, nameof(value));
104
105                 _codec = value;
106             }
107         }
108
109         private Size _resolution;
110
111         /// <summary>
112         /// Gets or sets the resolution of the video recording.
113         /// </summary>
114         /// <value>The output resolution for video stream recording.</value>
115         /// <exception cref="ArgumentOutOfRangeException">
116         ///     Width or height of <paramref name="value"/> is less than or equal to zero.
117         /// </exception>
118         /// <seealso cref="StreamRecorder.GetSupportedVideoResolutions"/>
119         public Size Resolution
120         {
121             get => _resolution;
122             set
123             {
124                 if (value.Width <= 0 || value.Height <= 0)
125                 {
126                     throw new ArgumentOutOfRangeException(nameof(value), value,
127                         "Resolution can't be less than or equal to zero.");
128                 }
129
130                 _resolution = value;
131             }
132         }
133
134         private int _frameRate;
135
136         /// <summary>
137         /// Gets or sets the frame rate for recording media stream.
138         /// </summary>
139         /// <value>The frame rate value for video stream recording.</value>
140         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
141         public int FrameRate
142         {
143             get => _frameRate;
144             set
145             {
146                 if (value <= 0)
147                 {
148                     throw new ArgumentOutOfRangeException(nameof(value), value,
149                         "Frame rate can't be less than or equal to zero.");
150                 }
151                 _frameRate = value;
152             }
153         }
154
155         private StreamRecorderVideoFormat _sourceFormat;
156
157         /// <summary>
158         /// Gets or sets the video source format for recording media stream.
159         /// </summary>
160         /// <value>The source format of buffers for video stream recording.</value>
161         /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
162         public StreamRecorderVideoFormat SourceFormat
163         {
164             get => _sourceFormat;
165             set
166             {
167                 ValidationUtil.ValidateEnum(typeof(StreamRecorderVideoFormat), value, nameof(value));
168
169                 _sourceFormat = value;
170             }
171         }
172
173         private int _bitRate;
174
175         /// <summary>
176         /// The bit rate of the video encoder in bits per second.
177         /// </summary>
178         /// <value>The bit rate value for video stream recording. The default is 0.</value>
179         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than zero.</exception>
180         public int BitRate
181         {
182             get => _bitRate;
183             set
184             {
185                 if (value < 0)
186                 {
187                     throw new ArgumentOutOfRangeException(nameof(value), value,
188                         "Bit rate can't be less than or equal to zero.");
189                 }
190                 _bitRate = value;
191             }
192         }
193
194         internal void Apply(StreamRecorder recorder)
195         {
196             recorder.ValidateVideoCodec(Codec);
197
198             Native.SetVideoEncoder(recorder.Handle, Codec.ToStreamRecorderEnum()).
199                 ThrowIfError("Failed to set video codec.");
200
201             recorder.ValidateVideoResolution(Resolution);
202
203             Native.SetVideoResolution(recorder.Handle, Resolution.Width, Resolution.Height).
204                 ThrowIfError("Failed to set video resolution.");
205
206             Native.SetVideoFrameRate(recorder.Handle, FrameRate).
207                 ThrowIfError("Failed to set video frame rate.");
208
209             Native.SetVideoEncoderBitRate(recorder.Handle, BitRate).
210                 ThrowIfError("Failed to set video bit rate.");
211
212             Native.SetVideoSourceFormat(recorder.Handle, SourceFormat).
213                 ThrowIfError("Failed to set video source format.");
214         }
215     }
216
217 }