summaryrefslogtreecommitdiff
path: root/server/mm_sound_mgr_pulse.c
blob: 1fe93c24d0d8186be399f5ef082fcc1abe885106 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@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 <stdlib.h>
#include <string.h>

#include <pthread.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>

#include <errno.h>

#include "include/mm_sound_mgr_common.h"
#include "../include/mm_sound_common.h"

#include <mm_error.h>
#include <mm_debug.h>

//#include <audio-session-manager.h>
//#include <avsys-audio.h>

#include <pulse/pulseaudio.h>
#include <pulse/ext-policy.h>

#define SUPPORT_MONO_AUDIO

#define SUPPORT_BT_SCO_DETECT

#include "include/mm_sound_mgr_pulse.h"
#include "include/mm_sound_mgr_session.h"

#include "include/mm_sound_msg.h"
#include "include/mm_sound_mgr_ipc.h"


#include <vconf.h>
#include <vconf-keys.h>

#define MAX_STRING	64

typedef struct _pulse_info
{
	pa_threaded_mainloop *m;
	pa_context *context;
	char sink_to_find[MAX_STRING];
	bool init_bt_status;
}pulse_info_t;

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

pulse_info_t* pulse_info = NULL;

/* -------------------------------- PULSEAUDIO --------------------------------------------*/

static void server_info_cb(pa_context *c, const pa_server_info *i, void *userdata)
{
	int ret = 0;

	if (!i) {
		debug_error("error in server info callback\n");

    } else {
		debug_msg ("We got default sink = [%s]\n", i->default_sink_name);

		/* ToDo: Update server info */
		ret = MMSoundMgrSessionSetDefaultSink (i->default_sink_name);
		if (ret != MM_ERROR_NONE) {
			/* TODO : Error Handling */
			debug_error ("MMSoundMgrSessionSetDefaultSink failed....ret = [%x]\n", ret);
		}
    }
}

static void init_card_info_cb (pa_context *c, const pa_card_info *i, int eol, void *userdata)
{
	pulse_info_t *pinfo = (pulse_info_t *)userdata;
	if (eol || i == NULL) {
		debug_msg ("signaling--------------\n");
		pa_threaded_mainloop_signal (pinfo->m, 0);
		return;
	}

	if (strstr (i->name, "bluez")) {
		pinfo->init_bt_status = true;
	}
}

static void card_info_cb (pa_context *c, const pa_card_info *i, int eol, void *userdata)
{
	int ret = 0;
	char* desc = NULL;

	if (eol || i == NULL) {
		return;
	}

	/* Get device name : eg. SBH-600 */
	desc = pa_proplist_gets(i->proplist, PA_PROP_DEVICE_DESCRIPTION);
	debug_msg ("[%s][%d] card name is [%s], card.property.device.description = [%s]\n", __func__, __LINE__, i->name, desc);

	/* ToDo: Update Device */
	ret = MMSoundMgrSessionSetDeviceAvailable (DEVICE_BT_A2DP, AVAILABLE, 0, (desc)? desc : "NONAME");
	if (ret != MM_ERROR_NONE) {
		/* TODO : Error Handling */
		debug_error ("MMSoundMgrSessionSetDeviceAvailable failed....ret = [%x]\n", ret);
	}
}

