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

Commit 3f902b72 authored by Venkata Narendra Kumar Gutta's avatar Venkata Narendra Kumar Gutta Committed by Linux Build Service Account
Browse files

audio: Support for MSM audio formats

  -Add audio format support for Voip.

Change-Id: Ie045ba926b3e1266afd3fb6fba09af3a55daaeef
(cherry picked from commit d1810964ecd2d50064a1fed51049ddc354b9a788)
(cherry picked from commit cd9c6dc56bedcabca77c2a01c6fb15f4d2e8edd5)
(cherry picked from commit 01c09aeae38716860ef1a806985cc946dca93f60)
parent 4024d1c1
Loading
Loading
Loading
Loading
+33 −30
Original line number Diff line number Diff line
@@ -40,14 +40,6 @@ static const char* const kClassPathName = "android/media/AudioRecord";
struct fields_t {
    // these fields provide access from C++ to the...
    jmethodID postNativeEventInJava; //... event post callback method
    int       PCM16;                 //...  format constants
    int       PCM8;                  //...  format constants
    int       AMRNB;                 //...  format constants
    int       AMRWB;                 //...  format constants
    int       EVRC;                  //...  format constants
    int       EVRCB;                 //...  format constants
    int       EVRCWB;                //...  format constants
    int       EVRCNW;                //...  format constants
    jfieldID  nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object
    jfieldID  nativeCallbackCookie;    // provides access to the AudioRecord callback data
};
@@ -63,6 +55,12 @@ struct audiorecord_callback_cookie {
// keep these values in sync with AudioFormat.java
#define ENCODING_PCM_16BIT 2
#define ENCODING_PCM_8BIT  3
#define ENCODING_AMRNB     100
#define ENCODING_AMRWB     101
#define ENCODING_EVRC      102
#define ENCODING_EVRCB     103
#define ENCODING_EVRCWB    104
#define ENCODING_EVRCNW    105

static Mutex sLock;
static SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies;
@@ -165,25 +163,30 @@ static sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioR
    env->SetIntField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (int)ar.get());
    return old;
}

int getformatrec(int audioformat)
{
    if(audioformat==javaAudioRecordFields.PCM16)
    switch (audioformat) {
    case ENCODING_PCM_16BIT:
        return AUDIO_FORMAT_PCM_16_BIT;
    else if(audioformat==javaAudioRecordFields.AMRNB)
    case ENCODING_PCM_8BIT:
        return AUDIO_FORMAT_PCM_8_BIT;
    case ENCODING_AMRNB:
        return AUDIO_FORMAT_AMR_NB;
    else if(audioformat==javaAudioRecordFields.AMRWB)
    case ENCODING_AMRWB:
        return AUDIO_FORMAT_AMR_WB;
    else if(audioformat==javaAudioRecordFields.EVRC)
    case ENCODING_EVRC:
        return AUDIO_FORMAT_EVRC;
    else if(audioformat==javaAudioRecordFields.EVRCB)
    case ENCODING_EVRCB:
        return AUDIO_FORMAT_EVRCB;
    else if(audioformat==javaAudioRecordFields.EVRCWB)
    case ENCODING_EVRCWB:
        return AUDIO_FORMAT_EVRCWB;
    else if(audioformat==javaAudioRecordFields.EVRCNW)
    case ENCODING_EVRCNW:
        return AUDIO_FORMAT_EVRCNW;
    else
    default:
        return AUDIO_FORMAT_PCM_8_BIT;
    }
}

