blob: bbd22bcbe235c1b1d63cdb7a8629576e56b8f2e2 [file] [log] [blame]
Dongwon Kang946bdb32018-11-14 10:12:00 -08001/*
2 * Copyright 2018, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "JMediaPlayer2Utils"
19
20#include "JMediaPlayer2Utils.h"
21#include <mediaplayer2/JavaVMHelper.h>
22
23#include <media/stagefright/MediaDefs.h>
24#include <media/stagefright/Utils.h>
25#include <nativehelper/JNIHelp.h>
26#include <utils/Log.h>
27
28#include "log/log.h"
29
30namespace android {
31
32static const int64_t kOffloadMinDurationSec = 60;
33
34// static
35bool JMediaPlayer2Utils::isOffloadedAudioPlaybackSupported(
36 const sp<MetaData>& meta, bool hasVideo, bool isStreaming, audio_stream_type_t streamType)
37{
38 if (hasVideo || streamType != AUDIO_STREAM_MUSIC) {
39 return false;
40 }
41
42 audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
43 if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
44 return false;
45 }
46
47 if (info.duration_us < kOffloadMinDurationSec * 1000000) {
48 return false;
49 }
50
51 int32_t audioFormat = audioFormatFromNative(info.format);
52 int32_t channelMask = outChannelMaskFromNative(info.channel_mask);
53 if (audioFormat == ENCODING_INVALID || channelMask == CHANNEL_INVALID) {
54 return false;
55 }
56
57 JNIEnv* env = JavaVMHelper::getJNIEnv();
58 jclass jMP2UtilsCls = env->FindClass("android/media/MediaPlayer2Utils");
59 jmethodID jSetAudioOutputDeviceById = env->GetStaticMethodID(
60 jMP2UtilsCls, "isOffloadedAudioPlaybackSupported", "(III)Z");
61 jboolean result = env->CallStaticBooleanMethod(
62 jMP2UtilsCls, jSetAudioOutputDeviceById, audioFormat, info.sample_rate, channelMask);
63 return result;
64}
65
66} // namespace android