2 * Copyright (c) 2019 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.
18 using System.Diagnostics;
19 using System.Collections.Generic;
22 namespace Tizen.Multimedia
25 /// Provides the ability to control the audio offload for <see cref="Multimedia.Player"/>.
27 /// <since_tizen> 6 </since_tizen>
28 public class AudioOffload
30 private IList<MediaFormatAudioMimeType> _supportedFormat;
31 private Player _player { get; }
34 /// Provides a means to retrieve audio offload information.
36 /// <since_tizen> 6 </since_tizen>
37 internal AudioOffload(Player player)
39 Debug.Assert(player != null);
43 private bool _enabled;
44 internal void CheckDisabled()
48 throw new InvalidOperationException("the audio offload is enabled.");
53 /// Enables or disables the audio offload.
55 /// <value>The value indicating whether or not AudioOffload is enabled. The default value is false.</value>
56 /// <remarks><para>The player lets the hardware decode and render the sound if the audio offload is enabled.
57 /// This will reduce the power consumption, but will disable the ability to handle output PCM.
58 /// Please check the below list of functions which will not work if offloading is enabled.</para>
59 /// <para>If audio offload is enabled, the following functions will return <see cref="InvalidOperationException"/>
60 /// and they will not work at all even if they were called before offload is enabled.<br/>
61 /// <see cref="AudioEffect"/><br/>
62 /// <see cref="EqualizerBand"/><br/>
63 /// <see cref="PlayerTrackInfo"/><br/>
64 /// <see cref="Player.EnableExportingAudioData"/><br/>
65 /// <see cref="Player.AudioLatencyMode"/><br/>
66 /// <see cref="Player.SetPlaybackRate"/><br/>
67 /// <see cref="Player.ReplayGain"/><br/>
68 /// <see cref="Player.AudioPitch"/><br/>
69 /// <see cref="Player.AudioPitchEnabled"/><br/></para>
70 /// <para>To set, the player must be in the <see cref="PlayerState.Idle"/> state.
71 /// The sound stream type of the player should be <see cref="AudioStreamType.Media"/>.</para></remarks>
72 /// <feature>http://tizen.org/feature/multimedia.player.audio_offload</feature>
73 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
74 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
75 /// <exception cref="InvalidOperationException">
76 /// The player is not in the valid state.<br/>
78 /// Operation failed; internal error.
80 /// <since_tizen> 6 </since_tizen>
85 ValidationUtil.ValidateFeatureSupported(PlayerFeatures.AudioOffload);
86 _player.ValidateNotDisposed();
88 NativePlayer.IsAudioOffloadEnabled(_player.Handle, out var value).
89 ThrowIfFailed(_player, "Failed to get whether the audio offload of the player is enabled or not");
95 ValidationUtil.ValidateFeatureSupported(PlayerFeatures.AudioOffload);
96 _player.ValidateNotDisposed();
97 _player.ValidatePlayerState(PlayerState.Idle);
99 NativePlayer.SetAudioOffloadEnabled(_player.Handle, value).
100 ThrowIfFailed(_player, "Failed to set the audio offload of the player");
106 /// Get a state whether or not the audio offload is activated.
108 /// <value>The value indicating whether or not AudioOffload is activated.</value>
110 /// Audio offload could be inactivated depending on the audio device capability even if the audio offload feature is supported.
111 /// The <see cref="Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
112 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
114 /// <feature>http://tizen.org/feature/multimedia.player.audio_offload</feature>
115 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
116 /// <exception cref="ObjectDisposedException">The player has already been disposed of.</exception>
117 /// <exception cref="InvalidOperationException">
118 /// The player is not in the valid state.<br/>
120 /// Operation failed; internal error.
122 /// <since_tizen> 6 </since_tizen>
123 public bool IsActivated
127 ValidationUtil.ValidateFeatureSupported(PlayerFeatures.AudioOffload);
128 _player.ValidateNotDisposed();
129 _player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
131 NativePlayer.IsAudioOffloadActivated(_player.Handle, out var value).
132 ThrowIfFailed(_player, "Failed to get whether the audio offload of the player is enabled or not");
138 /// Retrieves all formats for audio offload.
141 /// It returns a list contained all formats for audio offload.
143 /// <remarks>The supported media format can vary depending on the device capabilities.</remarks>
144 /// <feature>http://tizen.org/feature/multimedia.player.audio_offload</feature>
145 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
146 /// <exception cref="ObjectDisposedException">The <see cref="Player"/> has already been disposed of.</exception>
147 /// <exception cref="InvalidOperationException">
148 /// Operation failed; internal error.
150 /// <seealso cref="MediaFormatAudioMimeType"/>
151 /// <since_tizen> 6 </since_tizen>
152 public IEnumerable<MediaFormatAudioMimeType> SupportedFormats
156 if (_supportedFormat == null)
158 _supportedFormat = GetSupportedFormats();
161 return _supportedFormat;
165 private IList<MediaFormatAudioMimeType> GetSupportedFormats()
167 List<MediaFormatAudioMimeType> audioFormats = new List<MediaFormatAudioMimeType>();
169 NativePlayer.SupportedMediaFormatCallback callback = (int format, IntPtr userData) =>
171 if (!Enum.IsDefined(typeof(MediaFormatAudioMimeType), format))
173 Log.Warn(PlayerLog.Tag, "not supported : " + format.ToString());
177 Log.Debug(PlayerLog.Tag, "supported : " + ((MediaFormatAudioMimeType)format).ToString());
178 audioFormats.Add((MediaFormatAudioMimeType)format);
182 NativePlayer.SupportedAudioOffloadFormat(_player.Handle, callback, IntPtr.Zero).
183 ThrowIfFailed(_player, "Failed to get the supported formats for audio offload");
185 return audioFormats.AsReadOnly();