JMediaPlayer2Utils.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "JMediaPlayer2Utils"
  18. #include "JMediaPlayer2Utils.h"
  19. #include <mediaplayer2/JavaVMHelper.h>
  20. #include <media/stagefright/MediaDefs.h>
  21. #include <media/stagefright/Utils.h>
  22. #include <utils/Log.h>
  23. #include "log/log.h"
  24. namespace android {
  25. static const int64_t kOffloadMinDurationSec = 60;
  26. // static
  27. bool JMediaPlayer2Utils::isOffloadedAudioPlaybackSupported(
  28. const sp<MetaData>& meta, bool hasVideo, bool isStreaming, audio_stream_type_t streamType)
  29. {
  30. if (hasVideo || streamType != AUDIO_STREAM_MUSIC) {
  31. return false;
  32. }
  33. audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
  34. if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
  35. return false;
  36. }
  37. if (info.duration_us < kOffloadMinDurationSec * 1000000) {
  38. return false;
  39. }
  40. int32_t audioFormat = audioFormatFromNative(info.format);
  41. int32_t channelMask = outChannelMaskFromNative(info.channel_mask);
  42. if (audioFormat == ENCODING_INVALID || channelMask == CHANNEL_INVALID) {
  43. return false;
  44. }
  45. JNIEnv* env = JavaVMHelper::getJNIEnv();
  46. jclass jMP2UtilsCls = env->FindClass("android/media/MediaPlayer2Utils");
  47. jmethodID jSetAudioOutputDeviceById = env->GetStaticMethodID(
  48. jMP2UtilsCls, "isOffloadedAudioPlaybackSupported", "(III)Z");
  49. jboolean result = env->CallStaticBooleanMethod(
  50. jMP2UtilsCls, jSetAudioOutputDeviceById, audioFormat, info.sample_rate, channelMask);
  51. return result;
  52. }
  53. } // namespace android