1 /*
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 <utils/Log.h>
26 
27 #include "log/log.h"
28 
29 namespace android {
30 
31 static const int64_t kOffloadMinDurationSec = 60;
32 
33 // static
isOffloadedAudioPlaybackSupported(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType)34 bool JMediaPlayer2Utils::isOffloadedAudioPlaybackSupported(
35         const sp<MetaData>& meta, bool hasVideo, bool isStreaming, audio_stream_type_t streamType)
36 {
37     if (hasVideo || streamType != AUDIO_STREAM_MUSIC) {
38         return false;
39     }
40 
41     audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
42     if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
43         return false;
44     }
45 
46     if (info.duration_us < kOffloadMinDurationSec * 1000000) {
47         return false;
48     }
49 
50     int32_t audioFormat = audioFormatFromNative(info.format);
51     int32_t channelMask = outChannelMaskFromNative(info.channel_mask);
52     if (audioFormat == ENCODING_INVALID || channelMask == CHANNEL_INVALID) {
53         return false;
54     }
55 
56     JNIEnv* env = JavaVMHelper::getJNIEnv();
57     jclass jMP2UtilsCls = env->FindClass("android/media/MediaPlayer2Utils");
58     jmethodID jSetAudioOutputDeviceById = env->GetStaticMethodID(
59             jMP2UtilsCls, "isOffloadedAudioPlaybackSupported", "(III)Z");
60     jboolean result = env->CallStaticBooleanMethod(
61             jMP2UtilsCls, jSetAudioOutputDeviceById, audioFormat, info.sample_rate, channelMask);
62     return result;
63 }
64 
65 }  // namespace android
66