merge with master
[platform/core/multimedia/libmm-player.git] / src / mm_player_asm.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <mm_debug.h>
23 #include <mm_error.h>
24 #include "mm_player_priv.h"
25 #include "mm_player_asm.h"
26
27 #define MMPLAYER_CHECK_SESSION_SKIP(x_player_asm) \
28 do \
29 { \
30         if (x_player_asm->skip_session == TRUE) \
31         { \
32                 debug_log("skip session"); \
33                 return MM_ERROR_NONE; \
34         } \
35 }while(0);
36
37 #define MMPLAYER_CHECK_SESSION_INSTANCE(x_player_asm) \
38 do \
39 { \
40         if (!x_player_asm) \
41         { \
42                 debug_log("no session instance");\
43                 return MM_ERROR_SOUND_NOT_INITIALIZED; \
44         } \
45 }while(0);
46
47 static ASM_sound_events_t __mmplayer_asm_get_event_type(gint type);
48
49 gint
50 _mmplayer_asm_register(MMPlayerASM* sm, ASM_sound_cb_t callback, void* param)
51 {
52         /* read mm-session type */
53         gint sessionType = MM_SESSION_TYPE_SHARE;
54         gint errorcode = MM_ERROR_NONE;
55         gint asm_handle = -1;
56         gint event_type = ASM_EVENT_NONE;
57         gint pid = -1;
58
59         debug_log("\n");
60         MMPLAYER_CHECK_SESSION_INSTANCE(sm);
61         MMPLAYER_CHECK_SESSION_SKIP(sm);
62
63         /* check if it's running on the media_server */
64         if ( sm->pid > 0 )
65         {
66                 pid = sm->pid;
67                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
68         }
69         else
70         {
71                 debug_log("no pid has assigned. using default(current) context\n");
72         }
73
74         /* read session type */
75         errorcode = _mm_session_util_read_type(pid, &sessionType);
76         if ( errorcode )
77         {
78                 debug_warning("Read MMSession Type failed. use default \"share\" type\n");
79                 sessionType = MM_SESSION_TYPE_SHARE;
80
81                 /* init session */
82                 errorcode = mm_session_init(sessionType);
83                 if ( errorcode )
84                 {
85                         debug_critical("mm_session_init() failed\n");
86                         return errorcode;
87                 }
88         }
89
90         /* check if it's CALL */
91         if ( sessionType == MM_SESSION_TYPE_CALL )
92         {
93                 debug_log("session type is CALL\n");
94                 sm->event = ASM_EVENT_CALL;
95                 return MM_ERROR_NONE;
96         }
97         else if ( sessionType == MM_SESSION_TYPE_VIDEOCALL )
98         {
99                 debug_log("session type is VIDEOCALL\n");
100                 sm->event = ASM_EVENT_VIDEOCALL;
101                 return MM_ERROR_NONE;
102         }
103
104         /* interpret session type */
105         event_type = __mmplayer_asm_get_event_type(sessionType);
106
107         /* register audio-session-manager callback */
108         if( ! ASM_register_sound(pid, &asm_handle, event_type, ASM_STATE_NONE, callback, (void*)param, ASM_RESOURCE_NONE, &errorcode))
109         {
110                 debug_critical("ASM_register_sound() failed\n");
111                 return errorcode;
112         }
113
114         /* now succeed to register our callback. take result */
115         sm->handle = asm_handle;
116         sm->state = ASM_STATE_NONE;
117         sm->event = event_type;
118
119         return MM_ERROR_NONE;
120 }
121
122 gint
123 _mmplayer_asm_unregister(MMPlayerASM* sm)
124 {
125         gint event_type = ASM_EVENT_NONE;
126         gint errorcode = 0;
127         gint pid = -1;
128
129         MMPLAYER_CHECK_SESSION_INSTANCE(sm);
130         MMPLAYER_CHECK_SESSION_SKIP(sm);
131
132         /* check if it's running on the media_server */
133         if ( sm->pid > 0 )
134         {
135                 pid = sm->pid;
136                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
137         }
138         else
139         {
140                 debug_log("no pid has assigned. using default(current) context\n");
141         }
142
143         /* check if it's CALL */
144         if(sm->event == ASM_EVENT_CALL || sm->event == ASM_EVENT_VIDEOCALL)
145         {
146                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sm->event); 
147                 return MM_ERROR_NONE;
148         }
149         event_type = sm->event;
150
151         if( ! ASM_unregister_sound( sm->handle, event_type, &errorcode) )
152         {
153                 debug_error("Unregister sound failed 0x%X\n", errorcode);
154                 return MM_ERROR_POLICY_INTERNAL;
155         }
156
157         return MM_ERROR_NONE;
158 }
159
160 gint _mmplayer_asm_set_state(MMHandleType hplayer, ASM_sound_states_t state)
161 {
162         gint event_type = ASM_EVENT_NONE;
163         gint pid = -1;
164         ASM_resource_t resource = ASM_RESOURCE_NONE;
165         mm_player_t *player = (mm_player_t *)hplayer;
166         MMPlayerASM* sm  = &player->sm;
167
168         MMPLAYER_CHECK_SESSION_INSTANCE(sm);
169         MMPLAYER_CHECK_SESSION_SKIP(sm);
170
171         /* check if it's running on the media_server */
172         if ( sm->pid > 0 )
173         {
174                 pid = sm->pid;
175                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
176         }
177         else
178         {
179                 debug_log("no pid has assigned. using default(current) context\n");
180         }
181
182         /* check if it's CALL */
183         if(sm->event == ASM_EVENT_CALL || sm->event == ASM_EVENT_VIDEOCALL)
184         {
185                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sm->event); 
186                 return MM_ERROR_NONE;
187         }
188
189
190         if ( ! sm->by_asm_cb )//|| sm->state == ASM_STATE_PLAYING )
191         {
192                 int ret = 0;
193                 event_type = sm->event;
194
195                 /* check if there is video */
196                 /* NOTE: resource can be set as NONE when it's not occupied or unknown resource is used. */
197                 if(ASM_STATE_PLAYING == state || ASM_STATE_PAUSE == state)
198                 {
199                         if(player->pipeline && player->pipeline->videobin)
200                                 resource = ASM_RESOURCE_VIDEO_OVERLAY | ASM_RESOURCE_HW_DECODER;
201                 }
202
203                 if( ! ASM_set_sound_state( sm->handle, event_type, state, resource, &ret) )
204                 {
205                         debug_error("Set state to [%d] failed 0x%X\n", state, ret);
206                         return MM_ERROR_POLICY_BLOCKED;
207                 }
208
209                 sm->state = state;
210         }
211         else
212         {
213                 sm->by_asm_cb = 0;
214                 sm->state = state;
215         }
216
217         return MM_ERROR_NONE;
218 }
219
220 static ASM_sound_events_t
221 __mmplayer_asm_get_event_type(gint type)
222 {
223         gint event_type = ASM_EVENT_NONE;
224
225         /* interpret session type */
226         switch(type)
227         {
228                 case MM_SESSION_TYPE_SHARE:
229                         event_type = ASM_EVENT_SHARE_MMPLAYER;
230                         break;
231
232                 case MM_SESSION_TYPE_EXCLUSIVE:
233                         event_type = ASM_EVENT_EXCLUSIVE_MMPLAYER;
234                         break;
235
236                 case MM_SESSION_TYPE_NOTIFY:
237                         event_type = ASM_EVENT_NOTIFY;
238                         break;
239
240                 case MM_SESSION_TYPE_ALARM:
241                         event_type = ASM_EVENT_ALARM;
242                         break;
243
244                 case MM_SESSION_TYPE_EMERGENCY:
245                         event_type = ASM_EVENT_EMERGENCY;
246                         break;
247
248                 default:
249                         debug_critical("unexpected case!\n");
250                         event_type = ASM_EVENT_SHARE_MMPLAYER;
251                         break;
252         }
253
254         return event_type;
255 }
256
257 gint
258 _mmplayer_asm_ignore_session(MMHandleType hplayer)
259 {
260         mm_player_t *player = (mm_player_t *)hplayer;
261
262         debug_fenter();
263
264         return_val_if_fail (player, MM_ERROR_PLAYER_NOT_INITIALIZED);
265
266         /* check state */
267         if (player->state != MM_PLAYER_STATE_NULL)
268         {
269                 debug_log("invalid state to make session mix");
270                 return MM_ERROR_PLAYER_INVALID_STATE;
271         }
272
273         if (player->sm.skip_session == FALSE && player->sm.handle)
274         {
275                 int error_code = 0;
276
277                 if (!ASM_unregister_sound(player->sm.handle, player->sm.event, &error_code))
278                 {
279                         debug_error("Unregister sound failed 0x%X", error_code);
280                         return MM_ERROR_POLICY_INTERNAL;
281                 }
282                 player->sm.skip_session = TRUE;
283                 player->sm.handle = 0;
284
285                 debug_log("session skip enabled");
286         }
287
288         debug_fleave();
289
290         return MM_ERROR_NONE;
291 }