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