summaryrefslogtreecommitdiff
path: root/src/mm_player_sound_focus.c
blob: c9c42bfde9bff644019d24be859e62c319691430 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * libmm-player
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>,
 * Seungbae Shin <seungbae.shin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
 *
 * 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 <sys/types.h>
#include <unistd.h>
#include <dlog.h>
#include <mm_error.h>
#include "mm_player_utils.h"
#include "mm_player_priv.h"
#include "mm_player_sound_focus.h"

#define MMPLAYER_CHECK_SOUND_FOCUS_INSTANCE(x_player_sound_focus) \
do { \
	if (!x_player_sound_focus) { \
		LOGD("no sound focus instance");\
		return MM_ERROR_SOUND_NOT_INITIALIZED; \
	} \
} while (0);

void __mmplayer_sound_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
{
	MMPlayerSoundFocus *sound_focus = (MMPlayerSoundFocus*)user_data;
	MMPLAYER_RETURN_IF_FAIL(sound_focus);

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

	if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS && value == 1)
		_mmplayer_sound_unregister(sound_focus);
}

static void
__mmplayer_sound_device_connected_cb_func(MMSoundDevice_t device_h, bool is_connected, void *user_data)
{
	mm_player_t* player = (mm_player_t*) user_data;
	MMPLAYER_RETURN_IF_FAIL(player);

	mm_sound_device_type_e device_type;
	int ret;

	LOGW("device_connected_cb is called, device_h[0x%x], is_connected[%d]\n", device_h, is_connected);

	/* get device type with device_h*/
	ret = mm_sound_get_device_type(device_h, &device_type);

	if (!is_connected && MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_PLAYING
		&& (player->sound_focus.focus_id > 0 || player->sound_focus.watch_id > 0)) {
		switch (device_type) {
		case MM_SOUND_DEVICE_TYPE_AUDIOJACK:
		case MM_SOUND_DEVICE_TYPE_BLUETOOTH:
		case MM_SOUND_DEVICE_TYPE_HDMI:
		case MM_SOUND_DEVICE_TYPE_USB_AUDIO:
		{
			int result = MM_ERROR_NONE;
			LOGW("pause immediately");

			player->sound_focus.by_asm_cb = TRUE;
			player->sound_focus.focus_changed_msg = MM_PLAYER_FOCUS_CHANGED_BY_EARJACK_UNPLUG;

			result = _mmplayer_pause((MMHandleType)player);
			if (result != MM_ERROR_NONE) {
				LOGE("focus_id [%d], watch_id [%d], connected_id [%d], change_state result[%d]",
					player->sound_focus.focus_id, player->sound_focus.watch_id,
					player->sound_focus.connected_id, ret);
			}
			player->sound_focus.by_asm_cb = FALSE;
		}
		break;

		default:
			LOGD("do nothing");
		}
	}
}

const gchar *
__mmplayer_sound_get_stream_type(gint type)
{
	switch (type) {
	case MM_SESSION_TYPE_CALL:
	case MM_SESSION_TYPE_VIDEOCALL:
	case MM_SESSION_TYPE_VOIP:
		return "ringtone-voip";
	case MM_SESSION_TYPE_MEDIA:
		return "media";
	case MM_SESSION_TYPE_NOTIFY:
		return "notification";
	case MM_SESSION_TYPE_ALARM:
		return "alarm";
	case MM_SESSION_TYPE_EMERGENCY:
		return "emergency";
	default:
		LOGW("unexpected case!\n");
		return "media";
	}

	return "media";
}

static bool
__mmplayer_check_need_block(const char *focus_acquired_by)
{
    if (!focus_acquired_by)
        return false;

    if (!strcmp(focus_acquired_by, "alarm") ||
        !strcmp(focus_acquired_by, "ringtone-voip") ||
        !strcmp(focus_acquired_by, "ringtone-call") ||
        !strcmp(focus_acquired_by, "voip") ||
        !strcmp(focus_acquired_by, "call-voice") ||
        !strcmp(focus_acquired_by, "call-video")) {
        LOGW("Blocked by session policy, focus_acquired_by[%s]", focus_acquired_by);
        return true;
    }

    return false;
}

