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

Commit a60eaa87 authored by Dichen Zhang's avatar Dichen Zhang
Browse files

MediaPlayer2: create MP2AudioOutput in constructor

Move MP2AudioOutput creator from setDataSource() to constructor.
Test: MediaPlayer2Test, RoutingTest

Change-Id: Ibe19c8a259c735ea2b5822f769babaf4d7f2bed0
parents ba92a60c c2465c59
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ JAudioTrack::JAudioTrack( // < Usages of the argumen
        void* user,                                   // Offload
        size_t frameCount,                            // bufferSizeInBytes
        audio_session_t sessionId,                    // AudioTrack
        const audio_attributes_t* pAttributes,        // AudioAttributes
        const jobject attributes,                     // AudioAttributes
        float maxRequiredSpeed) {                     // bufferSizeInBytes

    JNIEnv *env = JavaVMHelper::getJNIEnv();
@@ -65,13 +65,17 @@ JAudioTrack::JAudioTrack( // < Usages of the argumen
    jmethodID jBuilderCtor = env->GetMethodID(jBuilderCls, "<init>", "()V");
    jobject jBuilderObj = env->NewObject(jBuilderCls, jBuilderCtor);

    jobject jAudioAttributesObj = JAudioAttributes::createAudioAttributesObj(env, pAttributes);
    mAudioAttributesObj = reinterpret_cast<jobject>(env->NewGlobalRef(jAudioAttributesObj));
    env->DeleteLocalRef(jAudioAttributesObj);
    if (attributes != NULL) {
        mAudioAttributesObj = new JObjectHolder(attributes);
    } else {
        mAudioAttributesObj = new JObjectHolder(
                JAudioAttributes::createAudioAttributesObj(env, NULL));
    }

    jmethodID jSetAudioAttributes = env->GetMethodID(jBuilderCls, "setAudioAttributes",
            "(Landroid/media/AudioAttributes;)Landroid/media/AudioTrack$Builder;");
    jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetAudioAttributes, mAudioAttributesObj);
    jBuilderObj = env->CallObjectMethod(jBuilderObj,
            jSetAudioAttributes, mAudioAttributesObj->getJObject());

    jmethodID jSetAudioFormat = env->GetMethodID(jBuilderCls, "setAudioFormat",
            "(Landroid/media/AudioFormat;)Landroid/media/AudioTrack$Builder;");
@@ -125,7 +129,6 @@ JAudioTrack::~JAudioTrack() {
    JNIEnv *env = JavaVMHelper::getJNIEnv();
    env->DeleteGlobalRef(mAudioTrackCls);
    env->DeleteGlobalRef(mAudioTrackObj);
    env->DeleteGlobalRef(mAudioAttributesObj);
}

size_t JAudioTrack::frameCount() {
@@ -509,11 +512,15 @@ status_t JAudioTrack::setPreferredDevice(jobject device) {
}

audio_stream_type_t JAudioTrack::getAudioStreamType() {
    if (mAudioAttributesObj == NULL) {
        return AUDIO_STREAM_DEFAULT;
    }
    JNIEnv *env = JavaVMHelper::getJNIEnv();
    jclass jAudioAttributesCls = env->FindClass("android/media/AudioAttributes");
    jmethodID jGetVolumeControlStream = env->GetMethodID(jAudioAttributesCls,
            "getVolumeControlStream", "()I");
    int javaAudioStreamType = env->CallIntMethod(mAudioAttributesObj, jGetVolumeControlStream);
    int javaAudioStreamType = env->CallIntMethod(
            mAudioAttributesObj->getJObject(), jGetVolumeControlStream);
    return (audio_stream_type_t)javaAudioStreamType;
}

+14 −34
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ status_t MediaPlayer2AudioOutput::dump(int fd, const Vector<String16>& args) con
}

MediaPlayer2AudioOutput::MediaPlayer2AudioOutput(audio_session_t sessionId, uid_t uid, int pid,
        const audio_attributes_t* attr, std::vector<jobject>& routingDelegatesBackup)
        const jobject attributes)
    : mCallback(nullptr),
      mCallbackCookie(nullptr),
      mCallbackData(nullptr),
