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

Commit 0d7042d5 authored by Dongwon Kang's avatar Dongwon Kang
Browse files

MediaPlayer2: remove AudioManager private API usage

Private API AudioManager.getDevicesStatic() is being used in
MediaPlayer2 for conversion between id <=> AudioDeviceInfo.
This conversion was added to talk with internal native AudioTrack API,
but in MediaPlayer2 since we will use public java AudioTrack API,
the given AudioDeviceInfo can be directly passed to java AudioTrack
instance.

Test: CtsMediaTestCases:android.media.cts.RoutingTest
Bug: 112767549
Change-Id: I9aeae02202edb6b5f0eb72b4f4dd3c936103ced2
parent 62cab4de
Loading
Loading
Loading
Loading
+7 −17
Original line number Diff line number Diff line
@@ -486,19 +486,11 @@ status_t JAudioTrack::dump(int fd, const Vector<String16>& args __unused) const
    return NO_ERROR;
}

audio_port_handle_t JAudioTrack::getRoutedDeviceId() {
jobject JAudioTrack::getRoutedDevice() {
    JNIEnv *env = JavaVMHelper::getJNIEnv();
    jmethodID jGetRoutedDevice = env->GetMethodID(mAudioTrackCls, "getRoutedDevice",
            "()Landroid/media/AudioDeviceInfo;");
    jobject jAudioDeviceInfoObj = env->CallObjectMethod(mAudioTrackObj, jGetRoutedDevice);
    if (env->IsSameObject(jAudioDeviceInfoObj, NULL)) {
        return AUDIO_PORT_HANDLE_NONE;
    }

    jclass jAudioDeviceInfoCls = env->FindClass("android/media/AudioDeviceInfo");
    jmethodID jGetId = env->GetMethodID(jAudioDeviceInfoCls, "getId", "()I");
    jint routedDeviceId = env->CallIntMethod(jAudioDeviceInfoObj, jGetId);
    return routedDeviceId;
    return env->CallObjectMethod(mAudioTrackObj, jGetRoutedDevice);
}

audio_session_t JAudioTrack::getAudioSessionId() {
@@ -508,13 +500,11 @@ audio_session_t JAudioTrack::getAudioSessionId() {
    return (audio_session_t) sessionId;
}

status_t JAudioTrack::setOutputDevice(audio_port_handle_t deviceId) {
status_t JAudioTrack::setPreferredDevice(jobject device) {
    JNIEnv *env = JavaVMHelper::getJNIEnv();
    jclass jMP2ImplCls = env->FindClass("android/media/MediaPlayer2Impl");
    jmethodID jSetAudioOutputDeviceById = env->GetStaticMethodID(
            jMP2ImplCls, "setAudioOutputDeviceById", "(Landroid/media/AudioTrack;I)Z");
    jboolean result = env->CallStaticBooleanMethod(
            jMP2ImplCls, jSetAudioOutputDeviceById, mAudioTrackObj, deviceId);
    jmethodID jSetPreferredDeviceId = env->GetMethodID(mAudioTrackCls, "setPreferredDevice",
            "(Landroid/media/AudioDeviceInfo;)Z");
    jboolean result = env->CallBooleanMethod(mAudioTrackObj, jSetPreferredDeviceId, device);
    return result == true ? NO_ERROR : BAD_VALUE;
}

+16 −14
Original line number Diff line number Diff line
@@ -74,9 +74,7 @@ MediaPlayer2AudioOutput::MediaPlayer2AudioOutput(audio_session_t sessionId, uid_
      mPid(pid),
      mSendLevel(0.0),
      mAuxEffectId(0),
      mFlags(AUDIO_OUTPUT_FLAG_NONE),
      mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
      mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE) {
      mFlags(AUDIO_OUTPUT_FLAG_NONE) {
    ALOGV("MediaPlayer2AudioOutput(%d)", sessionId);
    if (attr != nullptr) {
        mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
@@ -388,7 +386,9 @@ status_t MediaPlayer2AudioOutput::updateTrack_l() {
            res = mJAudioTrack->attachAuxEffect(mAuxEffectId);
        }
    }
    mJAudioTrack->setOutputDevice(mSelectedDeviceId);
    if (mPreferredDevice != nullptr) {
        mJAudioTrack->setPreferredDevice(mPreferredDevice->getJObject());
    }

    mJAudioTrack->registerRoutingDelegates(mRoutingDelegates);

@@ -518,24 +518,26 @@ status_t MediaPlayer2AudioOutput::attachAuxEffect(int effectId) {
    return NO_ERROR;
}

status_t MediaPlayer2AudioOutput::setOutputDevice(audio_port_handle_t deviceId) {
    ALOGV("setOutputDevice(%d)", deviceId);
status_t MediaPlayer2AudioOutput::setPreferredDevice(jobject device) {
    ALOGV("setPreferredDevice");
    Mutex::Autolock lock(mLock);
    mSelectedDeviceId = deviceId;
    status_t ret = NO_ERROR;
    if (mJAudioTrack != nullptr) {
        return mJAudioTrack->setOutputDevice(deviceId);
        ret = mJAudioTrack->setPreferredDevice(device);
    }
    return NO_ERROR;
    if (ret == NO_ERROR) {
        mPreferredDevice = new JObjectHolder(device);
    }
    return ret;
}

status_t MediaPlayer2AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId) {
    ALOGV("getRoutedDeviceId");
jobject MediaPlayer2AudioOutput::getRoutedDevice() {
    ALOGV("getRoutedDevice");
    Mutex::Autolock lock(mLock);
    if (mJAudioTrack != nullptr) {
        mRoutedDeviceId = mJAudioTrack->getRoutedDeviceId();
        return mJAudioTrack->getRoutedDevice();
    }
    *deviceId = mRoutedDeviceId;
    return NO_ERROR;
    return nullptr;
}

status_t MediaPlayer2AudioOutput::addAudioDeviceCallback(jobject jRoutingDelegate) {
+7 −8
Original line number Diff line number Diff line
@@ -335,25 +335,24 @@ public:
     */
    status_t dump(int fd, const Vector<String16>& args) const;

    /* Returns the ID of the audio device actually used by the output to which this AudioTrack is
     * attached. When the AudioTrack is inactive, it will return AUDIO_PORT_HANDLE_NONE.
    /* Returns the AudioDeviceInfo used by the output to which this AudioTrack is
     * attached.
     */
    audio_port_handle_t getRoutedDeviceId();
    jobject getRoutedDevice();

    /* Returns the ID of the audio session this AudioTrack belongs to. */
    audio_session_t getAudioSessionId();

    /* Selects the audio device to use for output of this AudioTrack. A value of
     * AUDIO_PORT_HANDLE_NONE indicates default routing.
    /* Sets the preferred audio device to use for output of this AudioTrack.
     *
     * Parameters:
     *  The device ID of the selected device (as returned by the AudioDevicesManager API).
     * Device: an AudioDeviceInfo object.
     *
     * Returned value:
     *  - NO_ERROR: successful operation
     *  - BAD_VALUE: failed to find the valid output device with given device Id.
     *  - BAD_VALUE: failed to set the device
     */
    status_t setOutputDevice(audio_port_handle_t deviceId);
    status_t setPreferredDevice(jobject device);

    // TODO: Add AUDIO_OUTPUT_FLAG_DIRECT when it is possible to check.
    // TODO: Add AUDIO_FLAG_HW_AV_SYNC when it is possible to check.
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright 2018, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef JOBJECT_HOLDER_H_

#define JOBJECT_HOLDER_H_

#include "jni.h"
#include <mediaplayer2/JavaVMHelper.h>
#include <utils/RefBase.h>

namespace android {

// Helper class for managing global reference of jobject.
struct JObjectHolder : public RefBase {
    JObjectHolder(jobject obj) {
        JNIEnv *env = JavaVMHelper::getJNIEnv();
        mJObject = reinterpret_cast<jobject>(env->NewGlobalRef(obj));
    }

    virtual ~JObjectHolder() {
        JNIEnv *env = JavaVMHelper::getJNIEnv();
        env->DeleteGlobalRef(mJObject);
    }

    jobject getJObject() { return mJObject; }

private:
    jobject mJObject;
};

}  //" android

#endif  // JOBJECT_HOLDER_H_
+6 −4
Original line number Diff line number Diff line
@@ -20,12 +20,15 @@

#include <mediaplayer2/MediaPlayer2Interface.h>
#include <mediaplayer2/JAudioTrack.h>
#include <mediaplayer2/JObjectHolder.h>

#include <vector>
#include <utility>
#include <utils/String16.h>
#include <utils/Vector.h>

#include "jni.h"

namespace android {

class AudioTrack;
@@ -92,8 +95,8 @@ public:
        //return mNextOutput == NULL;
    }
    // AudioRouting
    virtual status_t setOutputDevice(audio_port_handle_t deviceId);
    virtual status_t getRoutedDeviceId(audio_port_handle_t* deviceId);
    virtual status_t setPreferredDevice(jobject device);
    virtual jobject getRoutedDevice();
    virtual status_t addAudioDeviceCallback(jobject routingDelegate);
    virtual status_t removeAudioDeviceCallback(jobject listener);
    virtual void copyAudioDeviceCallback(std::vector<jobject>& routingDelegateTarget);
@@ -121,8 +124,7 @@ private:
    float                   mSendLevel;
    int                     mAuxEffectId;
    audio_output_flags_t    mFlags;
    audio_port_handle_t     mSelectedDeviceId;
    audio_port_handle_t     mRoutedDeviceId;
    sp<JObjectHolder>       mPreferredDevice;
    mutable Mutex           mLock;
    std::vector<std::pair<jobject, jobject>> mRoutingDelegates; // <listener, routingDelegate>

Loading