接上一篇繼續分析:【Bluedroid】A2DP Sink播放流程源碼分析(一)_安卓a2dp sink播放流程-CSDN博客
AVDTP接收端(Sink)流事件處理
bta_av_sink_data_cback
?是 Bluedroid 中 A2DP Sink 角色的?AVDTP 數據回調函數,負責處理接收端的音頻數據事件,將底層接收到的音頻數據傳遞給上層模塊。
bta_av_sink_data_cback
packages/modules/Bluetooth/system/bta/av/bta_av_aact.cc
/********************************************************************************* Function bta_av_sink_data_cback** Description This is the AVDTP callback function for sink stream events.** Returns void*******************************************************************************/
void bta_av_sink_data_cback(uint8_t handle, BT_HDR* p_pkt, uint32_t time_stamp,uint8_t m_pt) {int index = 0;tBTA_AV_SCB* p_scb;log::verbose("avdt_handle: {} pkt_len=0x{:x} offset = 0x{:x} number of frames 0x{:x} ""sequence number 0x{:x}",handle, p_pkt->len, p_pkt->offset,*((uint8_t*)(p_pkt + 1) + p_pkt->offset), p_pkt->layer_specific);// 1. 查找流控制塊(SCB)/* Get SCB and correct sep type */for (index = 0; index < BTA_AV_NUM_STRS; index++) {p_scb = bta_av_cb.p_scb[index]; // 存儲流連接的狀態(如角色、句柄、SEP 類型等)if ((p_scb->avdt_handle == handle) &&(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)) {break;}}if (index == BTA_AV_NUM_STRS) {/* cannot find correct handler */osi_free(p_pkt);return;}// 2. 觸發上層回調p_pkt->event = BTA_AV_SINK_MEDIA_DATA_EVT; // 設置事件類型為 Sink 媒體數據事件// 上層回調:通過 p_app_sink_data_cback 將數據傳遞給 A2DP Sink 應用層(如音頻解碼、播放模塊),觸發后續處理(如數據解析、緩沖區寫入)p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_DATA_EVT, (tBTA_AV_MEDIA*)p_pkt);/* Free the buffer: a copy of the packet has been delivered */osi_free(p_pkt);
}
bta_av_sink_data_cback
?用于處理藍牙 AVDTP接收端(Sink)流事件的回調函數。主要作用是在接收到相關的數據包后,根據數據包對應的句柄等信息找到對應的處理上下文(通過查找?SCB
),然后對數據包進行一些必要的處理,如設置事件類型,并調用相應的應用層回調函數將數據包及相關事件信息傳遞給上層應用進行進一步處理,最后釋放數據包所占用的內存,確保整個接收端流事件處理流程的完整性以及內存資源的合理管理。?
關鍵作用
- 數據通路橋梁:連接 AVDTP 層與上層應用,將原始音頻數據轉換為 Sink 角色可處理的事件。
- 狀態校驗:通過 SCB 篩選確保僅處理?Sink 角色的合法流連接,避免跨角色數據誤處理。
- 錯誤隔離:未找到 SCB 時及時釋放內存,防止無效指針操作導致的崩潰。
bta_av_sink_media_callback (BTA_AV_SINK_MEDIA_DATA_EVT)
packages/modules/Bluetooth/system/btif/src/btif_av.cc
// TODO: All processing should be done on the JNI thread
static void bta_av_sink_media_callback(const RawAddress& peer_address,tBTA_AV_EVT event,tBTA_AV_MEDIA* p_data) {log::verbose("event={}", event);switch (event) {case BTA_AV_SINK_MEDIA_DATA_EVT: { // 當接收到媒體數據時觸發BtifAvPeer* peer = btif_av_sink_find_peer(peer_address);if (peer != nullptr && peer->IsActivePeer()) {int state = peer->StateMachine().StateId();if ((state == BtifAvStateMachine::kStateStarted) ||(state == BtifAvStateMachine::kStateOpened)) {uint8_t queue_len = btif_a2dp_sink_enqueue_buf((BT_HDR*)p_data);log::verbose("Packets in Sink queue {}", queue_len);}}break;}...default:break;}
}
bta_av_sink_media_callback
?是一個回調函數,主要用于處理藍牙音頻 / 視頻(AV)接收端(Sink)相關的媒體事件。依據接收到的不同媒體事件類型來執行相應操作,目的是協調藍牙 A2DP 接收端在接收媒體數據時與系統內其他部分的交互,比如根據接收端狀態決定是否將接收到的數據入隊等操作,以此保障藍牙音頻 / 視頻接收流程能正常進行。
btif_a2dp_sink_enqueue_buf?
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
uint8_t btif_a2dp_sink_enqueue_buf(BT_HDR* p_pkt) {// 1. 加鎖與刷新檢查LockGuard lock(g_mutex);if (btif_a2dp_sink_cb.rx_flush) /* Flush enabled, do not enqueue */return fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);log::verbose("+");// 2. 分配內存并復制數據包/* Allocate and queue this buffer */BT_HDR* p_msg =reinterpret_cast<BT_HDR*>(osi_malloc(sizeof(*p_msg) + p_pkt->len));memcpy(p_msg, p_pkt, sizeof(*p_msg));p_msg->offset = 0;memcpy(p_msg->data, p_pkt->data + p_pkt->offset, p_pkt->len);// 將新的消息結構體添加到接收音頻隊列 rx_audio_queue 中fixed_queue_enqueue(btif_a2dp_sink_cb.rx_audio_queue, p_msg);// 3. 隊列長度檢查與處理if (fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) ==MAX_INPUT_A2DP_FRAME_QUEUE_SZ) { // 隊列已滿osi_free(fixed_queue_try_dequeue(btif_a2dp_sink_cb.rx_audio_queue));uint8_t ret = fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);return ret;}// 4. 解碼啟動檢查// Avoid other checks if alarm has already been initialized.if (btif_a2dp_sink_cb.decode_alarm == nullptr &&fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) >=MAX_A2DP_DELAYED_START_FRAME_COUNT) {log::verbose("Initiate decoding. Current focus state:{}",btif_a2dp_sink_cb.rx_focus_state);if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_GRANTED) {btif_a2dp_sink_audio_handle_start_decoding();}}return fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);
}
處理藍牙 A2DP接收端(Sink)的數據入隊操作。通過合理的加鎖機制保障多線程環境下資源訪問的安全性,根據刷新機制決定是否入隊數據,在入隊過程中進行內存分配、數據復制以及隊列長度控制等操作,同時還能根據特定條件觸發音頻解碼相關操作,確保接收端音頻數據能夠正確地緩存、管理以及適時地進行解碼播放,對于維持整個藍牙音頻接收和播放流程的順暢以及資源的合理利用有著重要意義。
fixed_queue_enqueue?
packages/modules/Bluetooth/system/osi/src/fixed_queue.cc
void fixed_queue_enqueue(fixed_queue_t* queue, void* data) {CHECK(queue != NULL);CHECK(data != NULL);//等待入隊信號量。如果隊列已滿(即入隊信號量的計數為0),則當前線程將阻塞在這里,直到有其他線程從隊列中移除了元素并調用了?semaphore_post(queue->enqueue_sem)semaphore_wait(queue->enqueue_sem);{std::lock_guard<std::mutex> lock(*queue->mutex);list_append(queue->list, data); // 將數據添加到隊列的列表中}// 增加出隊信號量的計數。表示隊列中有新的元素可供出隊操作,如果之前有線程在等待出隊信號量(即隊列為空且線程嘗試從隊列中取出元素),則這些線程現在可以繼續執行semaphore_post(queue->dequeue_sem);
}
向一個固定隊列(fixed_queue_t
?類型的隊列)中添加元素(通過?void* data
?參數傳入要添加的數據指針),在添加過程中,通過嚴格的參數檢查、合理的信號量控制以及互斥鎖保護下的隊列元素添加操作,在多線程環境下實現了安全、可靠的隊列入隊功能。通過控制入隊和出隊信號量的等待與釋放,還能有效地協調多個線程對隊列的并發使用,避免出現數據競爭、不一致等并發問題,保障了整個固定隊列數據結構在多線程系統中的正常運行,對于需要在并發環境下進行數據緩存、排隊處理等應用場景有著重要的支撐作用。
btif_a2dp_sink_audio_handle_start_decoding
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
// Must be called while locked.
static void btif_a2dp_sink_audio_handle_start_decoding() {log::info("");if (btif_a2dp_sink_cb.decode_alarm != nullptr)return; // Already started decoding#ifdef __ANDROID__BtifAvrcpAudioTrackStart(btif_a2dp_sink_cb.audio_track);
#endif//創建一個新的周期性定時器,并將其地址存儲在?btif_a2dp_sink_cb.decode_alarm?中btif_a2dp_sink_cb.decode_alarm = alarm_new_periodic("btif.a2dp_sink_decode");if (btif_a2dp_sink_cb.decode_alarm == nullptr) {log::error("unable to allocate decode alarm");return;}// 設置定時器的觸發間隔為?BTIF_SINK_MEDIA_TIME_TICK_MS(20,定時器觸發的毫秒間隔)// 當定時器觸發時,將調用?btif_decode_alarm_cb?回調函數(傳遞?nullptr?作為參)alarm_set(btif_a2dp_sink_cb.decode_alarm, BTIF_SINK_MEDIA_TIME_TICK_MS,btif_decode_alarm_cb, nullptr);
}
負責啟動藍牙 A2DP(接收端(Sink)的音頻解碼相關操作。在調用時要求處于加鎖狀態,以確保在多線程環境下操作相關共享資源(如?btif_a2dp_sink_cb
?結構體相關成員)的安全性。首先通過檢查避免重復啟動解碼操作,然后針對安卓平臺進行相應的音頻播放前置操作(在__ANDROID__環境下),接著創建周期性的定時器(或稱為“alarm”)并設置好相關參數和回調函數,以此來建立起一個定時執行音頻解碼任務的機制,保障音頻數據能夠在合適的時間間隔下持續地被解碼處理,為后續的音頻播放等流程提供解碼后的數據支持。
下面分析SBC編碼寫入audiotrack的過程,其它編碼格式的如法炮制。?
btif_decode_alarm_cb
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
static void btif_decode_alarm_cb(void* /* context */) {LockGuard lock(g_mutex);btif_a2dp_sink_cb.worker_thread.DoInThread(FROM_HERE, base::BindOnce(btif_a2dp_sink_avk_handle_timer));
}
btif_decode_alarm_cb
?函數主要作為一個回調函數來使用,其核心功能是在被觸發時,先通過加鎖機制確保線程安全地訪問共享資源,然后安排在特定的工作線程中執行另一個函數?btif_a2dp_sink_avk_handle_timer
,以此實現一種異步的、線程安全的操作觸發機制,用于在特定的定時或事件觸發場景下,讓相關的音頻處理邏輯(由?btif_a2dp_sink_avk_handle_timer
?函數具體實現)能夠在合適的線程環境中有序執行,保障音頻相關功能的正確運行。
btif_a2dp_sink_avk_handle_timer?
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
static void btif_a2dp_sink_avk_handle_timer() {// 1. 加鎖操作LockGuard lock(g_mutex);// 2. 檢查隊列是否為空BT_HDR* p_msg; // 用于后續存儲從隊列中取出的數據包if (fixed_queue_is_empty(btif_a2dp_sink_cb.rx_audio_queue)) {log::verbose("empty queue");return;}// 3. 檢查焦點狀態/* Don't do anything in case of focus not granted */if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_NOT_GRANTED) {log::verbose("skipping frames since focus is not present");return;}// 4. 檢查刷新標志/* Play only in BTIF_A2DP_SINK_FOCUS_GRANTED case */if (btif_a2dp_sink_cb.rx_flush) {// 清空接收音頻隊列,并釋放隊列中每個元素的內存fixed_queue_flush(btif_a2dp_sink_cb.rx_audio_queue, osi_free);return;}// 5. 處理隊列中的數據包log::verbose("process frames begin");while (true) { // 使用 while 循環不斷從隊列中取出數據包,直到隊列為空(即 fixed_queue_try_dequeue 返回 NULL)p_msg = (BT_HDR*)fixed_queue_try_dequeue(btif_a2dp_sink_cb.rx_audio_queue);if (p_msg == NULL) {break;}log::verbose("number of packets in queue {}",fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue));/* Queue packet has less frames */btif_a2dp_sink_handle_inc_media(p_msg); // 處理取出的音頻數據包osi_free(p_msg);}log::verbose("process frames end");
}
btif_a2dp_sink_avk_handle_timer
?函數是一個定時器處理函數,用于處理 A2DP Sink 端接收到的音頻數據包隊列。在定時器觸發時被調用,對 A2DP Sink 端的音頻數據包隊列進行檢查和處理。根據隊列狀態、焦點狀態和刷新標志來決定是直接返回、清空隊列還是逐個處理隊列中的數據包,確保音頻數據的處理符合系統的狀態和要求。
btif_a2dp_sink_handle_inc_media
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
// Must be called while locked.
static void btif_a2dp_sink_handle_inc_media(BT_HDR* p_msg) {if ((btif_av_get_peer_sep() == AVDT_TSEP_SNK) ||(btif_a2dp_sink_cb.rx_flush)) {log::verbose("state changed happened in this tick");return;}CHECK(btif_a2dp_sink_cb.decoder_interface != nullptr);if (!btif_a2dp_sink_cb.decoder_interface->decode_packet(p_msg)) {log::error("decoding failed");}
}
確保當前狀態允許的情況下,調用解碼器接口來解碼接收到的A2DP音頻數據包。保障了音頻數據在合適的條件下能夠被合理地進行解碼操作,對于維持音頻播放等后續功能的正常運行有著重要意義。?
a2dp_sbc_decoder_decode_packet
packages/modules/Bluetooth/system/stack/a2dp/a2dp_sbc_decoder.cc
bool a2dp_sbc_decoder_decode_packet(BT_HDR* p_buf) {// 1. 輸入數據準備uint8_t* data = p_buf->data + p_buf->offset;size_t data_size = p_buf->len;if (data_size == 0) {log::error("Empty packet");return false;}size_t num_frames = data[0] & 0xf; // 提取幀數量(低4位)data += 1; // 跳過幀數量字段,指向實際音頻數據data_size -= 1;// 2. 逐幀解碼const OI_BYTE* oi_data = data; // 轉換為 OI 庫所需的字節類型uint32_t oi_size = data_size; // 剩余待解碼數據大小size_t out_avail = sizeof(a2dp_sbc_decoder_cb.decode_buf); // 輸出緩沖區總大小int16_t* out_ptr = a2dp_sbc_decoder_cb.decode_buf; // 輸出緩沖區指針(PCM 數據)for (size_t i = 0; i < num_frames; ++i) {uint32_t out_size = out_avail; // 當前幀解碼所需的輸出空間// 解碼庫調用OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&a2dp_sbc_decoder_cb.decoder_context, // 解碼上下文(包含編解碼參數)&oi_data, // 輸入數據指針(按幀遞增)&oi_size, // 剩余輸入數據大小(按幀遞減)out_ptr, // 輸出 PCM 數據指針&out_size // 實際解碼出的 PCM 數據大小);if (!OI_SUCCESS(status)) {log::error("Decoding failure: {}", status);return false; // 解碼失敗,終止處理}out_avail -= out_size; // 更新剩余輸出空間out_ptr += out_size / sizeof(*out_ptr); // 移動輸出指針(按 int16 單位)
}// 3. 解碼結果回調size_t out_used = (out_ptr - a2dp_sbc_decoder_cb.decode_buf) * sizeof(*out_ptr);// 上層回調:通過 decode_callback 將解碼后的 PCM 數據傳遞給上層(如音頻播放模塊),觸發后續處理(如寫入音頻輸出設備)a2dp_sbc_decoder_cb.decode_callback(reinterpret_cast<uint8_t*>(a2dp_sbc_decoder_cb.decode_buf), // PCM 數據起始地址out_used // 實際使用的字節數);
}
a2dp_sbc_decoder_decode_packet
?是?A2DP Sink 端 SBC(Subband Coding,子帶編碼)編解碼器的核心解碼函數,負責將接收到的 SBC 格式音頻數據包解碼為 PCM 數據,供上層音頻播放模塊使用。其核心邏輯包括?數據包解析、逐幀解碼?和?結果回調。
OI_CODEC_SBC_DecodeFrame
packages/modules/Bluetooth/system/embdrv/sbc/decoder/srce/decoder-sbc.c
OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT* context,const OI_BYTE** frameData,uint32_t* frameBytes, int16_t* pcmData,uint32_t* pcmBytes) {OI_STATUS status;OI_UINT framelen;uint8_t crc;TRACE(("+OI_CODEC_SBC_DecodeFrame"));// 1. 同步字查找(Syncword Detection)TRACE(("Finding syncword"));status = FindSyncword(context, frameData, frameBytes);if (!OI_SUCCESS(status)) {return status;}// 2. 頭部解析與參數校驗/* Make sure enough data remains to read the header. */if (*frameBytes < SBC_HEADER_LEN) {TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA"));return OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA;}if (context->mSbcEnabled) {/** There is no parameter embedded in mSBC's header as the parameters are* fixed unlike the general SBC. We only need the packet's crc for mSBC.*/context->common.frameInfo.crc = (*frameData)[3]; // mSBC 僅需 CRC} else {TRACE(("Reading Header"));OI_SBC_ReadHeader(&context->common, *frameData); // 普通 SBC 解析完整頭部}/** Some implementations load the decoder into RAM and use overlays for 4 vs 8* subbands. We need* to ensure that the SBC parameters for this frame are compatible with the* restrictions imposed* by the loaded overlays.*/// // 參數校驗:子帶數量、通道數、PCM 步長等if (context->limitFrameFormat &&(context->common.frameInfo.subbands != context->restrictSubbands)) {ERROR(("SBC parameters incompatible with loaded overlay"));return OI_STATUS_INVALID_PARAMETERS;}if (context->common.frameInfo.nrof_channels > context->common.maxChannels) {ERROR(("SBC parameters incompatible with number of channels specified during ""reset"));return OI_STATUS_INVALID_PARAMETERS;}if (context->common.pcmStride < 1 || context->common.pcmStride > 2) {ERROR(("PCM stride not set correctly during reset"));return OI_STATUS_INVALID_PARAMETERS;}/** At this point a header has been read. However, it's possible that we found* a false syncword,* so the header data might be invalid. Make sure we have enough bytes to read* in the* CRC-protected header, but don't require we have the whole frame. That way,* if it turns out* that we're acting on bogus header data, we don't stall the decoding process* by waiting for* data that we don't actually need.*/framelen = OI_CODEC_SBC_CalculateFramelen(&context->common.frameInfo);if (*frameBytes < framelen) {TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA"));return OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA; // 數據不足}TRACE(("Calculating checksum"));// 3. 幀長度計算與數據完整性校驗crc = OI_SBC_CalculateChecksum(&context->common.frameInfo, *frameData);if (crc != context->common.frameInfo.crc) {TRACE(("CRC Mismatch: calc=%02x read=%02x\n", crc,context->common.frameInfo.crc));TRACE(("-OI_CODEC_SBC_DecodeFrame: OI_CODEC_SBC_CHECKSUM_MISMATCH"));return OI_CODEC_SBC_CHECKSUM_MISMATCH; // CRC 校驗失敗}// 4. 比特池合法性檢查/** Make sure the bitpool values are sane.*/if ((context->common.frameInfo.bitpool < SBC_MIN_BITPOOL) &&!context->common.frameInfo.enhanced) {ERROR(("Bitpool too small: %d (must be >= 2)",context->common.frameInfo.bitpool));return OI_STATUS_INVALID_PARAMETERS; }if (context->common.frameInfo.bitpool >OI_SBC_MaxBitpool(&context->common.frameInfo)) {ERROR(("Bitpool too large: %d (must be <= %ld)",context->common.frameInfo.bitpool,OI_SBC_MaxBitpool(&context->common.frameInfo)));return OI_STATUS_INVALID_PARAMETERS;}// 5. 音頻數據解碼(核心邏輯)/** Now decode the SBC data. Partial decode is not yet implemented for an SBC* stream, so pass FALSE to decode body to have it enforce the old rule that* you have to decode a whole packet at a time.*/status = DecodeBody(context, *frameData + SBC_HEADER_LEN, pcmData, pcmBytes,FALSE);if (OI_SUCCESS(status)) {*frameData += framelen; // 移動數據指針到下一幀*frameBytes -= framelen; // 更新剩余數據大小}TRACE(("-OI_CODEC_SBC_DecodeFrame: %d", status));// 6. mSBC 特殊處理/* mSBC is designed with 8 bits of zeros at the end for padding. */if (context->mSbcEnabled) {*frameBytes -= 1;}return status;
}
OI_CODEC_SBC_DecodeFrame
?是?SBC 音頻解碼的核心函數,負責對單個 SBC 音頻幀進行解碼,將編碼后的音頻數據轉換為 PCM 格式。其核心流程包括?同步字查找、頭部解析、參數校驗、CRC 校驗?和?音頻數據解碼,是 A2DP Sink 端音頻解碼的關鍵環節。兼顧了普通 SBC 和 mSBC(Modified SBC,改進型SBC)兩種模式,通過狀態機和上下文管理實現高效的幀級解碼。理解此函數有助于分析音頻解碼中的常見問題(如雜音、解碼失敗),并為編解碼器優化(如提升解碼速度、降低功耗)提供切入點。
繼續回到a2dp_sbc_decoder_decode_packet分析回調處理。
btif_a2dp_sink_on_decode_complete?
packages/modules/Bluetooth/system/btif/src/btif_a2dp_sink.cc
static void btif_a2dp_sink_on_decode_complete(uint8_t* data, uint32_t len) {
#ifdef __ANDROID__BtifAvrcpAudioTrackWriteData(btif_a2dp_sink_cb.audio_track,reinterpret_cast<void*>(data), len);
#endif
}
btif_a2dp_sink_on_decode_complete
?作為一個回調函數,用于在音頻數據解碼完成后進行后續的處理操作。在 Android 平臺下,調用BtifAvrcpAudioTrackWriteData
函數,將解碼后的音頻數據寫入到音頻通路。
BtifAvrcpAudioTrackWriteData
packages/modules/Bluetooth/system/btif/src/btif_avrcp_audio_track.cc
int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer,int bufferLength) {// 1. 參數校驗與初始化BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);CHECK(trackHolder != NULL);CHECK(trackHolder->stream != NULL); // 確保音頻流句柄有效aaudio_result_t retval = -1;// 2. 調試數據 Dump(可選)
#if (DUMP_PCM_DATA == TRUE)if (outputPcmSampleFile) { // 將接收到的音頻數據直接寫入文件,用于調試階段分析原始音頻數據fwrite((audioBuffer), 1, (size_t)bufferLength, outputPcmSampleFile);}
#endif// 3. 樣本大小計算// 根據音頻格式(如 16 位 PCM、浮點型)返回單個樣本的字節大小。例如:// 16 位 PCM 立體聲:每個樣本 2 字節 × 2 通道 = 4 字節 / 幀。// 32 位浮點立體聲:每個樣本 4 字節 × 2 通道 = 8 字節 / 幀。size_t sampleSize = sampleSizeFor(trackHolder);// 4. 數據轉碼與分段寫入int transcodedCount = 0;do {// 轉碼:將輸入數據(如 SBC 解碼后的 PCM)轉為 AAudio 所需的浮點格式transcodedCount += transcodeToPcmFloat(((uint8_t*)audioBuffer) + transcodedCount, // 輸入數據指針(按轉碼進度遞增)bufferLength - transcodedCount, // 剩余待轉碼數據長度trackHolder // 軌道句柄(含編碼格式、通道數等信息));// 寫入 AAudio 流:參數為流句柄、目標緩沖區、樣本數量、超時時間retval = AAudioStream_write(trackHolder->stream, // AAudio 流句柄trackHolder->buffer, // 轉碼后的浮點 PCM 緩沖區transcodedCount / (sampleSize * trackHolder->channelCount), // 樣本數量(總字節數 / 單樣本字節數)kTimeoutNanos // 寫入超時時間(納秒級));log::verbose("Track.cpp: btWriteData len = {} ret = {}", bufferLength,retval);} while (transcodedCount < bufferLength);return transcodedCount;
}
BtifAvrcpAudioTrackWriteData
?是?AVRCP 音頻軌道的數據寫入函數,負責將接收到的音頻數據轉換為可播放的 PCM 格式,并通過 AAudio 框架寫入音頻流,最終輸出到音頻設備。其核心邏輯包括?數據轉碼、AAudio 流寫入?和?調試數據 Dump。
AAudio ?|? Android NDK ?|? Android Developers