Git init
[framework/multimedia/libmm-radio.git] / src / mm_radio_asm.c
1 /*
2  * libmm-radio
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@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 <assert.h>
23 #include <mm_debug.h>
24 #include "mm_radio_asm.h"
25
26 static ASM_sound_events_t __mmradio_asm_get_event_type(int type);
27
28 int mmradio_asm_register(MMRadioASM* sm, ASM_sound_cb_t callback, void* param)
29 {
30         /* read mm-session type */
31         int sessionType = MM_SESSION_TYPE_SHARE;
32         int errorcode = MM_ERROR_NONE;
33         int asm_handle = -1;
34         int event_type = ASM_EVENT_NONE;
35         int pid = -1;
36
37         debug_log("\n");
38
39         if ( ! sm )
40         {
41                 debug_error("invalid session handle\n");
42                 return MM_ERROR_RADIO_NOT_INITIALIZED;
43         }
44
45         /* read session type */
46 //      errorcode = __MMSessionUtilReadType(&sessionType);
47         errorcode = _mm_session_util_read_type(-1, &sessionType);
48         if ( errorcode )
49         {
50                 debug_warning("Read MMSession Type failed. use default \"exclusive\" type\n");
51                 sessionType = MM_SESSION_TYPE_EXCLUSIVE;
52
53                 /* init session */
54                 errorcode = mm_session_init(sessionType);
55                 if ( errorcode )
56                 {
57                                 debug_critical("mm_session_init() failed\n");
58                                 return errorcode;
59                 }
60         }
61
62         /* check if it's CALL */
63         if ( sessionType == MM_SESSION_TYPE_CALL || sessionType == MM_SESSION_TYPE_VIDEOCALL)
64         {
65                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sessionType);
66                 return MM_ERROR_NONE;
67         }
68
69         /* interpret session type */
70         event_type = __mmradio_asm_get_event_type(sessionType);
71
72         /* check if it's running on the media_server */
73         if ( sm->pid > 0 )
74         {
75                 pid = sm->pid;
76                 debug_log("mm-radio is running on different process. Just faking pid to [%d]. :-p\n", pid);
77         }
78         else
79         {
80                 debug_log("no pid has assigned. using default(current) context\n");
81         }
82
83         /* register audio-session-manager callback */
84         if( ! ASM_register_sound(pid, &asm_handle, event_type, ASM_STATE_NONE, callback, (void*)param, ASM_RESOURCE_NONE, &errorcode))
85         {
86                 debug_critical("ASM_register_sound() failed\n");
87                 return errorcode;
88         }
89
90         /* now succeded to register our callback. take result */
91         sm->handle = asm_handle;
92         sm->state = ASM_STATE_NONE;
93
94         return MM_ERROR_NONE;
95 }
96
97 int mmradio_asm_deregister(MMRadioASM* sm)
98 {
99         int sessionType = MM_SESSION_TYPE_SHARE;
100         int event_type = ASM_EVENT_NONE;
101         int errorcode = 0;
102
103         if ( ! sm )
104         {
105                 debug_error("invalid session handle\n");
106                 return MM_ERROR_RADIO_NOT_INITIALIZED;
107         }
108
109         /* read session type */
110 //      errorcode = __MMSessionUtilReadType(&sessionType);
111         errorcode = _mm_session_util_read_type(-1, &sessionType);
112         if ( errorcode )
113         {
114                 debug_error("MMSessionReadType Fail %s\n",__func__);
115                 return MM_ERROR_POLICY_INTERNAL;
116         }
117
118         /* check if it's CALL */
119         if ( sessionType == MM_SESSION_TYPE_CALL || sessionType == MM_SESSION_TYPE_VIDEOCALL)
120         {
121                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sessionType);
122                 return MM_ERROR_NONE;
123         }
124
125         /* interpret session type */
126         event_type = __mmradio_asm_get_event_type(sessionType);
127
128         if( ! ASM_unregister_sound( sm->handle, event_type, &errorcode) )
129         {
130                 debug_error("Unregister sound failed 0x%X\n", errorcode);
131                 return MM_ERROR_POLICY_INTERNAL;
132         }
133
134         return MM_ERROR_NONE;
135 }
136
137 int mmradio_asm_set_state(MMRadioASM* sm, ASM_sound_states_t state, ASM_resource_t resource)
138 {
139         int sessionType = MM_SESSION_TYPE_SHARE;
140         int event_type = ASM_EVENT_NONE;
141         int errorcode = 0;
142
143         if ( ! sm )
144         {
145                 debug_error("invalid session handle\n");
146                 return MM_ERROR_RADIO_NOT_INITIALIZED;
147         }
148
149         /* read session type */
150 //      errorcode = __MMSessionUtilReadType(&sessionType);
151         errorcode = _mm_session_util_read_type(-1, &sessionType);
152         if ( errorcode )
153         {
154                 debug_error("MMSessionReadType Fail\n");
155                 return MM_ERROR_POLICY_INTERNAL;
156         }
157
158         /* check if it's CALL */
159         if ( sessionType == MM_SESSION_TYPE_CALL || sessionType == MM_SESSION_TYPE_VIDEOCALL )
160         {
161                 debug_log("session type is VOICE or VIDEO CALL (%d)\n", sessionType);
162                 return MM_ERROR_NONE;
163         }
164
165         if ( sm->by_asm_cb == MMRADIO_ASM_CB_NONE ) //|| sm->state == ASM_STATE_PLAYING )
166         {
167                 int ret = 0;
168
169                 event_type = __mmradio_asm_get_event_type(sessionType);
170
171                 if( ! ASM_set_sound_state( sm->handle, event_type, state, resource, &ret) )
172                 {
173                         debug_error("set ASM state to [%d] failed 0x%X\n", state, ret);
174                         return MM_ERROR_POLICY_BLOCKED;
175                 }
176
177                 sm->state = state;
178         }
179         else // by asm callback
180         {
181                 sm->by_asm_cb = MMRADIO_ASM_CB_NONE;
182                 sm->state = state;
183         }
184
185         return MM_ERROR_NONE;
186 }
187
188
189 ASM_sound_events_t __mmradio_asm_get_event_type(int type)
190 {
191         int event_type = ASM_EVENT_NONE;
192
193         /* interpret session type */
194         switch(type)
195         {
196                 case MM_SESSION_TYPE_SHARE:
197                         event_type = ASM_EVENT_SHARE_FMRADIO;
198                 break;
199
200                 case MM_SESSION_TYPE_EXCLUSIVE:
201                         event_type = ASM_EVENT_EXCLUSIVE_FMRADIO;
202                 break;
203
204                 case MM_SESSION_TYPE_ALARM:
205                         event_type = ASM_EVENT_ALARM;
206                 break;
207
208                 case MM_SESSION_TYPE_NOTIFY:
209                 case MM_SESSION_TYPE_CALL:
210                 case MM_SESSION_TYPE_VIDEOCALL:
211                 default:
212                         debug_critical("unexpected case! (%d)\n", type);
213                         assert(0);
214         }
215
216         return event_type;
217 }