4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "mm_radio_audio_focus.h"
25 #include "mm_radio_utils.h"
26 #include "mm_sound_focus.h"
29 int mmradio_audio_focus_register(MMRadioAudioFocus *sm, mm_sound_focus_changed_cb callback, void *param)
31 /* read mm-session information */
32 int session_type = MM_SESSION_TYPE_MEDIA;
33 int session_flags = 0;
34 int errorcode = MM_ERROR_NONE;
41 MMRADIO_LOG_ERROR("invalid session handle\n");
42 return MM_ERROR_RADIO_NOT_INITIALIZED;
44 sm->cur_focus_type = FOCUS_NONE;
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;
53 debug_warning("Read Session Information failed. skip sound focus register function. errorcode %x \n", errorcode);
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;
62 /* check if it's running on the media_server */
65 MMRADIO_LOG_DEBUG("sound register focus pid[%d]", pid);
67 mm_sound_focus_get_id(&handle);
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;
80 int mmradio_audio_focus_deregister(MMRadioAudioFocus *sm)
85 MMRADIO_LOG_ERROR("invalid session handle\n");
86 return MM_ERROR_RADIO_NOT_INITIALIZED;
89 if (MM_ERROR_NONE != mm_sound_unregister_focus(sm->handle))
90 MMRADIO_LOG_ERROR("mm_sound_unregister_focus failed\n");
97 int mmradio_acquire_audio_focus(MMRadioAudioFocus *sm)
99 int ret = MM_ERROR_NONE;
100 mm_sound_focus_type_e focus_type = FOCUS_NONE;
101 MMRADIO_LOG_FENTER();
103 MMRADIO_LOG_ERROR("mmradio_acquire_audio_focus sm->cur_focus_type : %d\n", sm->cur_focus_type);
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;
112 sm->cur_focus_type = FOCUS_FOR_BOTH;
115 MMRADIO_LOG_FLEAVE();
119 int mmradio_release_audio_focus(MMRadioAudioFocus *sm)
121 int ret = MM_ERROR_NONE;
122 MMRADIO_LOG_FENTER();
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;
131 sm->cur_focus_type = FOCUS_NONE;
134 MMRADIO_LOG_FLEAVE();
138 #define AUDIO_FOCUS_REASON_MAX 128
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)
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);
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))
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;
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;
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;
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;
173 } else if (0 == strncmp(reason_for_change, "media", AUDIO_FOCUS_REASON_MAX)) {
174 *event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
177 *event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
180 MMRADIO_LOG_FLEAVE();