Native如何使用Tunnel Mode

    mAudioSessionId = AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
    mHwSync = AudioSystem::getAudioHwSyncForSession((audio_session_t) mAudioSessionId);

format->setInt32("feature-tunneled-playback", 1);
format->setInt32("audio-hw-sync", mHwSync); 


            audio_attributes_t attr;
            memset(&attr, 0, sizeof(attr));
            attr.usage = AUDIO_USAGE_MEDIA;
            attr.content_type = AUDIO_CONTENT_TYPE_MUSIC;
            attr.flags = (audio_flags_mask_t)(AUDIO_FLAG_MUTE_HAPTIC | AUDIO_FLAG_HW_AV_SYNC);
            
            size_t frameCount;
            AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, sampleRate);
            ALOGD("frameCount = %d", frameCount);
            state->mAudioTrack = new AudioTrack(AttributionSourceState());
            state->mAudioTrack->set(AUDIO_STREAM_DEFAULT,
                                    sampleRate,
                                    AUDIO_FORMAT_PCM_16_BIT,
                                    audio_channel_out_mask_from_count(channelCount),
                                    frameCount * 4,
                                    AUDIO_OUTPUT_FLAG_NONE,
                                    NULL,
                                    NULL,
                                    0,
                                    0,
                                    true,
                                    (audio_session_t)mAudioSessionId,
                                    AudioTrack::TRANSFER_SYNC,
                                    NULL,
                                    AttributionSourceState(),
                                    &attr
                                    );

char AvSyncHeader[20];
            AvSyncHeader[0] = 0x55;
            AvSyncHeader[1] = 0x55;
            AvSyncHeader[2] = 0x00;
            AvSyncHeader[3] = 0x02;
            AvSyncHeader[4] = info->mSize >> 24;
            AvSyncHeader[5] = info->mSize >> 16;
            AvSyncHeader[6] = info->mSize >> 8;
            AvSyncHeader[7] = info->mSize;
            AvSyncHeader[8] = (info->mPresentationTimeUs*1000 >> 32) >> 24;
            AvSyncHeader[9] = (info->mPresentationTimeUs*1000 >> 32) >> 16;
            AvSyncHeader[10] = (info->mPresentationTimeUs*1000 >> 32) >> 8;
            AvSyncHeader[11] = (info->mPresentationTimeUs*1000 >> 32);
            AvSyncHeader[12] = (info->mPresentationTimeUs*1000 & 0xffffffff) >> 24;
            AvSyncHeader[13] = (info->mPresentationTimeUs*1000 & 0xffffffff) >> 16;
            AvSyncHeader[14] = (info->mPresentationTimeUs*1000 & 0xffffffff) >> 8;
            AvSyncHeader[15] = (info->mPresentationTimeUs*1000 & 0xffffffff);
            AvSyncHeader[16] = 0x00;
            AvSyncHeader[17] = 0x00;
            AvSyncHeader[18] = 0x00;
            AvSyncHeader[19] = 0x14;
            ssize_t nbytes1 = state->mAudioTrack->write(AvSyncHeader, 20);

ssize_t nbytes = state->mAudioTrack->write(
            buffer->base() + info->mOffset, copy);

 

posted @ 2022-10-10 16:31  青山渺渺  阅读(188)  评论(0编辑  收藏  举报