[NUI] TCSACR-226 code change (#1032)
[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.
52         ///     -or-<br/>
53         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
54         /// </exception>
55         /// <since_tizen> 3 </since_tizen>
56         public int GetCount()
57         {
58             _owner.ValidateNotDisposed();
59             _owner.AudioOffload.CheckDisabled();
60             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
61
62             NativePlayer.GetTrackCount(_owner.Handle, _streamType, out var count).
63                 ThrowIfFailed(_owner, "Failed to get count of the track");
64
65             Log.Info(PlayerLog.Tag, "get count : " + count);
66
67             return count;
68         }
69
70         /// <summary>
71         /// Gets the language code for the specified index, or null if the language is undefined.
72         /// </summary>
73         /// <param name="index">The index of track.</param>
74         /// <returns>The number of tracks.</returns>
75         /// <remarks>
76         ///     <para>The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
77         ///     <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</para>
78         ///     <para>The language codes are defined in ISO 639-1.</para>
79         /// </remarks>
80         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
81         /// <exception cref="InvalidOperationException">The <see cref="Player"/> that this instance belongs to is not in the valid state.
82         ///     -or-<br/>
83         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
84         /// </exception>
85         /// <exception cref="ArgumentOutOfRangeException">
86         ///     <paramref name="index"/> is less than zero.<br/>
87         ///     -or-<br/>
88         ///     <paramref name="index"/> is equal to or greater than <see cref="GetCount()"/>.
89         /// </exception>
90         /// <since_tizen> 3 </since_tizen>
91         public string GetLanguageCode(int index)
92         {
93             _owner.ValidateNotDisposed();
94
95             if (index < 0 || GetCount() <= index)
96             {
97                 throw new ArgumentOutOfRangeException(nameof(index), index,
98                     $"Valid index range is 0 <= x < {nameof(GetCount)}(), but got { index }.");
99             }
100
101             _owner.AudioOffload.CheckDisabled();
102             _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
103
104             IntPtr code = IntPtr.Zero;
105
106             try
107             {
108                 NativePlayer.GetTrackLanguageCode(_owner.Handle, _streamType, index, out code).
109                     ThrowIfFailed(_owner, "Failed to get the selected language of the player");
110
111                 string result = Marshal.PtrToStringAnsi(code);
112
113                 if (result == "und")
114                 {
115                     Log.Error(PlayerLog.Tag, "not defined code");
116                     return null;
117                 }
118                 Log.Info(PlayerLog.Tag, "get language code : " + result);
119                 return result;
120             }
121             finally
122             {
123                 LibcSupport.Free(code);
124             }
125         }
126
127         /// <summary>
128         /// Gets or sets the selected track index.
129         /// </summary>
130         /// <value>The currently selected track index.</value>
131         /// <remarks>
132         /// The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
133         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
134         /// </remarks>
135         /// <exception cref="ObjectDisposedException">The <see cref="Player"/> that this instance belongs to has been disposed of.</exception>
136         /// <exception cref="InvalidOperationException">
137         ///     The <see cref="Player"/> that this instance belongs to is not in the valid state.<br/>
138         ///     -or-<br/>
139         ///     If audio offload is enabled by calling <see cref="AudioOffload.IsEnabled"/>. (Since tizen 6.0)
140         /// </exception>
141         /// <exception cref="ArgumentOutOfRangeException">
142         ///     <paramref name="value"/> is less than zero.<br/>
143         ///     -or-<br/>
144         ///     <paramref name="value"/> is equal to or greater than <see cref="GetCount()"/>.
145         /// </exception>
146         /// <since_tizen> 3 </since_tizen>
147         public int Selected
148         {
149             get
150             {
151                 _owner.ValidateNotDisposed();
152                 _owner.AudioOffload.CheckDisabled();
153                 _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
154
155                 NativePlayer.GetCurrentTrack(_owner.Handle, _streamType, out var value).
156                     ThrowIfFailed(_owner, "Failed to get the selected index of the player");
157                 Log.Debug(PlayerLog.Tag, "get selected index : " + value);
158                 return value;
159             }
160             set
161             {
162                 _owner.ValidateNotDisposed();
163
164                 if (value < 0 || GetCount() <= value)
165                 {
166                     throw new ArgumentOutOfRangeException(nameof(value), value,
167                         $"Valid index range is 0 <= x < {nameof(GetCount)}(), but got { value }.");
168                 }
169
170                 _owner.AudioOffload.CheckDisabled();
171                 _owner.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
172
173                 NativePlayer.SelectTrack(_owner.Handle, _streamType, value).
174                     ThrowIfFailed(_owner, "Failed to set the selected index of the player");
175             }
176         }
177     }
178 }