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