Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / profiles / audio / sink.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2010  Nokia Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <errno.h>
32
33 #include <glib.h>
34 #include <dbus/dbus.h>
35
36 #include "lib/bluetooth.h"
37 #include "lib/sdp.h"
38
39 #include "gdbus/gdbus.h"
40
41 #include "src/log.h"
42 #include "src/adapter.h"
43 #include "src/device.h"
44 #include "src/service.h"
45 #include "src/error.h"
46 #include "src/dbus-common.h"
47 #include "src/shared/queue.h"
48
49 #include "avdtp.h"
50 #include "media.h"
51 #include "a2dp.h"
52 #include "sink.h"
53
54 #define STREAM_SETUP_RETRY_TIMER 2
55
56 struct sink {
57         struct btd_service *service;
58         struct avdtp *session;
59         struct avdtp_stream *stream;
60         unsigned int cb_id;
61         avdtp_session_state_t session_state;
62         avdtp_state_t stream_state;
63         sink_state_t state;
64         unsigned int connect_id;
65         unsigned int disconnect_id;
66         unsigned int avdtp_callback_id;
67 };
68
69 struct sink_state_callback {
70         sink_state_cb cb;
71         struct btd_service *service;
72         void *user_data;
73         unsigned int id;
74 };
75
76 static GSList *sink_callbacks = NULL;
77
78 static char *str_state[] = {
79         "SINK_STATE_DISCONNECTED",
80         "SINK_STATE_CONNECTING",
81         "SINK_STATE_CONNECTED",
82         "SINK_STATE_PLAYING",
83 };
84
85 static void sink_set_state(struct sink *sink, sink_state_t new_state)
86 {
87         struct btd_service *service = sink->service;
88         struct btd_device *dev = btd_service_get_device(service);
89         sink_state_t old_state = sink->state;
90         GSList *l;
91
92         sink->state = new_state;
93
94         DBG("State changed %s: %s -> %s", device_get_path(dev),
95                                 str_state[old_state], str_state[new_state]);
96
97         for (l = sink_callbacks; l != NULL; l = l->next) {
98                 struct sink_state_callback *cb = l->data;
99
100                 if (cb->service != service)
101                         continue;
102
103                 cb->cb(service, old_state, new_state, cb->user_data);
104         }
105
106         if (new_state != SINK_STATE_DISCONNECTED)
107                 return;
108
109 #ifdef __TIZEN_PATCH__
110         btd_service_disconnecting_complete(service, 0);
111 #endif
112
113         if (sink->session) {
114                 avdtp_unref(sink->session);
115                 sink->session = NULL;
116 #ifdef __TIZEN_PATCH__
117                 sink->connect_id = 0;
118                 sink->disconnect_id = 0;
119 #endif
120         }
121 }
122
123 static void avdtp_state_callback(struct btd_device *dev,
124                                         struct avdtp *session,
125                                         avdtp_session_state_t old_state,
126                                         avdtp_session_state_t new_state,
127                                         void *user_data)
128 {
129         struct sink *sink = user_data;
130
131         switch (new_state) {
132         case AVDTP_SESSION_STATE_DISCONNECTED:
133                 sink_set_state(sink, SINK_STATE_DISCONNECTED);
134                 break;
135         case AVDTP_SESSION_STATE_CONNECTING:
136                 sink_set_state(sink, SINK_STATE_CONNECTING);
137                 break;
138         case AVDTP_SESSION_STATE_CONNECTED:
139                 break;
140         }
141
142         sink->session_state = new_state;
143 }
144
145 static void stream_state_changed(struct avdtp_stream *stream,
146                                         avdtp_state_t old_state,
147                                         avdtp_state_t new_state,
148                                         struct avdtp_error *err,
149                                         void *user_data)
150 {
151         struct btd_service *service = user_data;
152         struct sink *sink = btd_service_get_user_data(service);
153
154         if (err)
155                 return;
156
157         switch (new_state) {
158         case AVDTP_STATE_IDLE:
159 #ifndef __TIZEN_PATCH__
160                 btd_service_disconnecting_complete(sink->service, 0);
161 #endif
162
163                 if (sink->disconnect_id > 0) {
164                         a2dp_cancel(sink->disconnect_id);
165                         sink->disconnect_id = 0;
166                 }
167
168                 if (sink->session) {
169                         avdtp_unref(sink->session);
170                         sink->session = NULL;
171                 }
172                 sink->stream = NULL;
173                 sink->cb_id = 0;
174                 break;
175         case AVDTP_STATE_OPEN:
176                 btd_service_connecting_complete(sink->service, 0);
177                 sink_set_state(sink, SINK_STATE_CONNECTED);
178                 break;
179         case AVDTP_STATE_STREAMING:
180                 sink_set_state(sink, SINK_STATE_PLAYING);
181                 break;
182         case AVDTP_STATE_CONFIGURED:
183         case AVDTP_STATE_CLOSING:
184         case AVDTP_STATE_ABORTING:
185         default:
186                 break;
187         }
188
189         sink->stream_state = new_state;
190 }
191
192 static void stream_setup_complete(struct avdtp *session, struct a2dp_sep *sep,
193                                         struct avdtp_stream *stream,
194                                         struct avdtp_error *err, void *user_data)
195 {
196         struct sink *sink = user_data;
197
198         sink->connect_id = 0;
199
200         if (stream)
201                 return;
202
203         avdtp_unref(sink->session);
204         sink->session = NULL;
205 #ifdef __TIZEN_PATCH__
206         if (err && avdtp_error_category(err) == AVDTP_ERRNO
207                                 && avdtp_error_posix_errno(err) != EHOSTDOWN)
208 #else
209         if (avdtp_error_category(err) == AVDTP_ERRNO
210                                 && avdtp_error_posix_errno(err) != EHOSTDOWN)
211 #endif
212                 btd_service_connecting_complete(sink->service, -EAGAIN);
213         else
214                 btd_service_connecting_complete(sink->service, -EIO);
215 }
216
217 static void select_complete(struct avdtp *session, struct a2dp_sep *sep,
218                         GSList *caps, void *user_data)
219 {
220         struct sink *sink = user_data;
221         int id;
222
223         sink->connect_id = 0;
224
225         id = a2dp_config(session, sep, stream_setup_complete, caps, sink);
226         if (id == 0)
227                 goto failed;
228
229         sink->connect_id = id;
230         return;
231
232 failed:
233         btd_service_connecting_complete(sink->service, -EIO);
234
235         avdtp_unref(sink->session);
236         sink->session = NULL;
237 }
238
239 static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
240                                 void *user_data)
241 {
242         struct sink *sink = user_data;
243         int id, perr;
244
245         sink->connect_id = 0;
246
247         if (err) {
248                 avdtp_unref(sink->session);
249                 sink->session = NULL;
250
251                 perr = -avdtp_error_posix_errno(err);
252                 if (perr != -EHOSTDOWN) {
253                         if (avdtp_error_category(err) == AVDTP_ERRNO)
254                                 perr = -EAGAIN;
255                         else
256                                 perr = -EIO;
257                 }
258                 goto failed;
259         }
260
261         DBG("Discovery complete");
262
263         id = a2dp_select_capabilities(sink->session, AVDTP_SEP_TYPE_SINK, NULL,
264                                                 select_complete, sink);
265         if (id == 0) {
266                 perr = -EIO;
267                 goto failed;
268         }
269
270         sink->connect_id = id;
271         return;
272
273 failed:
274         btd_service_connecting_complete(sink->service, perr);
275         avdtp_unref(sink->session);
276         sink->session = NULL;
277 }
278
279 gboolean sink_setup_stream(struct btd_service *service, struct avdtp *session)
280 {
281         struct sink *sink = btd_service_get_user_data(service);
282
283         if (sink->connect_id > 0 || sink->disconnect_id > 0)
284                 return FALSE;
285
286         if (session && !sink->session)
287                 sink->session = avdtp_ref(session);
288
289         if (!sink->session)
290                 return FALSE;
291
292         sink->connect_id = a2dp_discover(sink->session, discovery_complete,
293                                                                 sink);
294         if (sink->connect_id == 0)
295                 return FALSE;
296
297         return TRUE;
298 }
299
300 int sink_connect(struct btd_service *service)
301 {
302         struct sink *sink = btd_service_get_user_data(service);
303
304 #ifndef __TIZEN_PATCH__
305         if (!sink->session)
306                 sink->session = a2dp_avdtp_get(btd_service_get_device(service));
307
308         if (!sink->session) {
309                 DBG("Unable to get a session");
310                 return -EIO;
311         }
312 #endif
313
314         if (sink->connect_id > 0 || sink->disconnect_id > 0)
315                 return -EBUSY;
316
317         if (sink->state == SINK_STATE_CONNECTING)
318                 return -EBUSY;
319
320         if (sink->stream_state >= AVDTP_STATE_OPEN)
321                 return -EALREADY;
322
323 #ifdef __TIZEN_PATCH__
324         if (!sink->session)
325                 sink->session = a2dp_avdtp_get(btd_service_get_device(service));
326
327         if (!sink->session) {
328                 DBG("Unable to get a session");
329                 return -EIO;
330         }
331 #endif
332
333         if (!sink_setup_stream(service, NULL)) {
334                 DBG("Failed to create a stream");
335                 return -EIO;
336         }
337
338         DBG("stream creation in progress");
339
340         return 0;
341 }
342
343 static void sink_free(struct btd_service *service)
344 {
345         struct sink *sink = btd_service_get_user_data(service);
346
347         if (sink->cb_id)
348                 avdtp_stream_remove_cb(sink->session, sink->stream,
349                                         sink->cb_id);
350
351 #ifdef __TIZEN_PATCH__
352         if (sink->session) {
353                 /* We need to clear the avdtp discovery procedure */
354                 finalize_discovery(sink->session, ECANCELED);
355                 avdtp_unref(sink->session);
356         }
357 #else
358         if (sink->session)
359                 avdtp_unref(sink->session);
360 #endif
361
362         if (sink->connect_id > 0) {
363                 btd_service_connecting_complete(sink->service, -ECANCELED);
364                 a2dp_cancel(sink->connect_id);
365                 sink->connect_id = 0;
366         }
367
368         if (sink->disconnect_id > 0) {
369                 btd_service_disconnecting_complete(sink->service, -ECANCELED);
370                 a2dp_cancel(sink->disconnect_id);
371                 sink->disconnect_id = 0;
372         }
373
374         avdtp_remove_state_cb(sink->avdtp_callback_id);
375         btd_service_unref(sink->service);
376
377         g_free(sink);
378 }
379
380 void sink_unregister(struct btd_service *service)
381 {
382         struct btd_device *dev = btd_service_get_device(service);
383
384         DBG("%s", device_get_path(dev));
385
386         sink_free(service);
387 }
388
389 int sink_init(struct btd_service *service)
390 {
391         struct btd_device *dev = btd_service_get_device(service);
392         struct sink *sink;
393
394         DBG("%s", device_get_path(dev));
395
396         sink = g_new0(struct sink, 1);
397
398         sink->service = btd_service_ref(service);
399
400         sink->avdtp_callback_id = avdtp_add_state_cb(dev, avdtp_state_callback,
401                                                                         sink);
402
403         btd_service_set_user_data(service, sink);
404
405         return 0;
406 }
407
408 gboolean sink_is_active(struct btd_service *service)
409 {
410         struct sink *sink = btd_service_get_user_data(service);
411
412         if (sink->session)
413                 return TRUE;
414
415         return FALSE;
416 }
417
418 gboolean sink_new_stream(struct btd_service *service, struct avdtp *session,
419                                 struct avdtp_stream *stream)
420 {
421         struct sink *sink = btd_service_get_user_data(service);
422
423         if (sink->stream)
424                 return FALSE;
425
426         if (!sink->session)
427                 sink->session = avdtp_ref(session);
428
429         sink->stream = stream;
430
431         sink->cb_id = avdtp_stream_add_cb(session, stream,
432                                                 stream_state_changed, service);
433
434         return TRUE;
435 }
436
437 int sink_disconnect(struct btd_service *service)
438 {
439         struct sink *sink = btd_service_get_user_data(service);
440
441         if (!sink->session)
442                 return -ENOTCONN;
443
444         /* cancel pending connect */
445         if (sink->connect_id > 0) {
446                 a2dp_cancel(sink->connect_id);
447                 sink->connect_id = 0;
448                 btd_service_disconnecting_complete(sink->service, 0);
449
450                 avdtp_unref(sink->session);
451                 sink->session = NULL;
452
453                 return 0;
454         }
455
456         /* disconnect already ongoing */
457         if (sink->disconnect_id > 0)
458                 return -EBUSY;
459
460         if (!sink->stream)
461                 return -ENOTCONN;
462
463         return avdtp_close(sink->session, sink->stream, FALSE);
464 }
465
466 unsigned int sink_add_state_cb(struct btd_service *service, sink_state_cb cb,
467                                                                 void *user_data)
468 {
469         struct sink_state_callback *state_cb;
470         static unsigned int id = 0;
471
472         state_cb = g_new(struct sink_state_callback, 1);
473         state_cb->cb = cb;
474         state_cb->service = service;
475         state_cb->user_data = user_data;
476         state_cb->id = ++id;
477
478         sink_callbacks = g_slist_append(sink_callbacks, state_cb);
479
480         return state_cb->id;
481 }
482
483 gboolean sink_remove_state_cb(unsigned int id)
484 {
485         GSList *l;
486
487         for (l = sink_callbacks; l != NULL; l = l->next) {
488                 struct sink_state_callback *cb = l->data;
489                 if (cb && cb->id == id) {
490                         sink_callbacks = g_slist_remove(sink_callbacks, cb);
491                         g_free(cb);
492                         return TRUE;
493                 }
494         }
495
496         return FALSE;
497 }