int
_mmplayer_sound_acquire_focus(MMPlayerSoundFocus* sound_focus)
{
	int ret = MM_ERROR_NONE;

	MMPLAYER_FENTER();
	MMPLAYER_CHECK_SOUND_FOCUS_INSTANCE(sound_focus);

	if (sound_focus->acquired) {
		LOGW("focus is already acquired. can't acquire again.");
		return MM_ERROR_NONE;
	}

	if (_mmplayer_is_using_internal_sound_focus(sound_focus)) {
		if (sound_focus->session_type == MM_SESSION_TYPE_MEDIA)
			/* option: 1 for no-resume */
			ret = mm_sound_acquire_focus_with_option(sound_focus->focus_id, FOCUS_FOR_BOTH, 1, "mm-player acquire focus");
		else
			ret = mm_sound_acquire_focus(sound_focus->focus_id, FOCUS_FOR_BOTH, "mm-player acquire focus");

		if (ret != MM_ERROR_NONE) {
			LOGE("failed to acquire sound focus [0x%X]", ret);
			return ret;
		}

		sound_focus->acquired = TRUE;

		ret = mm_sound_update_focus_status(sound_focus->focus_id, 1);
		if (ret != MM_ERROR_NONE)
			LOGE("failed to update focus status [0x%X]", ret);
	} else {
		if (sound_focus->watch_id > 0) {
			char *stream_type = NULL;
			char *ext_info = NULL;
			int option = 0;

			ret = mm_sound_get_stream_type_of_acquired_focus(FOCUS_FOR_BOTH, &stream_type, &option, &ext_info);
			if (ret == MM_ERROR_NONE) {
				LOGD("Focus is acquired by stream_type[%s], option[%d], ext_info[%s]", stream_type, option, ext_info);
				if (__mmplayer_check_need_block((const char*)stream_type)) {
					LOGE("Blocked by an acquired focus[%s]", stream_type);
					ret = MM_ERROR_POLICY_INTERNAL;
				}
			} else {
				LOGW("failed to get stream type of acquired focus [0x%X]", ret);
				if (ret == MM_ERROR_SOUND_NO_DATA) /* there is no acquired focus, it is normal case */
					ret = MM_ERROR_NONE;
			}

			if (stream_type) free(stream_type);
			if (ext_info) free(ext_info);
		}
	}

	MMPLAYER_FLEAVE();
	return ret;
}

int
_mmplayer_sound_release_focus(MMPlayerSoundFocus* sound_focus)
{
	int ret = MM_ERROR_NONE;

	MMPLAYER_FENTER();
	MMPLAYER_CHECK_SOUND_FOCUS_INSTANCE(sound_focus);

	if (!sound_focus->acquired) {
		LOGW("focus is not acquired. no need to release.");
		return MM_ERROR_NONE;
	}

	if (_mmplayer_is_using_internal_sound_focus(sound_focus)) {
		if (sound_focus->session_type == MM_SESSION_TYPE_MEDIA)
			/* option: 1 for no-resume */
			ret = mm_sound_release_focus_with_option(sound_focus->focus_id, FOCUS_FOR_BOTH, 1, "mm-player release focus");
		else
			ret = mm_sound_release_focus(sound_focus->focus_id, FOCUS_FOR_BOTH, "mm-player release focus");

		if (ret != MM_ERROR_NONE) {
			LOGE("failed to release sound focus\n");
			return ret;
		}

		sound_focus->acquired = FALSE;

		ret = mm_sound_update_focus_status(sound_focus->focus_id, 0);
		if (ret != MM_ERROR_NONE)
			LOGE("failed to update focus status\n");
	}

	MMPLAYER_FLEAVE();
	return MM_ERROR_NONE;
}

