Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f736a2cc authored by Zach Johnson's avatar Zach Johnson
Browse files

Migrate from libaudioclient to libaaudio

Need to move to a library that doesn't use libbinder.

Bug: 144441978
Test: compile, and verify with an audio sink platform
Change-Id: I9111c34d8611531d2adcaf3b08172f7328f01014
parent 9738290f
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -96,7 +96,7 @@ cc_library_static {
        "libmedia_headers",
        "libmedia_headers",
    ],
    ],
    shared_libs: [
    shared_libs: [
        "libaudioclient",
        "libaaudio",
        "libcutils",
        "libcutils",
        "libfmq",
        "libfmq",
        "liblog",
        "liblog",
@@ -139,7 +139,7 @@ cc_test {
    ],
    ],
    header_libs: ["libbluetooth_headers"],
    header_libs: ["libbluetooth_headers"],
    shared_libs: [
    shared_libs: [
        "libaudioclient",
        "libaaudio",
        "android.hardware.bluetooth.a2dp@1.0",
        "android.hardware.bluetooth.a2dp@1.0",
        "android.hardware.bluetooth.audio@2.0",
        "android.hardware.bluetooth.audio@2.0",
        "libfmq",
        "libfmq",
+3 −3
Original line number Original line Diff line number Diff line
@@ -35,8 +35,8 @@
 * should eventually be
 * should eventually be
 * deleted using BtifAvrcpAudioTrackDelete (see below).
 * deleted using BtifAvrcpAudioTrackDelete (see below).
 */
 */
void* BtifAvrcpAudioTrackCreate(int trackFreq, int bits_per_sample,
void* BtifAvrcpAudioTrackCreate(int trackFreq, int bitsPerSample,
                                int channelType);
                                int channelCount);


/**
/**
 * Starts the audio track.
 * Starts the audio track.
@@ -66,4 +66,4 @@ void BtifAvrcpAudioTrackDelete(void* handle);
 * Used only for debugging.
 * Used only for debugging.
 */
 */
int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer,
int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer,
                                 int bufferlen);
                                 int bufferLength);
+1 −1
Original line number Original line Diff line number Diff line
@@ -595,7 +595,7 @@ static void btif_a2dp_sink_decoder_update_event(
  APPL_TRACE_DEBUG("%s: create audio track", __func__);
  APPL_TRACE_DEBUG("%s: create audio track", __func__);
  btif_a2dp_sink_cb.audio_track =
  btif_a2dp_sink_cb.audio_track =
#ifndef OS_GENERIC
#ifndef OS_GENERIC
      BtifAvrcpAudioTrackCreate(sample_rate, bits_per_sample, channel_type);
      BtifAvrcpAudioTrackCreate(sample_rate, bits_per_sample, channel_count);
#else
#else
      NULL;
      NULL;
#endif
#endif
+118 −53
Original line number Original line Diff line number Diff line
@@ -19,8 +19,8 @@


#include "btif_avrcp_audio_track.h"
#include "btif_avrcp_audio_track.h"


#include <aaudio/AAudio.h>
#include <base/logging.h>
#include <base/logging.h>
#include <media/AudioTrack.h>
#include <utils/StrongPointer.h>
#include <utils/StrongPointer.h>


#include "bt_target.h"
#include "bt_target.h"
@@ -28,50 +28,49 @@


using namespace android;
using namespace android;


typedef struct { android::sp<android::AudioTrack> track; } BtifAvrcpAudioTrack;
typedef struct {
  AAudioStream* stream;
  int bitsPerSample;
  int channelCount;
  float* buffer;
  size_t bufferLength;
} BtifAvrcpAudioTrack;


#if (DUMP_PCM_DATA == TRUE)
#if (DUMP_PCM_DATA == TRUE)
FILE* outputPcmSampleFile;
FILE* outputPcmSampleFile;
char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm";
char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm";
#endif
#endif


void* BtifAvrcpAudioTrackCreate(int trackFreq, int bits_per_sample,
void* BtifAvrcpAudioTrackCreate(int trackFreq, int bitsPerSample,
                                int channelType) {
                                int channelCount) {
  audio_format_t format;
  LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btCreateTrack freq %d bps %d channel %d ",
  switch (bits_per_sample) {
              __func__, trackFreq, bitsPerSample, channelCount);
    default:

    case 16:
  AAudioStreamBuilder* builder;
      format = AUDIO_FORMAT_PCM_16_BIT;
  AAudioStream* stream;
      break;
  aaudio_result_t result = AAudio_createStreamBuilder(&builder);
    case 24:
  AAudioStreamBuilder_setSampleRate(builder, trackFreq);
      format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
  AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_FLOAT);
      break;
  AAudioStreamBuilder_setChannelCount(builder, channelCount);
    case 32:
  AAudioStreamBuilder_setSessionId(builder, AAUDIO_SESSION_ID_ALLOCATE);
      format = AUDIO_FORMAT_PCM_32_BIT;
  AAudioStreamBuilder_setPerformanceMode(builder,
      break;
                                         AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
  }
  result = AAudioStreamBuilder_openStream(builder, &stream);
  LOG_VERBOSE(LOG_TAG,
  CHECK(result == AAUDIO_OK);
              "%s Track.cpp: btCreateTrack freq %d format 0x%x channel %d ",
  AAudioStreamBuilder_delete(builder);
              __func__, trackFreq, format, channelType);
  sp<android::AudioTrack> track = new android::AudioTrack(
      AUDIO_STREAM_MUSIC, trackFreq, format, channelType,
      (size_t)0 /*frameCount*/, (audio_output_flags_t)AUDIO_OUTPUT_FLAG_FAST,
      NULL /*callback_t*/, NULL /*void* user*/, 0 /*notificationFrames*/,
      AUDIO_SESSION_ALLOCATE, android::AudioTrack::TRANSFER_SYNC);
  CHECK(track != NULL);


  BtifAvrcpAudioTrack* trackHolder = new BtifAvrcpAudioTrack;
  BtifAvrcpAudioTrack* trackHolder = new BtifAvrcpAudioTrack;
  CHECK(trackHolder != NULL);
  CHECK(trackHolder != NULL);
  trackHolder->track = track;
  trackHolder->stream = stream;

  trackHolder->bitsPerSample = bitsPerSample;
  if (trackHolder->track->initCheck() != 0) {
  trackHolder->channelCount = channelCount;
    return nullptr;
  trackHolder->bufferLength =
  }
      trackHolder->channelCount * AAudioStream_getBufferSizeInFrames(stream);
  trackHolder->buffer = new float[trackHolder->bufferLength]();


#if (DUMP_PCM_DATA == TRUE)
#if (DUMP_PCM_DATA == TRUE)
  outputPcmSampleFile = fopen(outputFilename, "ab");
  outputPcmSampleFile = fopen(outputFilename, "ab");
#endif
#endif
  trackHolder->track->setVolume(1, 1);
  return (void*)trackHolder;
  return (void*)trackHolder;
}
}


@@ -82,9 +81,9 @@ void BtifAvrcpAudioTrackStart(void* handle) {
  }
  }
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  CHECK(trackHolder != NULL);
  CHECK(trackHolder != NULL);
  CHECK(trackHolder->track != NULL);
  CHECK(trackHolder->stream != NULL);
  LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
  LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
  trackHolder->track->start();
  AAudioStream_requestStart(trackHolder->stream);
}
}


