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.
18 using Tizen.Applications;
19 using Native = Interop.MediaControllerClient;
20 using NativePlaylist = Interop.MediaControllerPlaylist;
22 namespace Tizen.Multimedia.Remoting
24 public partial class MediaControllerManager
27 private Native.ServerUpdatedCallback _serverUpdatedCallback;
28 private Native.PlaybackUpdatedCallback _playbackUpdatedCallback;
29 private NativePlaylist.PlaylistUpdatedCallback _playlistUpdatedCallback;
30 private NativePlaylist.MetadataUpdatedCallback _metadataUpdatedCallback;
31 private Native.ShuffleModeUpdatedCallback _shufflemodeUpdatedCallback;
32 private Native.RepeatModeUpdatedCallback _repeatmodeUpdatedCallback;
33 private Native.BoolAttributeUpdatedCallback _subtitleModeUpdatedCallback;
34 private Native.BoolAttributeUpdatedCallback _mode360UpdatedCallback;
35 private Native.DisplayModeUpdatedCallback _displayModeUpdatedCallback;
36 private Native.DisplayRotationUpdatedCallback _displayRotationUpdatedCallback;
38 // Capability updated event
39 private Native.PlaybackCapabilityUpdatedCallback _playbackCapabilityUpdatedCallback;
40 private Native.SimpleCapabilityUpdatedCallback _categoryCapabilityUpdatedCallback;
41 private Native.CategoryAttributeCapabilityUpdatedCallback _displayModeCapabilityUpdatedCallback;
42 private Native.CategoryAttributeCapabilityUpdatedCallback _displayRotationCapabilityUpdatedCallback;
45 private Native.CommandCompletedCallback _commandCompletedCallback;
46 private Native.CustomCommandReceivedCallback _customEventReceivedCallback;
50 /// Occurs when a server is started.
52 /// <since_tizen> 4 </since_tizen>
53 public event EventHandler<MediaControlServerStartedEventArgs> ServerStarted;
56 /// Occurs when a server is stopped.
58 /// <since_tizen> 4 </since_tizen>
59 public event EventHandler<MediaControlServerStoppedEventArgs> ServerStopped;
61 private void InitializeEvents()
63 RegisterPlaybackUpdatedEvent();
64 RegisterServerUpdatedEvent();
65 RegisterMetadataUpdatedEvent();
66 RegisterShuffleModeUpdatedEvent();
67 RegisterRepeatModeUpdatedEvent();
68 RegisterPlaylistUpdatedEvent();
69 RegisterSubtitleModeUpdateEvent();
70 RegisterMode360UpdateEvent();
71 RegisterDisplayModeUpdateEvent();
72 RegisterDisplayRotationUpdateEvent();
74 RegisterPlaybackCapabilitiesEvent();
75 RegisterDisplayModeCapabilityUpdatedEvent();
76 RegisterDisplayRotationCapabilityUpdatedEvent();
77 RegisterSimpleCapabilityUpdatedEvent();
79 RegisterCommandCompletedEvent();
80 RegisterCustomCommandReceivedEvent();
83 private void RaiseServerChangedEvent(MediaControllerNativeServerState state, MediaController controller)
85 if (controller == null)
90 if (state == MediaControllerNativeServerState.Activated)
92 ServerStarted?.Invoke(this, new MediaControlServerStartedEventArgs(controller));
96 controller.RaiseStoppedEvent();
97 ServerStopped?.Invoke(this, new MediaControlServerStoppedEventArgs(controller.ServerAppId));
102 #region Updated event
103 private void RegisterServerUpdatedEvent()
105 _serverUpdatedCallback = (serverName, state, _) =>
107 RaiseServerChangedEvent(state, HandleServerUpdated(serverName, state));
110 Native.SetServerUpdatedCb(Handle, _serverUpdatedCallback).
111 ThrowIfError("Failed to register server changed event.");
114 private void RegisterPlaybackUpdatedEvent()
116 _playbackUpdatedCallback = (serverName, playbackHandle, _) =>
118 GetController(serverName)?.RaisePlaybackUpdatedEvent(playbackHandle);
121 Native.SetPlaybackUpdatedCb(Handle, _playbackUpdatedCallback).
122 ThrowIfError("Failed to register PlaybackUpdated event.");
125 private void RegisterPlaylistUpdatedEvent()
127 _playlistUpdatedCallback = (serverName, playlistMode, name, playlistHandle, _) =>
129 GetController(serverName)?.RaisePlaylistUpdatedEvent(playlistMode, name, playlistHandle);
132 NativePlaylist.SetPlaylistModeUpdatedCb(Handle, _playlistUpdatedCallback).
133 ThrowIfError("Failed to register PlaylistUpdated event.");
136 private void RegisterMetadataUpdatedEvent()
138 _metadataUpdatedCallback = (serverName, metadata, _) =>
140 GetController(serverName)?.RaiseMetadataUpdatedEvent(metadata);
143 NativePlaylist.SetMetadataUpdatedCb(Handle, _metadataUpdatedCallback).
144 ThrowIfError("Failed to register MetadataUpdated event.");
147 private void RegisterShuffleModeUpdatedEvent()
149 _shufflemodeUpdatedCallback = (serverName, shuffleMode, _) =>
151 GetController(serverName)?.RaiseShuffleModeUpdatedEvent(shuffleMode);
154 Native.SetShuffleModeUpdatedCb(Handle, _shufflemodeUpdatedCallback).
155 ThrowIfError("Failed to register ShuffleModeUpdated event.");
158 private void RegisterRepeatModeUpdatedEvent()
160 _repeatmodeUpdatedCallback = (serverName, repeatMode, _) =>
162 GetController(serverName)?.RaiseRepeatModeUpdatedEvent(repeatMode.ToPublic());
165 Native.SetRepeatModeUpdatedCb(Handle, _repeatmodeUpdatedCallback).
166 ThrowIfError("Failed to register RepeatModeUpdated event.");
169 private void RegisterSubtitleModeUpdateEvent()
171 _subtitleModeUpdatedCallback = (serverName, isEnabled, _) =>
173 GetController(serverName)?.RaiseSubtitleModeUpdatedEvent(isEnabled);
176 Native.SetSubtitleUpdatedCb(Handle, _subtitleModeUpdatedCallback).
177 ThrowIfError("Failed to register SubtitleModeUpdated event.");
180 private void RegisterMode360UpdateEvent()
182 _mode360UpdatedCallback = (serverName, isEnabled, _) =>
184 GetController(serverName)?.RaiseMode360UpdatedEvent(isEnabled);
187 Native.SetMode360UpdatedCb(Handle, _mode360UpdatedCallback).
188 ThrowIfError("Failed to register Mode360Updated event.");
191 private void RegisterDisplayModeUpdateEvent()
193 _displayModeUpdatedCallback = (serverName, mode, _) =>
195 GetController(serverName)?.RaiseDisplayModeUpdatedEvent(mode);
198 Native.SetDisplayModeUpdatedCb(Handle, _displayModeUpdatedCallback).
199 ThrowIfError("Failed to register DisplayModeUpdated event.");
202 private void RegisterDisplayRotationUpdateEvent()
204 _displayRotationUpdatedCallback = (serverName, rotation, _) =>
206 GetController(serverName)?.RaiseDisplayRotationUpdatedEvent(rotation);
209 Native.SetDisplayRotationUpdatedCb(Handle, _displayRotationUpdatedCallback).
210 ThrowIfError("Failed to register DisplayRotationUpdated event.");
216 private void RegisterCommandCompletedEvent()
218 _commandCompletedCallback = (serverName, requestId, result, bundleHandle, _) =>
220 // SafeHandles cannot be marshaled from unmanaged to managed.
221 // So we use IntPtr type for 'bundleHandle' in native callback.
222 GetController(serverName)?.RaiseCommandCompletedEvent(requestId, result, bundleHandle);
225 Native.SetCommandCompletedCb(Handle, _commandCompletedCallback).
226 ThrowIfError("Failed to register CommandCompleted event.");
229 private void RegisterCustomCommandReceivedEvent()
231 _customEventReceivedCallback = (serverName, requestId, customEvent, bundleHandle, _) =>
233 CustomCommand command = null;
234 if (bundleHandle != IntPtr.Zero)
236 command = new CustomCommand(customEvent, new Bundle(new SafeBundleHandle(bundleHandle, true)));
240 command = new CustomCommand(customEvent);
243 command.SetResponseInformation(serverName, requestId);
245 GetController(serverName)?.RaiseCustomCommandReceivedEvent(command);
248 Native.SetCustomEventCb(Handle, _customEventReceivedCallback).
249 ThrowIfError("Failed to register CustomCommandReceived event.");
254 #region Capability updated event
255 private void RegisterPlaybackCapabilitiesEvent()
257 _playbackCapabilityUpdatedCallback = (serverName, playbackCapaHandle, _) =>
259 GetController(serverName)?.RaisePlaybackCapabilityUpdatedEvent(playbackCapaHandle);
262 Native.SetPlaybackCapabilityUpdatedCb(Handle, _playbackCapabilityUpdatedCallback).
263 ThrowIfError("Failed to register PlaybackCapabilityUpdated event.");
266 private void RegisterDisplayModeCapabilityUpdatedEvent()
268 _displayModeCapabilityUpdatedCallback = (serverName, modes, _) =>
270 GetController(serverName)?.RaiseDisplayModeCapabilityUpdatedEvent(
271 (MediaControlNativeDisplayMode)modes);
274 Native.SetDisplayModeCapabilityUpdatedCb(Handle, _displayModeCapabilityUpdatedCallback).
275 ThrowIfError("Failed to register DisplayModeCapabilityUpdated event.");
278 private void RegisterDisplayRotationCapabilityUpdatedEvent()
280 _displayRotationCapabilityUpdatedCallback = (serverName, rotations, _) =>
282 GetController(serverName)?.RaiseDisplayRotationCapabilityUpdatedEvent(
283 (MediaControlNativeDisplayRotation)rotations);
286 Native.SetDisplayRotationCapabilityUpdatedCb(Handle, _displayRotationCapabilityUpdatedCallback).
287 ThrowIfError("Failed to register DisplayRotationCapabilityUpdated event.");
290 private void RegisterSimpleCapabilityUpdatedEvent()
292 _categoryCapabilityUpdatedCallback = (serverName, category, support, _) =>
296 case MediaControlNativeCapabilityCategory.Repeat:
297 GetController(serverName)?.RaiseRepeatModeCapabilityUpdatedEvent(support);
299 case MediaControlNativeCapabilityCategory.Shuffle:
300 GetController(serverName)?.RaiseShuffleModeCapabilityUpdatedEvent(support);
305 Native.SetCategoryCapabilityUpdatedCb(Handle, _categoryCapabilityUpdatedCallback).
306 ThrowIfError("Failed to register capability updated event.");