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

Commit 864ae678 authored by Andy Hung's avatar Andy Hung
Browse files

Default to minimum buffer size for AudioRecord.Builder

Add AudioRecord.getNativeFrameCount to allow apps to
retrieve the size of the buffer created.

Change-Id: I4c539677726f89afd36fe7b1d24c8b29ada91402
parent c1c31d09
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -14740,6 +14740,7 @@ package android.media {
    method public int getChannelConfiguration();
    method public int getChannelCount();
    method public static int getMinBufferSize(int, int, int);
    method public int getNativeFrameCount() throws java.lang.IllegalStateException;
    method public int getNotificationMarkerPosition();
    method public int getPositionNotificationPeriod();
    method public int getRecordingState();
+1 −0
Original line number Diff line number Diff line
@@ -15950,6 +15950,7 @@ package android.media {
    method public int getChannelConfiguration();
    method public int getChannelCount();
    method public static int getMinBufferSize(int, int, int);
    method public int getNativeFrameCount() throws java.lang.IllegalStateException;
    method public int getNotificationMarkerPosition();
    method public int getPositionNotificationPeriod();
    method public int getRecordingState();
+13 −0
Original line number Diff line number Diff line
@@ -523,6 +523,17 @@ static jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject t
}


// ----------------------------------------------------------------------------
static jint android_media_AudioRecord_get_native_frame_count(JNIEnv *env,  jobject thiz) {
    sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
    if (lpRecorder == NULL) {
        jniThrowException(env, "java/lang/IllegalStateException",
            "Unable to retrieve AudioRecord pointer for getNativeFrameCount()");
        return (jint)AUDIO_JAVA_ERROR;
    }
    return lpRecorder->frameCount();
}

// ----------------------------------------------------------------------------
static jint android_media_AudioRecord_set_marker_pos(JNIEnv *env,  jobject thiz,
        jint markerPos) {
@@ -629,6 +640,8 @@ static JNINativeMethod gMethods[] = {
                             "([FIIZ)I", (void *)android_media_AudioRecord_readInFloatArray},
    {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I",
                                       (void *)android_media_AudioRecord_readInDirectBuffer},
    {"native_get_native_frame_count",
                             "()I",    (void *)android_media_AudioRecord_get_native_frame_count},
    {"native_set_marker_pos","(I)I",   (void *)android_media_AudioRecord_set_marker_pos},
    {"native_get_marker_pos","()I",    (void *)android_media_AudioRecord_get_marker_pos},
    {"native_set_pos_update_period",
+28 −3
Original line number Diff line number Diff line
@@ -398,8 +398,8 @@ public class AudioRecord
     * default output sample rate of the device (see
     * {@link AudioManager#PROPERTY_OUTPUT_SAMPLE_RATE}), its channel configuration will be
     * {@link AudioFormat#CHANNEL_IN_DEFAULT}.
     * <br>Failing to set an adequate buffer size with {@link #setBufferSizeInBytes(int)} will
     * prevent the successful creation of an <code>AudioRecord</code> instance.
     * <br>If the buffer size is not specified with {@link #setBufferSizeInBytes(int)},
     * the minimum buffer size for the source is used.
     */
    public static class Builder {
        private AudioAttributes mAttributes;
@@ -473,7 +473,9 @@ public class AudioRecord
         * during the recording. New audio data can be read from this buffer in smaller chunks
         * than this size. See {@link #getMinBufferSize(int, int, int)} to determine the minimum
         * required buffer size for the successful creation of an AudioRecord instance.
         * Using values smaller than getMinBufferSize() will result in an initialization failure.
         * Since bufferSizeInBytes may be internally increased to accommodate the source
         * requirements, use {@link #getNativeFrameCount()} to determine the actual buffer size
         * in frames.
         * @param bufferSizeInBytes a value strictly greater than 0
         * @return the same Builder instance.
         * @throws IllegalArgumentException
@@ -520,6 +522,13 @@ public class AudioRecord
                        .build();
            }
            try {
                // If the buffer size is not specified,
                // use a single frame for the buffer size and let the
                // native code figure out the minimum buffer size.
                if (mBufferSizeInBytes == 0) {
                    mBufferSizeInBytes = mFormat.getChannelCount()
                            * mFormat.getBytesPerSample(mFormat.getEncoding());
                }
                return new AudioRecord(mAttributes, mFormat, mBufferSizeInBytes, mSessionId);
            } catch (IllegalArgumentException e) {
                throw new UnsupportedOperationException(e.getMessage());
@@ -710,6 +719,20 @@ public class AudioRecord
        }
    }

    /**
     *  Returns the "native frame count" of the <code>AudioRecord</code> buffer.
     *  This is greater than or equal to the bufferSizeInBytes converted to frame units
     *  specified in the <code>AudioRecord</code> constructor or Builder.
     *  The native frame count may be enlarged to accommodate the requirements of the
     *  source on creation or if the <code>AudioRecord</code>
     *  is subsequently rerouted.
     *  @return current size in frames of the <code>AudioRecord</code> buffer.
     *  @throws IllegalStateException
     */
    public int getNativeFrameCount() throws IllegalStateException {
        return native_get_native_frame_count();
    }

    /**
     * Returns the notification marker position expressed in frames.
     */
@@ -1173,6 +1196,8 @@ public class AudioRecord

    private native final int native_read_in_direct_buffer(Object jBuffer, int sizeInBytes);

    private native final int native_get_native_frame_count();

    private native final int native_set_marker_pos(int marker);
    private native final int native_get_marker_pos();