Expand the prestate of radio_scan_start function
[platform/core/multimedia/libmm-radio.git] / src / mm_radio_audio_focus.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_audio_focus.h"
25 #include "mm_radio_utils.h"
26 #include "mm_sound_focus.h"
27 #include "unistd.h"
28
29 int mmradio_audio_focus_register(MMRadioAudioFocus *sm, mm_sound_focus_changed_cb callback, void *param)
30 {
31         /* read mm-session information */
32         int session_type = MM_SESSION_TYPE_MEDIA;
33         int session_flags = 0;
34         int errorcode = MM_ERROR_NONE;
35         int pid = getpid();
36         int handle;
37
38         MMRADIO_LOG_FENTER();
39
40         if (!sm) {
41                 MMRADIO_LOG_ERROR("invalid session handle\n");
42                 return MM_ERROR_RADIO_NOT_INITIALIZED;
43         }
44         sm->cur_focus_type = FOCUS_NONE;
45
46         /* read session information */
47         errorcode = _mm_session_util_read_information(pid, &session_type, &session_flags);
48         if (errorcode == MM_ERROR_NONE) {
49                 debug_warning("Read Session Information success. session_type : %d flags: %d \n", session_type, session_flags);
50                 sm->snd_session_flags = session_flags;
51                 session_type = MM_SESSION_TYPE_MEDIA;
52         } else {
53                 debug_warning("Read Session Information failed. skip sound focus register function. errorcode %x \n", errorcode);
54         }
55
56         /* check if it's MEDIA type */
57         if (session_type != MM_SESSION_TYPE_MEDIA) {
58                 MMRADIO_LOG_DEBUG("session type is not MEDIA (%d)\n", session_type);
59                 return MM_ERROR_RADIO_INTERNAL;
60         }
61
62         /* check if it's running on the media_server */
63         sm->pid = pid;
64
65         MMRADIO_LOG_DEBUG("sound register focus pid[%d]", pid);
66
67         mm_sound_focus_get_id(&handle);
68         sm->handle = handle;
69
70         if (mm_sound_register_focus_for_session(handle, pid, "radio", callback, param) != MM_ERROR_NONE) {
71                 MMRADIO_LOG_DEBUG("mm_sound_register_focus_for_session is failed\n");
72                 return MM_ERROR_POLICY_BLOCKED;
73         }
74
75         MMRADIO_LOG_FLEAVE();
76
77         return MM_ERROR_NONE;
78 }
79
80 int mmradio_audio_focus_deregister(MMRadioAudioFocus *sm)
81 {
82         MMRADIO_LOG_FENTER();
83
84         if (!sm) {
85                 MMRADIO_LOG_ERROR("invalid session handle\n");
86                 return MM_ERROR_RADIO_NOT_INITIALIZED;
87         }
88
89         if (MM_ERROR_NONE != mm_sound_unregister_focus(sm->handle))
90                 MMRADIO_LOG_ERROR("mm_sound_unregister_focus failed\n");
91
92         MMRADIO_LOG_FLEAVE();
93
94         return MM_ERROR_NONE;
95 }
96
97 int mmradio_acquire_audio_focus(MMRadioAudioFocus *sm)
98 {
99         int ret = MM_ERROR_NONE;
100         mm_sound_focus_type_e focus_type = FOCUS_NONE;
101         MMRADIO_LOG_FENTER();
102
103         MMRADIO_LOG_ERROR("mmradio_acquire_audio_focus sm->cur_focus_type : %d\n", sm->cur_focus_type);
104
105         focus_type = FOCUS_FOR_BOTH & ~(sm->cur_focus_type);
106         if (focus_type != FOCUS_NONE) {
107                 ret = mm_sound_acquire_focus(sm->handle, focus_type, NULL);
108                 if (ret != MM_ERROR_NONE) {
109                         MMRADIO_LOG_ERROR("mm_sound_acquire_focus is failed\n");
110                         return MM_ERROR_POLICY_BLOCKED;
111                 }
112                 sm->cur_focus_type = FOCUS_FOR_BOTH;
113         }
114
115         MMRADIO_LOG_FLEAVE();
116         return ret;
117 }
118
119 int mmradio_release_audio_focus(MMRadioAudioFocus *sm)
120 {
121         int ret = MM_ERROR_NONE;
122         MMRADIO_LOG_FENTER();
123
124         MMRADIO_LOG_ERROR("mmradio_release_audio_focus sm->cur_focus_type : %d\n", sm->cur_focus_type);
125         if (sm->cur_focus_type != FOCUS_NONE) {
126                 ret = mm_sound_release_focus(sm->handle, sm->cur_focus_type, NULL);
127                 if (ret != MM_ERROR_NONE) {
128                         MMRADIO_LOG_ERROR("mm_sound_release_focus is failed\n");
129                         return MM_ERROR_POLICY_BLOCKED;
130                 }
131                 sm->cur_focus_type = FOCUS_NONE;
132         }
133
134         MMRADIO_LOG_FLEAVE();
135         return ret;
136 }
137
138 #define AUDIO_FOCUS_REASON_MAX  128
139
140 void mmradio_get_audio_focus_reason(mm_sound_focus_state_e focus_state, const char *reason_for_change, enum MMMessageInterruptedCode *event_source, int *postMsg)
141 {
142         MMRADIO_LOG_FENTER();
143         MMRADIO_LOG_ERROR("mmradio_get_audio_focus_reason focus_state : %d reason_for_change :%s\n", focus_state, reason_for_change);
144
145         if (0 == strncmp(reason_for_change, "call-voice", AUDIO_FOCUS_REASON_MAX)
146                 || (0 == strncmp(reason_for_change, "voip", AUDIO_FOCUS_REASON_MAX))
147                 || (0 == strncmp(reason_for_change, "ringtone-voip", AUDIO_FOCUS_REASON_MAX))
148                 || (0 == strncmp(reason_for_change, "ringtone-call", AUDIO_FOCUS_REASON_MAX))
149                 ) {
150                 if (focus_state == FOCUS_IS_RELEASED)
151                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_CALL_START;
152                 else if (focus_state == FOCUS_IS_ACQUIRED)
153                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_CALL_END;
154                 *postMsg = true;
155         } else if (0 == strncmp(reason_for_change, "alarm", AUDIO_FOCUS_REASON_MAX)) {
156                 if (focus_state == FOCUS_IS_RELEASED)
157                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_ALARM_START;
158                 else if (focus_state == FOCUS_IS_ACQUIRED)
159                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_ALARM_END;
160                 *postMsg = true;
161         } else if (0 == strncmp(reason_for_change, "notification", AUDIO_FOCUS_REASON_MAX)) {
162                 if (focus_state == FOCUS_IS_RELEASED)
163                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_NOTIFICATION_START;
164                 else if (focus_state == FOCUS_IS_ACQUIRED)
165                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_NOTIFICATION_END;
166                 *postMsg = true;
167         } else if (0 == strncmp(reason_for_change, "emergency", AUDIO_FOCUS_REASON_MAX)) {
168                 if (focus_state == FOCUS_IS_RELEASED)
169                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_EMERGENCY_START;
170                 else if (focus_state == FOCUS_IS_ACQUIRED)
171                         *event_source = MM_MSG_CODE_INTERRUPTED_BY_EMERGENCY_END;
172                 *postMsg = false;
173         } else if (0 == strncmp(reason_for_change, "media", AUDIO_FOCUS_REASON_MAX)) {
174                 *event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
175                 *postMsg = false;
176         } else {
177                 *event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
178                 *postMsg = false;
179         }
180         MMRADIO_LOG_FLEAVE();
181 }