android_media_MediaCodecList.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright 2012, 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 "MediaCodec-JNI"
  18. #include <utils/Log.h>
  19. #include <media/stagefright/foundation/ADebug.h>
  20. #include <media/stagefright/foundation/AMessage.h>
  21. #include <media/stagefright/MediaCodecList.h>
  22. #include <media/IMediaCodecList.h>
  23. #include <media/MediaCodecInfo.h>
  24. #include <utils/Vector.h>
  25. #include <vector>
  26. #include "android_runtime/AndroidRuntime.h"
  27. #include "jni.h"
  28. #include <nativehelper/JNIHelp.h>
  29. #include "android_media_Streams.h"
  30. using namespace android;
  31. /**
  32. * This object unwraps codec aliases into individual codec infos as the Java interface handles
  33. * aliases in this way.
  34. */
  35. class JavaMediaCodecListWrapper {
  36. public:
  37. struct Info {
  38. sp<MediaCodecInfo> info;
  39. AString alias;
  40. };
  41. const Info getCodecInfo(size_t index) const {
  42. if (index < mInfoList.size()) {
  43. return mInfoList[index];
  44. }
  45. // return
  46. return Info { nullptr /* info */, "(none)" /* alias */ };
  47. }
  48. size_t countCodecs() const {
  49. return mInfoList.size();
  50. }
  51. sp<IMediaCodecList> getCodecList() const {
  52. return mCodecList;
  53. }
  54. size_t findCodecByName(AString name) const {
  55. auto it = mInfoIndex.find(name);
  56. return it == mInfoIndex.end() ? -ENOENT : it->second;
  57. }
  58. JavaMediaCodecListWrapper(sp<IMediaCodecList> mcl)
  59. : mCodecList(mcl) {
  60. size_t numCodecs = mcl->countCodecs();
  61. for (size_t ix = 0; ix < numCodecs; ++ix) {
  62. sp<MediaCodecInfo> info = mcl->getCodecInfo(ix);
  63. Vector<AString> namesAndAliases;
  64. info->getAliases(&namesAndAliases);
  65. namesAndAliases.insertAt(0);
  66. namesAndAliases.editItemAt(0) = info->getCodecName();
  67. for (const AString &nameOrAlias : namesAndAliases) {
  68. if (mInfoIndex.count(nameOrAlias) > 0) {
  69. // skip duplicate names or aliases
  70. continue;
  71. }
  72. mInfoIndex.emplace(nameOrAlias, mInfoList.size());
  73. mInfoList.emplace_back(Info { info, nameOrAlias });
  74. }
  75. }
  76. }
  77. private:
  78. sp<IMediaCodecList> mCodecList;
  79. std::vector<Info> mInfoList;
  80. std::map<AString, size_t> mInfoIndex;
  81. };
  82. static std::mutex sMutex;
  83. static std::unique_ptr<JavaMediaCodecListWrapper> sListWrapper;
  84. static const JavaMediaCodecListWrapper *getCodecList(JNIEnv *env) {
  85. std::lock_guard<std::mutex> lock(sMutex);
  86. if (sListWrapper == nullptr) {
  87. sp<IMediaCodecList> mcl = MediaCodecList::getInstance();
  88. if (mcl == NULL) {
  89. // This should never happen unless something is really wrong
  90. jniThrowException(
  91. env, "java/lang/RuntimeException", "cannot get MediaCodecList");
  92. }
  93. sListWrapper.reset(new JavaMediaCodecListWrapper(mcl));
  94. }
  95. return sListWrapper.get();
  96. }
  97. static JavaMediaCodecListWrapper::Info getCodecInfo(JNIEnv *env, jint index) {
  98. const JavaMediaCodecListWrapper *mcl = getCodecList(env);
  99. if (mcl == nullptr) {
  100. // Runtime exception already pending.
  101. return JavaMediaCodecListWrapper::Info { nullptr /* info */, "(none)" /* alias */ };
  102. }
  103. JavaMediaCodecListWrapper::Info info = mcl->getCodecInfo(index);
  104. if (info.info == NULL) {
  105. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  106. }
  107. return info;
  108. }
  109. static jint android_media_MediaCodecList_getCodecCount(
  110. JNIEnv *env, jobject /* thiz */) {
  111. const JavaMediaCodecListWrapper *mcl = getCodecList(env);
  112. if (mcl == NULL) {
  113. // Runtime exception already pending.
  114. return 0;
  115. }
  116. return mcl->countCodecs();
  117. }
  118. static jstring android_media_MediaCodecList_getCodecName(
  119. JNIEnv *env, jobject /* thiz */, jint index) {
  120. JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
  121. if (info.info == NULL) {
  122. // Runtime exception already pending.
  123. return NULL;
  124. }
  125. const char *name = info.alias.c_str();
  126. return env->NewStringUTF(name);
  127. }
  128. static jstring android_media_MediaCodecList_getCanonicalName(
  129. JNIEnv *env, jobject /* thiz */, jint index) {
  130. JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
  131. if (info.info == NULL) {
  132. // Runtime exception already pending.
  133. return NULL;
  134. }
  135. const char *name = info.info->getCodecName();
  136. return env->NewStringUTF(name);
  137. }
  138. static jint android_media_MediaCodecList_findCodecByName(
  139. JNIEnv *env, jobject /* thiz */, jstring name) {
  140. if (name == NULL) {
  141. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  142. return -ENOENT;
  143. }
  144. const char *nameStr = env->GetStringUTFChars(name, NULL);
  145. if (nameStr == NULL) {
  146. // Out of memory exception already pending.
  147. return -ENOENT;
  148. }
  149. const JavaMediaCodecListWrapper *mcl = getCodecList(env);
  150. if (mcl == NULL) {
  151. // Runtime exception already pending.
  152. env->ReleaseStringUTFChars(name, nameStr);
  153. return -ENOENT;
  154. }
  155. jint ret = mcl->findCodecByName(nameStr);
  156. env->ReleaseStringUTFChars(name, nameStr);
  157. return ret;
  158. }
  159. static jboolean android_media_MediaCodecList_getAttributes(
  160. JNIEnv *env, jobject /* thiz */, jint index) {
  161. JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
  162. if (info.info == NULL) {
  163. // Runtime exception already pending.
  164. return 0;
  165. }
  166. return info.info->getAttributes();
  167. }
  168. static jarray android_media_MediaCodecList_getSupportedTypes(
  169. JNIEnv *env, jobject /* thiz */, jint index) {
  170. JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
  171. if (info.info == NULL) {
  172. // Runtime exception already pending.
  173. return NULL;
  174. }
  175. Vector<AString> types;
  176. info.info->getSupportedMediaTypes(&types);
  177. jclass clazz = env->FindClass("java/lang/String");
  178. CHECK(clazz != NULL);
  179. jobjectArray array = env->NewObjectArray(types.size(), clazz, NULL);
  180. for (size_t i = 0; i < types.size(); ++i) {
  181. jstring obj = env->NewStringUTF(types.itemAt(i).c_str());
  182. env->SetObjectArrayElement(array, i, obj);
  183. env->DeleteLocalRef(obj);
  184. obj = NULL;
  185. }
  186. return array;
  187. }
  188. static jobject android_media_MediaCodecList_getCodecCapabilities(
  189. JNIEnv *env, jobject /* thiz */, jint index, jstring type) {
  190. if (type == NULL) {
  191. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  192. return NULL;
  193. }
  194. JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
  195. if (info.info == NULL) {
  196. // Runtime exception already pending.
  197. return NULL;
  198. }
  199. const char *typeStr = env->GetStringUTFChars(type, NULL);
  200. if (typeStr == NULL) {
  201. // Out of memory exception already pending.
  202. return NULL;
  203. }
  204. Vector<MediaCodecInfo::ProfileLevel> profileLevels;
  205. Vector<uint32_t> colorFormats;
  206. sp<AMessage> defaultFormat = new AMessage();
  207. defaultFormat->setString("mime", typeStr);
  208. // TODO query default-format also from codec/codec list
  209. const sp<MediaCodecInfo::Capabilities> &capabilities =
  210. info.info->getCapabilitiesFor(typeStr);
  211. env->ReleaseStringUTFChars(type, typeStr);
  212. typeStr = NULL;
  213. if (capabilities == NULL) {
  214. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  215. return NULL;
  216. }
  217. capabilities->getSupportedColorFormats(&colorFormats);
  218. capabilities->getSupportedProfileLevels(&profileLevels);
  219. sp<AMessage> details = capabilities->getDetails();
  220. bool isEncoder = info.info->isEncoder();
  221. jobject defaultFormatObj = NULL;
  222. if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) {
  223. return NULL;
  224. }
  225. jobject infoObj = NULL;
  226. if (ConvertMessageToMap(env, details, &infoObj)) {
  227. env->DeleteLocalRef(defaultFormatObj);
  228. return NULL;
  229. }
  230. jclass capsClazz =
  231. env->FindClass("android/media/MediaCodecInfo$CodecCapabilities");
  232. CHECK(capsClazz != NULL);
  233. jclass profileLevelClazz =
  234. env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel");
  235. CHECK(profileLevelClazz != NULL);
  236. jobjectArray profileLevelArray =
  237. env->NewObjectArray(profileLevels.size(), profileLevelClazz, NULL);
  238. jfieldID profileField =
  239. env->GetFieldID(profileLevelClazz, "profile", "I");
  240. jfieldID levelField =
  241. env->GetFieldID(profileLevelClazz, "level", "I");
  242. for (size_t i = 0; i < profileLevels.size(); ++i) {
  243. const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i);
  244. jobject profileLevelObj = env->AllocObject(profileLevelClazz);
  245. env->SetIntField(profileLevelObj, profileField, src.mProfile);
  246. env->SetIntField(profileLevelObj, levelField, src.mLevel);
  247. env->SetObjectArrayElement(profileLevelArray, i, profileLevelObj);
  248. env->DeleteLocalRef(profileLevelObj);
  249. profileLevelObj = NULL;
  250. }
  251. jintArray colorFormatsArray = env->NewIntArray(colorFormats.size());
  252. for (size_t i = 0; i < colorFormats.size(); ++i) {
  253. jint val = colorFormats.itemAt(i);
  254. env->SetIntArrayRegion(colorFormatsArray, i, 1, &val);
  255. }
  256. jmethodID capsConstructID = env->GetMethodID(capsClazz, "<init>",
  257. "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZ"
  258. "Ljava/util/Map;Ljava/util/Map;)V");
  259. jobject caps = env->NewObject(capsClazz, capsConstructID,
  260. profileLevelArray, colorFormatsArray, isEncoder,
  261. defaultFormatObj, infoObj);
  262. env->DeleteLocalRef(profileLevelArray);
  263. profileLevelArray = NULL;
  264. env->DeleteLocalRef(colorFormatsArray);
  265. colorFormatsArray = NULL;
  266. env->DeleteLocalRef(defaultFormatObj);
  267. defaultFormatObj = NULL;
  268. env->DeleteLocalRef(infoObj);
  269. infoObj = NULL;
  270. return caps;
  271. }
  272. static jobject android_media_MediaCodecList_getGlobalSettings(JNIEnv *env, jobject /* thiz */) {
  273. const JavaMediaCodecListWrapper *mcl = getCodecList(env);
  274. if (mcl == NULL) {
  275. // Runtime exception already pending.
  276. return NULL;
  277. }
  278. const sp<AMessage> settings = mcl->getCodecList()->getGlobalSettings();
  279. if (settings == NULL) {
  280. jniThrowException(env, "java/lang/RuntimeException", "cannot get global settings");
  281. return NULL;
  282. }
  283. jobject settingsObj = NULL;
  284. if (ConvertMessageToMap(env, settings, &settingsObj)) {
  285. return NULL;
  286. }
  287. return settingsObj;
  288. }
  289. static void android_media_MediaCodecList_native_init(JNIEnv* /* env */) {
  290. }
  291. static const JNINativeMethod gMethods[] = {
  292. { "native_getCodecCount", "()I", (void *)android_media_MediaCodecList_getCodecCount },
  293. { "getCanonicalName", "(I)Ljava/lang/String;",
  294. (void *)android_media_MediaCodecList_getCanonicalName },
  295. { "getCodecName", "(I)Ljava/lang/String;",
  296. (void *)android_media_MediaCodecList_getCodecName },
  297. { "getAttributes", "(I)I", (void *)android_media_MediaCodecList_getAttributes },
  298. { "getSupportedTypes", "(I)[Ljava/lang/String;",
  299. (void *)android_media_MediaCodecList_getSupportedTypes },
  300. { "getCodecCapabilities",
  301. "(ILjava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
  302. (void *)android_media_MediaCodecList_getCodecCapabilities },
  303. { "native_getGlobalSettings",
  304. "()Ljava/util/Map;",
  305. (void *)android_media_MediaCodecList_getGlobalSettings },
  306. { "findCodecByName", "(Ljava/lang/String;)I",
  307. (void *)android_media_MediaCodecList_findCodecByName },
  308. { "native_init", "()V", (void *)android_media_MediaCodecList_native_init },
  309. };
  310. int register_android_media_MediaCodecList(JNIEnv *env) {
  311. return AndroidRuntime::registerNativeMethods(env,
  312. "android/media/MediaCodecList", gMethods, NELEM(gMethods));
  313. }