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