summaryrefslogtreecommitdiff
path: root/src/mm_radio_sound_focus.c
blob: a780fd15b6fdb555753f901a6d1760fb745acee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * mm_radio_sound_focus.c
 *
 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <assert.h>
#include <unistd.h>
#include <mm_debug.h>
#include <mm_sound.h>
#include <mm_sound_focus.h>
#include <mm_sound_device.h>
#include "mm_radio_utils.h"
#include "mm_radio_priv_hal.h"
#include "mm_radio_sound_focus.h"

static void _mmradio_device_connected_cb(MMSoundDevice_t device, bool is_connected, void *user_data)
{
	mm_radio_t *radio = (mm_radio_t *)user_data;
	int result = MM_ERROR_NONE;
	mm_sound_device_type_e type;

	MMRADIO_LOG_FENTER();
	MMRADIO_CHECK_INSTANCE_RETURN_VOID(radio);

	if (mm_sound_get_device_type(device, &type) != MM_ERROR_NONE) {
		MMRADIO_LOG_ERROR("getting device type failed");
	} else {
		switch (type) {
		case MM_SOUND_DEVICE_TYPE_AUDIOJACK:
		case MM_SOUND_DEVICE_TYPE_BLUETOOTH:
		case MM_SOUND_DEVICE_TYPE_HDMI:
		case MM_SOUND_DEVICE_TYPE_MIRRORING:
		case MM_SOUND_DEVICE_TYPE_USB_AUDIO:
			if (!is_connected) {
				MMRADIO_LOG_ERROR("sound device unplugged");
				radio->sound_focus.by_focus_cb = MMRADIO_FOCUS_CB_POSTMSG;
				radio->sound_focus.event_src = MM_MSG_CODE_INTERRUPTED_BY_EARJACK_UNPLUG;

				result = _mmradio_stop(radio);
				if (result != MM_ERROR_NONE)
					MMRADIO_LOG_ERROR("failed to stop radio\n");
			}
			break;
		default:
			break;
		}
	}
	MMRADIO_LOG_FLEAVE();
}

void _mmradio_sound_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
{
	mm_radio_sound_focus *sound_focus = (mm_radio_sound_focus *)user_data;
	int ret = MM_ERROR_NONE;

	MMRADIO_LOG_DEBUG("sound signal callback %d / %d", signal, value);

	if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
		if (value == 1) {
			/* unregister watch callback */
			if (sound_focus->watch_id > 0) {
				MMRADIO_LOG_DEBUG("unset the focus watch cb %d", sound_focus->watch_id);

				ret = mm_sound_unset_focus_watch_callback(sound_focus->watch_id);
				sound_focus->watch_id = 0;
				if (ret != MM_ERROR_NONE)
					MMRADIO_LOG_ERROR("failed to mm_sound_unset_focus_watch_callback()");
			}
			/* unregister focus callback */
			if (sound_focus->focus_id > 0) {
				ret = mm_sound_unregister_focus(sound_focus->focus_id);
				sound_focus->focus_id = 0;
				if (ret != MM_ERROR_NONE)
					MMRADIO_LOG_ERROR("failed to mm_sound_unregister_focus() %d", sound_focus->focus_id);
			}
			/* unregister device connected callback */
			if (sound_focus->device_subs_id > 0) {
				MMRADIO_LOG_DEBUG("unset the device connected cb %d", sound_focus->device_subs_id);
				ret = mm_sound_remove_device_connected_callback(sound_focus->device_subs_id);
				sound_focus->device_subs_id = 0;
				if (ret != MM_ERROR_NONE)
					MMRADIO_LOG_ERROR("failed to mm_sound_remove_device_connected_callback()");
			}
		}
	}
}

