android_media_AudioPresentation.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef _ANDROID_MEDIA_AUDIOPRESENTATION_H_
  17. #define _ANDROID_MEDIA_AUDIOPRESENTATION_H_
  18. #include "jni.h"
  19. #include <media/stagefright/foundation/ADebug.h> // CHECK
  20. #include <media/stagefright/foundation/AudioPresentationInfo.h>
  21. #include <nativehelper/ScopedLocalRef.h>
  22. namespace android {
  23. struct JAudioPresentationInfo {
  24. struct fields_t {
  25. jclass clazz = NULL;
  26. jmethodID constructID;
  27. // list parameters
  28. jclass listClazz = NULL;
  29. jmethodID listConstructId;
  30. jmethodID listAddId;
  31. // hashmap parameters
  32. jclass hashMapClazz = NULL;
  33. jmethodID hashMapConstructID;
  34. jmethodID hashMapPutID;
  35. // ulocale parameters
  36. jclass ulocaleClazz = NULL;
  37. jmethodID ulocaleConstructID;
  38. void init(JNIEnv *env) {
  39. jclass lclazz = env->FindClass("android/media/AudioPresentation");
  40. CHECK(lclazz != NULL);
  41. clazz = (jclass)env->NewGlobalRef(lclazz);
  42. CHECK(clazz != NULL);
  43. constructID = env->GetMethodID(clazz, "<init>",
  44. "(IILandroid/icu/util/ULocale;IZZZLjava/util/Map;)V");
  45. CHECK(constructID != NULL);
  46. // list objects
  47. jclass llistClazz = env->FindClass("java/util/ArrayList");
  48. CHECK(llistClazz != NULL);
  49. listClazz = static_cast<jclass>(env->NewGlobalRef(llistClazz));
  50. CHECK(listClazz != NULL);
  51. listConstructId = env->GetMethodID(listClazz, "<init>", "()V");
  52. CHECK(listConstructId != NULL);
  53. listAddId = env->GetMethodID(listClazz, "add", "(Ljava/lang/Object;)Z");
  54. CHECK(listAddId != NULL);
  55. // hashmap objects
  56. jclass lhashMapClazz = env->FindClass("java/util/HashMap");
  57. CHECK(lhashMapClazz != NULL);
  58. hashMapClazz = (jclass)env->NewGlobalRef(lhashMapClazz);
  59. CHECK(hashMapClazz != NULL);
  60. hashMapConstructID = env->GetMethodID(hashMapClazz, "<init>", "()V");
  61. CHECK(hashMapConstructID != NULL);
  62. hashMapPutID = env->GetMethodID(
  63. hashMapClazz,
  64. "put",
  65. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  66. CHECK(hashMapPutID != NULL);
  67. jclass lulocaleClazz = env->FindClass("android/icu/util/ULocale");
  68. CHECK(lulocaleClazz != NULL);
  69. ulocaleClazz = (jclass)env->NewGlobalRef(lulocaleClazz);
  70. CHECK(ulocaleClazz != NULL);
  71. ulocaleConstructID = env->GetMethodID(ulocaleClazz, "<init>", "(Ljava/lang/String;)V");
  72. CHECK(ulocaleConstructID != NULL);
  73. }
  74. void exit(JNIEnv *env) {
  75. env->DeleteGlobalRef(clazz); clazz = NULL;
  76. env->DeleteGlobalRef(listClazz); listClazz = NULL;
  77. env->DeleteGlobalRef(hashMapClazz); hashMapClazz = NULL;
  78. env->DeleteGlobalRef(ulocaleClazz); ulocaleClazz = NULL;
  79. }
  80. };
  81. static jobject asJobject(JNIEnv *env, const fields_t& fields) {
  82. return env->NewObject(fields.listClazz, fields.listConstructId);
  83. }
  84. static void addPresentations(JNIEnv *env, const fields_t& fields,
  85. const AudioPresentationCollection& presentations, jobject presentationsJObj) {
  86. for (const auto& ap : presentations) {
  87. ScopedLocalRef<jobject> jLabelObject = convertLabelsToMap(env, fields, ap.mLabels);
  88. if (jLabelObject == nullptr) return;
  89. ScopedLocalRef<jstring> jLanguage(env, env->NewStringUTF(ap.mLanguage.c_str()));
  90. if (jLanguage == nullptr) return;
  91. ScopedLocalRef<jobject> jLocale(env, env->NewObject(
  92. fields.ulocaleClazz, fields.ulocaleConstructID, jLanguage.get()));
  93. ScopedLocalRef<jobject> jValueObj(env, env->NewObject(fields.clazz, fields.constructID,
  94. static_cast<jint>(ap.mPresentationId),
  95. static_cast<jint>(ap.mProgramId),
  96. jLocale.get(),
  97. static_cast<jint>(ap.mMasteringIndication),
  98. static_cast<jboolean>((ap.mAudioDescriptionAvailable == 1) ? 1 : 0),
  99. static_cast<jboolean>((ap.mSpokenSubtitlesAvailable == 1) ? 1 : 0),
  100. static_cast<jboolean>((ap.mDialogueEnhancementAvailable == 1) ? 1 : 0),
  101. jLabelObject.get()));
  102. if (jValueObj != nullptr) {
  103. env->CallBooleanMethod(presentationsJObj, fields.listAddId, jValueObj.get());
  104. }
  105. }
  106. }
  107. private:
  108. static ScopedLocalRef<jobject> convertLabelsToMap(
  109. JNIEnv *env, const fields_t& fields, const std::map<std::string, std::string> &labels) {
  110. ScopedLocalRef<jobject> nullMap(env, nullptr);
  111. ScopedLocalRef<jobject> hashMap(env, env->NewObject(
  112. fields.hashMapClazz, fields.hashMapConstructID));
  113. if (hashMap == nullptr) {
  114. return nullMap;
  115. }
  116. for (const auto& label : labels) {
  117. ScopedLocalRef<jstring> jLanguage(env, env->NewStringUTF(label.first.c_str()));
  118. if (jLanguage == nullptr) return nullMap;
  119. ScopedLocalRef<jobject> jLocale(env, env->NewObject(
  120. fields.ulocaleClazz,
  121. fields.ulocaleConstructID,
  122. jLanguage.get()));
  123. if (jLocale == nullptr) return nullMap;
  124. ScopedLocalRef<jobject> jValue(env, env->NewStringUTF(label.second.c_str()));
  125. if (jValue == nullptr) return nullMap;
  126. env->CallObjectMethod(hashMap.get(), fields.hashMapPutID, jLocale.get(), jValue.get());
  127. }
  128. return hashMap;
  129. }
  130. };
  131. } // namespace android
  132. #endif // _ANDROID_MEDIA_AUDIO_PRESENTATION_H_