AVRCP Browsing - Browse Media Players and Media Information

Source Code: avrcp_browsing_client.c

This example demonstrates how to use the AVRCP Controller Browsing service to browse madia players and media information on a remote AVRCP Source device.

To test with a remote device, e.g. a mobile phone, pair from the remote device with the demo, then use the UI for browsing. If HAVE_BTSTACK_STDIN is set, press SPACE on the console to show the available AVDTP and AVRCP commands.

Main Application Setup

The Listing here shows how to setup AVRCP Controller Browsing service. To announce AVRCP Controller Browsing service, you need to create corresponding SDP record and register it with the SDP service. You'll also need to register several packet handlers:

static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void a2dp_sink_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);

#ifdef HAVE_BTSTACK_STDIN
static void stdin_process(char cmd);
#endif


int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
  (void)argc;
  (void)argv;

  // Initialize L2CAP.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();

#ifdef ENABLE_BLE
  // Initialize LE Security Manager. Needed for cross-transport key derivation
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L70">a2dp_sink_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L106">a2dp_sink_register_packet_handler</a>(&amp;a2dp_sink_packet_handler);

  avdtp_stream_endpoint_t * local_stream_endpoint = <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L83">a2dp_sink_create_stream_endpoint</a>(AVDTP_AUDIO, 
    AVDTP_CODEC_SBC, media_sbc_codec_capabilities, sizeof(media_sbc_codec_capabilities), 
    media_sbc_codec_configuration, sizeof(media_sbc_codec_configuration));
  if (!local_stream_endpoint){
    printf("A2DP Sink: not enough memory to create local stream endpoint\n");
    return 1;
  }
  a2dp_local_seid = avdtp_local_seid(local_stream_endpoint);

  // Initialize AVRCP service.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp.h#L990">avrcp_init</a>();
  // Initialize AVRCP Controller &amp; Target Service.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L83">avrcp_controller_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_target.h#L80">avrcp_target_init</a>();

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp.h#L996">avrcp_register_packet_handler</a>(&amp;avrcp_packet_handler);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L89">avrcp_controller_register_packet_handler</a>(&amp;avrcp_packet_handler);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_target.h#L86">avrcp_target_register_packet_handler</a>(&amp;avrcp_packet_handler);

  // Initialize AVRCP Browsing Service. 
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing.h#L64">avrcp_browsing_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing_controller.h#L59">avrcp_browsing_controller_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing_target.h#L59">avrcp_browsing_target_init</a>();

  // Register for HCI events.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing_controller.h#L68">avrcp_browsing_controller_register_packet_handler</a>(&amp;avrcp_browsing_controller_packet_handler);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing_target.h#L72">avrcp_browsing_target_register_packet_handler</a>(&amp;avrcp_browsing_controller_packet_handler);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_browsing.h#L70">avrcp_browsing_register_packet_handler</a>(&amp;avrcp_browsing_controller_packet_handler);

  // Initialize SDP. 
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  // setup AVDTP sink
  memset(sdp_avdtp_sink_service_buffer, 0, sizeof(sdp_avdtp_sink_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/a2dp_sink.h#L65">a2dp_sink_create_sdp_record</a>(sdp_avdtp_sink_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), AVDTP_SINK_FEATURE_MASK_HEADPHONE, NULL, NULL);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( sdp_avdtp_sink_service_buffer) &lt;= sizeof(sdp_avdtp_sink_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(sdp_avdtp_sink_service_buffer);

  // Create AVRCP service record and register it with SDP.
  memset(sdp_avrcp_browsing_controller_service_buffer, 0, sizeof(sdp_avrcp_browsing_controller_service_buffer));

  uint16_t supported_features = AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER;
#ifdef AVRCP_BROWSING_ENABLED
  supported_features |= AVRCP_FEATURE_MASK_BROWSING;
#endif
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/avrcp_controller.h#L78">avrcp_controller_create_sdp_record</a>(sdp_avrcp_browsing_controller_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), supported_features, NULL, NULL);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( sdp_avrcp_browsing_controller_service_buffer) &lt;= sizeof(sdp_avrcp_browsing_controller_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(sdp_avrcp_browsing_controller_service_buffer);

  // Set local name with a template Bluetooth address, that will be automatically
  // replaced with a actual address once it is available, i.e. when BTstack boots
  // up and starts talking to a Bluetooth module.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("AVRCP Browsing Client 00:00:00:00:00:00");
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x200408);

  // Register for HCI events.
  hci_event_callback_registration.callback = &amp;avrcp_browsing_controller_packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&amp;hci_event_callback_registration);


#ifdef HAVE_BTSTACK_STDIN
  // Parse human readable Bluetooth address.
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L287">sscanf_bd_addr</a>(device_addr_string, device_addr);
  btstack_stdin_setup(stdin_process);
#endif
  printf("Starting BTstack ...\n");
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);
  return 0;
}