add new internal api at wearable profile too.
[platform/core/api/player.git] / src / player_internal.c
1 /*
2 * Copyright (c) 2011 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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <muse_core.h>
21 #include <muse_core_msg_json.h>
22 #include <muse_core_ipc.h>
23 #include <mm_error.h>
24 #include <dlog.h>
25 #include <Evas.h>
26 #include <Ecore_Evas.h>
27 #ifdef HAVE_WAYLAND
28 #include <Ecore_Wayland.h>
29 #endif
30 #include <muse_player.h>
31 #include <muse_player_msg.h>
32 #include "player_private.h"
33 #include "player_internal.h"
34
35 int player_set_pcm_extraction_mode(player_h player, bool sync, player_audio_pcm_extraction_cb callback, void *user_data)
36 {
37         PLAYER_INSTANCE_CHECK(player);
38         PLAYER_NULL_ARG_CHECK(callback);
39         int ret = PLAYER_ERROR_NONE;
40         muse_player_api_e api = MUSE_PLAYER_API_SET_PCM_EXTRACTION_MODE;
41         player_cli_s *pc = (player_cli_s *) player;
42         char *ret_buf = NULL;
43         muse_player_event_e event = MUSE_PLAYER_EVENT_TYPE_AUDIO_FRAME;
44
45         LOGD("ENTER");
46
47         player_msg_send1(api, pc, ret_buf, ret, INT, sync);
48
49         if (ret == PLAYER_ERROR_NONE) {
50                 pc->cb_info->user_cb[event] = callback;
51                 pc->cb_info->user_data[event] = user_data;
52                 LOGI("Event type : %d ", event);
53         }
54
55         g_free(ret_buf);
56         return ret;
57 }
58
59 int player_set_pcm_spec(player_h player, const char *format, int samplerate, int channel)
60 {
61         PLAYER_INSTANCE_CHECK(player);
62         int ret = PLAYER_ERROR_NONE;
63         muse_player_api_e api = MUSE_PLAYER_API_SET_PCM_SPEC;
64         player_cli_s *pc = (player_cli_s *) player;
65         char *ret_buf = NULL;
66
67         LOGD("ENTER");
68
69         player_msg_send3(api, pc, ret_buf, ret, STRING, format, INT, samplerate, INT, channel);
70
71         g_free(ret_buf);
72         return ret;
73 }
74
75 int player_set_streaming_playback_rate(player_h player, float rate)
76 {
77         PLAYER_INSTANCE_CHECK(player);
78         int ret = PLAYER_ERROR_NONE;
79         muse_player_api_e api = MUSE_PLAYER_API_SET_STREAMING_PLAYBACK_RATE;
80         player_cli_s *pc = (player_cli_s *) player;
81         char *ret_buf = NULL;
82
83         LOGD("ENTER");
84
85         player_msg_send1(api, pc, ret_buf, ret, DOUBLE, rate);
86         g_free(ret_buf);
87         return ret;
88 }
89
90 int player_set_media_stream_buffer_status_cb_ex(player_h player, player_stream_type_e stream_type, player_media_stream_buffer_status_cb_ex callback, void *user_data)
91 {
92         PLAYER_INSTANCE_CHECK(player);
93         PLAYER_NULL_ARG_CHECK(callback);
94         int ret = PLAYER_ERROR_NONE;
95         player_cli_s *pc = (player_cli_s *) player;
96         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
97         char *ret_buf = NULL;
98         muse_player_event_e type;
99         int set = 1;
100
101         LOGD("ENTER");
102
103         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
104                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS_WITH_INFO;
105         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
106                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS_WITH_INFO;
107         else {
108                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
109                 return PLAYER_ERROR_INVALID_PARAMETER;
110         }
111
112         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
113
114         if (ret == PLAYER_ERROR_NONE) {
115                 pc->cb_info->user_cb[type] = callback;
116                 pc->cb_info->user_data[type] = user_data;
117                 LOGI("Event type : %d ", type);
118         }
119
120         g_free(ret_buf);
121         return ret;
122 }
123
124 static void set_null_user_cb(callback_cb_info_s * cb_info, muse_player_event_e event)
125 {
126         if (cb_info && event < MUSE_PLAYER_EVENT_TYPE_NUM) {
127                 cb_info->user_cb[event] = NULL;
128                 cb_info->user_data[event] = NULL;
129         }
130 }
131
132 static void set_null_user_cb_lock(callback_cb_info_s * cb_info, muse_player_event_e event)
133 {
134         bool lock = g_thread_self() != cb_info->event_queue.thread;
135
136         if (lock)
137                 g_mutex_lock(&cb_info->event_queue.mutex);
138
139         set_null_user_cb(cb_info, event);
140
141         if (lock)
142                 g_mutex_unlock(&cb_info->event_queue.mutex);
143 }
144
145 int player_unset_media_stream_buffer_status_cb_ex(player_h player, player_stream_type_e stream_type)
146 {
147         PLAYER_INSTANCE_CHECK(player);
148         int ret = PLAYER_ERROR_NONE;
149         player_cli_s *pc = (player_cli_s *) player;
150         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
151         char *ret_buf = NULL;
152         muse_player_event_e type;
153         int set = 0;
154
155         LOGD("ENTER");
156
157         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
158                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS_WITH_INFO;
159         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
160                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS_WITH_INFO;
161         else {
162                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
163                 return PLAYER_ERROR_INVALID_PARAMETER;
164         }
165
166         set_null_user_cb_lock(pc->cb_info, type);
167
168         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
169
170         g_free(ret_buf);
171         return ret;
172 }
173
174 static void __evas_resize_cb(void *data, Evas * e, Evas_Object * eo, void *event_info)
175 {
176         player_cli_s *pc = (player_cli_s *) data;
177         wl_win_msg_type wl_win;
178         char *wl_win_msg = (char *)&wl_win;
179         char *ret_buf = NULL;
180         int rotation;
181         Ecore_Evas *ecore_evas;
182         muse_player_api_e api = MUSE_PLAYER_API_RESIZE_VIDEO_RENDER_RECT;
183         int ret = PLAYER_ERROR_NONE;
184         LOGD("ret =%d", ret);
185
186         evas_object_geometry_get(eo, &wl_win.wl_window_x, &wl_win.wl_window_y, &wl_win.wl_window_width, &wl_win.wl_window_height);
187         ecore_evas = ecore_evas_ecore_evas_get(e);
188         rotation = ecore_evas_rotation_get(ecore_evas);
189         LOGD("rotation(%d)", rotation);
190         LOGD("get window rectangle: x(%d) y(%d) width(%d) height(%d)",
191                         wl_win.wl_window_x, wl_win.wl_window_y, wl_win.wl_window_width, wl_win.wl_window_height);
192         if (rotation == 270 || rotation == 90) {
193                 LOGD("swap w and h");
194                 int temp;
195                 temp = wl_win.wl_window_width;
196                 wl_win.wl_window_width = wl_win.wl_window_height;
197                 wl_win.wl_window_height = temp;
198         }
199         LOGD("get window rectangle: x(%d) y(%d) width(%d) height(%d)",
200                         wl_win.wl_window_x, wl_win.wl_window_y, wl_win.wl_window_width, wl_win.wl_window_height);
201         wl_win.type = 0;                        /*init  but not use */
202         wl_win.wl_surface_id = 0;       /*init  but not use */
203
204         player_msg_send_array(api, pc, ret_buf, ret, wl_win_msg, sizeof(wl_win_msg_type), sizeof(char));
205
206         g_free(ret_buf);
207         return;
208
209 }
210
211 static void __evas_del_cb(void *data, Evas * e, Evas_Object * eo, void *event_info)
212 {
213
214         player_cli_s *pc = (player_cli_s *) data;
215
216         evas_object_event_callback_del(eo, EVAS_CALLBACK_RESIZE, __evas_resize_cb);
217         evas_object_event_callback_del(eo, EVAS_CALLBACK_DEL, __evas_del_cb);
218
219         LOGD("evas callback del %p", eo);
220         pc->have_evas_callback = FALSE;
221
222         return;
223 }
224
225 int player_set_evas_object_cb(player_h player, Evas_Object * eo)
226 {
227
228         PLAYER_INSTANCE_CHECK(player);
229         return_val_if_fail(eo != NULL, MM_ERROR_INVALID_ARGUMENT);
230
231         player_cli_s *pc = (player_cli_s *) player;
232
233         if (pc->have_evas_callback && pc->eo == eo) {
234                 LOGW("evas object had callback already %p", pc->eo);
235                 return MM_ERROR_UNKNOWN;
236         }
237         pc->eo = eo;
238         evas_object_event_callback_add(eo, EVAS_CALLBACK_RESIZE, __evas_resize_cb, player);
239         evas_object_event_callback_add(eo, EVAS_CALLBACK_DEL, __evas_del_cb, player);
240         LOGD("evas callback add %p", pc->eo);
241         pc->have_evas_callback = TRUE;
242
243         return MM_ERROR_NONE;
244 }
245
246 int player_unset_evas_object_cb(player_h player)
247 {
248         PLAYER_INSTANCE_CHECK(player);
249         player_cli_s *pc = (player_cli_s *) player;
250         return_val_if_fail(pc->eo != NULL, MM_ERROR_INVALID_ARGUMENT);
251
252         evas_object_event_callback_del(pc->eo, EVAS_CALLBACK_RESIZE, __evas_resize_cb);
253         evas_object_event_callback_del(pc->eo, EVAS_CALLBACK_DEL, __evas_del_cb);
254         LOGD("evas callback del %p", pc->eo);
255         pc->eo = NULL;
256         pc->have_evas_callback = FALSE;
257
258         return MM_ERROR_NONE;
259 }
260
261 #ifdef HAVE_WAYLAND
262 int player_set_ecore_wl_display(player_h player, player_display_type_e type, Ecore_Wl_Window *ecore_wl_window, int x, int y, int  width, int height)
263 {
264         PLAYER_INSTANCE_CHECK(player);
265         int ret = PLAYER_ERROR_NONE;
266         muse_player_api_e api = MUSE_PLAYER_API_SET_DISPLAY;
267         player_cli_s *pc = (player_cli_s *) player;
268         char *ret_buf = NULL;
269         wl_win_msg_type wl_win;
270         char *wl_win_msg = (char *)&wl_win;
271         unsigned int wl_surface_id;
272         struct wl_surface *wl_surface;
273         struct wl_display *wl_display;
274         Ecore_Wl_Window *wl_window = NULL;
275
276         LOGD("ENTER");
277         if (type !=PLAYER_DISPLAY_TYPE_OVERLAY) {
278                 LOGE("Display type(%d) is not overlay", type);
279                 return PLAYER_ERROR_INVALID_PARAMETER;
280         }
281         if (!ecore_wl_window)
282                 return PLAYER_ERROR_INVALID_PARAMETER;
283
284         LOGI("Wayland overlay surface type");
285         LOGD("Ecore Wayland Window handle(%p), size (%d, %d, %d, %d)", ecore_wl_window, x, y, width, height);
286         wl_window = ecore_wl_window;
287
288         /* set wl_win */
289         wl_win.type = type;
290         wl_win.wl_window_x = x;
291         wl_win.wl_window_y = y;
292         wl_win.wl_window_width = width;
293         wl_win.wl_window_height = height;
294
295         wl_surface = (struct wl_surface *)ecore_wl_window_surface_get(wl_window);
296         /* get wl_display */
297         wl_display = (struct wl_display *)ecore_wl_display_get();
298
299         if (!pc->wlclient) {
300                 ret = _wlclient_create(&pc->wlclient);
301                 if (ret != MM_ERROR_NONE) {
302                         LOGE("Wayland client create failure");
303                         return ret;
304                 }
305         }
306
307         if (wl_surface && wl_display) {
308                 LOGD("surface = %p, wl_display = %p", wl_surface, wl_display);
309                 wl_surface_id = _wlclient_get_wl_window_wl_surface_id(pc->wlclient, wl_surface, wl_display);
310                 LOGD("wl_surface_id = %d", wl_surface_id);
311                 wl_win.wl_surface_id = wl_surface_id;
312                 LOGD("wl_win.wl_surface_id = %d", wl_win.wl_surface_id);
313         } else {
314                 LOGE("Fail to get wl_surface or wl_display");
315                 return PLAYER_ERROR_INVALID_OPERATION;
316         }
317
318         if (pc->wlclient) {
319                 g_free(pc->wlclient);
320                 pc->wlclient = NULL;
321         }
322
323         player_msg_send_array(api, pc, ret_buf, ret, wl_win_msg, sizeof(wl_win_msg_type), sizeof(char));
324
325         g_free(ret_buf);
326
327         return ret;
328 }
329
330 #endif
331