[MediaController] Add new APIs for sending command and its callback (#396)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaControlServer.Events.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using Tizen.Applications;
18 using System;
19 using Native = Interop.MediaControllerServer;
20
21 namespace Tizen.Multimedia.Remoting
22 {
23     public static partial class MediaControlServer
24     {
25         private static Native.PlaybackStateCommandReceivedCallback _playbackCommandCallback;
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.CustomCommandReceivedCallback _customCommandCallback;
32
33         /// <summary>
34         /// Occurs when a client sends playback command.
35         /// </summary>
36         /// <since_tizen> 4 </since_tizen>
37         [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
38         public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
39
40         /// <summary>
41         /// Occurs when a client sends playback command.
42         /// </summary>
43         /// <since_tizen> 5 </since_tizen>
44         public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
45
46         /// <summary>
47         /// Occurs when a client sends playback position command.
48         /// </summary>
49         /// <since_tizen> 5 </since_tizen>
50         public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
51
52         /// <summary>
53         /// Occurs when a client sends playlist command.
54         /// </summary>
55         /// <since_tizen> 5 </since_tizen>
56         public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
57
58         /// <summary>
59         /// Occurs when a client sends shuffle mode command.
60         /// </summary>
61         /// <since_tizen> 5 </since_tizen>
62         public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
63
64         /// <summary>
65         /// Occurs when a client sends repeat mode command.
66         /// </summary>
67         /// <since_tizen> 5 </since_tizen>
68         public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
69
70         /// <summary>
71         /// Occurs when a client sends custom command.
72         /// </summary>
73         /// <since_tizen> 5 </since_tizen>
74         public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
75
76         private static void RegisterPlaybackCommandReceivedEvent()
77         {
78             _playbackCommandCallback = (clientName, playbackCode, _) =>
79             {
80                 PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCode.ToPublic()));
81             };
82             Native.SetPlaybackStateCommandReceivedCb(Handle, _playbackCommandCallback).
83                 ThrowIfError("Failed to init PlaybackStateCommandReceived event.");
84         }
85
86         private static void RegisterPlaybackActionCommandReceivedEvent()
87         {
88             _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
89             {
90                 var command = new PlaybackCommand(playbackCommand.ToPublic());
91                 command.SetClientInfo(clientName, requestId);
92
93                 PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
94             };
95             Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
96                 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
97         }
98
99         private static void RegisterPlaybackPositionCommandReceivedEvent()
100         {
101             _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
102             {
103                 var command = new PlaybackPositionCommand(playbackPosition);
104                 command.SetClientInfo(clientName, requestId);
105
106                 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
107             };
108             Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
109                 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
110         }
111
112         private static void RegisterPlaylistCommandReceivedEvent()
113         {
114             _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
115             {
116                 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
117                 command.SetClientInfo(clientName, requestId);
118
119                 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
120             };
121             Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
122                 ThrowIfError("Failed to init PlaylistCommandReceived event.");
123         }
124
125         private static void RegisterShuffleModeCommandReceivedEvent()
126         {
127             _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
128             {
129                 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
130                 command.SetClientInfo(clientName, requestId);
131
132                 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
133             };
134             Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
135                 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
136         }
137
138         private static void RegisterRepeatModeCommandReceivedEvent()
139         {
140             _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
141             {
142                 var command = new RepeatModeCommand(mode.ToPublic());
143                 command.SetClientInfo(clientName, requestId);
144
145                 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
146             };
147             Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
148                 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
149         }
150
151         private static void RegisterCustomCommandReceivedEvent()
152         {
153             _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
154             {
155                 CustomCommand command = null;
156                 if (bundleHandle != IntPtr.Zero)
157                 {
158                     command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
159                 }
160                 else
161                 {
162                     command = new CustomCommand(customCommand);
163                 }
164
165                 command.SetClientInfo(clientName, requestId);
166
167                 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
168             };
169             Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
170                 ThrowIfError("Failed to init CustomCommandReceived event.");
171         }
172     }
173 }