[Multimedia] Redesigned Recorder API
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Recorder / Recorder / VideoRecorder.Capabilities.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.Collections.Generic;
19 using System.Diagnostics;
20 using System.Linq;
21 using Native = Interop.Recorder;
22 using NativeHandle = Interop.RecorderHandle;
23
24 namespace Tizen.Multimedia
25 {
26     public partial class VideoRecorder
27     {
28         private static IEnumerable<Size> _frontResolutions;
29         private static IEnumerable<Size> _rearResolutions;
30
31         private static IEnumerable<Size> GetVideoResolutions(NativeHandle handle)
32         {
33             var result = new List<Size>();
34
35             var ret = Native.GetVideoResolutions(handle, (w, h, _) => { result.Add(new Size(w, h)); return true; });
36
37             if (ret == RecorderErrorCode.NotSupported)
38             {
39                 throw new NotSupportedException("Video recording is not supported.");
40             }
41
42             ret.ThrowIfError("Failed to load the resolutions");
43
44             return result.AsReadOnly();
45         }
46
47         private static IEnumerable<Size> LoadVideoResolutions(CameraDevice device)
48         {
49             using (var camera = new Camera(device))
50             {
51                 Native.CreateVideo(camera.Handle, out var handle).ThrowIfError("Failed to get the resolutions");
52
53                 using (handle)
54                 {
55                     return GetVideoResolutions(handle);
56                 }
57             }
58         }
59
60         /// <summary>
61         /// Gets the video resolutions that the current device supports.
62         /// </summary>
63         /// <feature>http://tizen.org/feature/camera</feature>
64         /// <param name="device">The camera device to retrieve the supported resolutions</param>
65         /// <exception cref="NotSupportedException">A required feature is not supported.</exception>
66         /// <exception cref="ArgumentException"><paramref name="device"/> is invalid.</exception>
67         public static IEnumerable<Size> GetSupportedVideoResolutions(CameraDevice device)
68         {
69             ValidationUtil.ValidateEnum(typeof(CameraDevice), device, nameof(device));
70
71             if (device == CameraDevice.Front)
72             {
73                 return _frontResolutions ?? (_frontResolutions = LoadVideoResolutions(CameraDevice.Front));
74             }
75
76             if (device == CameraDevice.Front)
77             {
78                 return _rearResolutions ?? (_rearResolutions = LoadVideoResolutions(CameraDevice.Rear));
79             }
80
81             Debug.Fail($"No cache for {device}.");
82
83             return LoadVideoResolutions(device);
84         }
85
86         /// <summary>
87         /// Gets the video encoders that the current device supports.
88         /// </summary>
89         /// <feature>http://tizen.org/feature/camera</feature>
90         /// <exception cref="NotSupportedException">A required feature is not supported.</exception>
91         public static IEnumerable<RecorderVideoCodec> GetSupportedVideoCodecs()
92             => Capabilities.Value.SupportedVideoCodecs ?? throw new NotSupportedException("Video recording is not supported.");
93
94         internal static void ValidateVideoCodec(RecorderVideoCodec codec)
95         {
96             if (GetSupportedVideoCodecs().Contains(codec) == false)
97             {
98                 throw new NotSupportedException($"{codec.ToString()} is not supported.");
99             }
100         }
101     }
102 }