2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Diagnostics;
19 using Native = Interop.Recorder;
20 using NativeHandle = Interop.RecorderHandle;
22 namespace Tizen.Multimedia
25 /// Provides the ability to control video recording.
27 public partial class VideoRecorder : Recorder
29 private static NativeHandle CreateHandle(Camera camera)
33 throw new ArgumentNullException(nameof(camera));
36 Native.CreateVideo(camera.Handle, out var handle).
37 ThrowIfError("Failed to create video recorder.");
42 private static void ThrowIfCodecAndFormatNotValid(RecorderVideoCodec videoCodec,
43 RecorderAudioCodec audioCodec, RecorderFileFormat fileFormat)
45 videoCodec.ThrowIfFormatNotSupported(fileFormat);
47 if (audioCodec != RecorderAudioCodec.None)
49 audioCodec.ThrowIfFormatNotSupported(fileFormat);
54 /// Initializes a new instance of the <see cref="VideoRecorder"/> class with the specified camera, video codec and file format.
57 /// If the state of <see cref="Camera"/> is <see cref="CameraState.Created"/>,
58 /// the <see cref="CameraSettings.PreviewPixelFormat"/> will be changed to the recommended format for recording.\n
60 /// The initial state of the Recorder will be <see cref="RecorderState.Ready"/>
61 /// if the state of <see cref="Camera"/> is <see cref="CameraState.Preview"/> or <see cref="CameraState.Captured"/>.
63 /// <param name="camera">The camera object.</param>
64 /// <param name="videoCodec">The codec for video encoding.</param>
65 /// <param name="fileFormat">The format of result file.</param>
66 /// <feature>http://tizen.org/feature/camera</feature>
67 /// <exception cref="InvalidOperationException">An internal error occurred.</exception>
68 /// <exception cref="NotSupportedException">
69 /// A required feature is not supported.\n
71 /// <paramref name="videoCodec"/> is not supported.\n
73 /// <paramref name="fileFormat"/> is not supported with the specified video codec.
75 /// <exception cref="ArgumentException">
76 /// <paramref name="videoCodec"/> is not valid.\n
78 /// <paramref name="fileFormat"/> is not valid.
80 /// <exception cref="ObjectDisposedException"><paramref name="camera"/> has been disposed of.</exception>
81 /// <exception cref="ArgumentNullException"><paramref name="camera"/> is null.</exception>
82 /// <seealso cref="GetSupportedVideoCodecs"/>
83 /// <seealso cref="Recorder.GetSupportedFileFormats"/>
84 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderVideoCodec)"/>
85 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderFileFormat)"/>
86 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderAudioCodec, RecorderFileFormat)"/>
87 public VideoRecorder(Camera camera, RecorderVideoCodec videoCodec, RecorderFileFormat fileFormat) :
88 this(camera, videoCodec, RecorderAudioCodec.None, fileFormat)
93 /// Initializes a new instance of the <see cref="VideoRecorder"/> class with the specified camera, video codec,
94 /// audio codec and file format.
97 /// If the state of <see cref="Camera"/> is <see cref="CameraState.Created"/>,
98 /// the <see cref="CameraSettings.PreviewPixelFormat"/> will be changed to the recommended format for recording.\n
100 /// The initial state of the Recorder will be <see cref="RecorderState.Ready"/>
101 /// if the state of <see cref="Camera"/> is <see cref="CameraState.Preview"/> or <see cref="CameraState.Captured"/>.
103 /// <param name="camera">The camera object.</param>
104 /// <param name="videoCodec">The codec for video encoding.</param>
105 /// <param name="audioCodec">The codec for audio encoding.</param>
106 /// <param name="fileFormat">The format of result file.</param>
107 /// <feature>http://tizen.org/feature/camera</feature>
108 /// <exception cref="InvalidOperationException">An internal error occurred.</exception>
109 /// <exception cref="NotSupportedException">
110 /// A required feature is not supported.\n
112 /// <paramref name="videoCodec"/> is not supported.\n
114 /// <paramref name="audioCodec"/> is not supported.\n
116 /// <paramref name="fileFormat"/> is not supported with the specified video codec.
118 /// <paramref name="fileFormat"/> is not supported with the specified audio codec.
120 /// <exception cref="ArgumentException">
121 /// <paramref name="videoCodec"/> is not valid.\n
123 /// <paramref name="audioCodec"/> is not valid.\n
125 /// <paramref name="fileFormat"/> is not valid.
127 /// <exception cref="ObjectDisposedException"><paramref name="camera"/> has been disposed of.</exception>
128 /// <exception cref="ArgumentNullException"><paramref name="camera"/> is null.</exception>
129 /// <seealso cref="Recorder.GetSupportedAudioCodecs"/>
130 /// <seealso cref="GetSupportedVideoCodecs"/>
131 /// <seealso cref="Recorder.GetSupportedFileFormats"/>
132 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderAudioCodec)"/>
133 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderVideoCodec)"/>
134 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderFileFormat)"/>
135 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderAudioCodec, RecorderFileFormat)"/>
136 public VideoRecorder(Camera camera, RecorderVideoCodec videoCodec,
137 RecorderAudioCodec audioCodec, RecorderFileFormat fileFormat) : base(CreateHandle(camera))
139 SetFormatAndCodec(videoCodec, RecorderAudioCodec.None, fileFormat);
143 /// Sets the video codec and the file format for recording. Audio will not recorded.
145 /// <param name="videoCodec">The codec for video encoding.</param>
146 /// <param name="fileFormat">The format of result file.</param>
147 /// <exception cref="NotSupportedException">
148 /// <paramref name="videoCodec"/> is not supported.\n
150 /// <paramref name="fileFormat"/> is not supported with the specified video codec.
152 /// <exception cref="ArgumentException">
153 /// <paramref name="videoCodec"/> is not valid.\n
155 /// <paramref name="fileFormat"/> is not valid.
157 /// <seealso cref="GetSupportedVideoCodecs"/>
158 /// <seealso cref="Recorder.GetSupportedFileFormats"/>
159 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderVideoCodec)"/>
160 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderAudioCodec, RecorderFileFormat)"/>
161 /// <seealso cref="Recorder.Start(string)"/>
162 public void SetFormatAndCodec(RecorderVideoCodec videoCodec, RecorderFileFormat fileFormat)
164 SetFormatAndCodec(videoCodec, RecorderAudioCodec.None, fileFormat);
168 /// Sets the video codec, audio codec and the file format for recording.
170 /// <param name="videoCodec">The codec for video encoding.</param>
171 /// <param name="audioCodec">The codec for audio encoding.</param>
172 /// <param name="fileFormat">The format of result file.</param>
173 /// <exception cref="NotSupportedException">
174 /// <paramref name="videoCodec"/> is not supported.\n
176 /// <paramref name="audioCodec"/> is not supported.\n
178 /// <paramref name="fileFormat"/> is not supported with the specified video codec.
180 /// <paramref name="fileFormat"/> is not supported with the specified audio codec.
182 /// <exception cref="ArgumentException">
183 /// <paramref name="videoCodec"/> is not valid.\n
185 /// <paramref name="audioCodec"/> is not valid.\n
187 /// <paramref name="fileFormat"/> is not valid.
189 /// <seealso cref="Recorder.GetSupportedAudioCodecs"/>
190 /// <seealso cref="VideoRecorder.GetSupportedVideoCodecs"/>
191 /// <seealso cref="Recorder.GetSupportedFileFormats"/>
192 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderAudioCodec)"/>
193 /// <seealso cref="RecorderExtensions.GetSupportedFileFormats(RecorderVideoCodec)"/>
194 /// <seealso cref="SetFormatAndCodec(RecorderVideoCodec, RecorderFileFormat)"/>
195 /// <seealso cref="Recorder.Start(string)"/>
196 public void SetFormatAndCodec(RecorderVideoCodec videoCodec, RecorderAudioCodec audioCodec, RecorderFileFormat fileFormat)
198 ThrowIfCodecAndFormatNotValid(videoCodec, audioCodec, fileFormat);
200 VideoCodec = videoCodec;
201 AudioCodec = audioCodec;
202 FileFormat = fileFormat;
207 private RecorderVideoCodec _videoCodec;
210 /// Gets the audio codec for encoding an audio stream.
212 public RecorderVideoCodec VideoCodec
217 Debug.Assert(Enum.IsDefined(typeof(RecorderVideoCodec), value));
219 ValidateVideoCodec(value);
221 Native.SetVideoEncoder(Handle, value).ThrowIfError("Failed to set video codec.");
228 /// Gets or sets the video recording motion rate.
231 /// The attribute is valid only in a video recorder.\n
232 /// If the rate is in range of 0-1, video is recorded in a slow motion mode.\n
233 /// If the rate is bigger than 1, video is recorded in a fast motion mode.\n
235 /// To set, the recorder must be in the <see cref="RecorderState.Idle"/> or <see cref="RecorderState.Ready"/> state.
237 /// <exception cref="ArgumentOutOfRangeException">The <paramref name="value"/> is less than or equal to 0.</exception>
238 /// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
239 /// <exception cref="ObjectDisposedException">The recorder already has been disposed of.</exception>
240 public double VideoMotionRate
244 Native.GetMotionRate(Handle, out var val).ThrowIfError("Failed to get video motion rate.");
251 ValidateState(RecorderState.Idle, RecorderState.Ready);
255 throw new ArgumentOutOfRangeException(nameof(value), value,
256 "Video Motion rate can't be less than zero.");
259 Native.SetMotionRate(Handle, value).
260 ThrowIfError("Failed to set video motion rate");
265 /// Gets or sets the orientation in a video metadata tag.
267 /// <value>A <see cref="Rotation"/> that specifies the type of orientation.</value>
268 /// <exception cref="ArgumentException"><paramref name="value"/> is not valid.</exception>
269 /// <exception cref="ObjectDisposedException">The recorder already has been disposed of.</exception>
270 public Rotation VideoOrientationTag
274 Native.GetOrientationTag(Handle, out var val).ThrowIfError("Failed to get recorder orientation.");
281 ValidationUtil.ValidateEnum(typeof(Rotation), value, nameof(value));
283 Native.SetOrientationTag(Handle, value).
284 ThrowIfError("Failed to set recorder orientation");
289 /// Gets or sets the resolution of the video recording.
292 /// To set, the recorder must be in the <see cref="RecorderState.Idle"/> or <see cref="RecorderState.Ready"/> state.
294 /// <exception cref="ArgumentOutOfRangeException">
295 /// Width or height of <paramref name="value"/> is less than or equal to zero.
297 /// <exception cref="NotSupportedException"><paramref name="value"> is not supported.</exception>
298 /// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
299 /// <exception cref="ObjectDisposedException">The recorder already has been disposed of.</exception>
300 /// <seealso cref="Recorder.GetSupportedVideoResolutions(CameraDevice)"/>
301 public Size VideoResolution
305 Native.GetVideoResolution(Handle, out var width, out var height).
306 ThrowIfError("Failed to get camera video resolution");
308 return new Size(width, height);
313 ValidateState(RecorderState.Idle, RecorderState.Ready);
315 if (value.Width <= 0 || value.Height <= 0)
317 throw new ArgumentOutOfRangeException(nameof(value), value,
318 "Resolution can't be less than or equal to zero.");
321 var ret = Native.SetVideoResolution(Handle, value.Width, value.Height);
323 if (ret == RecorderErrorCode.InvalidParameter)
325 throw new NotSupportedException($"Resolution({value.ToString()}) is not supported.");
328 ret.ThrowIfError("Failed to set video resolution.");
333 /// Gets or sets the bitrate of an video encoder in bits per second.
336 /// To set, the recorder must be in the <see cref="RecorderState.Idle"/> or <see cref="RecorderState.Ready"/> state.
338 /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is less than or equal to zero.</exception>
339 /// <exception cref="InvalidOperationException">The recorder is not in the valid state.</exception>
340 /// <exception cref="ObjectDisposedException">The recorder already has been disposed of.</exception>
341 public int VideoBitRate
345 Native.GetVideoEncoderBitrate(Handle, out var val).ThrowIfError("Failed to get video bitrate.");
352 ValidateState(RecorderState.Idle, RecorderState.Ready);
356 throw new ArgumentOutOfRangeException(nameof(value), value,
357 "Bit rate can't be less than or equal to zero.");
360 Native.SetVideoEncoderBitrate(Handle, value).
361 ThrowIfError("Failed to set video bitrate");