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.
19 using System.Collections.Generic;
20 using Tizen.Applications;
21 using Native = Interop.MediaControllerClient;
23 namespace Tizen.Multimedia.Remoting
26 /// Provides a means to to send commands to and handle events from media control server.
28 /// <since_tizen> 4 </since_tizen>
29 public partial class MediaController
32 /// Occurs when the server is stopped.
34 /// <since_tizen> 4 </since_tizen>
35 public event EventHandler ServerStopped;
37 internal void RaiseStoppedEvent()
40 ServerStopped?.Invoke(this, EventArgs.Empty);
45 /// Occurs when the playback state is updated.
47 /// <since_tizen> 4 </since_tizen>
48 public event EventHandler<PlaybackStateUpdatedEventArgs> PlaybackStateUpdated;
50 private PlaybackStateUpdatedEventArgs CreatePlaybackUpdatedEventArgs(IntPtr playbackHandle)
54 Native.GetPlaybackState(playbackHandle, out var playbackCode).ThrowIfError("Failed to get state.");
56 Native.GetPlaybackPosition(playbackHandle, out var position).ThrowIfError("Failed to get position.");
58 return new PlaybackStateUpdatedEventArgs(playbackCode.ToPublic(), (long)position);
62 Log.Error(GetType().FullName, e.ToString());
67 internal void RaisePlaybackUpdatedEvent(IntPtr playbackHandle)
69 var eventHandler = PlaybackStateUpdated;
71 if (eventHandler == null)
76 var args = CreatePlaybackUpdatedEventArgs(playbackHandle);
80 eventHandler.Invoke(this, args);
85 /// Occurs when the playlist is updated.
87 /// <since_tizen> 5 </since_tizen>
88 public event EventHandler<PlaylistUpdatedEventArgs> PlaylistUpdated;
90 internal void RaisePlaylistUpdatedEvent(MediaControlPlaylistMode mode, string name, IntPtr playlistHandle)
92 PlaylistUpdated?.Invoke(this, new PlaylistUpdatedEventArgs(mode, name, new MediaControlPlaylist(playlistHandle)));
96 /// Occurs when the metadata is updated.
98 /// <since_tizen> 4 </since_tizen>
99 public event EventHandler<MetadataUpdatedEventArgs> MetadataUpdated;
101 private MetadataUpdatedEventArgs CreateMetadataUpdatedEventArgs(IntPtr metadataHandle)
105 return new MetadataUpdatedEventArgs(new MediaControlMetadata(metadataHandle));
109 Log.Error(GetType().FullName, e.ToString());
114 internal void RaiseMetadataUpdatedEvent(IntPtr metadataHandle)
116 var eventHandler = MetadataUpdated;
118 if (eventHandler == null)
123 var args = CreateMetadataUpdatedEventArgs(metadataHandle);
127 eventHandler.Invoke(this, args);
132 /// Occurs when the shuffle mode is updated.
134 /// <since_tizen> 4 </since_tizen>
135 public event EventHandler<ShuffleModeUpdatedEventArgs> ShuffleModeUpdated;
137 internal void RaiseShuffleModeUpdatedEvent(MediaControllerNativeShuffleMode mode)
139 ShuffleModeUpdated?.Invoke(this, new ShuffleModeUpdatedEventArgs(mode == MediaControllerNativeShuffleMode.On));
143 /// Occurs when the repeat mode is updated.
145 /// <since_tizen> 4 </since_tizen>
146 public event EventHandler<RepeatModeUpdatedEventArgs> RepeatModeUpdated;
148 internal void RaiseRepeatModeUpdatedEvent(MediaControlRepeatMode mode)
150 RepeatModeUpdated?.Invoke(this, new RepeatModeUpdatedEventArgs(mode));
154 /// Occurs when the subtitle mode is updated.
156 /// <since_tizen> 6 </since_tizen>
157 public event EventHandler<SubtitleModeUpdatedEventArgs> SubtitleModeUpdated;
158 internal void RaiseSubtitleModeUpdatedEvent(bool isEnabled)
160 SubtitleModeUpdated?.Invoke(this, new SubtitleModeUpdatedEventArgs(isEnabled));
164 /// Occurs when the 360 mode is updated.
166 /// <since_tizen> 6 </since_tizen>
167 public event EventHandler<Mode360UpdatedEventArgs> Mode360Updated;
168 internal void RaiseMode360UpdatedEvent(bool isEnabled)
170 Mode360Updated?.Invoke(this, new Mode360UpdatedEventArgs(isEnabled));
174 /// Occurs when the display mode is updated.
176 /// <since_tizen> 6 </since_tizen>
177 public event EventHandler<DisplayModeUpdatedEventArgs> DisplayModeUpdated;
178 internal void RaiseDisplayModeUpdatedEvent(MediaControlNativeDisplayMode mode)
180 DisplayModeUpdated?.Invoke(this, new DisplayModeUpdatedEventArgs(mode.ToPublic()));
184 /// Occurs when the display rotation is updated.
186 /// <since_tizen> 6 </since_tizen>
187 public event EventHandler<DisplayRotationUpdatedEventArgs> DisplayRotationUpdated;
188 internal void RaiseDisplayRotationUpdatedEvent(MediaControlNativeDisplayRotation rotation)
190 DisplayRotationUpdated?.Invoke(this, new DisplayRotationUpdatedEventArgs(rotation.ToPublic()));
195 #region Capability updated event
197 /// Occurs when the playback capabilities are updated.
199 /// <since_tizen> 5 </since_tizen>
200 public event EventHandler<PlaybackCapabilityUpdatedEventArgs> PlaybackCapabilityUpdated;
202 private PlaybackCapabilityUpdatedEventArgs CreatePlaybackCapabilityUpdatedEventArgs(IntPtr playbackCapaHandle)
204 var capabilities = new Dictionary<MediaControlPlaybackCommand, MediaControlCapabilitySupport>();
207 foreach (MediaControllerNativePlaybackAction action in Enum.GetValues(typeof(MediaControllerNativePlaybackAction)))
209 Native.GetPlaybackCapability(playbackCapaHandle, action, out MediaControlCapabilitySupport support);
210 capabilities.Add(action.ToPublic(), support);
213 return new PlaybackCapabilityUpdatedEventArgs(capabilities);
217 Log.Error(GetType().FullName, e.ToString());
222 internal void RaisePlaybackCapabilityUpdatedEvent(IntPtr playbackCapaHandle)
224 var eventHandler = PlaybackCapabilityUpdated;
226 if (eventHandler == null)
231 var args = CreatePlaybackCapabilityUpdatedEventArgs(playbackCapaHandle);
235 eventHandler.Invoke(this, args);
240 /// Occurs when the repeat mode capabilities are updated.
242 /// <since_tizen> 5 </since_tizen>
243 public event EventHandler<RepeatModeCapabilityUpdatedEventArgs> RepeatModeCapabilityUpdated;
245 internal void RaiseRepeatModeCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
247 RepeatModeCapabilityUpdated?.Invoke(this, new RepeatModeCapabilityUpdatedEventArgs(support));
251 /// Occurs when the shuffle mode capabilities are updated.
253 /// <since_tizen> 5 </since_tizen>
254 public event EventHandler<ShuffleModeCapabilityUpdatedEventArgs> ShuffleModeCapabilityUpdated;
256 internal void RaiseShuffleModeCapabilityUpdatedEvent(MediaControlCapabilitySupport support)
258 ShuffleModeCapabilityUpdated?.Invoke(this, new ShuffleModeCapabilityUpdatedEventArgs(support));
262 /// Occurs when the display mode capabilities are updated.
264 /// <since_tizen> 6 </since_tizen>
265 public event EventHandler<DisplayModeCapabilityUpdatedEventArgs> DisplayModeCapabilityUpdated;
267 internal void RaiseDisplayModeCapabilityUpdatedEvent(MediaControlNativeDisplayMode modes)
269 DisplayModeCapabilityUpdated?.Invoke(this, new DisplayModeCapabilityUpdatedEventArgs(modes.ToPublicList()));
273 /// Occurs when the display rotation capabilities are updated.
275 /// <since_tizen> 6 </since_tizen>
276 public event EventHandler<DisplayRotationCapabilityUpdatedEventArgs> DisplayRotationCapabilityUpdated;
278 internal void RaiseDisplayRotationCapabilityUpdatedEvent(MediaControlNativeDisplayRotation rotations)
280 DisplayRotationCapabilityUpdated?.Invoke(this, new DisplayRotationCapabilityUpdatedEventArgs(rotations.ToPublicList()));
287 /// Occurs when the command is completed.
290 /// User can match the command and this event using <see cref="CommandCompletedEventArgs.RequestId"/> field.
292 /// <since_tizen> 5 </since_tizen>
293 internal event EventHandler<CommandCompletedEventArgs> CommandCompleted;
295 internal void RaiseCommandCompletedEvent(string requestId, int result, IntPtr bundleHandle)
297 if (bundleHandle != IntPtr.Zero)
299 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
303 CommandCompleted?.Invoke(this, new CommandCompletedEventArgs(requestId, result));
308 /// Occurs when a server sends custom event.
310 /// <since_tizen> 5 </since_tizen>
311 public event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
313 internal void RaiseCustomCommandReceivedEvent(CustomCommand command)
315 CustomCommandReceived?.Invoke(this, new CustomCommandReceivedEventArgs(command));