2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.Threading.Tasks;
18 using System.Runtime.InteropServices;
19 using System.Diagnostics;
21 using System.Threading;
22 using NativeDisplay = Interop.Display;
25 namespace Tizen.Multimedia
27 /// <since_tizen> 3 </since_tizen>
28 public partial class Player
31 /// Gets the native handle of the player.
33 /// <value>An IntPtr that contains the native handle of the player.</value>
34 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
35 /// <since_tizen> 3 </since_tizen>
40 ValidateNotDisposed();
41 return _handle.DangerousGetHandle();
45 #region Network configuration
46 private string _cookie = "";
47 private string _userAgent = "";
50 /// Gets or sets the cookie for streaming playback.
52 /// <remarks>To set, the player must be in the <see cref="PlayerState.Idle"/> state.</remarks>
53 /// <exception cref="InvalidOperationException">The player is not in the valid state.</exception>
54 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
55 /// <exception cref="ArgumentNullException">The value to set is null.</exception>
56 /// <since_tizen> 3 </since_tizen>
65 ValidatePlayerState(PlayerState.Idle);
69 throw new ArgumentNullException(nameof(value), "Cookie can't be null.");
72 NativePlayer.SetStreamingCookie(Handle, value, value.Length).
73 ThrowIfFailed(this, "Failed to set the cookie to the player");
80 /// Gets or sets the user agent for streaming playback.
82 /// <remarks>To set, the player must be in the <see cref="PlayerState.Idle"/> state.</remarks>
83 /// <exception cref="InvalidOperationException">The player is not in the valid state.</exception>
84 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
85 /// <exception cref="ArgumentNullException">The value to set is null.</exception>
86 /// <since_tizen> 3 </since_tizen>
87 public string UserAgent
95 ValidatePlayerState(PlayerState.Idle);
99 throw new ArgumentNullException(nameof(value), "UserAgent can't be null.");
102 NativePlayer.SetStreamingUserAgent(Handle, value, value.Length).
103 ThrowIfFailed(this, "Failed to set the user agent to the player");
111 /// Gets the state of the player.
113 /// <value>The current state of the player.</value>
114 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
115 /// <since_tizen> 3 </since_tizen>
116 public PlayerState State
120 ValidateNotDisposed();
124 return PlayerState.Preparing;
127 NativePlayer.GetState(Handle, out var state).
128 ThrowIfFailed(this, "Failed to retrieve the state of the player");
130 Debug.Assert(Enum.IsDefined(typeof(PlayerState), state));
132 return (PlayerState)state;
137 /// Gets or sets the audio latency mode.
139 /// <value>A <see cref="AudioLatencyMode"/> that specifies the mode. The default is <see cref="AudioLatencyMode.Mid"/>.</value>
141 /// If the mode is <see cref="AudioLatencyMode.High"/>,
142 /// audio output interval can be increased, so it can keep more audio data to play.
143 /// But, state transition like pause or resume can be more slower than default(<see cref="AudioLatencyMode.Mid"/>).
145 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
146 /// <exception cref="ArgumentException">The value is not valid.</exception>
147 /// <since_tizen> 3 </since_tizen>
148 public AudioLatencyMode AudioLatencyMode
152 NativePlayer.GetAudioLatencyMode(Handle, out var value).
153 ThrowIfFailed(this, "Failed to get the audio latency mode of the player");
159 ValidateNotDisposed();
161 ValidationUtil.ValidateEnum(typeof(AudioLatencyMode), value);
163 NativePlayer.SetAudioLatencyMode(Handle, value).
164 ThrowIfFailed(this, "Failed to set the audio latency mode of the player");
169 /// Gets or sets the looping state.
171 /// <value>true if the playback is looping; otherwise, false. The default value is false.</value>
172 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
173 /// <since_tizen> 3 </since_tizen>
174 public bool IsLooping
178 NativePlayer.IsLooping(Handle, out var value).
179 ThrowIfFailed(this, "Failed to get the looping state of the player");
185 ValidateNotDisposed();
187 NativePlayer.SetLooping(Handle, value).
188 ThrowIfFailed(this, "Failed to set the looping state of the player");
192 #region Display methods
195 /// Gets the display settings.
197 /// <value>A <see cref="PlayerDisplaySettings"/> that specifies the display settings.</value>
198 /// <since_tizen> 3 </since_tizen>
199 public PlayerDisplaySettings DisplaySettings { get; }
201 private Display _display;
203 private PlayerErrorCode SetDisplay(Display display)
207 return NativeDisplay.SetDisplay(Handle, PlayerDisplayType.None, IntPtr.Zero);
210 return display.ApplyTo(this);
213 private void ReplaceDisplay(Display newDisplay)
215 _display?.SetOwner(null);
216 _display = newDisplay;
217 _display?.SetOwner(this);
221 /// Gets or sets the display.
223 /// <value>A <see cref="Multimedia.Display"/> that specifies the display.</value>
225 /// The player must be in the <see cref="PlayerState.Idle"/> state.<br/>
226 /// The raw video feature(http://tizen.org/feature/multimedia.raw_video) is required if
227 /// the display is created with <see cref="MediaView"/>.
229 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
230 /// <exception cref="ArgumentException">The value has already been assigned to another player.</exception>
231 /// <exception cref="InvalidOperationException">The player is not in the valid state.</exception>
232 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
233 /// <since_tizen> 3 </since_tizen>
234 public Display Display
242 ValidatePlayerState(PlayerState.Idle);
244 if (value != null && value.HasMediaView)
246 ValidationUtil.ValidateFeatureSupported(PlayerFeatures.RawVideo);
249 if (value?.Owner != null)
251 if (ReferenceEquals(this, value.Owner))
256 throw new ArgumentException("The display has already been assigned to another.");
259 ReplaceDisplay(value);
263 PlayerErrorCode IDisplayable<PlayerErrorCode>.ApplyEvasDisplay(DisplayType type, ElmSharp.EvasObject evasObject)
265 Debug.Assert(IsDisposed == false);
267 Debug.Assert(Enum.IsDefined(typeof(DisplayType), type));
268 Debug.Assert(type != DisplayType.None);
270 return NativeDisplay.SetDisplay(Handle,
271 type == DisplayType.Overlay ? PlayerDisplayType.Overlay : PlayerDisplayType.Evas, evasObject);
274 PlayerErrorCode IDisplayable<PlayerErrorCode>.ApplyEcoreWindow(IntPtr windowHandle)
276 Debug.Assert(IsDisposed == false);
278 return NativeDisplay.SetEcoreDisplay(Handle, PlayerDisplayType.Overlay, windowHandle);
282 private PlayerTrackInfo _audioTrack;
285 /// Gets the track info for the audio.
287 /// <value>A <see cref="PlayerTrackInfo"/> for audio.</value>
288 /// <since_tizen> 3 </since_tizen>
289 public PlayerTrackInfo AudioTrackInfo
293 if (_audioTrack == null)
295 _audioTrack = new PlayerTrackInfo(this, StreamType.Audio);
301 private PlayerTrackInfo _subtitleTrackInfo;
304 /// Gets the track info for the subtitle.
306 /// <value>A <see cref="PlayerTrackInfo"/> for the subtitle.</value>
307 /// <since_tizen> 3 </since_tizen>
308 public PlayerTrackInfo SubtitleTrackInfo
312 if (_subtitleTrackInfo == null)
314 _subtitleTrackInfo = new PlayerTrackInfo(this, StreamType.Text);
316 return _subtitleTrackInfo;
320 private StreamInfo _streamInfo;
323 /// Gets the stream information.
325 /// <value>A <see cref="StreamInfo"/> for this player.</value>
326 /// <since_tizen> 3 </since_tizen>
327 public StreamInfo StreamInfo
331 if (_streamInfo == null)
333 _streamInfo = new StreamInfo(this);
339 private readonly AudioEffect _audioEffect;
342 /// Gets the audio effect.
344 /// <feature>http://tizen.org/feature/multimedia.custom_audio_effect</feature>
345 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
346 /// <since_tizen> 3 </since_tizen>
347 public AudioEffect AudioEffect
351 if (_audioEffect == null)
353 throw new NotSupportedException($"The feature({PlayerFeatures.AudioEffect}) is not supported.");
361 /// Gets or sets the mute state.
363 /// <value>true if the player is muted; otherwise, false.</value>
364 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
365 /// <since_tizen> 3 </since_tizen>
370 NativePlayer.IsMuted(Handle, out var value).
371 ThrowIfFailed(this, "Failed to get the mute state of the player");
373 Log.Info(PlayerLog.Tag, "get mute : " + value);
379 NativePlayer.SetMute(Handle, value).ThrowIfFailed(this, "Failed to set the mute state of the player");
384 /// Gets or sets the current volume.
386 /// <remarks>Valid volume range is from 0 to 1.0, inclusive.</remarks>
387 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
388 /// <exception cref="ArgumentOutOfRangeException">
389 /// <paramref name="value"/> is less than zero.<br/>
391 /// <paramref name="value"/> is greater than 1.0.
393 /// <since_tizen> 3 </since_tizen>
399 NativePlayer.GetVolume(Handle, out value, out value).
400 ThrowIfFailed(this, "Failed to get the volume of the player");
406 if (value < 0F || 1.0F < value)
408 throw new ArgumentOutOfRangeException(nameof(value), value,
409 $"Valid volume range is 0 <= value <= 1.0, but got { value }.");
412 NativePlayer.SetVolume(Handle, value, value).
413 ThrowIfFailed(this, "Failed to set the volume of the player");