@@ -76,22 +76,13 @@ MediaPlayer2AudioOutput::MediaPlayer2AudioOutput(audio_session_t sessionId, uid_
      mAuxEffectId(0),
      mFlags(AUDIO_OUTPUT_FLAG_NONE) {
    ALOGV("MediaPlayer2AudioOutput(%d)", sessionId);
    if (attr != nullptr) {
        mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
        if (mAttributes != nullptr) {
            memcpy(mAttributes, attr, sizeof(audio_attributes_t));
        }
    } else {
        mAttributes = nullptr;

    if (attributes != nullptr) {
        mAttributes = new JObjectHolder(attributes);
    }

    setMinBufferCount();
    mRoutingDelegates.clear();
    for (auto routingDelegate : routingDelegatesBackup) {
        mRoutingDelegates.push_back(std::pair<jobject, jobject>(
                JAudioTrack::getListener(routingDelegate), routingDelegate));
    }
    routingDelegatesBackup.clear();
}

MediaPlayer2AudioOutput::~MediaPlayer2AudioOutput() {
@@ -99,7 +90,6 @@ MediaPlayer2AudioOutput::~MediaPlayer2AudioOutput() {
        JAudioTrack::removeGlobalRef(routingDelegate.second);
    }
    close();
    free(mAttributes);
    delete mCallbackData;
}

@@ -251,16 +241,10 @@ status_t MediaPlayer2AudioOutput::getFramesWritten(uint32_t *frameswritten) cons
    return status;
}

void MediaPlayer2AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
void MediaPlayer2AudioOutput::setAudioAttributes(const jobject attributes) {
    Mutex::Autolock lock(mLock);
    if (attributes == nullptr) {
        free(mAttributes);
        mAttributes = nullptr;
    } else {
        if (mAttributes == nullptr) {
            mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
        }
        memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
    if (attributes != nullptr) {
        sp<JObjectHolder> x = new JObjectHolder(attributes);
    }
}

@@ -325,7 +309,7 @@ status_t MediaPlayer2AudioOutput::open(
                 newcbd,
                 frameCount,
                 mSessionId,
                 mAttributes,
                 mAttributes != nullptr ? mAttributes->getJObject() : nullptr,
                 1.0f);  // default value for maxRequiredSpeed
    } else {
        // TODO: Due to buffer memory concerns, we use a max target playback speed
@@ -344,7 +328,7 @@ status_t MediaPlayer2AudioOutput::open(
                 nullptr,
                 frameCount,
                 mSessionId,
                 mAttributes,
                 mAttributes != nullptr ? mAttributes->getJObject() : nullptr,
                 targetSpeed);
    }

@@ -569,15 +553,6 @@ status_t MediaPlayer2AudioOutput::removeAudioDeviceCallback(jobject listener) {
    return NO_ERROR;
}

void MediaPlayer2AudioOutput::copyAudioDeviceCallback(
        std::vector<jobject>& routingDelegateTarget) {
    ALOGV("copyAudioDeviceCallback");
    for (std::vector<std::pair<jobject, jobject>>::iterator it = mRoutingDelegates.begin();
            it != mRoutingDelegates.end(); it++) {
        routingDelegateTarget.push_back(it->second);
    }
}

// static
void MediaPlayer2AudioOutput::CallbackWrapper(
        int event, void *cookie, void *info) {
@@ -655,6 +630,11 @@ audio_session_t MediaPlayer2AudioOutput::getSessionId() const {
    return mSessionId;
}

void MediaPlayer2AudioOutput::setSessionId(const audio_session_t id) {
    Mutex::Autolock lock(mLock);
    mSessionId = id;
}

uint32_t MediaPlayer2AudioOutput::getSampleRate() const {
    Mutex::Autolock lock(mLock);
    if (mJAudioTrack == 0) {
+3 −3
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
#include <media/VolumeShaper.h>
#include <system/audio.h>
#include <utils/Errors.h>

#include <mediaplayer2/JObjectHolder.h>
#include <media/AudioTimestamp.h>   // It has dependency on audio.h/Errors.h, but doesn't
                                    // include them in it. Therefore it is included here at last.

@@ -115,7 +115,7 @@ public:
                void* user,
                size_t frameCount = 0,
                audio_session_t sessionId  = AUDIO_SESSION_ALLOCATE,
                const audio_attributes_t* pAttributes = NULL,
                const jobject pAttributes = NULL,
                float maxRequiredSpeed = 1.0f);

    /*
@@ -446,7 +446,7 @@ private:

    jclass mAudioTrackCls;
    jobject mAudioTrackObj;
    jobject mAudioAttributesObj;
    sp<JObjectHolder> mAudioAttributesObj;

    /* Creates a Java VolumeShaper.Configuration object from VolumeShaper::Configuration */
    jobject createVolumeShaperConfigurationObj(
+4 −5
Original line number Diff line number Diff line
@@ -41,8 +41,7 @@ public:
    MediaPlayer2AudioOutput(audio_session_t sessionId,
                            uid_t uid,
                            int pid,
                            const audio_attributes_t * attr,
                            std::vector<jobject>& routingDelegatesBackup);
                            const jobject attributes);
    virtual ~MediaPlayer2AudioOutput();

    virtual bool ready() const {
@@ -59,6 +58,7 @@ public:
    virtual int64_t getPlayedOutDurationUs(int64_t nowUs) const;
    virtual status_t getFramesWritten(uint32_t *frameswritten) const;
    virtual audio_session_t getSessionId() const;
    virtual void setSessionId(const audio_session_t id);
    virtual uint32_t getSampleRate() const;
    virtual int64_t getBufferDurationInUs() const;

@@ -76,7 +76,7 @@ public:
    virtual void flush();
    virtual void pause();
    virtual void close();
    void setAudioAttributes(const audio_attributes_t * attributes);
    void setAudioAttributes(const jobject attributes);
    virtual audio_stream_type_t getAudioStreamType() const;

    void setVolume(float volume);
@@ -99,7 +99,6 @@ public:
    virtual jobject getRoutedDevice();
    virtual status_t addAudioDeviceCallback(jobject routingDelegate);
    virtual status_t removeAudioDeviceCallback(jobject listener);
    virtual void copyAudioDeviceCallback(std::vector<jobject>& routingDelegateTarget);

private:
    static void setMinBufferCount();
@@ -112,7 +111,7 @@ private:
    AudioCallback           mCallback;
    void *                  mCallbackCookie;
    CallbackData *          mCallbackData;
    audio_attributes_t *    mAttributes;
    sp<JObjectHolder>       mAttributes;
    float                   mVolume;
    AudioPlaybackRate       mPlaybackRate;
    uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
+6 −7
Original line number Diff line number Diff line
@@ -24,8 +24,8 @@
#include <media/mediaplayer_common.h>
#include <mediaplayer2/MediaPlayer2Interface.h>
#include <mediaplayer2/MediaPlayer2Types.h>
#include <mediaplayer2/JObjectHolder.h>

#include <vector>
#include <jni.h>
#include <utils/Errors.h>
#include <utils/Mutex.h>
@@ -100,7 +100,8 @@ public:
            audio_session_t getAudioSessionId();
            status_t        setAuxEffectSendLevel(float level);
            status_t        attachAuxEffect(int effectId);
            status_t        setParameter(int key, const Parcel& request);
            status_t        setAudioAttributes(const jobject attributes);
            jobject         getAudioAttributes();
            status_t        getParameter(int key, Parcel* reply);

            // Modular DRM
@@ -121,14 +122,14 @@ private:
    // Disconnect from the currently connected ANativeWindow.
    void disconnectNativeWindow_l();

    status_t setAudioAttributes_l(const Parcel &request);
    status_t setAudioAttributes_l(const jobject attributes);

    void clear_l();
    status_t seekTo_l(int64_t msec, MediaPlayer2SeekMode mode);
    status_t prepareAsync_l();
    status_t getDuration_l(int64_t *msec);
    status_t reset_l();
    status_t checkStateForKeySet_l(int key);
    status_t checkState_l();

    pid_t                       mPid;
    uid_t                       mUid;
@@ -145,15 +146,13 @@ private:
    int64_t                     mSeekPosition;
    MediaPlayer2SeekMode        mSeekMode;
    audio_stream_type_t         mStreamType;
    Parcel*                     mAudioAttributesParcel;
    bool                        mLoop;
    float                       mVolume;
    int                         mVideoWidth;
    int                         mVideoHeight;
    audio_session_t             mAudioSessionId;
    audio_attributes_t *        mAudioAttributes;
    sp<JObjectHolder>           mAudioAttributes;
    float                       mSendLevel;
    std::vector<jobject>        mRoutingDelegates;
    sp<ANativeWindowWrapper>    mConnectedWindow;
};

Loading