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

Commit 946bdb36 authored by Dongwon Kang's avatar Dongwon Kang
Browse files

MediaPlayer2: use AudioManager.isOffloadedPlaybackSupported

Test: MediaPlayer2Test
Bug: 112549970
Change-Id: I85c689b029a82122ff148c381a021657db2893d4
parent 54cbc298
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
cc_library_static {

    srcs: [
        "JMediaPlayer2Utils.cpp",
        "JWakeLock.cpp",
        "GenericSource2.cpp",
        "HTTPLiveSource2.cpp",
@@ -29,6 +30,7 @@ cc_library_static {
        "frameworks/av/media/libstagefright/rtsp",
        "frameworks/av/media/libstagefright/timedtext",
        "frameworks/av/media/ndk",
        "frameworks/base/core/jni",
    ],

    cflags: [
@@ -57,6 +59,7 @@ cc_library_static {
    static_libs: [
        "libmedia_helper",
        "libmediaplayer2-protos",
        "libmedia2_jni_core",
    ],

    name: "libstagefright_nuplayer2",
+66 −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.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "JMediaPlayer2Utils"

#include "JMediaPlayer2Utils.h"
#include <mediaplayer2/JavaVMHelper.h>

#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/Utils.h>
#include <nativehelper/JNIHelp.h>
#include <utils/Log.h>

#include "log/log.h"

namespace android {

static const int64_t kOffloadMinDurationSec = 60;

// static
bool JMediaPlayer2Utils::isOffloadedAudioPlaybackSupported(
        const sp<MetaData>& meta, bool hasVideo, bool isStreaming, audio_stream_type_t streamType)
{
    if (hasVideo || streamType != AUDIO_STREAM_MUSIC) {
        return false;
    }

    audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
    if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
        return false;
    }

    if (info.duration_us < kOffloadMinDurationSec * 1000000) {
        return false;
    }

    int32_t audioFormat = audioFormatFromNative(info.format);
    int32_t channelMask = outChannelMaskFromNative(info.channel_mask);
    if (audioFormat == ENCODING_INVALID || channelMask == CHANNEL_INVALID) {
        return false;
    }

    JNIEnv* env = JavaVMHelper::getJNIEnv();
    jclass jMP2UtilsCls = env->FindClass("android/media/MediaPlayer2Utils");
    jmethodID jSetAudioOutputDeviceById = env->GetStaticMethodID(
            jMP2UtilsCls, "isOffloadedAudioPlaybackSupported", "(III)Z");
    jboolean result = env->CallStaticBooleanMethod(
            jMP2UtilsCls, jSetAudioOutputDeviceById, audioFormat, info.sample_rate, channelMask);
    return result;
}

}  // namespace android
+35 −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 _J_MEDIAPLAYER2_UTILS2_H_
#define _J_MEDIAPLAYER2_UTILS2_H_

#include <media/stagefright/MetaData.h>

#include "jni.h"
#include "android_media_AudioFormat.h"

namespace android {

struct JMediaPlayer2Utils {
    static bool isOffloadedAudioPlaybackSupported(
            const sp<MetaData>& meta, bool hasVideo, bool isStreaming,
            audio_stream_type_t streamType);
};

}  // namespace android

#endif  // _J_MEDIAPLAYER2_UTILS2_H_
+3 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "NuPlayer2.h"

#include "HTTPLiveSource2.h"
#include "JMediaPlayer2Utils.h"
#include "NuPlayer2CCDecoder.h"
#include "NuPlayer2Decoder.h"
#include "NuPlayer2DecoderBase.h"
@@ -1699,7 +1700,8 @@ void NuPlayer2::onStart(bool play) {
    }

    mOffloadAudio =
        canOffloadStream(audioMeta, hasVideo, mCurrentSourceInfo.mSource->isStreaming(), streamType)
        JMediaPlayer2Utils::isOffloadedAudioPlaybackSupported(
                audioMeta, hasVideo, mCurrentSourceInfo.mSource->isStreaming(), streamType)
                && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);

    // Modular DRM: Disabling audio offload if the source is protected
+27 −18
Original line number Diff line number Diff line
@@ -1830,43 +1830,43 @@ const struct aac_format_conv_t* p = &profileLookup[0];
    return;
}

bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
                      bool isStreaming, audio_stream_type_t streamType)
status_t getAudioOffloadInfo(const sp<MetaData>& meta, bool hasVideo,
        bool isStreaming, audio_stream_type_t streamType, audio_offload_info_t *info)
{
    const char *mime;
    if (meta == NULL) {
        return false;
        return BAD_VALUE;
    }
    CHECK(meta->findCString(kKeyMIMEType, &mime));

    audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
    (*info) = AUDIO_INFO_INITIALIZER;

    info.format = AUDIO_FORMAT_INVALID;
    if (mapMimeToAudioFormat(info.format, mime) != OK) {
    info->format = AUDIO_FORMAT_INVALID;
    if (mapMimeToAudioFormat(info->format, mime) != OK) {
        ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
        return false;
        return BAD_VALUE;
    } else {
        ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format);
        ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info->format);
    }

    if (AUDIO_FORMAT_INVALID == info.format) {
    if (AUDIO_FORMAT_INVALID == info->format) {
        // can't offload if we don't know what the source format is
        ALOGE("mime type \"%s\" not a known audio format", mime);
        return false;
        return BAD_VALUE;
    }

    // Redefine aac format according to its profile
    // Offloading depends on audio DSP capabilities.
    int32_t aacaot = -1;
    if (meta->findInt32(kKeyAACAOT, &aacaot)) {
        mapAACProfileToAudioFormat(info.format,(OMX_AUDIO_AACPROFILETYPE) aacaot);
        mapAACProfileToAudioFormat(info->format,(OMX_AUDIO_AACPROFILETYPE) aacaot);
    }

    int32_t srate = -1;
    if (!meta->findInt32(kKeySampleRate, &srate)) {
        ALOGV("track of type '%s' does not publish sample rate", mime);
    }
    info.sample_rate = srate;
    info->sample_rate = srate;

    int32_t cmask = 0;
    if (!meta->findInt32(kKeyChannelMask, &cmask) || cmask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
@@ -1880,25 +1880,34 @@ bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
            cmask = audio_channel_out_mask_from_count(channelCount);
        }
    }
    info.channel_mask = cmask;
    info->channel_mask = cmask;

    int64_t duration = 0;
    if (!meta->findInt64(kKeyDuration, &duration)) {
        ALOGV("track of type '%s' does not publish duration", mime);
    }
    info.duration_us = duration;
    info->duration_us = duration;

    int32_t brate = -1;
    if (!meta->findInt32(kKeyBitRate, &brate)) {
        ALOGV("track of type '%s' does not publish bitrate", mime);
    }
    info.bit_rate = brate;
    info->bit_rate = brate;


    info.stream_type = streamType;
    info.has_video = hasVideo;
    info.is_streaming = isStreaming;
    info->stream_type = streamType;
    info->has_video = hasVideo;
    info->is_streaming = isStreaming;
    return OK;
}

bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
                      bool isStreaming, audio_stream_type_t streamType)
{
    audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
    if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
        return false;
    }
    // Check if offload is possible for given format, stream type, sample rate,
    // bit rate, duration, video and streaming
    return AudioSystem::isOffloadSupported(info);
Loading