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