gint
_mmplayer_sound_register(MMPlayerSoundFocus* sound_focus,
		mm_sound_focus_changed_cb focus_cb, mm_sound_focus_changed_watch_cb watch_cb, void* param)
{
	gint pid = -1;
	gint ret = MM_ERROR_NONE;
	const gchar *stream_type = NULL;
	mm_player_t* player = MM_PLAYER_CAST(param);

	MMPLAYER_FENTER();
	MMPLAYER_CHECK_SOUND_FOCUS_INSTANCE(sound_focus);
	MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_INVALID_ARGUMENT);

	/* check if it's running on the media_server */
	if (sound_focus->pid > 0)
		pid = sound_focus->pid;
	else
		return MM_ERROR_INVALID_ARGUMENT;

	LOGD("sound register focus pid[%d]", pid);
	/* read session information */
	ret = _mm_session_util_read_information(pid, &sound_focus->session_type, &sound_focus->session_flags);
	LOGW("Read Session Type -> ret:0x%X \n", ret);

	if (ret == MM_ERROR_INVALID_HANDLE) {
		/* case 1. if there is no session */
		LOGW("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)__mmplayer_sound_signal_callback, (void*)sound_focus);
			if (ret != MM_ERROR_NONE) {
				LOGE("mm_sound_subscribe_signal is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}

			LOGD("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) {
				LOGE("mm_sound_set_focus_watch_callback is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}
			/* register device connected callback */
			ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
					(mm_sound_device_connected_cb)__mmplayer_sound_device_connected_cb_func, (void*)param, &sound_focus->connected_id);
			if (ret != MM_ERROR_NONE) {
				LOGE("mm_sound_add_device_connected_callback is failed\n");
				return MM_ERROR_POLICY_BLOCKED;
			}
			LOGD("register device connected callback for the value is 0, sub_cb id %d\n", sound_focus->connected_id);
		}
		ret = MM_ERROR_NONE;
	} else if (ret == MM_ERROR_NONE) {
		/* case 2. if sessoin exists */
		/* in this case, this process is using stream info created by using sound-manager,
		 * we're going to skip working on backward compatibility of session. */
		if (sound_focus->session_type == MM_SESSION_TYPE_REPLACED_BY_STREAM) {
			LOGW("this process is using stream info. skip it..");
		} else {
			/* interpret session information */
			stream_type = __mmplayer_sound_get_stream_type(sound_focus->session_type);
			LOGD("fid [%d] wid [%d] type[%s], flags[0x%02X]\n",
				sound_focus->focus_id, sound_focus->watch_id, stream_type, sound_focus->session_flags);

			if (sound_focus->focus_id == 0) {
				/* get unique id */
				ret = mm_sound_focus_get_id(&sound_focus->focus_id);
				if (ret != MM_ERROR_NONE) {
					LOGE("failed to get unique focus id\n");
					return MM_ERROR_POLICY_BLOCKED;
				}

				/* register sound focus callback */
				ret = mm_sound_register_focus_for_session(sound_focus->focus_id, pid,
						stream_type, focus_cb, (void*)param);
				if (ret != MM_ERROR_NONE) {
					LOGE("mm_sound_register_focus is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
			}

			if ((sound_focus->watch_id == 0) &&
				(strstr(stream_type, "media")) &&
				!(sound_focus->session_flags & MM_SESSION_OPTION_PAUSE_OTHERS) &&
				!(sound_focus->session_flags & MM_SESSION_OPTION_UNINTERRUPTIBLE)) {
				LOGD("register focus watch callback\n");
				/* 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) {
					LOGE("mm_sound_set_focus_watch_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
			}

			if ((strstr(stream_type, "media")) && (sound_focus->connected_id == 0)) {
				/* register device connected callback */
				ret = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG,
						(mm_sound_device_connected_cb)__mmplayer_sound_device_connected_cb_func, (void*)param, &sound_focus->connected_id);
				if (ret != MM_ERROR_NONE) {
					LOGE("mm_sound_add_device_connected_callback is failed\n");
					return MM_ERROR_POLICY_BLOCKED;
				}
				LOGD("register device connected callback for the value is 0, sub_cb id %d\n", sound_focus->connected_id);
			}

			ret = mm_player_set_attribute(player, NULL, "sound_stream_type", stream_type, strlen(stream_type), (char *)NULL);
			if (ret != MM_ERROR_NONE) {
				LOGE("mm_player_set_attribute for sound_stream_type is failed\n");
				return ret;
			}
		}
		ret = MM_ERROR_NONE;
	} else {
		LOGE("_mm_session_util_read_information is failed");
		ret =  MM_ERROR_POLICY_BLOCKED;
	}

	MMPLAYER_FLEAVE();
	return ret;
}

gint
_mmplayer_sound_unregister(MMPlayerSoundFocus* sound_focus)
{
	int ret = MM_ERROR_NONE;

	MMPLAYER_FENTER();

	MMPLAYER_CHECK_SOUND_FOCUS_INSTANCE(sound_focus);

	LOGD("unregister sound focus callback\n");

	if (sound_focus->focus_id > 0) {
		ret = mm_sound_unregister_focus(sound_focus->focus_id);
		if (ret != MM_ERROR_NONE)
			LOGE("failed to mm_sound_unregister_focus() %d", sound_focus->focus_id);
		sound_focus->focus_id = 0;
	}

	if (sound_focus->watch_id > 0) {
		ret = mm_sound_unset_focus_watch_callback(sound_focus->watch_id);
		if (ret != MM_ERROR_NONE)
			LOGE("failed to mm_sound_unset_focus_watch_callback() %d", sound_focus->watch_id);
		sound_focus->watch_id = 0;
	}

	if (sound_focus->subscribe_id > 0) {
		mm_sound_unsubscribe_signal(sound_focus->subscribe_id);
		sound_focus->subscribe_id = 0;
	}
	if (sound_focus->connected_id > 0) {
		ret = mm_sound_remove_device_connected_callback(sound_focus->connected_id);
		if (ret != MM_ERROR_NONE)
			LOGE("failed to mm_sound_remove_device_connected_callback() %d", sound_focus->connected_id);
		sound_focus->connected_id = 0;
	}

	MMPLAYER_FLEAVE();

	return MM_ERROR_NONE;
}

bool _mmplayer_is_using_internal_sound_focus(MMPlayerSoundFocus* sound_focus)
{
	MMPLAYER_RETURN_VAL_IF_FAIL(sound_focus, false);

	/* Perhaps, already got release signal or application may use stream focus directly */
	if (sound_focus->focus_id == 0)
		return false;

	if ((sound_focus->session_type == MM_SESSION_TYPE_MEDIA && sound_focus->session_flags & MM_SESSION_OPTION_PAUSE_OTHERS) ||
		(sound_focus->session_type == MM_SESSION_TYPE_NOTIFY) ||
		(sound_focus->session_type == MM_SESSION_TYPE_ALARM) ||
		(sound_focus->session_type == MM_SESSION_TYPE_EMERGENCY))
		return true;
	else
		return false;
}