int a2dp_source_stream_send_media_payload(uint16_t a2dp_cid, uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){ avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); if (!stream_endpoint) { log_error("A2DP source: no stream_endpoint with seid %d", local_seid); return 0; } if (a2dp_source_context.avdtp_cid != a2dp_cid){ log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid); return 0; } if (stream_endpoint->l2cap_media_cid == 0){ log_error("A2DP source: no media connection for seid %d", local_seid); return 0; } int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); int offset = 0; l2cap_reserve_packet_buffer(); uint8_t * media_packet = l2cap_get_outgoing_buffer(); //int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); a2dp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number); a2dp_source_copy_media_payload(media_packet, size, &offset, storage, num_bytes_to_copy, num_frames); stream_endpoint->sequence_number++; l2cap_send_prepared(stream_endpoint->l2cap_media_cid, offset); return size; }
static int avdtp_source_stream_send_media_payload(uint8_t local_seid, uint8_t * storage, int num_bytes_to_copy, uint8_t num_frames, uint8_t marker){ avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); if (!stream_endpoint) { log_error("no stream_endpoint found for seid %d", local_seid); return 0; } if (stream_endpoint->l2cap_media_cid == 0){ log_error("no media cid found for seid %d", local_seid); return 0; } int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); int offset = 0; // HACK / PTS requests ERTM although we did not offer it in L2CAP Information Request. Withouth ERTM support, default MTU of 48 will be used, but that's to // small for a 44.1kHz/16/8/Stereo/bitpool 53 sbc frame of 119 bytes (+ media info) size = 0x290; l2cap_reserve_packet_buffer(); uint8_t * media_packet = l2cap_get_outgoing_buffer(); //int size = l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid); a2dp_source_setup_media_header(media_packet, size, &offset, marker, stream_endpoint->sequence_number); a2dp_source_copy_media_payload(media_packet, size, &offset, storage, num_bytes_to_copy, num_frames); stream_endpoint->sequence_number++; l2cap_send_prepared(stream_endpoint->l2cap_media_cid, offset); return size; }
uint8_t a2dp_source_establish_stream(bd_addr_t remote_addr, uint8_t loc_seid, uint16_t * a2dp_cid){ sc.local_stream_endpoint = avdtp_stream_endpoint_for_seid(loc_seid, &a2dp_source_context); if (!sc.local_stream_endpoint){ log_error(" no local_stream_endpoint for seid %d", loc_seid); return AVDTP_SEID_DOES_NOT_EXIST; } memcpy(sc.remote_addr, remote_addr, 6); return avdtp_source_connect(remote_addr, a2dp_cid); }
void a2dp_source_stream_endpoint_request_can_send_now(uint16_t a2dp_cid, uint8_t local_seid){ avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); if (!stream_endpoint) { log_error("A2DP source: no stream_endpoint with seid %d", local_seid); return; } if (a2dp_source_context.avdtp_cid != a2dp_cid){ log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid); return; } stream_endpoint->send_stream = 1; avdtp_request_can_send_now_initiator(stream_endpoint->connection, stream_endpoint->l2cap_media_cid); }
int a2dp_max_media_payload_size(uint16_t a2dp_cid, uint8_t local_seid){ avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(local_seid, &a2dp_source_context); if (!stream_endpoint) { log_error("A2DP source: no stream_endpoint with seid %d", local_seid); return 0; } if (a2dp_source_context.avdtp_cid != a2dp_cid){ log_error("A2DP source: a2dp cid 0x%02x not known, expected 0x%02x", a2dp_cid, a2dp_source_context.avdtp_cid); return 0; } if (stream_endpoint->l2cap_media_cid == 0){ log_error("A2DP source: no media connection for seid %d", local_seid); return 0; } return l2cap_get_remote_mtu_for_local_cid(stream_endpoint->l2cap_media_cid) - AVDTP_MEDIA_PAYLOAD_HEADER_SIZE; }
static void avdtp_audio_timeout_handler(btstack_timer_source_t * timer){ a2dp_media_sending_context_t * context = (a2dp_media_sending_context_t *) btstack_run_loop_get_timer_context(timer); btstack_run_loop_set_timer(&context->audio_timer, AUDIO_TIMEOUT_MS); btstack_run_loop_add_timer(&context->audio_timer); uint32_t now = btstack_run_loop_get_time_ms(); uint32_t update_period_ms = AUDIO_TIMEOUT_MS; if (context->time_audio_data_sent > 0){ update_period_ms = now - context->time_audio_data_sent; } uint32_t num_samples = (update_period_ms * a2dp_sample_rate()) / 1000; context->acc_num_missed_samples += (update_period_ms * a2dp_sample_rate()) % 1000; while (context->acc_num_missed_samples >= 1000){ num_samples++; context->acc_num_missed_samples -= 1000; } context->time_audio_data_sent = now; context->samples_ready += num_samples; if (context->sbc_ready_to_send) return; fill_sbc_audio_buffer(context); if ((context->sbc_storage_count + btstack_sbc_encoder_sbc_buffer_length()) > context->max_media_payload_size){ // schedule sending context->sbc_ready_to_send = 1; // a2dp_source_stream_endpoint_request_can_send_now(context->local_seid); avdtp_stream_endpoint_t * stream_endpoint = avdtp_stream_endpoint_for_seid(context->local_seid, &a2dp_source_context); if (!stream_endpoint) { printf("no stream_endpoint for seid %d\n", context->local_seid); return; } stream_endpoint->send_stream = 1; if (stream_endpoint->connection){ avdtp_request_can_send_now_initiator(stream_endpoint->connection, stream_endpoint->l2cap_media_cid); } } }