void BtifAvrcpAudioTrackStop(void* handle) {
void BtifAvrcpAudioTrackStop(void* handle) {
@@ -93,9 +92,9 @@ void BtifAvrcpAudioTrackStop(void* handle) {
    return;
    return;
  }
  }
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  if (trackHolder != NULL && trackHolder->track != NULL) {
  if (trackHolder != NULL && trackHolder->stream != NULL) {
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
    trackHolder->track->stop();
    AAudioStream_requestStop(trackHolder->stream);
  }
  }
}
}


@@ -105,8 +104,10 @@ void BtifAvrcpAudioTrackDelete(void* handle) {
    return;
    return;
  }
  }
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  if (trackHolder != NULL && trackHolder->track != NULL) {
  if (trackHolder != NULL && trackHolder->stream != NULL) {
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
    AAudioStream_close(trackHolder->stream);
    delete trackHolder->buffer;
    delete trackHolder;
    delete trackHolder;
  }
  }


@@ -124,10 +125,10 @@ void BtifAvrcpAudioTrackPause(void* handle) {
    return;
    return;
  }
  }
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  if (trackHolder != NULL && trackHolder->track != NULL) {
  if (trackHolder != NULL && trackHolder->stream != NULL) {
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btStartTrack", __func__);
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btPauseTrack", __func__);
    trackHolder->track->pause();
    AAudioStream_requestPause(trackHolder->stream);
    trackHolder->track->flush();
    AAudioStream_requestFlush(trackHolder->stream);
  }
  }
}
}