// ----------------------------------------------------------------------------
static int
@@ -203,21 +206,21 @@ android_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this,
    uint32_t nbChannels = popcount(channelMask);

    // compare the format against the Java constants
    if ((audioFormat != javaAudioRecordFields.PCM16)
        && (audioFormat != javaAudioRecordFields.PCM8)
        && (audioFormat != javaAudioRecordFields.AMRNB)
        && (audioFormat != javaAudioRecordFields.AMRWB)
        && (audioFormat != javaAudioRecordFields.EVRC)
        && (audioFormat != javaAudioRecordFields.EVRCB)
        && (audioFormat != javaAudioRecordFields.EVRCWB)
        && (audioFormat != javaAudioRecordFields.EVRCNW)) {
     if ((audioFormat != ENCODING_PCM_16BIT)
         && (audioFormat != ENCODING_PCM_8BIT)
         && (audioFormat != ENCODING_AMRNB)
         && (audioFormat != ENCODING_AMRWB)
         && (audioFormat != ENCODING_EVRC)
         && (audioFormat != ENCODING_EVRCB)
         && (audioFormat != ENCODING_EVRCWB)
         && (audioFormat != ENCODING_EVRCNW)) {
         ALOGE("Error creating AudioRecord: unsupported audio format.");
         return AUDIORECORD_ERROR_SETUP_INVALIDFORMAT;
    }
    int bytesPerSample;
    if(audioFormat == javaAudioRecordFields.PCM16)
    if(audioFormat == ENCODING_PCM_16BIT)
        bytesPerSample = 2;
    else if((audioFormat == javaAudioRecordFields.AMRWB) &&
    else if((audioFormat == ENCODING_AMRWB) &&
            ((uint32_t)source != AUDIO_SOURCE_VOICE_COMMUNICATION))
        bytesPerSample = 61;
    else
@@ -564,9 +567,9 @@ static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject th
        return -1;
    }
    int bytesPerSample;
    if(audioFormat == javaAudioRecordFields.PCM16)
    if(audioFormat == ENCODING_PCM_16BIT)
        bytesPerSample = 2;
    else if(audioFormat == javaAudioRecordFields.AMRWB)
    else if(audioFormat == ENCODING_AMRWB)
        bytesPerSample = 61;
    else
        bytesPerSample = 1;
+47 −39
Original line number Diff line number Diff line
@@ -43,14 +43,7 @@ static const char* const kClassPathName = "android/media/AudioTrack";
struct fields_t {
    // these fields provide access from C++ to the...
    jmethodID postNativeEventInJava; //... event post callback method
    int       PCM16;                 //...  format constants
    int       PCM8;                  //...  format constants
    int       AMRNB;                 //...  format constants
    int       AMRWB;                 //...  format constants
    int       EVRC;                  //...  format constants
    int       EVRCB;                 //...  format constants
    int       EVRCWB;                //...  format constants
    int       EVRCNW;                //...  format constants
    // FIX ME: Are the below int type members used anywhere??
    int       STREAM_VOICE_CALL;     //...  stream type constants
    int       STREAM_SYSTEM;         //...  stream type constants
    int       STREAM_RING;           //...  stream type constants
@@ -77,6 +70,14 @@ struct audiotrack_callback_cookie {
#define MODE_STATIC 0
#define MODE_STREAM 1
// keep these values in sync with AudioFormat.java
#define ENCODING_PCM_16BIT 2
#define ENCODING_PCM_8BIT  3
#define ENCODING_AMRNB     100
#define ENCODING_AMRWB     101
#define ENCODING_EVRC      102
#define ENCODING_EVRCB     103
#define ENCODING_EVRCWB    104
#define ENCODING_EVRCNW    105

// ----------------------------------------------------------------------------
class AudioTrackJniStorage {
@@ -215,23 +216,27 @@ static sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTra
// ----------------------------------------------------------------------------
int getformat(int audioformat)
{
    if(audioformat==javaAudioTrackFields.PCM16)
    switch (audioformat) {
    case ENCODING_PCM_16BIT:
        return AUDIO_FORMAT_PCM_16_BIT;
    else if(audioformat==javaAudioTrackFields.AMRNB)
    case ENCODING_PCM_8BIT:
        return AUDIO_FORMAT_PCM_8_BIT;
    case ENCODING_AMRNB:
        return AUDIO_FORMAT_AMR_NB;
    else if(audioformat==javaAudioTrackFields.AMRWB)
    case ENCODING_AMRWB:
        return AUDIO_FORMAT_AMR_WB;
    else if(audioformat==javaAudioTrackFields.EVRC)
    case ENCODING_EVRC:
        return AUDIO_FORMAT_EVRC;
    else if(audioformat==javaAudioTrackFields.EVRCB)
    case ENCODING_EVRCB:
        return AUDIO_FORMAT_EVRCB;
    else if(audioformat==javaAudioTrackFields.EVRCWB)
    case ENCODING_EVRCWB:
        return AUDIO_FORMAT_EVRCWB;
    else if(audioformat==javaAudioTrackFields.EVRCNW)
    case ENCODING_EVRCNW:
        return AUDIO_FORMAT_EVRCNW;

    default:
        return AUDIO_FORMAT_PCM_8_BIT;
    }
}

static int
android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
@@ -284,14 +289,14 @@ android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_th

    // check the format.
    // This function was called from Java, so we compare the format against the Java constants
    if ((audioFormat != javaAudioTrackFields.PCM16)
        && (audioFormat != javaAudioTrackFields.PCM8)
        && (audioFormat != javaAudioTrackFields.AMRNB)
        && (audioFormat != javaAudioTrackFields.AMRWB)
        && (audioFormat != javaAudioTrackFields.EVRC)
        && (audioFormat != javaAudioTrackFields.EVRCB)
        && (audioFormat != javaAudioTrackFields.EVRCWB)
        && (audioFormat != javaAudioTrackFields.EVRCNW)) {
    if ((audioFormat != ENCODING_PCM_16BIT)
        && (audioFormat != ENCODING_PCM_8BIT)
        && (audioFormat != ENCODING_AMRNB)
        && (audioFormat != ENCODING_AMRWB)
        && (audioFormat != ENCODING_EVRC)
        && (audioFormat != ENCODING_EVRCB)
        && (audioFormat != ENCODING_EVRCWB)
        && (audioFormat != ENCODING_EVRCNW)) {
        ALOGE("Error creating AudioTrack: unsupported audio format.");
        return AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT;
    }
@@ -299,20 +304,23 @@ android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_th
    // for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class
    // so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled
    // in android_media_AudioTrack_native_write_byte()
    if ((audioFormat == javaAudioTrackFields.PCM8)
    if ((audioFormat == ENCODING_PCM_8BIT)
        && (memoryMode == MODE_STATIC)) {
        ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit \
            buff size of %dbytes, switching to 16bit, buff size of %dbytes",
            buffSizeInBytes, 2*buffSizeInBytes);
        audioFormat = javaAudioTrackFields.PCM16;
        audioFormat = ENCODING_PCM_16BIT;
        // we will need twice the memory to store the data
        buffSizeInBytes *= 2;
    }

    // compute the frame count
    int bytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1;
    audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ?
            AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;
    int bytesPerSample;
    if(audioFormat == ENCODING_PCM_16BIT)
        bytesPerSample = 2;
    else
        bytesPerSample = 1;
    audio_format_t format = (audio_format_t)getformat(audioFormat);
    int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample);

    jclass clazz = env->GetObjectClass(thiz);
@@ -565,20 +573,20 @@ jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, jbyte* data,
            written = 0;
        }
    } else {
        if ((audioFormat == javaAudioTrackFields.PCM16)
        || (audioFormat == javaAudioTrackFields.AMRNB)
        || (audioFormat == javaAudioTrackFields.AMRWB)
        || (audioFormat == javaAudioTrackFields.EVRC)
        || (audioFormat == javaAudioTrackFields.EVRCB)
        || (audioFormat == javaAudioTrackFields.EVRCWB)
        || (audioFormat == javaAudioTrackFields.EVRCNW)) {
        if ((audioFormat == ENCODING_PCM_16BIT)
        || (audioFormat == ENCODING_AMRNB)
        || (audioFormat == ENCODING_AMRWB)
        || (audioFormat == ENCODING_EVRC)
        || (audioFormat == ENCODING_EVRCB)
        || (audioFormat == ENCODING_EVRCWB)
        || (audioFormat == ENCODING_EVRCNW)) {
            // writing to shared memory, check for capacity
            if ((size_t)sizeInBytes > track->sharedBuffer()->size()) {
                sizeInBytes = track->sharedBuffer()->size();
            }
            memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
            written = sizeInBytes;
        } else if (audioFormat == javaAudioTrackFields.PCM8) {
        } else if (audioFormat == ENCODING_PCM_8BIT) {
            // data contains 8bit data we need to expand to 16bit before copying
            // to the shared memory
            // writing to shared memory, check for capacity,
@@ -887,7 +895,7 @@ static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thi
            sampleRateInHertz) != NO_ERROR) {
        return -1;
    }
    return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1);
    return frameCount * nbChannels * (audioFormat == ENCODING_PCM_16BIT ? 2 : 1);
}

// ----------------------------------------------------------------------------