3f28fe9ab908c7d89ae8bb8fe5313df84010d227
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / MediaTool / TextMediaFormat.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 using System;
17 using System.Diagnostics;
18 using Tizen.Internals.Errors;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Represents a text media format. This class cannot be inherited.
24     /// </summary>
25     public sealed class TextMediaFormat : MediaFormat
26     {
27         /// <summary>
28         /// Initializes a new instance of the TextMediaFormat class with the specified mime type
29         ///     and text type.
30         /// </summary>
31         /// <param name="mimeType">The mime type of the format.</param>
32         /// <param name="textType">The text type of the format.</param>
33         /// <exception cref="ArgumentException">
34         ///                     mimeType or textType is invalid(i.e. undefined value).</exception>
35         public TextMediaFormat(MediaFormatTextMimeType mimeType, MediaFormatTextType textType)
36             : base(MediaFormatType.Text)
37         {
38             if (!Enum.IsDefined(typeof(MediaFormatTextMimeType), mimeType))
39             {
40                 throw new ArgumentException($"Invalid mime type value : { (int)mimeType }");
41             }
42             if (!Enum.IsDefined(typeof(MediaFormatTextType), textType))
43             {
44                 throw new ArgumentException($"Invalid text type value : { (int)textType }");
45             }
46             MimeType = mimeType;
47             TextType = textType;
48         }
49
50         /// <summary>
51         /// Initializes a new instance of the TextMediaFormat class from a native handle.
52         /// </summary>
53         /// <param name="handle">A native handle.</param>
54         internal TextMediaFormat(IntPtr handle)
55             : base(MediaFormatType.Text)
56         {
57             Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");
58
59             MediaFormatTextMimeType mimeType;
60             MediaFormatTextType textType;
61
62             GetInfo(handle, out mimeType, out textType);
63
64             MimeType = mimeType;
65             TextType = textType;
66         }
67
68         /// <summary>
69         /// Retrieves text properties of media format from a native handle.
70         /// </summary>
71         /// <param name="handle">A native handle that properties are retrieved from.</param>
72         /// <param name="mimeType">An out parameter for mime type.</param>
73         /// <param name="textType">An out parameter for text type.</param>
74         private static void GetInfo(IntPtr handle, out MediaFormatTextMimeType mimeType,
75             out MediaFormatTextType textType)
76         {
77             int mimeTypeValue = 0;
78             int textTypeValue = 0;
79
80             int ret = Interop.MediaFormat.GetTextInfo(handle, out mimeTypeValue, out textTypeValue);
81
82             MultimediaDebug.AssertNoError(ret);
83
84             mimeType = (MediaFormatTextMimeType)mimeTypeValue;
85             textType = (MediaFormatTextType)textTypeValue;
86
87             Debug.Assert(Enum.IsDefined(typeof(MediaFormatTextMimeType), mimeType),
88                 "Invalid text mime type!");
89             Debug.Assert(Enum.IsDefined(typeof(MediaFormatTextType), textType),
90                 "Invalid text type!");
91         }
92
93         internal override void AsNativeHandle(IntPtr handle)
94         {
95             Debug.Assert(Type == MediaFormatType.Text);
96
97             int ret = Interop.MediaFormat.SetTextMimeType(handle, (int)MimeType);
98             MultimediaDebug.AssertNoError(ret);
99
100             ret = Interop.MediaFormat.SetTextType(handle, (int)TextType);
101             MultimediaDebug.AssertNoError(ret);
102         }
103
104         /// <summary>
105         /// Gets the mime type of the current format.
106         /// </summary>
107         public MediaFormatTextMimeType MimeType { get; }
108
109         /// <summary>
110         /// Gets the text type of the current format.
111         /// </summary>
112         public MediaFormatTextType TextType { get; }
113
114         public override string ToString()
115         {
116             return $"MimeType={ MimeType.ToString() }, TextType={ TextType.ToString() }";
117         }
118
119         public override bool Equals(object obj)
120         {
121             var rhs = obj as TextMediaFormat;
122             if (rhs == null)
123             {
124                 return false;
125             }
126
127             return MimeType == rhs.MimeType && TextType == rhs.TextType;
128         }
129
130         public override int GetHashCode()
131         {
132             return new { MimeType, TextType }.GetHashCode();
133         }
134     }
135 }