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

Commit b6a7bde6 authored by Paul McLean's avatar Paul McLean
Browse files

Surface MicrophoneDirectionAPI in MediaRecorder

Bug: 126185930
Test: MicrophoneDirectionTest app
Change-Id: I056a45ed8312b33593b6102546652676d5f9ec1b
parent 3d6fff4d
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -25737,7 +25737,7 @@ package android.media {
    field public static final int MEDIA_TRACK_TYPE_VIDEO = 1; // 0x1
  }
  public class MediaRecorder implements android.media.AudioRecordingMonitor android.media.AudioRouting {
  public class MediaRecorder implements android.media.AudioRecordingMonitor android.media.AudioRouting android.media.MicrophoneDirection {
    ctor public MediaRecorder();
    method public void addOnRoutingChangedListener(android.media.AudioRouting.OnRoutingChangedListener, android.os.Handler);
    method protected void finalize();
@@ -25767,6 +25767,8 @@ package android.media {
    method public void setLocation(float, float);
    method public void setMaxDuration(int) throws java.lang.IllegalArgumentException;
    method public void setMaxFileSize(long) throws java.lang.IllegalArgumentException;
    method public boolean setMicrophoneDirection(int);
    method public boolean setMicrophoneFieldDimension(@FloatRange(from=-1.0, to=1.0) float);
    method public void setNextOutputFile(java.io.FileDescriptor) throws java.io.IOException;
    method public void setNextOutputFile(java.io.File) throws java.io.IOException;
    method public void setOnErrorListener(android.media.MediaRecorder.OnErrorListener);
+8 −5
Original line number Diff line number Diff line
@@ -1782,13 +1782,14 @@ public class AudioRecord implements AudioRouting, MicrophoneDirection,
    // MicrophoneDirection
    //--------------------
    /**
     * Specifies the logical microphone (for processing).
     * Specifies the logical microphone (for processing). Applications can use this to specify
     * which side of the device to optimize capture from. Typically used in conjunction with
     * the camera capturing video.
     *
     * @param direction Direction constant.
     * @return true if sucessful.
     */
    public boolean setMicrophoneDirection(int direction) {
        return native_set_microphone_direction(direction) == 0;
    public boolean setMicrophoneDirection(@DirectionMode int direction) {
        return native_set_microphone_direction(direction) == AudioSystem.SUCCESS;
    }

    /**
@@ -1800,7 +1801,9 @@ public class AudioRecord implements AudioRouting, MicrophoneDirection,
     * @return true if sucessful.
     */
    public boolean setMicrophoneFieldDimension(@FloatRange(from = -1.0, to = 1.0) float zoom) {
        return native_set_microphone_field_dimension(zoom) == 0;
        Preconditions.checkArgument(
                zoom >= -1 && zoom <= 1, "Argument must fall between -1 & 1 (inclusive)");
        return native_set_microphone_field_dimension(zoom) == AudioSystem.SUCCESS;
    }

    //---------------------------------------------------------
+34 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.media;

import android.annotation.CallbackExecutor;
import android.annotation.FloatRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
@@ -35,6 +36,7 @@ import android.util.Pair;
import android.view.Surface;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;

import java.io.File;
import java.io.FileDescriptor;
@@ -45,7 +47,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;


/**
 * Used to record audio and video. The recording control is based on a
 * simple state machine (see below).
@@ -90,7 +91,8 @@ import java.util.concurrent.Executor;
 */
public class MediaRecorder implements AudioRouting,
                                      AudioRecordingMonitor,
                                      AudioRecordingMonitorClient
                                      AudioRecordingMonitorClient,
                                      MicrophoneDirection
{
    static {
        System.loadLibrary("media_jni");
@@ -1526,6 +1528,36 @@ public class MediaRecorder implements AudioRouting,
    private native final int native_getActiveMicrophones(
            ArrayList<MicrophoneInfo> activeMicrophones);

    //--------------------------------------------------------------------------
    // MicrophoneDirection
    //--------------------
    /**
     * Specifies the logical microphone (for processing).
     *
     * @param direction Direction constant.
     * @return true if sucessful.
     */
    public boolean setMicrophoneDirection(@DirectionMode int direction) {
        return native_setMicrophoneDirection(direction) == 0;
    }

    /**
     * Specifies the zoom factor (i.e. the field dimension) for the selected microphone
     * (for processing). The selected microphone is determined by the use-case for the stream.
     *
     * @param zoom the desired field dimension of microphone capture. Range is from -1 (wide angle),
     * though 0 (no zoom) to 1 (maximum zoom).
     * @return true if sucessful.
     */
    public boolean setMicrophoneFieldDimension(@FloatRange(from = -1.0, to = 1.0) float zoom) {
        Preconditions.checkArgument(
                zoom >= -1 && zoom <= 1, "Argument must fall between -1 & 1 (inclusive)");
        return native_setMicrophoneFieldDimension(zoom) == 0;
    }

    private native int native_setMicrophoneDirection(int direction);
    private native int native_setMicrophoneFieldDimension(float zoom);

    //--------------------------------------------------------------------------
    // Implementation of AudioRecordingMonitor interface
    //--------------------
+10 −4
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ import android.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Interface defining mechanism for controlling the directionality and field width of
 * audio capture.
 */
public interface MicrophoneDirection {
    /**
     * Don't do any directionality processing of the activated microphone(s).
@@ -41,21 +45,23 @@ public interface MicrophoneDirection {
    int MIC_DIRECTION_EXTERNAL = 3;

    /** @hide */
    @IntDef({
    /*public*/ @IntDef({
            MIC_DIRECTION_UNSPECIFIED,
            MIC_DIRECTION_FRONT,
            MIC_DIRECTION_BACK,
            MIC_DIRECTION_EXTERNAL
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface Directionmode{};
    @interface DirectionMode{};
    /**
     * Specifies the logical microphone (for processing).
     * Specifies the logical microphone (for processing). Applications can use this to specify
     * which side of the device to optimize capture from. Typically used in conjunction with
     * the camera capturing video.
     *
     * @param direction Direction constant.
     * @return true if sucessful.
     */
    boolean setMicrophoneDirection(@Directionmode int direction);
    boolean setMicrophoneDirection(@DirectionMode int direction);

    /**
     * Specifies the zoom factor (i.e. the field dimension) for the selected microphone
+40 −0
Original line number Diff line number Diff line
@@ -760,6 +760,44 @@ android_media_MediaRecord_getActiveMicrophones(JNIEnv *env,
    return jStatus;
}

static jint android_media_MediaRecord_setMicrophoneDirection(
        JNIEnv *env, jobject thiz, jint direction) {
    ALOGV("setMicrophoneDirection(%d)", direction);
    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
    if (mr == NULL) {
        jniThrowException(env, "java/lang/IllegalStateException", NULL);
        return (jint)AUDIO_JAVA_NO_INIT;
    }

    jint jStatus = AUDIO_JAVA_SUCCESS;
    status_t status =
        mr->setMicrophoneDirection(static_cast<audio_microphone_direction_t>(direction));
    if (status != NO_ERROR) {
        jStatus = nativeToJavaStatus(status);
    }

    return jStatus;
}

static jint  android_media_MediaRecord_setMicrophoneFieldDimension(
        JNIEnv *env, jobject thiz, jfloat zoom) {
    ALOGV("setMicrophoneFieldDimension(%f)", zoom);
    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
    if (mr == NULL) {
        jniThrowException(env, "java/lang/IllegalStateException", NULL);
        return (jint)AUDIO_JAVA_NO_INIT;
    }

    jint jStatus = AUDIO_JAVA_SUCCESS;
    status_t status = mr->setMicrophoneFieldDimension(zoom);
    if (status != NO_ERROR) {
        jStatus = nativeToJavaStatus(status);
    }

    return jStatus;

}

static jint android_media_MediaRecord_getPortId(JNIEnv *env,  jobject thiz) {
    sp<MediaRecorder> mr = getMediaRecorder(env, thiz);
    if (mr == NULL) {
@@ -812,6 +850,8 @@ static const JNINativeMethod gMethods[] = {

    {"native_getActiveMicrophones", "(Ljava/util/ArrayList;)I", (void *)android_media_MediaRecord_getActiveMicrophones},
    {"native_getPortId", "()I", (void *)android_media_MediaRecord_getPortId},
    {"native_setMicrophoneDirection", "(I)I", (void *)android_media_MediaRecord_setMicrophoneDirection},
    {"native_setMicrophoneFieldDimension", "(F)I", (void *)android_media_MediaRecord_setMicrophoneFieldDimension},
};

// This function only registers the native methods, and is called from