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