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.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
27 private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
28 private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
29 private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
30 private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
31 private static Native.SimpleCommandReceivedCallback _subtitleModeCommandCallback;
32 private static Native.SimpleCommandReceivedCallback _mode360CommandCallback;
33 private static Native.DisplayModeCommandReceivedCallback _displayModeCommandCallback;
34 private static Native.DisplayRotationCommandReceivedCallback _displayRotationCommandCallback;
36 private static Native.CustomCommandReceivedCallback _customCommandCallback;
37 private static Native.SearchCommandReceivedCallback _searchCommandCallback;
38 private static Native.CommandCompletedCallback _commandCompletedCallback;
42 /// Occurs when a client sends playback command.
44 /// <since_tizen> 4 </since_tizen>
45 [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
46 public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
49 /// Occurs when a client sends playback command.
51 /// <since_tizen> 5 </since_tizen>
52 public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
55 /// Occurs when a client sends playback position command.
57 /// <since_tizen> 5 </since_tizen>
58 public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
61 /// Occurs when a client sends playlist command.
63 /// <since_tizen> 5 </since_tizen>
64 public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
67 /// Occurs when a client sends shuffle mode command.
69 /// <since_tizen> 5 </since_tizen>
70 public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
73 /// Occurs when a client sends repeat mode command.
75 /// <since_tizen> 5 </since_tizen>
76 public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
79 /// Occurs when a client sends subtitle mode command.
81 /// <since_tizen> 6 </since_tizen>
82 public static event EventHandler<SubtitleModeCommandReceivedEventArgs> SubtitleModeCommandReceived;
85 /// Occurs when a client sends mode 360 command.
87 /// <since_tizen> 6 </since_tizen>
88 public static event EventHandler<Mode360CommandReceivedEventArgs> Mode360CommandReceived;
91 /// Occurs when a client sends display mode command.
93 /// <since_tizen> 6 </since_tizen>
94 public static event EventHandler<DisplayModeCommandReceivedEventArgs> DisplayModeCommandReceived;
97 /// Occurs when a client sends display rotation command.
99 /// <since_tizen> 6 </since_tizen>
100 public static event EventHandler<DisplayRotationCommandReceivedEventArgs> DisplayRotationCommandReceived;
103 /// Occurs when a client sends custom command.
105 /// <since_tizen> 5 </since_tizen>
106 public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
109 /// Occurs when a client sends search command.
111 /// <since_tizen> 5 </since_tizen>
112 public static event EventHandler<SearchCommandReceivedEventArgs> SearchCommandReceived;
115 /// Occurs when a client sends custom command.
117 /// <since_tizen> 5 </since_tizen>
118 internal static event EventHandler<CommandCompletedEventArgs> CommandCompleted;
120 private static void RegisterPlaybackActionCommandReceivedEvent()
122 _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
124 // SendPlaybackCommand doesn't use requestId. It'll be removed in Level 7.
125 if (requestId == null)
127 #pragma warning disable CS0618 // Type or member is obsolete
128 PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCommand.ToPublic()));
129 #pragma warning restore CS0618 // Type or member is obsolete
133 var command = new PlaybackCommand(playbackCommand.ToPublic());
134 command.SetResponseInformation(clientName, requestId);
136 PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
139 Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
140 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
143 private static void RegisterPlaybackPositionCommandReceivedEvent()
145 _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
147 var command = new PlaybackPositionCommand(playbackPosition);
148 command.SetResponseInformation(clientName, requestId);
150 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
152 Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
153 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
156 private static void RegisterPlaylistCommandReceivedEvent()
158 _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
160 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
161 command.SetResponseInformation(clientName, requestId);
163 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
165 Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
166 ThrowIfError("Failed to init PlaylistCommandReceived event.");
169 private static void RegisterShuffleModeCommandReceivedEvent()
171 _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
173 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
174 command.SetResponseInformation(clientName, requestId);
176 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
178 Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
179 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
182 private static void RegisterRepeatModeCommandReceivedEvent()
184 _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
186 var command = new RepeatModeCommand(mode.ToPublic());
187 command.SetResponseInformation(clientName, requestId);
189 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
191 Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
192 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
195 private static void RegisterSubtitleModeCommandReceivedEvent()
197 _subtitleModeCommandCallback = (clientName, requestId, isEnabled, _) =>
199 var command = new SubtitleModeCommand(isEnabled);
200 command.SetResponseInformation(clientName, requestId);
202 SubtitleModeCommandReceived?.Invoke(null, new SubtitleModeCommandReceivedEventArgs(command));
204 Native.SetSubtitleModeCommandReceivedCb(Handle, _subtitleModeCommandCallback).
205 ThrowIfError("Failed to init SubtitleModeCommandReceived event.");
208 private static void RegisterMode360CommandReceivedEvent()
210 _mode360CommandCallback = (clientName, requestId, isEnabled, _) =>
212 var command = new Mode360Command(isEnabled);
213 command.SetResponseInformation(clientName, requestId);
215 Mode360CommandReceived?.Invoke(null, new Mode360CommandReceivedEventArgs(command));
217 Native.SetMode360CommandReceivedCb(Handle, _mode360CommandCallback).
218 ThrowIfError("Failed to init Mode360CommandReceived event.");
221 private static void RegisterDisplayModeCommandReceivedEvent()
223 _displayModeCommandCallback = (clientName, requestId, mode, _) =>
225 var command = new DisplayModeCommand(mode.ToPublic());
226 command.SetResponseInformation(clientName, requestId);
228 DisplayModeCommandReceived?.Invoke(null, new DisplayModeCommandReceivedEventArgs(command));
230 Native.SetDisplayModeCommandReceivedCb(Handle, _displayModeCommandCallback).
231 ThrowIfError("Failed to init DisplayModeCommandReceived event.");
234 private static void RegisterDisplayRotationCommandReceivedEvent()
236 _displayRotationCommandCallback = (clientName, requestId, rotation, _) =>
238 var command = new DisplayRotationCommand(rotation.ToPublic());
239 command.SetResponseInformation(clientName, requestId);
241 DisplayRotationCommandReceived?.Invoke(null, new DisplayRotationCommandReceivedEventArgs(command));
243 Native.SetDisplayRotationCommandReceivedCb(Handle, _displayRotationCommandCallback).
244 ThrowIfError("Failed to init DisplayRotationCommandReceived event.");
247 private static void RegisterCustomCommandReceivedEvent()
249 _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
251 CustomCommand command = null;
252 if (bundleHandle != IntPtr.Zero)
254 command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
258 command = new CustomCommand(customCommand);
261 command.SetResponseInformation(clientName, requestId);
263 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
265 Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
266 ThrowIfError("Failed to init CustomCommandReceived event.");
269 private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
271 var searchConditions = new List<MediaControlSearchCondition>();
273 Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
275 Bundle bundle = null;
277 if (bundleHandle != IntPtr.Zero)
279 bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
282 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
287 Native.ForeachSearchCondition(searchHandle, searchItemCallback).
288 ThrowIfError("Failed to get search items.");
290 return new SearchCommand(searchConditions, searchHandle);
293 private static void RegisterSearchCommandReceivedEvent()
295 _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
297 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
299 command.SetResponseInformation(clientName, requestId);
301 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
303 Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
304 ThrowIfError("Failed to init SearchCommandReceived event.");
307 private static void RegisterCommandCompletedEvent()
309 _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
311 if (bundleHandle != IntPtr.Zero)
313 CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
317 CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
320 Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
321 ThrowIfError("Failed to init RegisterEventCompletedEvent.");