int mmradio_sound_focus_register(mm_radio_sound_focus *sound_focus,
	mm_sound_focus_changed_cb focus_cb, mm_sound_focus_changed_watch_cb watch_cb, void *param)
{
	/* read mm-session information */
	int session_type = MM_SESSION_TYPE_MEDIA;
	int session_flags = 0;
	int ret = MM_ERROR_NONE;
	int pid = getpid();
	int handle = 0;

	MMRADIO_LOG_FENTER();

	if (!sound_focus) {
		MMRADIO_LOG_ERROR("invalid session handle\n");
		return MM_ERROR_RADIO_NOT_INITIALIZED;
	}
	sound_focus->cur_focus_type = FOCUS_NONE;

	/* read session information */
	ret = _mm_session_util_read_information(pid, &session_type, &session_flags);
	MMRADIO_LOG_DEBUG("Read Session type ret:0x%X", ret);
	if (ret == MM_ERROR_INVALID_HANDLE) {
		MMRADIO_LOG_WARNING("subscribe_id=%d\n", sound_focus->subscribe_id);
		if (sound_focus->subscribe_id == 0) {
			ret = mm_sound_subscribe_signal_for_daemon(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, pid, &sound_focus->subscribe_id,
									(mm_sound_signal_callback)_mmradio_sound_signal_callback, (void*)sound_focus);
			if (ret != MM_ERROR_NONE) {
				MMRADIO_LOG_ERROR("mm_sound_subscribe_signal is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}
			MMRADIO_LOG_DEBUG("register focus watch callback for the value is 0, sub_cb id %d\n", sound_focus->subscribe_id);
			/* register watch callback */
			ret = mm_sound_set_focus_watch_callback_for_session(pid,
					FOCUS_FOR_BOTH, watch_cb, (void *)param, &sound_focus->watch_id);
			if (ret != MM_ERROR_NONE) {
				MMRADIO_LOG_ERROR("mm_sound_set_focus_watch_callback is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}
			MMRADIO_LOG_DEBUG("(%p) set focus watch callback = %d", param, sound_focus->watch_id);

			/* register device connected callback */
			ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
					(mm_sound_device_connected_cb)_mmradio_device_connected_cb, (void *)param, &sound_focus->device_subs_id);
			if (ret != MM_ERROR_NONE) {
				MMRADIO_LOG_ERROR("mm_sound_add_device_connected_callback is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}
			MMRADIO_LOG_ERROR("register device connected callback for the value is 0, sub_cb id %d\n", sound_focus->device_subs_id);
		}
		ret = MM_ERROR_NONE;
	} else if (ret == MM_ERROR_NONE) {
		MMRADIO_LOG_WARNING("Read Session Information success. session_type : %d flags: %d \n", session_type, session_flags);
		sound_focus->session_type = session_type;
		sound_focus->snd_session_flags = session_flags;
		if (sound_focus->session_type == MM_SESSION_TYPE_MEDIA) {
			if (sound_focus->snd_session_flags & MM_SESSION_OPTION_PAUSE_OTHERS) {
				/* register focus session */
				sound_focus->pid = pid;
				MMRADIO_LOG_DEBUG("sound register focus pid[%d]", pid);
				ret = mm_sound_focus_get_id(&handle);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_ERROR("Failed to get sound focus id");
					return MM_ERROR_POLICY_BLOCKED;
				}
				sound_focus->handle = handle;

				ret = mm_sound_register_focus_for_session(handle, pid, "radio", focus_cb, param);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_DEBUG("mm_sound_register_focus_for_session is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				MMRADIO_LOG_DEBUG("(%p) register focus for session %d", param, pid);
				/* register device_connected_cb */
				ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
					(mm_sound_device_connected_cb)_mmradio_device_connected_cb, (void *)param, &sound_focus->device_subs_id);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_ERROR("mm_sound_add_device_connected_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				MMRADIO_LOG_DEBUG("(%p) add device connected callback = %d", param, sound_focus->device_subs_id);

			} else if (sound_focus->snd_session_flags & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
				/* register device_connected_cb */
				ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
					(mm_sound_device_connected_cb)_mmradio_device_connected_cb, (void*)param, &sound_focus->device_subs_id);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_ERROR("mm_sound_add_device_connected_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				MMRADIO_LOG_DEBUG("add device connected callback = %d", sound_focus->device_subs_id);
				MMRADIO_LOG_WARNING("settion flags isn't OPTION_PAUSE_OTHERS and UNINTERRUPTIBLE. skip it..");
			} else {
				/* register watch callback */
				ret = mm_sound_set_focus_watch_callback_for_session(pid,
						FOCUS_FOR_BOTH, watch_cb, (void*)param, &sound_focus->watch_id);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_ERROR("mm_sound_set_focus_watch_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				MMRADIO_LOG_DEBUG("set focus watch callback = %d", sound_focus->watch_id);

				/* register device connected callback */
				ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
						(mm_sound_device_connected_cb)_mmradio_device_connected_cb, (void*)param, &sound_focus->device_subs_id);
				if (ret != MM_ERROR_NONE) {
					MMRADIO_LOG_ERROR("mm_sound_add_device_connected_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				MMRADIO_LOG_DEBUG("(%p) add device connected callback = %d", param, sound_focus->device_subs_id);
			}
		} else if (sound_focus->session_type == MM_SESSION_TYPE_REPLACED_BY_STREAM) {
			/* didn't register device connected cb */
			MMRADIO_LOG_WARNING("this process is using stream info. skip it..");
		} else {
			MMRADIO_LOG_WARNING("seesion type is not MEDIA (%d)", sound_focus->session_type);
			return MM_ERROR_RADIO_INTERNAL;
		}
		ret = MM_ERROR_NONE;
	} else {
		MMRADIO_LOG_WARNING("Read Session Information failed. skip sound focus register function. errorcode %x \n", ret);
	}

	MMRADIO_LOG_FLEAVE();

	return MM_ERROR_NONE;
}

int mmradio_sound_focus_deregister(mm_radio_sound_focus *sound_focus)
{

	int ret = MM_ERROR_NONE;

	MMRADIO_LOG_FENTER();

	if (!sound_focus) {
		MMRADIO_LOG_ERROR("invalid session handle\n");
		return MM_ERROR_RADIO_NOT_INITIALIZED;
	}

	if (sound_focus->watch_id > 0) {
		MMRADIO_LOG_DEBUG("unset the focus watch cb %d", sound_focus->watch_id);

		ret = mm_sound_unset_focus_watch_callback(sound_focus->watch_id);
		sound_focus->watch_id = 0;
		if (ret != MM_ERROR_NONE)
			MMRADIO_LOG_ERROR("failed to mm_sound_unset_focus_watch_callback() ret = %d", ret);
	}
	/* unregister focus callback */
	if (sound_focus->focus_id > 0) {
		ret = mm_sound_unregister_focus(sound_focus->focus_id);
		sound_focus->focus_id = 0;
		if (ret != MM_ERROR_NONE)
			MMRADIO_LOG_ERROR("failed to mm_sound_unregister_focus() ret = %d", ret);
	}
	/* unregister device connected callback */
	if (sound_focus->device_subs_id > 0) {
		MMRADIO_LOG_DEBUG("unset the device connected cb %d", sound_focus->device_subs_id);
		ret = mm_sound_remove_device_connected_callback(sound_focus->device_subs_id);
		sound_focus->device_subs_id = 0;
		if (ret != MM_ERROR_NONE)
			MMRADIO_LOG_ERROR("failed to mm_sound_remove_device_connected_callback() ret = %d", ret);
	}

	MMRADIO_LOG_FLEAVE();

	return MM_ERROR_NONE;
}

int mmradio_acquire_sound_focus(mm_radio_sound_focus *sound_focus)
{
	int ret = MM_ERROR_NONE;
	mm_sound_focus_type_e focus_type = FOCUS_NONE;
	MMRADIO_LOG_FENTER();

	MMRADIO_LOG_ERROR("mmradio_acquire_sound_focus sound_focus->cur_focus_type : %d\n", sound_focus->cur_focus_type);

	focus_type = FOCUS_FOR_BOTH & ~(sound_focus->cur_focus_type);
	if (focus_type != FOCUS_NONE) {
		ret = mm_sound_acquire_focus(sound_focus->handle, focus_type, NULL);
		if (ret != MM_ERROR_NONE) {
			MMRADIO_LOG_ERROR("mm_sound_acquire_focus is failed\n");
			return MM_ERROR_POLICY_BLOCKED;
		}
		sound_focus->cur_focus_type = FOCUS_FOR_BOTH;
	}

	MMRADIO_LOG_FLEAVE();
	return ret;
}

int mmradio_release_sound_focus(mm_radio_sound_focus *sound_focus)
{
	int ret = MM_ERROR_NONE;
	MMRADIO_LOG_FENTER();

	MMRADIO_LOG_ERROR("mmradio_release_sound_focus sound_focus->cur_focus_type : %d\n", sound_focus->cur_focus_type);
	if (sound_focus->cur_focus_type != FOCUS_NONE) {
		ret = mm_sound_release_focus(sound_focus->handle, sound_focus->cur_focus_type, NULL);
		if (ret != MM_ERROR_NONE) {
			MMRADIO_LOG_ERROR("mm_sound_release_focus is failed\n");
			return MM_ERROR_POLICY_BLOCKED;
		}
		sound_focus->cur_focus_type = FOCUS_NONE;
	}

	MMRADIO_LOG_FLEAVE();
	return ret;
}

#define AUDIO_FOCUS_REASON_MAX	128

void mmradio_get_sound_focus_reason(mm_sound_focus_state_e focus_state, const char *reason_for_change, enum MMMessageInterruptedCode *event_source, int *postMsg)
{
	MMRADIO_LOG_FENTER();
	MMRADIO_LOG_ERROR("mmradio_get_sound_focus_reason focus_state : %d reason_for_change :%s\n", focus_state, reason_for_change);

	if (0 == strncmp(reason_for_change, "call-voice", AUDIO_FOCUS_REASON_MAX)
		|| (0 == strncmp(reason_for_change, "voip", AUDIO_FOCUS_REASON_MAX))
		|| (0 == strncmp(reason_for_change, "ringtone-voip", AUDIO_FOCUS_REASON_MAX))
		|| (0 == strncmp(reason_for_change, "ringtone-call", AUDIO_FOCUS_REASON_MAX))
		) {
		if (focus_state == FOCUS_IS_RELEASED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_CALL_START;
		else if (focus_state == FOCUS_IS_ACQUIRED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_CALL_END;
		*postMsg = true;
	} else if (0 == strncmp(reason_for_change, "alarm", AUDIO_FOCUS_REASON_MAX)) {
		if (focus_state == FOCUS_IS_RELEASED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_ALARM_START;
		else if (focus_state == FOCUS_IS_ACQUIRED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_ALARM_END;
		*postMsg = true;
	} else if (0 == strncmp(reason_for_change, "notification", AUDIO_FOCUS_REASON_MAX)) {
		if (focus_state == FOCUS_IS_RELEASED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_NOTIFICATION_START;
		else if (focus_state == FOCUS_IS_ACQUIRED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_NOTIFICATION_END;
		*postMsg = true;
	} else if (0 == strncmp(reason_for_change, "emergency", AUDIO_FOCUS_REASON_MAX)) {
		if (focus_state == FOCUS_IS_RELEASED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_EMERGENCY_START;
		else if (focus_state == FOCUS_IS_ACQUIRED)
			*event_source = MM_MSG_CODE_INTERRUPTED_BY_EMERGENCY_END;
		*postMsg = false;
	} else if (0 == strncmp(reason_for_change, "media", AUDIO_FOCUS_REASON_MAX)) {
		*event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
		*postMsg = false;
	} else {
		*event_source = MM_MSG_CODE_INTERRUPTED_BY_MEDIA;
		*postMsg = false;
	}
	MMRADIO_LOG_FLEAVE();
}