2 * Copyright (c) 2018 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 Tizen.Applications;
19 using System.Collections.Generic;
20 using Native = Interop.MediaControllerServer;
22 namespace Tizen.Multimedia.Remoting
24 public static partial class MediaControlServer
26 private static Native.PlaybackStateCommandReceivedCallback _playbackCommandCallback;
27 private static Native.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
28 private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
29 private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
30 private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
31 private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
32 private static Native.CustomCommandReceivedCallback _customCommandCallback;
33 private static Native.SearchCommandReceivedCallback _searchCommandCallback;
34 private static Native.CommandCompletedCallback _commandCompletedCallback;
37 /// Occurs when a client sends playback command.
39 /// <since_tizen> 4 </since_tizen>
40 [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
41 public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
44 /// Occurs when a client sends playback command.
46 /// <since_tizen> 5 </since_tizen>
47 public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
50 /// Occurs when a client sends playback position command.
52 /// <since_tizen> 5 </since_tizen>
53 public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
56 /// Occurs when a client sends playlist command.
58 /// <since_tizen> 5 </since_tizen>
59 public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
62 /// Occurs when a client sends shuffle mode command.
64 /// <since_tizen> 5 </since_tizen>
65 public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
68 /// Occurs when a client sends repeat mode command.
70 /// <since_tizen> 5 </since_tizen>
71 public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
74 /// Occurs when a client sends custom command.
76 /// <since_tizen> 5 </since_tizen>
77 public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
80 /// Occurs when a client sends search command.
82 /// <since_tizen> 5 </since_tizen>
83 public static event EventHandler<SearchCommandReceivedEventArgs> SearchCommandReceived;
86 /// Occurs when a client sends custom command.
88 /// <since_tizen> 5 </since_tizen>
89 internal static event EventHandler<CommandCompletedEventArgs> CommandCompleted;
91 private static void RegisterPlaybackCommandReceivedEvent()
93 _playbackCommandCallback = (clientName, playbackCode, _) =>
95 PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCode.ToPublic()));
97 Native.SetPlaybackStateCommandReceivedCb(Handle, _playbackCommandCallback).
98 ThrowIfError("Failed to init PlaybackStateCommandReceived event.");
101 private static void RegisterPlaybackActionCommandReceivedEvent()
103 _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
105 var command = new PlaybackCommand(playbackCommand.ToPublic());
106 command.SetResponseInformation(clientName, requestId);
108 PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
110 Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
111 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
114 private static void RegisterPlaybackPositionCommandReceivedEvent()
116 _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
118 var command = new PlaybackPositionCommand(playbackPosition);
119 command.SetResponseInformation(clientName, requestId);
121 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
123 Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
124 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
127 private static void RegisterPlaylistCommandReceivedEvent()
129 _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
131 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
132 command.SetResponseInformation(clientName, requestId);
134 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
136 Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
137 ThrowIfError("Failed to init PlaylistCommandReceived event.");
140 private static void RegisterShuffleModeCommandReceivedEvent()
142 _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
144 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
145 command.SetResponseInformation(clientName, requestId);
147 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
149 Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
150 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
153 private static void RegisterRepeatModeCommandReceivedEvent()
155 _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
157 var command = new RepeatModeCommand(mode.ToPublic());
158 command.SetResponseInformation(clientName, requestId);
160 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
162 Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
163 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
166 private static void RegisterCustomCommandReceivedEvent()
168 _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
170 CustomCommand command = null;
171 if (bundleHandle != IntPtr.Zero)
173 command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
177 command = new CustomCommand(customCommand);
180 command.SetResponseInformation(clientName, requestId);
182 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
184 Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
185 ThrowIfError("Failed to init CustomCommandReceived event.");
188 private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
190 var searchConditions = new List<MediaControlSearchCondition>();
192 Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
194 Bundle bundle = null;
196 if (bundleHandle != IntPtr.Zero)
198 bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
201 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
206 Native.ForeachSearchCondition(searchHandle, searchItemCallback).
207 ThrowIfError("Failed to get search items.");
209 return new SearchCommand(searchConditions, searchHandle);
212 private static void RegisterSearchCommandReceivedEvent()
214 _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
216 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
218 command.SetResponseInformation(clientName, requestId);
220 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
222 Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
223 ThrowIfError("Failed to init SearchCommandReceived event.");
226 private static void RegisterCommandCompletedEvent()
228 _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
230 if (bundleHandle != IntPtr.Zero)
232 CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
236 CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
239 Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
240 ThrowIfError("Failed to init RegisterEventCompletedEvent.");