[NUI][API10] Create Context/CookieManager when WebView is created.
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaTool / MediaFormat.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.Diagnostics;
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// MediaFormat is a base class for media formats.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public abstract class MediaFormat
28     {
29         /// <summary>
30         /// Initializes a new instance of the ContainerMediaFormat class with a type.
31         /// </summary>
32         /// <param name="type">A type for the format.</param>
33         internal MediaFormat(MediaFormatType type)
34         {
35             Type = type;
36         }
37
38         /// <summary>
39         /// Gets the type of the current format.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         public MediaFormatType Type { get; }
43
44         /// <summary>
45         /// Creates a media format from a native handle.
46         /// </summary>
47         /// <param name="handle">A native handle.</param>
48         /// <returns>An object of one of the subclasses of <see cref="MediaFormat"/>.</returns>
49         internal static MediaFormat FromHandle(IntPtr handle)
50         {
51             if (handle == IntPtr.Zero)
52             {
53                 throw new ArgumentException("The handle value is invalid.", nameof(handle));
54             }
55
56             int ret = Interop.MediaFormat.GetType(handle, out var type);
57
58             if (ret != (int)ErrorCode.InvalidOperation)
59             {
60                 MultimediaDebug.AssertNoError(ret);
61
62                 switch (type)
63                 {
64                     case MediaFormatType.Container:
65                         return new ContainerMediaFormat(handle);
66
67                     case MediaFormatType.Video:
68                         return new VideoMediaFormat(handle);
69
70                     case MediaFormatType.Audio:
71                         return new AudioMediaFormat(handle);
72
73                     case MediaFormatType.Text:
74                         return new TextMediaFormat(handle);
75                 }
76             }
77
78             throw new ArgumentException("looks like handle is corrupted.");
79         }
80
81         /// <summary>
82         /// Creates a native media format from this object.
83         /// </summary>
84         /// <returns>A converted native handle.</returns>
85         /// <remarks>The returned handle must be destroyed using <see cref="Interop.MediaFormat.Unref(IntPtr)"/>.</remarks>
86         internal IntPtr AsNativeHandle()
87         {
88             int ret = Interop.MediaFormat.Create(out var handle);
89
90             MultimediaDebug.AssertNoError(ret);
91
92             AsNativeHandle(handle);
93
94             return handle;
95         }
96
97         internal static void ReleaseNativeHandle(IntPtr handle)
98         {
99             Interop.MediaFormat.Unref(handle);
100         }
101
102         /// <summary>
103         /// Fills out properties of a native media format with the current media format object.
104         /// </summary>
105         /// <param name="handle">A native handle to be written.</param>
106         internal abstract void AsNativeHandle(IntPtr handle);
107     }
108 }