[MediaPlayer] Added ErrorHandler registration methods for internal use. (#33)
[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     /// <since_tizen> 3 </since_tizen>
29     public class PlayerTrackInfo
30     {
31         private readonly StreamType _streamType;
32         private readonly Player _owner;
33
34         internal PlayerTrackInfo(Player player, StreamType streamType)
35         {
36             Debug.Assert(player != null);
37
38             _streamType = 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         /// <since_tizen> 3 </since_tizen>
53         public int GetCount()
54         {
55             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
56
57             NativePlayer.GetTrackCount(_owner.Handle, _streamType, out var count).
58                 ThrowIfFailed(_owner, "Failed to get count of the track");
59
60             Log.Info(PlayerLog.Tag, "get count : " + count);
61
62             return count;
63         }
64
65         /// <summary>
66         /// Gets the language code for the specified index, or null if the language is undefined.
67         /// </summary>
68         /// <returns>The number of tracks.</returns>
69         /// <remarks>
70         ///     <para>The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
71         ///     <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</para>
72         ///     <para>The language codes are defined in ISO 639-1.</para>
73         /// </remarks>
74         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
75         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.</exception>
76         /// <exception cref="ArgumentOutOfRangeException">
77         ///     <paramref name="index"/> is less than zero.<br/>
78         ///     -or-<br/>
79         ///     <paramref name="index"/> is equal to or greater than <see cref="GetCount()"/>.
80         /// </exception>
81         /// <since_tizen> 3 </since_tizen>
82         public string GetLanguageCode(int index)
83         {
84             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
85
86             if (index < 0 || GetCount() <= index)
87             {
88                 throw new ArgumentOutOfRangeException(nameof(index), index,
89                     $"Valid index range is 0 <= x < {nameof(GetCount)}(), but got { index }.");
90             }
91
92             IntPtr code = IntPtr.Zero;
93
94             try
95             {
96                 NativePlayer.GetTrackLanguageCode(_owner.Handle, _streamType, index, out code).
97                     ThrowIfFailed(_owner, "Failed to get the selected language of the player");
98
99                 string result = Marshal.PtrToStringAnsi(code);
100
101                 if (result == "und")
102                 {
103                     Log.Error(PlayerLog.Tag, "not defined code");
104                     return null;
105                 }
106                 Log.Info(PlayerLog.Tag, "get language code : " + result);
107                 return result;
108             }
109             finally
110             {
111                 LibcSupport.Free(code);
112             }
113         }
114
115         /// <summary>
116         /// Gets or sets the selected track index.
117         /// </summary>
118         /// <value>The currently selected track index.</value>
119         /// <remarks>
120         /// The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
121         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
122         /// </remarks>
123         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
124         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.</exception>
125         /// <exception cref="ArgumentOutOfRangeException">
126         ///     <paramref name="value"/> is less than zero.<br/>
127         ///     -or-<br/>
128         ///     <paramref name="value"/> is equal to or greater than <see cref="GetCount()"/>.
129         /// </exception>
130         /// <since_tizen> 3 </since_tizen>
131         public int Selected
132         {
133             get
134             {
135                 _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
136
137                 NativePlayer.GetCurrentTrack(_owner.Handle, _streamType, out var value).
138                     ThrowIfFailed(_owner, "Failed to get the selected index of the player");
139                 Log.Debug(PlayerLog.Tag, "get selected index : " + value);
140                 return value;
141             }
142             set
143             {
144                 if (value < 0 || GetCount() <= value)
145                 {
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(_owner, "Failed to set the selected index of the player");
154             }
155         }
156     }
157 }