static void context_subscribe_cb (pa_context * c, pa_subscription_event_type_t t, uint32_t idx, void *userdata)
{
	debug_msg (">>>>>>>>> [%s][%d] type=(0x%x) idx=(%u)\n", __func__, __LINE__,  t, idx);

	if ((t &  PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CARD) {
		/* FIXME: We assumed that card is bt, card new/remove = bt new/remove */

		if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { /* BT is removed */
			/* ToDo: Update Device */
			//MMSoundMgrSessionSetDeviceBT (STATUS_NONE, NULL);
			MMSoundMgrSessionSetDeviceAvailable (DEVICE_BT_A2DP, NOT_AVAILABLE, 0, NULL);

		} else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) { /* BT is loaded */
			/* Get Card info for BT name. Update device after that */
			pa_operation *o;
			if (!(o = 	pa_context_get_card_info_by_index (c, idx, card_info_cb, NULL))) {
				return;
			}
			pa_operation_unref(o);

		}

	} else if ((t &  PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SERVER) {

		pa_operation_unref(pa_context_get_server_info(c, server_info_cb, NULL));


	} else {
		debug_msg ("[%s][%d] type=(0x%x) idx=(%u) is not card or server event, skip...\n", __func__, __LINE__,  t, idx);
		return;
	}
}


static void context_state_cb (pa_context *c, void *userdata)
{
	pulse_info_t *pinfo = (pulse_info_t *)userdata;

	switch (pa_context_get_state(c)) {
		case PA_CONTEXT_UNCONNECTED:
		case PA_CONTEXT_CONNECTING:
		case PA_CONTEXT_AUTHORIZING:
		case PA_CONTEXT_SETTING_NAME:
		case PA_CONTEXT_FAILED:
		case PA_CONTEXT_TERMINATED:
			break;

		case PA_CONTEXT_READY:
		{
			 if (pinfo->context == c) {

				 /* Do CARD and SERVER Subscribe */
				pa_context_set_subscribe_callback(c, context_subscribe_cb, pinfo);
				pa_operation *o;
				if (!(o = pa_context_subscribe(c, (pa_subscription_mask_t)PA_SUBSCRIPTION_MASK_CARD | PA_SUBSCRIPTION_MASK_SERVER, NULL, NULL))) {
					return;
				}
				pa_operation_unref(o);

				pa_operation_unref(pa_context_get_card_info_list (pinfo->context, init_card_info_cb, pinfo));

				/* signaling will be done after get card info in card info callback */
			}
			break;
		}
	}
}


static int pulse_init (pulse_info_t * pinfo)
{
	int res;

	debug_msg (">>>>>>>>> [%s][%d]\n", __func__, __LINE__);

	/* Create new mainloop */
	pinfo->m = pa_threaded_mainloop_new();
	//g_assert(g_m);

	res = pa_threaded_mainloop_start (pinfo->m);
	//g_assert (res == 0);

	/* LOCK thread */
	pa_threaded_mainloop_lock (pinfo->m);

	/* Get mainloop API */
	pa_mainloop_api *api = pa_threaded_mainloop_get_api(pinfo->m);

	/* Create new Context */
	pinfo->context = pa_context_new(api, "SOUND_SERVER_ROUTE_MANAGER");

	/* Set Callback */
	pa_context_set_state_callback (pinfo->context, context_state_cb, pinfo);

	/* Connect */
	if (pa_context_connect (pinfo->context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
		debug_error ("connection error\n");
	}

	for (;;) {
		pa_context_state_t state;

		state = pa_context_get_state (pinfo->context);

		debug_msg ("context state is now %d\n", state);

		if (!PA_CONTEXT_IS_GOOD (state)) {
			debug_error ("connection failed\n");
			break;
		}

		if (state == PA_CONTEXT_READY)
			break;

		/* Wait until the context is ready */
		debug_msg ("waiting..................\n");
		pa_threaded_mainloop_wait (pinfo->m);
	}

	/* UNLOCK thread */
	pa_threaded_mainloop_unlock (pinfo->m);

	debug_msg ("<<<<<<<<<< [%s][%d]\n", __func__, __LINE__);

	return res;
}


static void sink_info_cb (pa_context *c, const pa_sink_info *i, int is_last, void *userdata)
{
	pulse_info_t * pinfo = (pulse_info_t *)userdata;

	if (is_last || i == NULL || pinfo == NULL) {
		if (is_last < 0) {
			debug_error("Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
		}
		return;
	}

	if (i->name && i->proplist) {
		const char *api_string = pa_proplist_gets (i->proplist, "device.api");
		if (api_string) {
			debug_log ("sink name = [%s], api = [%s]\n", i->name, api_string);
			if (strcmp (api_string, pinfo->sink_to_find) == 0) {
				debug_log ("FOUND!!! set default sink to [%s]\n", i->name);
				pa_operation_unref(pa_context_set_default_sink(pinfo->context, i->name, NULL, NULL));

			} else {
				debug_warning ("No string [%s] match!!!!\n", pinfo->sink_to_find);
			}
		}
	}
}


static void pulse_set_default_sink (pulse_info_t * pinfo, char *default_sink_name)
{
	strcpy (pinfo->sink_to_find, default_sink_name);
	pa_operation_unref(pa_context_get_sink_info_list(pinfo->context, sink_info_cb, pinfo));
}

static int pulse_deinit (pulse_info_t * pinfo)
{
	pa_threaded_mainloop_lock (pinfo->m);
	if (pinfo->context) {
		pa_context_disconnect (pinfo->context);

		/* Make sure we don't get any further callbacks */
		pa_context_set_state_callback (pinfo->context, NULL, NULL);

		pa_context_unref (pinfo->context);
		pinfo->context = NULL;
	}
	pa_threaded_mainloop_unlock (pinfo->m);

	pa_threaded_mainloop_stop (pinfo->m);
	pa_threaded_mainloop_free (pinfo->m);

	debug_msg ("<<<<<<<<<< [%s][%d]\n", __func__, __LINE__);

	return 0;

}

/* -------------------------------- MONO AUDIO --------------------------------------------*/
#ifdef SUPPORT_MONO_AUDIO
#define MONO_KEY VCONFKEY_SETAPPL_ACCESSIBILITY_MONO_AUDIO

static void success_cb (pa_context *c, int success, void *userdata)
{
	debug_msg ("success = %d\n", success);
}

static void mono_changed_cb(keynode_t* node, void* data)
{
	int key_value;
	pulse_info_t* pinfo = (pulse_info_t*)data;

	debug_msg ("%s changed callback called\n",vconf_keynode_get_name(node));

	vconf_get_bool(MONO_KEY, &key_value);
	debug_msg ("key value = %d\n", key_value);

	pa_operation_unref (pa_ext_policy_set_mono (pinfo->context, key_value, success_cb, NULL));
}

int MMSoundMgrPulseHandleRegisterMonoAudio (void* pinfo)
{
	int ret = vconf_notify_key_changed(MONO_KEY, mono_changed_cb, pinfo);
	debug_msg ("vconf [%s] set ret = %d\n", MONO_KEY, ret);
	return ret;
}
#endif /* SUPPORT_MONO_AUDIO */


/* -------------------------------- BT SCO --------------------------------------------*/
#ifdef SUPPORT_BT_SCO_DETECT

static void bt_changed_cb(keynode_t* node, void* data)
{
	int bt_status = VCONFKEY_BT_DEVICE_NONE;
	int available = 0;
	pulse_info_t* pinfo = (pulse_info_t*)data;

	debug_msg ("[%s] changed callback called\n", vconf_keynode_get_name(node));

	/* Get actual vconf value */
	vconf_get_int(VCONFKEY_BT_DEVICE, &bt_status);
	debug_msg ("key value = 0x%x\n", bt_status);

	/* Set device available based on vconf key value */
	available = (bt_status & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)? AVAILABLE : NOT_AVAILABLE;
	MMSoundMgrSessionSetDeviceAvailable (DEVICE_BT_SCO, available, 0, NULL);
}

int MMSoundMgrPulseHandleRegisterBluetoothStatus (void* pinfo)
{
	/* set callback for vconf key change */
	int ret = vconf_notify_key_changed(VCONFKEY_BT_DEVICE , bt_changed_cb, pinfo);
	debug_msg ("vconf [%s] set ret = %d\n", VCONFKEY_BT_DEVICE, ret);
	return ret;
}
#endif /* SUPPORT_BT_SCO_DETECT */

/* -------------------------------- MGR MAIN --------------------------------------------*/

int MMSoundMgrPulseHandleIsBtA2DPOnReq (mm_ipc_msg_t *msg, int (*sendfunc)(mm_ipc_msg_t*))
{
	int ret = 0;
	mm_ipc_msg_t respmsg = {0,};
	char* bt_name;
	bool is_bt_on = false;
	pthread_mutex_lock(&g_mutex);

	debug_enter("msg = %p, sendfunc = %p\n", msg, sendfunc);

	bt_name = MMSoundMgrSessionGetBtA2DPName();
	if (bt_name && strlen(bt_name) > 0) {
		is_bt_on = true;
	}

	debug_log ("is_bt_on = [%d], name = [%s]\n", is_bt_on, bt_name);

	SOUND_MSG_SET(respmsg.sound_msg,
				MM_SOUND_MSG_RES_IS_BT_A2DP_ON, msg->sound_msg.handle, is_bt_on, msg->sound_msg.msgid);
	strncpy (respmsg.sound_msg.filename, bt_name,  sizeof (respmsg.sound_msg.filename)-1);

	/* Send Response */
	ret = sendfunc (&respmsg);
	if (ret != MM_ERROR_NONE) {
		/* TODO : Error Handling */
		debug_error ("sendfunc failed....ret = [%x]\n", ret);
	}

	pthread_mutex_unlock(&g_mutex);

	debug_leave("\n");

	return ret;
}

void MMSoundMgrPulseSetDefaultSink (char* default_sink_name)
{
	debug_enter("\n");

	pulse_set_default_sink(pulse_info, default_sink_name);

	debug_leave("\n");
}

void MMSoundMgrPulseGetInitialBTStatus (bool *a2dp, bool *sco)
{
	int bt_status = VCONFKEY_BT_DEVICE_NONE;

	if (a2dp == NULL || sco == NULL) {
		debug_error ("Invalide arguments!!!\n");
		return;
	}

	/* Get saved bt status */
	*a2dp = pulse_info->init_bt_status;

	/* Get actual vconf value */
	vconf_get_int(VCONFKEY_BT_DEVICE, &bt_status);
	debug_msg ("key value = 0x%x\n", bt_status);
	*sco = (bt_status & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED)? true : false;

	debug_msg ("returning a2dp=[%d], sco=[%d]\n", *a2dp, *sco);
}

void* MMSoundMgrPulseInit(void)
{
	pulse_info = (pulse_info_t*) malloc (sizeof(pulse_info_t));
	memset (pulse_info, 0, sizeof(pulse_info_t));

	debug_enter("\n");

	pulse_init(pulse_info);

#ifdef SUPPORT_MONO_AUDIO
	MMSoundMgrPulseHandleRegisterMonoAudio(pulse_info);
#endif

#ifdef SUPPORT_BT_SCO_DETECT
	MMSoundMgrPulseHandleRegisterBluetoothStatus(pulse_info);
#endif

	debug_leave("\n");
	return pulse_info;
}

int MMSoundMgrPulseFini(void* handle)
{
	debug_enter("\n");

	pulse_deinit((pulse_info_t *)handle);

	debug_leave("\n");
	return MM_ERROR_NONE;
}