Release 4.0.0-preview1-00183
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.StreamRecorder / StreamRecorder / StreamRecorder.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.Linq;
20 using Native = Interop.StreamRecorder;
21
22 namespace Tizen.Multimedia
23 {
24     public partial class StreamRecorder
25     {
26         internal void LoadCapabilities()
27         {
28             _videoCodecs = LoadVideoCodecs(this);
29             _audioCodecs = LoadAudioCodecs(this);
30             _fileFormats = LoadFileFormats(this);
31             _videoResolutions = LoadResolutions(this);
32         }
33
34         private static IEnumerable<RecorderVideoCodec> LoadVideoCodecs(StreamRecorder recorder)
35         {
36             var result = new List<RecorderVideoCodec>();
37             Native.VideoEncoderCallback callback = (codec, _) =>
38             {
39                 result.Add(codec.ToRecorderEnum());
40                 return true;
41             };
42
43             Native.VideoEncoders(recorder.Handle, callback).ThrowIfError("Failed to get the supported video codecs.");
44
45             return result.AsReadOnly();
46         }
47
48         private static IEnumerable<RecorderAudioCodec> LoadAudioCodecs(StreamRecorder recorder)
49         {
50             var result = new List<RecorderAudioCodec>();
51
52             Native.AudioEncoders(recorder.Handle, (codec, _) =>
53             {
54                 result.Add(codec.ToRecorderEnum());
55                 return true;
56             }).ThrowIfError("Failed to get the supported audio codecs.");
57
58             return result.AsReadOnly();
59         }
60
61         private static IEnumerable<RecorderFileFormat> LoadFileFormats(StreamRecorder recorder)
62         {
63             var result = new List<RecorderFileFormat>();
64
65             Native.FileFormats(recorder.Handle, (fileFormat, _) =>
66             {
67                 result.Add(fileFormat.ToRecorderEnum());
68                 return true;
69             }).ThrowIfError("Failed to get the supported file formats.");
70
71             return result.AsReadOnly();
72         }
73
74         private static IEnumerable<Size> LoadResolutions(StreamRecorder recorder)
75         {
76             List<Size> result = new List<Size>();
77
78             Native.VideoResolutionCallback callback = (width, height, _) =>
79             {
80                 result.Add(new Size(width, height));
81                 return true;
82             };
83
84             Native.VideoResolution(recorder.Handle, callback).
85                 ThrowIfError("Failed to get the supported video resolutions.");
86
87             return result.AsReadOnly();
88         }
89
90         private IEnumerable<RecorderFileFormat> _fileFormats;
91
92         /// <summary>
93         /// Gets the file formats that the current device supports.
94         /// </summary>
95         /// <returns>An IEnumerable of <see cref="RecorderFileFormat"/> representing the supported file formats.</returns>
96         public IEnumerable<RecorderFileFormat> GetSupportedFileFormats() => _fileFormats;
97
98         private IEnumerable<RecorderAudioCodec> _audioCodecs;
99
100         /// <summary>
101         /// Gets the audio codecs that the current device supports.
102         /// </summary>
103         /// <returns>An IEnumerable of <see cref="RecorderAudioCodec"/> representing the supported audio codecs.</returns>
104         public IEnumerable<RecorderAudioCodec> GetSupportedAudioCodecs() => _audioCodecs;
105
106         private IEnumerable<RecorderVideoCodec> _videoCodecs;
107
108         /// <summary>
109         /// Gets the video codecs that the current device supports.
110         /// </summary>
111         /// <returns>An IEnumerable of <see cref="RecorderVideoCodec"/> representing the supported video codecs.</returns>
112         public IEnumerable<RecorderVideoCodec> GetSupportedVideoCodecs() => _videoCodecs;
113
114         private IEnumerable<Size> _videoResolutions;
115
116         /// <summary>
117         /// Gets the video resolutions that the current device supports.
118         /// </summary>
119         /// <returns>An IEnumerable of <see cref="Size"/> representing the supported resolutions.</returns>
120         public IEnumerable<Size> GetSupportedVideoResolutions() => _videoResolutions;
121
122         internal void ValidateFileFormat(RecorderFileFormat format)
123         {
124             if (_fileFormats.Contains(format) == false)
125             {
126                 throw new NotSupportedException($"{format.ToString()} is not supported.");
127             }
128         }
129
130         internal void ValidateAudioCodec(RecorderAudioCodec codec)
131         {
132             if (_audioCodecs.Contains(codec) == false)
133             {
134                 throw new NotSupportedException($"{codec.ToString()} is not supported.");
135             }
136         }
137
138         internal void ValidateVideoCodec(RecorderVideoCodec codec)
139         {
140             if (_videoCodecs.Contains(codec) == false)
141             {
142                 throw new NotSupportedException($"{codec.ToString()} is not supported.");
143             }
144         }
145
146         internal void ValidateVideoResolution(Size resolution)
147         {
148             if (_videoResolutions.Contains(resolution) == false)
149             {
150                 throw new NotSupportedException($"Resolution({resolution.ToString()}) is not supported.");
151             }
152         }
153     }
154 }