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