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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* Copyright 2018 The Android Open Source Project
*
* 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 "avrcp_service.h"
#include <base/bind.h>
#include <base/logging.h>
#include <base/task/cancelable_task_tracker.h>
#include <base/threading/thread.h>
#include <mutex>
#include <sstream>
#include "abstract_message_loop.h"
#include "bta/sys/bta_sys.h"
#include "btif_av.h"
#include "btif_common.h"
#include "btif_dm.h"
#include "device.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/btu.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"
namespace bluetooth {
namespace avrcp {
// Static variables and interface definitions
AvrcpService* AvrcpService::instance_ = nullptr;
AvrcpService::ServiceInterfaceImpl* AvrcpService::service_interface_ = nullptr;
void do_in_avrcp_jni(const base::Closure& task) {
do_in_jni_thread(FROM_HERE, task);
}
class A2dpInterfaceImpl : public A2dpInterface {
RawAddress active_peer() override { return btif_av_source_active_peer(); }
bool is_peer_in_silence_mode(const RawAddress& peer_address) override {
return btif_av_is_peer_silenced(peer_address);
}
} a2dp_interface_;
class AvrcpInterfaceImpl : public AvrcpInterface {
public:
uint16_t GetAvrcpVersion() {
return AVRC_GetProfileVersion();
}
uint16_t AddRecord(uint16_t service_uuid, const char* p_service_name,
const char* p_provider_name, uint16_t categories,
uint32_t sdp_handle, bool browse_supported,
uint16_t profile_version,
uint16_t cover_art_psm) override {
return AVRC_AddRecord(service_uuid, p_service_name, p_provider_name,
categories, sdp_handle, browse_supported,
profile_version, cover_art_psm);
}
uint16_t RemoveRecord(uint32_t sdp_handle) {
return AVRC_RemoveRecord(sdp_handle);
}
uint16_t FindService(uint16_t service_uuid, const RawAddress& bd_addr,
tAVRC_SDP_DB_PARAMS* p_db,
tAVRC_FIND_CBACK p_cback) override {
return AVRC_FindService(service_uuid, bd_addr, p_db, p_cback);
}
uint16_t Open(uint8_t* p_handle, tAVRC_CONN_CB* p_ccb,
const RawAddress& bd_addr) override {
return AVRC_Open(p_handle, p_ccb, bd_addr);
}
uint16_t OpenBrowse(uint8_t handle, uint8_t conn_role) override {
return AVRC_OpenBrowse(handle, conn_role);
}
uint16_t GetPeerMtu(uint8_t handle) override {
return AVCT_GetPeerMtu(handle);
}
uint16_t GetBrowseMtu(uint8_t handle) override {
return AVCT_GetBrowseMtu(handle);
}
uint16_t Close(uint8_t handle) override { return AVRC_Close(handle); }
uint16_t CloseBrowse(uint8_t handle) override {
return AVRC_CloseBrowse(handle);
}
uint16_t MsgReq(uint8_t handle, uint8_t label, uint8_t ctype,
BT_HDR* p_pkt) override {
return AVRC_MsgReq(handle, label, ctype, p_pkt);
}
} avrcp_interface_;
class SdpInterfaceImpl : public SdpInterface {
public:
bool InitDiscoveryDb(tSDP_DISCOVERY_DB* a, uint32_t b, uint16_t c,
const bluetooth::Uuid* d, uint16_t e,
uint16_t* f) override {
return SDP_InitDiscoveryDb(a, b, c, d, e, f);
}
bool ServiceSearchAttributeRequest(const RawAddress& a, tSDP_DISCOVERY_DB* b,
tSDP_DISC_CMPL_CB* c) override {
return SDP_ServiceSearchAttributeRequest(a, b, c);
}
tSDP_DISC_REC* FindServiceInDb(tSDP_DISCOVERY_DB* a, uint16_t b,
t_sdp_disc_rec* c) override {
return SDP_FindServiceInDb(a, b, c);
}
tSDP_DISC_ATTR* FindAttributeInRec(t_sdp_disc_rec* a, uint16_t b) override {
return SDP_FindAttributeInRec(a, b);
}
bool FindProfileVersionInRec(t_sdp_disc_rec* a, uint16_t b,
uint16_t* c) override {
return SDP_FindProfileVersionInRec(a, b, c);
}
} sdp_interface_;
// A wrapper class for the media callbacks that handles thread
// switching/synchronization so the devices don't have to worry about it.
class MediaInterfaceWrapper : public MediaInterface {
public:
MediaInterfaceWrapper(MediaInterface* cb) : wrapped_(cb){};
void SendKeyEvent(uint8_t key, KeyState state) override {
do_in_avrcp_jni(base::Bind(&MediaInterface::SendKeyEvent,
base::Unretained(wrapped_), key, state));
}
void GetSongInfo(SongInfoCallback info_cb) override {
auto cb_lambda = [](SongInfoCallback cb, SongInfo data) {
do_in_main_thread(FROM_HERE, base::Bind(cb, data));
};
auto bound_cb = base::Bind(cb_lambda, info_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::GetSongInfo,
base::Unretained(wrapped_), bound_cb));
}
void GetPlayStatus(PlayStatusCallback status_cb) override {
auto cb_lambda = [](PlayStatusCallback cb, PlayStatus status) {
do_in_main_thread(FROM_HERE, base::Bind(cb, status));
};
auto bound_cb = base::Bind(cb_lambda, status_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::GetPlayStatus,
base::Unretained(wrapped_), bound_cb));
}
void GetNowPlayingList(NowPlayingCallback now_playing_cb) override {
auto cb_lambda = [](NowPlayingCallback cb, std::string curr_media_id,
std::vector<SongInfo> song_list) {
do_in_main_thread(FROM_HERE,
base::Bind(cb, curr_media_id, std::move(song_list)));
};
auto bound_cb = base::Bind(cb_lambda, now_playing_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::GetNowPlayingList,
base::Unretained(wrapped_), bound_cb));
}
void GetMediaPlayerList(MediaListCallback list_cb) override {
auto cb_lambda = [](MediaListCallback cb, uint16_t curr_player,
std::vector<MediaPlayerInfo> player_list) {
do_in_main_thread(FROM_HERE,
base::Bind(cb, curr_player, std::move(player_list)));
};
auto bound_cb = base::Bind(cb_lambda, list_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::GetMediaPlayerList,
base::Unretained(wrapped_), bound_cb));
}
void GetFolderItems(uint16_t player_id, std::string media_id,
FolderItemsCallback folder_cb) override {
auto cb_lambda = [](FolderItemsCallback cb,
std::vector<ListItem> item_list) {
do_in_main_thread(FROM_HERE, base::Bind(cb, std::move(item_list)));
};
auto bound_cb = base::Bind(cb_lambda, folder_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::GetFolderItems,
base::Unretained(wrapped_), player_id, media_id,
bound_cb));
}
void SetBrowsedPlayer(uint16_t player_id,
SetBrowsedPlayerCallback browse_cb) override {
auto cb_lambda = [](SetBrowsedPlayerCallback cb, bool success,
std::string root_id, uint32_t num_items) {
do_in_main_thread(FROM_HERE, base::Bind(cb, success, root_id, num_items));
};
auto bound_cb = base::Bind(cb_lambda, browse_cb);
do_in_avrcp_jni(base::Bind(&MediaInterface::SetBrowsedPlayer,
base::Unretained(wrapped_), player_id,
bound_cb));
}
void PlayItem(uint16_t player_id, bool now_playing,
std::string media_id) override {
do_in_avrcp_jni(base::Bind(&MediaInterface::PlayItem,
base::Unretained(wrapped_), player_id,
now_playing, media_id));
}
void SetActiveDevice(const RawAddress& address) override {
do_in_avrcp_jni(base::Bind(&MediaInterface::SetActiveDevice,
base::Unretained(wrapped_), address));
}
void RegisterUpdateCallback(MediaCallbacks* callback) override {
wrapped_->RegisterUpdateCallback(callback);
}
void UnregisterUpdateCallback(MediaCallbacks* callback) override {
wrapped_->UnregisterUpdateCallback(callback);
}
private:
MediaInterface* wrapped_;
};
// A wrapper class for the media callbacks that handles thread
// switching/synchronization so the devices don't have to worry about it.
class VolumeInterfaceWrapper : public VolumeInterface {
public:
VolumeInterfaceWrapper(VolumeInterface* interface) : wrapped_(interface){};
void DeviceConnected(const RawAddress& bdaddr) override {
do_in_avrcp_jni(
base::Bind(static_cast<void (VolumeInterface::*)(const RawAddress&)>(
&VolumeInterface::DeviceConnected),
base::Unretained(wrapped_), bdaddr));
}
void DeviceConnected(const RawAddress& bdaddr, VolumeChangedCb cb) override {
auto cb_lambda = [](VolumeChangedCb cb, int8_t volume) {
do_in_main_thread(FROM_HERE, base::Bind(cb, volume));
};
auto bound_cb = base::Bind(cb_lambda, cb);
do_in_avrcp_jni(base::Bind(static_cast<void (VolumeInterface::*)(
const RawAddress&, VolumeChangedCb)>(
&VolumeInterface::DeviceConnected),
base::Unretained(wrapped_), bdaddr, bound_cb));
}
void DeviceDisconnected(const RawAddress& bdaddr) override {
do_in_avrcp_jni(base::Bind(&VolumeInterface::DeviceDisconnected,
base::Unretained(wrapped_), bdaddr));
}
void SetVolume(int8_t volume) override {
do_in_avrcp_jni(base::Bind(&VolumeInterface::SetVolume,
base::Unretained(wrapped_), volume));
}
private:
VolumeInterface* wrapped_;
};
void AvrcpService::Init(MediaInterface* media_interface,
VolumeInterface* volume_interface) {
LOG(INFO) << "AVRCP Target Service started";
profile_version = avrcp_interface_.GetAvrcpVersion();
uint16_t supported_features = GetSupportedFeatures(profile_version);
sdp_record_handle = SDP_CreateRecord();
avrcp_interface_.AddRecord(UUID_SERVCLASS_AV_REM_CTRL_TARGET,
"AV Remote Control Target", NULL,
supported_features, sdp_record_handle, true,
profile_version, 0);
bta_sys_add_uuid(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
ct_sdp_record_handle = SDP_CreateRecord();
avrcp_interface_.AddRecord(UUID_SERVCLASS_AV_REMOTE_CONTROL,
"AV Remote Control", NULL, AVRCP_SUPF_TG_CT,
ct_sdp_record_handle, false, AVRC_REV_1_3, 0);
bta_sys_add_uuid(UUID_SERVCLASS_AV_REMOTE_CONTROL);
media_interface_ = new MediaInterfaceWrapper(media_interface);
media_interface->RegisterUpdateCallback(instance_);
VolumeInterfaceWrapper* wrapped_volume_interface = nullptr;
if (volume_interface != nullptr) {
wrapped_volume_interface = new VolumeInterfaceWrapper(volume_interface);
}
volume_interface_ = wrapped_volume_interface;
ConnectionHandler::Initialize(
base::Bind(&AvrcpService::DeviceCallback, base::Unretained(instance_)),
&avrcp_interface_, &sdp_interface_, wrapped_volume_interface);
connection_handler_ = ConnectionHandler::Get();
}
uint16_t AvrcpService::GetSupportedFeatures(uint16_t profile_version) {
switch (profile_version) {
case AVRC_REV_1_6:
return AVRCP_SUPF_TG_1_6;
case AVRC_REV_1_5:
return AVRCP_SUPF_TG_1_5;
case AVRC_REV_1_4:
return AVRCP_SUPF_TG_1_4;
case AVRC_REV_1_3:
return AVRCP_SUPF_TG_1_3;
}
return AVRCP_SUPF_TG_DEFAULT;
}
void AvrcpService::Cleanup() {
LOG(INFO) << "AVRCP Target Service stopped";
avrcp_interface_.RemoveRecord(sdp_record_handle);
bta_sys_remove_uuid(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
sdp_record_handle = -1;
avrcp_interface_.RemoveRecord(ct_sdp_record_handle);
bta_sys_remove_uuid(UUID_SERVCLASS_AV_REMOTE_CONTROL);
ct_sdp_record_handle = -1;
connection_handler_->CleanUp();
connection_handler_ = nullptr;
if (volume_interface_ != nullptr) {
delete volume_interface_;
}
delete media_interface_;
}
void AvrcpService::RegisterBipServer(int psm) {
LOG(INFO) << "AVRCP Target Service has registered a BIP OBEX server, psm="
<< psm;
avrcp_interface_.RemoveRecord(sdp_record_handle);
uint16_t supported_features
= GetSupportedFeatures(profile_version) | AVRC_SUPF_TG_PLAYER_COVER_ART;
sdp_record_handle = SDP_CreateRecord();
avrcp_interface_.AddRecord(UUID_SERVCLASS_AV_REM_CTRL_TARGET,
"AV Remote Control Target", NULL,
supported_features, sdp_record_handle, true,
profile_version, psm);
}
void AvrcpService::UnregisterBipServer() {
LOG(INFO) << "AVRCP Target Service has unregistered a BIP OBEX server";
avrcp_interface_.RemoveRecord(sdp_record_handle);
uint16_t supported_features = GetSupportedFeatures(profile_version);
sdp_record_handle = SDP_CreateRecord();
avrcp_interface_.AddRecord(UUID_SERVCLASS_AV_REM_CTRL_TARGET,
"AV Remote Control Target", NULL,
supported_features, sdp_record_handle, true,
profile_version, 0);
}
AvrcpService* AvrcpService::Get() {
CHECK(instance_);
return instance_;
}
ServiceInterface* AvrcpService::GetServiceInterface() {
if (service_interface_ == nullptr) {
service_interface_ = new ServiceInterfaceImpl();
}
return service_interface_;
}
void AvrcpService::ConnectDevice(const RawAddress& bdaddr) {
LOG(INFO) << __PRETTY_FUNCTION__ << ": address=" << bdaddr.ToString();
connection_handler_->ConnectDevice(bdaddr);
}
void AvrcpService::DisconnectDevice(const RawAddress& bdaddr) {
LOG(INFO) << __PRETTY_FUNCTION__ << ": address=" << bdaddr.ToString();
connection_handler_->DisconnectDevice(bdaddr);
}
void AvrcpService::SetBipClientStatus(const RawAddress& bdaddr,
bool connected) {
LOG(INFO) << __PRETTY_FUNCTION__ << ": address=" << bdaddr.ToString()
<< ", connected=" << connected;
connection_handler_->SetBipClientStatus(bdaddr, connected);
}
void AvrcpService::SendMediaUpdate(bool track_changed, bool play_state,
bool queue) {
LOG(INFO) << __PRETTY_FUNCTION__ << " track_changed=" << track_changed
<< " : "
<< " play_state=" << play_state << " : "
<< " queue=" << queue;
// This function may be called on any thread, we need to make sure that the
// device update happens on the main thread.
for (const auto& device :
instance_->connection_handler_->GetListOfDevices()) {
do_in_main_thread(FROM_HERE,
base::Bind(&Device::SendMediaUpdate, device.get()->Get(), track_changed, play_state, queue));
}
}
void AvrcpService::SendFolderUpdate(bool available_players,
bool addressed_players, bool uids) {
LOG(INFO) << __PRETTY_FUNCTION__ << " available_players=" << available_players
<< " : "
<< " addressed_players=" << addressed_players << " : "
<< " uids=" << uids;
// Ensure that the update is posted to the correct thread
for (const auto& device :
instance_->connection_handler_->GetListOfDevices()) {
do_in_main_thread(FROM_HERE, base::Bind(&Device::SendFolderUpdate, device.get()->Get(), available_players,
addressed_players, uids));
}
}
// Send out the track changed info to update the playback state for each device
void AvrcpService::SendActiveDeviceChanged(const RawAddress& address) {
SendMediaUpdate(false, true, false);
}
void AvrcpService::DeviceCallback(std::shared_ptr<Device> new_device) {
if (new_device == nullptr) return;
// TODO (apanicke): Pass the interfaces into the connection handler
// so that the devices can be created with any interfaces they need.
new_device->RegisterInterfaces(media_interface_, &a2dp_interface_,
volume_interface_);
}
// Service Interface
void AvrcpService::ServiceInterfaceImpl::Init(
MediaInterface* media_interface, VolumeInterface* volume_interface) {
std::lock_guard<std::mutex> lock(service_interface_lock_);
// TODO: This function should block until the service is completely up so
// that its possible to call Get() on the service immediately after calling
// init without issues.
CHECK(instance_ == nullptr);
instance_ = new AvrcpService();
do_in_main_thread(FROM_HERE,
base::Bind(&AvrcpService::Init, base::Unretained(instance_),
media_interface, volume_interface));
}
void AvrcpService::ServiceInterfaceImpl::RegisterBipServer(int psm) {
std::lock_guard<std::mutex> lock(service_interface_lock_);
CHECK(instance_ != nullptr);
do_in_main_thread(FROM_HERE, base::Bind(&AvrcpService::RegisterBipServer,
base::Unretained(instance_), psm));
}
void AvrcpService::ServiceInterfaceImpl::UnregisterBipServer() {
std::lock_guard<std::mutex> lock(service_interface_lock_);
CHECK(instance_ != nullptr);
do_in_main_thread(FROM_HERE, base::Bind(&AvrcpService::UnregisterBipServer,
base::Unretained(instance_)));
}
bool AvrcpService::ServiceInterfaceImpl::ConnectDevice(
const RawAddress& bdaddr) {
std::lock_guard<std::mutex> lock(service_interface_lock_);
CHECK(instance_ != nullptr);
do_in_main_thread(FROM_HERE, base::Bind(&AvrcpService::ConnectDevice,
base::Unretained(instance_), bdaddr));
return true;
}
bool AvrcpService::ServiceInterfaceImpl::DisconnectDevice(
const RawAddress& bdaddr) {
std::lock_guard<std::mutex> lock(service_interface_lock_);
CHECK(instance_ != nullptr);
do_in_main_thread(FROM_HERE, base::Bind(&AvrcpService::DisconnectDevice,
base::Unretained(instance_), bdaddr));
return true;
}
void AvrcpService::ServiceInterfaceImpl::SetBipClientStatus(
const RawAddress& bdaddr, bool connected) {
std::lock_guard<std::mutex> lock(service_interface_lock_);
CHECK(instance_ != nullptr);
do_in_main_thread(FROM_HERE, base::Bind(&AvrcpService::SetBipClientStatus,
base::Unretained(instance_), bdaddr,
connected));
}
bool AvrcpService::ServiceInterfaceImpl::Cleanup() {
std::lock_guard<std::mutex> lock(service_interface_lock_);
if (instance_ == nullptr) return false;
do_in_main_thread(FROM_HERE,
base::Bind(&AvrcpService::Cleanup, base::Owned(instance_)));
// Setting instance to nullptr here is fine since it will be deleted on the
// other thread.
instance_ = nullptr;
return true;
}
void AvrcpService::DebugDump(int fd) {
if (instance_ == nullptr) {
dprintf(fd, "\nAVRCP Target Service not started\n");
return;
}
auto handler = instance_->connection_handler_;
if (handler == nullptr) {
dprintf(fd, "\nAVRCP connection handler is null\n");
return;
}
auto device_list = handler->GetListOfDevices();
dprintf(fd, "\nAVRCP Target Native Service: %zu devices\n",
device_list.size());
std::stringstream stream;
{
ScopedIndent indent(stream);
for (const auto& device : device_list) {
stream << *device << std::endl;
}
}
dprintf(fd, "%s", stream.str().c_str());
}
} // namespace avrcp
} // namespace bluetooth
|