@@ -136,26 +137,90 @@ void BtifAvrcpSetAudioTrackGain(void* handle, float gain) {
    LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
    LOG_DEBUG(LOG_TAG, "%s handle is null.", __func__);
    return;
    return;
  }
  }
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  // Does nothing right now
  if (trackHolder != NULL && trackHolder->track != NULL) {
}
    LOG_VERBOSE(LOG_TAG, "%s set gain %f", __func__, gain);

    trackHolder->track->setVolume(gain);
constexpr float kScaleQ15ToFloat = 1.0f / 32768.0f;
constexpr float kScaleQ23ToFloat = 1.0f / 8388608.0f;
constexpr float kScaleQ31ToFloat = 1.0f / 2147483648.0f;

static size_t sampleSizeFor(BtifAvrcpAudioTrack* trackHolder) {
  return trackHolder->bitsPerSample / 8;
}

static size_t transcodeQ15ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  for (; i <= length / sampleSize; i++) {
    trackHolder->buffer[i] = ((int16_t*)buffer)[i] * kScaleQ15ToFloat;
  }
  return i * sampleSize;
}
}

static size_t transcodeQ23ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  for (; i <= length / sampleSize; i++) {
    size_t offset = i * sampleSize;
    int32_t sample = *((int32_t*)(buffer + offset - 1)) & 0x00FFFFFF;
    trackHolder->buffer[i] = sample * kScaleQ23ToFloat;
  }
  return i * sampleSize;
}
}


static size_t transcodeQ31ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  for (; i <= length / sampleSize; i++) {
    trackHolder->buffer[i] = ((int32_t*)buffer)[i] * kScaleQ31ToFloat;
  }
  return i * sampleSize;
}

static size_t transcodeToPcmFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
  switch (trackHolder->bitsPerSample) {
    case 16:
      return transcodeQ15ToFloat(buffer, length, trackHolder);
    case 24:
      return transcodeQ23ToFloat(buffer, length, trackHolder);
    case 32:
      return transcodeQ31ToFloat(buffer, length, trackHolder);
  }
  return -1;
}

constexpr int64_t kTimeoutNanos = 100 * 1000 * 1000;  // 100 ms

int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer,
int BtifAvrcpAudioTrackWriteData(void* handle, void* audioBuffer,
                                 int bufferlen) {
                                 int bufferLength) {
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  CHECK(trackHolder != NULL);
  CHECK(trackHolder != NULL);
  CHECK(trackHolder->track != NULL);
  CHECK(trackHolder->stream != NULL);
  int retval = -1;
  aaudio_result_t retval = -1;
#if (DUMP_PCM_DATA == TRUE)
#if (DUMP_PCM_DATA == TRUE)
  if (outputPcmSampleFile) {
  if (outputPcmSampleFile) {
    fwrite((audioBuffer), 1, (size_t)bufferlen, outputPcmSampleFile);
    fwrite((audioBuffer), 1, (size_t)bufferLength, outputPcmSampleFile);
  }
  }
#endif
#endif
  retval = trackHolder->track->write(audioBuffer, (size_t)bufferlen);

  LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d", __func__,
  size_t sampleSize = sampleSizeFor(trackHolder);
              bufferlen, retval);
  int transcodedCount = 0;
  return retval;
  do {
    transcodedCount +=
        transcodeToPcmFloat(((uint8_t*)audioBuffer) + transcodedCount,
                            bufferLength - transcodedCount, trackHolder);

    retval = AAudioStream_write(
        trackHolder->stream, trackHolder->buffer,
        transcodedCount / (sampleSize * trackHolder->channelCount),
        kTimeoutNanos);
    LOG_VERBOSE(LOG_TAG, "%s Track.cpp: btWriteData len = %d ret = %d",
                __func__, bufferLength, retval);
  } while (transcodedCount < bufferLength);

  return transcodedCount;
}
}
+1 −1
Original line number Original line Diff line number Diff line
@@ -57,7 +57,7 @@ cc_library_shared {
        "android.hardware.bluetooth@1.1",
        "android.hardware.bluetooth@1.1",
        "android.hardware.bluetooth.a2dp@1.0",
        "android.hardware.bluetooth.a2dp@1.0",
        "android.hardware.bluetooth.audio@2.0",
        "android.hardware.bluetooth.audio@2.0",
        "libaudioclient",
        "libaaudio",
        "libcutils",
        "libcutils",
        "libdl",
        "libdl",
        "libfmq",
        "libfmq",
Loading