Release 4.0.0-preview1-00321
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / PlayerTrackInfo.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 System.Runtime.InteropServices;
19 using static Interop;
20
21 namespace Tizen.Multimedia
22 {
23     /// <summary>
24     /// Provides a means to retrieve the track information.
25     /// </summary>
26     /// <seealso cref="Player.SubtitleTrackInfo"/>
27     /// <seealso cref="Player.AudioTrackInfo"/>
28     public class PlayerTrackInfo
29     {
30         private readonly int _streamType;
31         private readonly Player _owner;
32
33         internal PlayerTrackInfo(Player player, StreamType streamType)
34         {
35             Debug.Assert(player != null);
36
37             Log.Debug(PlayerLog.Tag, "streamType : " + streamType);
38             _streamType = (int)streamType;
39             _owner = player;
40         }
41
42         /// <summary>
43         /// Gets the number of tracks.
44         /// </summary>
45         /// <returns>The number of tracks.</returns>
46         /// <remarks>
47         /// The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
48         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
49         /// </remarks>
50         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
51         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.</exception>
52         public int GetCount()
53         {
54             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
55
56             int count = 0;
57             NativePlayer.GetTrackCount(_owner.Handle, _streamType, out count).
58                 ThrowIfFailed("Failed to get count of the track");
59             Log.Info(PlayerLog.Tag, "get count : " + count);
60             return count;
61         }
62
63         /// <summary>
64         /// Gets the language code for the specified index, or null if the language is undefined.
65         /// </summary>
66         /// <returns>The number of tracks.</returns>
67         /// <remarks>
68         ///     <para>The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
69         ///     <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</para>
70         ///     <para>The language codes are defined in ISO 639-1.</para>
71         /// </remarks>
72         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
73         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.</exception>
74         /// <exception cref="ArgumentOutOfRangeException">
75         ///     <paramref name="index"/> is less than zero.<br/>
76         ///     -or-<br/>
77         ///     <paramref name="index"/> is equal to or greater than <see cref="GetCount()"/>
78         /// </exception>
79         public string GetLanguageCode(int index)
80         {
81             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
82
83             if (index < 0 || GetCount() <= index)
84             {
85                 Log.Error(PlayerLog.Tag, "invalid index");
86                 throw new ArgumentOutOfRangeException(nameof(index), index,
87                     $"valid index range is 0 <= x < {nameof(GetCount)}(), but got { index }.");
88             }
89
90             IntPtr code = IntPtr.Zero;
91
92             try
93             {
94                 NativePlayer.GetTrackLanguageCode(_owner.Handle, _streamType, index, out code).
95                     ThrowIfFailed("Failed to get the selected language of the player");
96
97                 string result = Marshal.PtrToStringAnsi(code);
98
99                 if (result == "und")
100                 {
101                     Log.Error(PlayerLog.Tag, "not defined code");
102                     return null;
103                 }
104                 Log.Info(PlayerLog.Tag, "get language code : " + result);
105                 return result;
106             }
107             finally
108             {
109                 LibcSupport.Free(code);
110             }
111         }
112
113         /// <summary>
114         /// Gets or sets the selected track index.
115         /// </summary>
116         /// <value>The currently selected track index.</value>
117         /// <remarks>
118         /// The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
119         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
120         /// </remarks>
121         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
122         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.</exception>
123         /// <exception cref="ArgumentOutOfRangeException">
124         ///     <paramref name="value"/> is less than zero.<br/>
125         ///     -or-<br/>
126         ///     <paramref name="value"/> is equal to or greater than <see cref="GetCount()"/>
127         /// </exception>
128         public int Selected
129         {
130             get
131             {
132                 _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
133
134                 int value = 0;
135
136                 NativePlayer.GetCurrentTrack(_owner.Handle, _streamType, out value).
137                     ThrowIfFailed("Failed to get the selected index of the player");
138                 Log.Debug(PlayerLog.Tag, "get selected index : " + value);
139                 return value;
140             }
141             set
142             {
143                 if (value < 0 || GetCount() <= value)
144                 {
145                     Log.Error(PlayerLog.Tag, "invalid index");
146                     throw new ArgumentOutOfRangeException(nameof(value), value,
147                         $"valid index range is 0 <= x < {nameof(GetCount)}(), but got { value }.");
148                 }
149
150                 _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
151
152                 NativePlayer.SelectTrack(_owner.Handle, _streamType, value).
153                     ThrowIfFailed("Failed to set the selected index of the player");
154             }
155         }
156     }
157 }