sync with private (add mm_player_get_track_count, seek check bug)
[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
31 _mmplayer_asm_register(MMPlayerASM* sm, ASM_sound_cb_t callback, void* param)
32 {
33         /* read mm-session type */
34         gint sessionType = MM_SESSION_TYPE_SHARE;
35         gint errorcode = MM_ERROR_NONE;
36         gint asm_handle = -1;
37         gint event_type = ASM_EVENT_NONE;
38         gint pid = -1;
39
40         debug_log("\n");
41
42         if ( ! sm )
43         {
44                 debug_error("invalid session handle\n");
45                 return MM_ERROR_PLAYER_NOT_INITIALIZED;
46         }
47
48         /* check if it's running on the media_server */
49         if ( sm->pid > 0 )
50         {
51                 pid = sm->pid;
52                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
53         }
54         else
55         {
56                 debug_log("no pid has assigned. using default(current) context\n");
57         }
58
59         /* read session type */
60         errorcode = _mm_session_util_read_type(pid, &sessionType);
61         if ( errorcode )
62         {
63                 debug_warning("Read MMSession Type failed. use default \"share\" type\n");
64                 sessionType = MM_SESSION_TYPE_SHARE;
65
66                 /* init session */
67                 errorcode = mm_session_init(sessionType);
68                 if ( errorcode )
69                 {
70                                 debug_critical("mm_session_init() failed\n");
71                                 return errorcode;
72                 }
73         }
74
75         /* check if it's CALL */
76         if ( sessionType == MM_SESSION_TYPE_CALL )
77         {
78                 debug_log("session type is CALL\n");
79                 sm->event = ASM_EVENT_CALL;
80                 return MM_ERROR_NONE;
81         }
82         else if ( sessionType == MM_SESSION_TYPE_VIDEOCALL )
83         {
84                 debug_log("session type is VIDEOCALL\n");
85                 sm->event = ASM_EVENT_VIDEOCALL;
86                 return MM_ERROR_NONE;
87         }
88
89         /* interpret session type */
90         event_type = __mmplayer_asm_get_event_type(sessionType);
91
92
93
94         /* register audio-session-manager callback */
95         if( ! ASM_register_sound(pid, &asm_handle, event_type, ASM_STATE_NONE, callback, (void*)param, ASM_RESOURCE_NONE, &errorcode))
96         {
97                 debug_critical("ASM_register_sound() failed\n");
98                 return errorcode;
99         }
100
101         /* now succeed to register our callback. take result */
102         sm->handle = asm_handle;
103         sm->state = ASM_STATE_NONE;
104         sm->event = event_type;
105
106         return MM_ERROR_NONE;
107 }
108
109 gint
110 _mmplayer_asm_deregister(MMPlayerASM* sm)
111 {
112         gint event_type = ASM_EVENT_NONE;
113         gint errorcode = 0;
114         gint pid = -1;
115
116         if ( ! sm )
117         {
118                 debug_error("invalid session handle\n");
119                 return MM_ERROR_PLAYER_NOT_INITIALIZED;
120         }
121
122         /* check if it's running on the media_server */
123         if ( sm->pid > 0 )
124         {
125                 pid = sm->pid;
126                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
127         }
128         else
129         {
130                 debug_log("no pid has assigned. using default(current) context\n");
131         }
132
133         /* check if it's CALL */
134         if(sm->event == ASM_EVENT_CALL || sm->event == ASM_EVENT_VIDEOCALL)
135         {
136                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sm->event); 
137                 return MM_ERROR_NONE;
138         }
139         event_type = sm->event;
140
141         if( ! ASM_unregister_sound( sm->handle, event_type, &errorcode) )
142         {
143                 debug_error("Unregister sound failed 0x%X\n", errorcode);
144                 return MM_ERROR_POLICY_INTERNAL;
145         }
146
147         return MM_ERROR_NONE;
148 }
149
150 gint _mmplayer_asm_set_state(MMHandleType hplayer, ASM_sound_states_t state)
151 {
152         gint event_type = ASM_EVENT_NONE;
153         gint pid = -1;
154         ASM_resource_t resource = ASM_RESOURCE_NONE;
155         mm_player_t *player = (mm_player_t *)hplayer;
156         MMPlayerASM* sm  = &player->sm;
157
158         if ( ! sm )
159         {
160                 debug_error("invalid session handle\n");
161                 return MM_ERROR_PLAYER_NOT_INITIALIZED;
162         }
163
164         /* check if it's running on the media_server */
165         if ( sm->pid > 0 )
166         {
167                 pid = sm->pid;
168                 debug_log("mm-player is running on different process. Just faking pid to [%d]. :-p\n", pid);
169         }
170         else
171         {
172                 debug_log("no pid has assigned. using default(current) context\n");
173         }
174
175         /* check if it's CALL */
176         if(sm->event == ASM_EVENT_CALL || sm->event == ASM_EVENT_VIDEOCALL)
177         {
178                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sm->event); 
179                 return MM_ERROR_NONE;
180         }
181
182
183         if ( ! sm->by_asm_cb )//|| sm->state == ASM_STATE_PLAYING )
184         {
185                 int ret = 0;
186                 event_type = sm->event;
187
188                 /* check if there is video */
189                 /* NOTE: resource can be set as NONE when it's not occupied or unknown resource is used. */
190                 if(ASM_STATE_PLAYING == state || ASM_STATE_PAUSE == state)
191                 {
192                         if(player->pipeline && player->pipeline->videobin)
193                                 resource = ASM_RESOURCE_VIDEO_OVERLAY | ASM_RESOURCE_HW_DECODER;
194                 }
195
196                 if( ! ASM_set_sound_state( sm->handle, event_type, state, resource, &ret) )
197                 {
198                         debug_error("Set state to [%d] failed 0x%X\n", state, ret);
199                         return MM_ERROR_POLICY_BLOCKED;
200                 }
201
202                 sm->state = state;
203         }
204         else
205         {
206                 sm->by_asm_cb = 0;
207                 sm->state = state;
208         }
209
210         return MM_ERROR_NONE;
211 }
212
213 static ASM_sound_events_t
214 __mmplayer_asm_get_event_type(gint type)
215 {
216         gint event_type = ASM_EVENT_NONE;
217
218         /* interpret session type */
219                 switch(type)
220                 {
221                         case MM_SESSION_TYPE_SHARE:
222                                 event_type = ASM_EVENT_SHARE_MMPLAYER;
223                         break;
224
225                         case MM_SESSION_TYPE_EXCLUSIVE:
226                                 event_type = ASM_EVENT_EXCLUSIVE_MMPLAYER;
227                         break;
228
229                         case MM_SESSION_TYPE_NOTIFY:
230                                 event_type = ASM_EVENT_NOTIFY;
231                         break;
232                         
233                         case MM_SESSION_TYPE_ALARM:
234                                 event_type = ASM_EVENT_ALARM;
235                         break;
236                         default:
237                                 debug_critical("unexpected case!\n");
238                                 g_assert(0);
239                 }
240
241                 return event_type;
242 }