android_media_MediaExtractor.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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 "MediaExtractor-JNI"
  18. #include <utils/Log.h>
  19. #include "android_media_AudioPresentation.h"
  20. #include "android_media_MediaDataSource.h"
  21. #include "android_media_MediaExtractor.h"
  22. #include "android_media_MediaMetricsJNI.h"
  23. #include "android_media_Streams.h"
  24. #include "android_os_HwRemoteBinder.h"
  25. #include "android_runtime/AndroidRuntime.h"
  26. #include "android_runtime/Log.h"
  27. #include "android_util_Binder.h"
  28. #include "jni.h"
  29. #include <nativehelper/JNIHelp.h>
  30. #include <android/hardware/cas/1.0/BpHwCas.h>
  31. #include <android/hardware/cas/1.0/BnHwCas.h>
  32. #include <hidl/HybridInterface.h>
  33. #include <media/IMediaHTTPService.h>
  34. #include <media/hardware/CryptoAPI.h>
  35. #include <media/stagefright/foundation/ABuffer.h>
  36. #include <media/stagefright/foundation/ADebug.h>
  37. #include <media/stagefright/foundation/AMessage.h>
  38. #include <media/DataSource.h>
  39. #include <media/stagefright/InterfaceUtils.h>
  40. #include <media/stagefright/MediaErrors.h>
  41. #include <media/stagefright/MetaData.h>
  42. #include <media/stagefright/NuMediaExtractor.h>
  43. #include <nativehelper/ScopedLocalRef.h>
  44. namespace android {
  45. using namespace hardware::cas::V1_0;
  46. struct fields_t {
  47. jfieldID context;
  48. jmethodID cryptoInfoSetID;
  49. jmethodID cryptoInfoSetPatternID;
  50. };
  51. static fields_t gFields;
  52. static JAudioPresentationInfo::fields_t gAudioPresentationFields;
  53. JMediaExtractor::JMediaExtractor(JNIEnv *env, jobject thiz)
  54. : mClass(NULL),
  55. mObject(NULL) {
  56. jclass clazz = env->GetObjectClass(thiz);
  57. CHECK(clazz != NULL);
  58. mClass = (jclass)env->NewGlobalRef(clazz);
  59. mObject = env->NewWeakGlobalRef(thiz);
  60. mImpl = new NuMediaExtractor;
  61. }
  62. JMediaExtractor::~JMediaExtractor() {
  63. JNIEnv *env = AndroidRuntime::getJNIEnv();
  64. env->DeleteWeakGlobalRef(mObject);
  65. mObject = NULL;
  66. env->DeleteGlobalRef(mClass);
  67. mClass = NULL;
  68. }
  69. status_t JMediaExtractor::setDataSource(
  70. const sp<IMediaHTTPService> &httpService,
  71. const char *path,
  72. const KeyedVector<String8, String8> *headers) {
  73. return mImpl->setDataSource(httpService, path, headers);
  74. }
  75. status_t JMediaExtractor::setDataSource(int fd, off64_t offset, off64_t size) {
  76. return mImpl->setDataSource(fd, offset, size);
  77. }
  78. status_t JMediaExtractor::setDataSource(const sp<DataSource> &datasource) {
  79. return mImpl->setDataSource(datasource);
  80. }
  81. status_t JMediaExtractor::setMediaCas(JNIEnv *env, jobject casBinderObj) {
  82. if (casBinderObj == NULL) {
  83. return BAD_VALUE;
  84. }
  85. sp<hardware::IBinder> hwBinder =
  86. JHwRemoteBinder::GetNativeContext(env, casBinderObj)->getBinder();
  87. if (hwBinder == NULL) {
  88. return BAD_VALUE;
  89. }
  90. sp<ICas> cas = hardware::fromBinder<ICas, BpHwCas, BnHwCas>(hwBinder);
  91. if (cas == NULL) {
  92. return BAD_VALUE;
  93. }
  94. HalToken halToken;
  95. if (!createHalToken(cas, &halToken)) {
  96. return BAD_VALUE;
  97. }
  98. return mImpl->setMediaCas(halToken);
  99. }
  100. size_t JMediaExtractor::countTracks() const {
  101. return mImpl->countTracks();
  102. }
  103. status_t JMediaExtractor::getTrackFormat(size_t index, jobject *format) const {
  104. sp<AMessage> msg;
  105. status_t err;
  106. if ((err = mImpl->getTrackFormat(index, &msg)) != OK) {
  107. return err;
  108. }
  109. JNIEnv *env = AndroidRuntime::getJNIEnv();
  110. return ConvertMessageToMap(env, msg, format);
  111. }
  112. status_t JMediaExtractor::getFileFormat(jobject *format) const {
  113. sp<AMessage> msg;
  114. status_t err;
  115. if ((err = mImpl->getFileFormat(&msg)) != OK) {
  116. return err;
  117. }
  118. JNIEnv *env = AndroidRuntime::getJNIEnv();
  119. return ConvertMessageToMap(env, msg, format);
  120. }
  121. status_t JMediaExtractor::selectTrack(size_t index) {
  122. return mImpl->selectTrack(index);
  123. }
  124. status_t JMediaExtractor::unselectTrack(size_t index) {
  125. return mImpl->unselectTrack(index);
  126. }
  127. status_t JMediaExtractor::seekTo(
  128. int64_t timeUs, MediaSource::ReadOptions::SeekMode mode) {
  129. return mImpl->seekTo(timeUs, mode);
  130. }
  131. status_t JMediaExtractor::advance() {
  132. return mImpl->advance();
  133. }
  134. status_t JMediaExtractor::readSampleData(
  135. jobject byteBuf, size_t offset, size_t *sampleSize) {
  136. JNIEnv *env = AndroidRuntime::getJNIEnv();
  137. void *dst = env->GetDirectBufferAddress(byteBuf);
  138. size_t dstSize;
  139. jbyteArray byteArray = NULL;
  140. ScopedLocalRef<jclass> byteBufClass(env, env->FindClass("java/nio/ByteBuffer"));
  141. CHECK(byteBufClass.get() != NULL);
  142. if (dst == NULL) {
  143. jmethodID arrayID =
  144. env->GetMethodID(byteBufClass.get(), "array", "()[B");
  145. CHECK(arrayID != NULL);
  146. byteArray =
  147. (jbyteArray)env->CallObjectMethod(byteBuf, arrayID);
  148. if (byteArray == NULL) {
  149. return INVALID_OPERATION;
  150. }
  151. jboolean isCopy;
  152. dst = env->GetByteArrayElements(byteArray, &isCopy);
  153. dstSize = (size_t) env->GetArrayLength(byteArray);
  154. } else {
  155. dstSize = (size_t) env->GetDirectBufferCapacity(byteBuf);
  156. }
  157. if (dstSize < offset) {
  158. if (byteArray != NULL) {
  159. env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
  160. }
  161. return -ERANGE;
  162. }
  163. sp<ABuffer> buffer = new ABuffer((char *)dst + offset, dstSize - offset);
  164. status_t err = mImpl->readSampleData(buffer);
  165. if (byteArray != NULL) {
  166. env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
  167. }
  168. if (err != OK) {
  169. return err;
  170. }
  171. *sampleSize = buffer->size();
  172. jmethodID positionID = env->GetMethodID(
  173. byteBufClass.get(), "position", "(I)Ljava/nio/Buffer;");
  174. CHECK(positionID != NULL);
  175. jmethodID limitID = env->GetMethodID(
  176. byteBufClass.get(), "limit", "(I)Ljava/nio/Buffer;");
  177. CHECK(limitID != NULL);
  178. jobject me = env->CallObjectMethod(
  179. byteBuf, limitID, offset + *sampleSize);
  180. env->DeleteLocalRef(me);
  181. me = env->CallObjectMethod(
  182. byteBuf, positionID, offset);
  183. env->DeleteLocalRef(me);
  184. me = NULL;
  185. return OK;
  186. }
  187. status_t JMediaExtractor::getSampleTrackIndex(size_t *trackIndex) {
  188. return mImpl->getSampleTrackIndex(trackIndex);
  189. }
  190. status_t JMediaExtractor::getSampleTime(int64_t *sampleTimeUs) {
  191. return mImpl->getSampleTime(sampleTimeUs);
  192. }
  193. status_t JMediaExtractor::getSampleSize(size_t *sampleSize) {
  194. return mImpl->getSampleSize(sampleSize);
  195. }
  196. status_t JMediaExtractor::getSampleFlags(uint32_t *sampleFlags) {
  197. *sampleFlags = 0;
  198. sp<MetaData> meta;
  199. status_t err = mImpl->getSampleMeta(&meta);
  200. if (err != OK) {
  201. return err;
  202. }
  203. int32_t val;
  204. if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
  205. (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_SYNC;
  206. }
  207. uint32_t type;
  208. const void *data;
  209. size_t size;
  210. if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
  211. (*sampleFlags) |= NuMediaExtractor::SAMPLE_FLAG_ENCRYPTED;
  212. }
  213. return OK;
  214. }
  215. status_t JMediaExtractor::getMetrics(Parcel *reply) const {
  216. status_t status = mImpl->getMetrics(reply);
  217. return status;
  218. }
  219. status_t JMediaExtractor::getSampleMeta(sp<MetaData> *sampleMeta) {
  220. return mImpl->getSampleMeta(sampleMeta);
  221. }
  222. bool JMediaExtractor::getCachedDuration(int64_t *durationUs, bool *eos) const {
  223. return mImpl->getCachedDuration(durationUs, eos);
  224. }
  225. status_t JMediaExtractor::getAudioPresentations(size_t trackIdx,
  226. AudioPresentationCollection *presentations) const {
  227. return mImpl->getAudioPresentations(trackIdx, presentations);
  228. }
  229. } // namespace android
  230. ////////////////////////////////////////////////////////////////////////////////
  231. using namespace android;
  232. static sp<JMediaExtractor> setMediaExtractor(
  233. JNIEnv *env, jobject thiz, const sp<JMediaExtractor> &extractor) {
  234. sp<JMediaExtractor> old =
  235. (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
  236. if (extractor != NULL) {
  237. extractor->incStrong(thiz);
  238. }
  239. if (old != NULL) {
  240. old->decStrong(thiz);
  241. }
  242. env->SetLongField(thiz, gFields.context, (jlong)extractor.get());
  243. return old;
  244. }
  245. static sp<JMediaExtractor> getMediaExtractor(JNIEnv *env, jobject thiz) {
  246. return (JMediaExtractor *)env->GetLongField(thiz, gFields.context);
  247. }
  248. static void android_media_MediaExtractor_release(JNIEnv *env, jobject thiz) {
  249. setMediaExtractor(env, thiz, NULL);
  250. }
  251. static jint android_media_MediaExtractor_getTrackCount(
  252. JNIEnv *env, jobject thiz) {
  253. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  254. if (extractor == NULL) {
  255. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  256. return -1;
  257. }
  258. return (jint) extractor->countTracks();
  259. }
  260. static jobject android_media_MediaExtractor_getTrackFormatNative(
  261. JNIEnv *env, jobject thiz, jint index) {
  262. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  263. if (extractor == NULL) {
  264. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  265. return NULL;
  266. }
  267. jobject format;
  268. status_t err = extractor->getTrackFormat(index, &format);
  269. if (err != OK) {
  270. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  271. return NULL;
  272. }
  273. return format;
  274. }
  275. static jobject android_media_MediaExtractor_getFileFormatNative(
  276. JNIEnv *env, jobject thiz) {
  277. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  278. if (extractor == NULL) {
  279. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  280. return NULL;
  281. }
  282. jobject format;
  283. status_t err = extractor->getFileFormat(&format);
  284. if (err != OK) {
  285. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  286. return NULL;
  287. }
  288. return format;
  289. }
  290. static void android_media_MediaExtractor_selectTrack(
  291. JNIEnv *env, jobject thiz, jint index) {
  292. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  293. if (extractor == NULL) {
  294. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  295. return;
  296. }
  297. status_t err = extractor->selectTrack(index);
  298. if (err != OK) {
  299. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  300. return;
  301. }
  302. }
  303. static void android_media_MediaExtractor_unselectTrack(
  304. JNIEnv *env, jobject thiz, jint index) {
  305. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  306. if (extractor == NULL) {
  307. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  308. return;
  309. }
  310. status_t err = extractor->unselectTrack(index);
  311. if (err != OK) {
  312. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  313. return;
  314. }
  315. }
  316. static void android_media_MediaExtractor_seekTo(
  317. JNIEnv *env, jobject thiz, jlong timeUs, jint mode) {
  318. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  319. if (extractor == NULL) {
  320. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  321. return;
  322. }
  323. if (mode < MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC
  324. || mode >= MediaSource::ReadOptions::SEEK_CLOSEST) {
  325. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  326. return;
  327. }
  328. extractor->seekTo(timeUs, (MediaSource::ReadOptions::SeekMode)mode);
  329. }
  330. static jboolean android_media_MediaExtractor_advance(
  331. JNIEnv *env, jobject thiz) {
  332. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  333. if (extractor == NULL) {
  334. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  335. return JNI_FALSE;
  336. }
  337. status_t err = extractor->advance();
  338. if (err == ERROR_END_OF_STREAM) {
  339. return JNI_FALSE;
  340. } else if (err != OK) {
  341. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  342. return JNI_FALSE;
  343. }
  344. return JNI_TRUE;
  345. }
  346. static jint android_media_MediaExtractor_readSampleData(
  347. JNIEnv *env, jobject thiz, jobject byteBuf, jint offset) {
  348. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  349. if (extractor == NULL) {
  350. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  351. return -1;
  352. }
  353. size_t sampleSize;
  354. status_t err = extractor->readSampleData(byteBuf, offset, &sampleSize);
  355. if (err == ERROR_END_OF_STREAM) {
  356. return -1;
  357. } else if (err != OK) {
  358. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  359. return -1;
  360. }
  361. return (jint) sampleSize;
  362. }
  363. static jint android_media_MediaExtractor_getSampleTrackIndex(
  364. JNIEnv *env, jobject thiz) {
  365. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  366. if (extractor == NULL) {
  367. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  368. return -1;
  369. }
  370. size_t trackIndex;
  371. status_t err = extractor->getSampleTrackIndex(&trackIndex);
  372. if (err == ERROR_END_OF_STREAM) {
  373. return -1;
  374. } else if (err != OK) {
  375. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  376. return -1;
  377. }
  378. return (jint) trackIndex;
  379. }
  380. static jlong android_media_MediaExtractor_getSampleTime(
  381. JNIEnv *env, jobject thiz) {
  382. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  383. if (extractor == NULL) {
  384. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  385. return -1LL;
  386. }
  387. int64_t sampleTimeUs;
  388. status_t err = extractor->getSampleTime(&sampleTimeUs);
  389. if (err == ERROR_END_OF_STREAM) {
  390. return -1LL;
  391. } else if (err != OK) {
  392. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  393. return -1LL;
  394. }
  395. return (jlong) sampleTimeUs;
  396. }
  397. static jlong android_media_MediaExtractor_getSampleSize(
  398. JNIEnv *env, jobject thiz) {
  399. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  400. if (extractor == NULL) {
  401. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  402. return -1LL;
  403. }
  404. size_t sampleSize;
  405. status_t err = extractor->getSampleSize(&sampleSize);
  406. if (err == ERROR_END_OF_STREAM) {
  407. return -1LL;
  408. } else if (err != OK) {
  409. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  410. return -1LL;
  411. }
  412. return (jlong) sampleSize;
  413. }
  414. static jint android_media_MediaExtractor_getSampleFlags(
  415. JNIEnv *env, jobject thiz) {
  416. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  417. if (extractor == NULL) {
  418. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  419. return -1;
  420. }
  421. uint32_t sampleFlags;
  422. status_t err = extractor->getSampleFlags(&sampleFlags);
  423. if (err == ERROR_END_OF_STREAM) {
  424. return -1;
  425. } else if (err != OK) {
  426. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  427. return -1;
  428. }
  429. return (jint) sampleFlags;
  430. }
  431. static jboolean android_media_MediaExtractor_getSampleCryptoInfo(
  432. JNIEnv *env, jobject thiz, jobject cryptoInfoObj) {
  433. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  434. if (extractor == NULL) {
  435. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  436. return JNI_FALSE;
  437. }
  438. sp<MetaData> meta;
  439. status_t err = extractor->getSampleMeta(&meta);
  440. if (err != OK) {
  441. return JNI_FALSE;
  442. }
  443. uint32_t type;
  444. const void *data;
  445. size_t size;
  446. if (!meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
  447. return JNI_FALSE;
  448. }
  449. size_t numSubSamples = size / sizeof(int32_t);
  450. if (numSubSamples == 0) {
  451. return JNI_FALSE;
  452. }
  453. jintArray numBytesOfEncryptedDataObj = env->NewIntArray(numSubSamples);
  454. jboolean isCopy;
  455. jint *dst = env->GetIntArrayElements(numBytesOfEncryptedDataObj, &isCopy);
  456. for (size_t i = 0; i < numSubSamples; ++i) {
  457. dst[i] = ((const int32_t *)data)[i];
  458. }
  459. env->ReleaseIntArrayElements(numBytesOfEncryptedDataObj, dst, 0);
  460. dst = NULL;
  461. size_t encSize = size;
  462. jintArray numBytesOfPlainDataObj = NULL;
  463. if (meta->findData(kKeyPlainSizes, &type, &data, &size)) {
  464. if (size != encSize) {
  465. // The two must be of the same length.
  466. return JNI_FALSE;
  467. }
  468. numBytesOfPlainDataObj = env->NewIntArray(numSubSamples);
  469. jboolean isCopy;
  470. jint *dst = env->GetIntArrayElements(numBytesOfPlainDataObj, &isCopy);
  471. for (size_t i = 0; i < numSubSamples; ++i) {
  472. dst[i] = ((const int32_t *)data)[i];
  473. }
  474. env->ReleaseIntArrayElements(numBytesOfPlainDataObj, dst, 0);
  475. dst = NULL;
  476. }
  477. jbyteArray keyObj = NULL;
  478. if (meta->findData(kKeyCryptoKey, &type, &data, &size)) {
  479. if (size != 16) {
  480. // Keys must be 16 bytes in length.
  481. return JNI_FALSE;
  482. }
  483. keyObj = env->NewByteArray(size);
  484. jboolean isCopy;
  485. jbyte *dst = env->GetByteArrayElements(keyObj, &isCopy);
  486. memcpy(dst, data, size);
  487. env->ReleaseByteArrayElements(keyObj, dst, 0);
  488. dst = NULL;
  489. }
  490. jbyteArray ivObj = NULL;
  491. if (meta->findData(kKeyCryptoIV, &type, &data, &size)) {
  492. if (size != 16) {
  493. // IVs must be 16 bytes in length.
  494. return JNI_FALSE;
  495. }
  496. ivObj = env->NewByteArray(size);
  497. jboolean isCopy;
  498. jbyte *dst = env->GetByteArrayElements(ivObj, &isCopy);
  499. memcpy(dst, data, size);
  500. env->ReleaseByteArrayElements(ivObj, dst, 0);
  501. dst = NULL;
  502. }
  503. int32_t mode;
  504. if (!meta->findInt32(kKeyCryptoMode, &mode)) {
  505. mode = CryptoPlugin::kMode_AES_CTR;
  506. }
  507. env->CallVoidMethod(
  508. cryptoInfoObj,
  509. gFields.cryptoInfoSetID,
  510. (jint)numSubSamples,
  511. numBytesOfPlainDataObj,
  512. numBytesOfEncryptedDataObj,
  513. keyObj,
  514. ivObj,
  515. mode);
  516. int32_t encryptedByteBlock = 0, skipByteBlock = 0;
  517. meta->findInt32(kKeyEncryptedByteBlock, &encryptedByteBlock);
  518. meta->findInt32(kKeySkipByteBlock, &skipByteBlock);
  519. env->CallVoidMethod(
  520. cryptoInfoObj,
  521. gFields.cryptoInfoSetPatternID,
  522. encryptedByteBlock,
  523. skipByteBlock);
  524. return JNI_TRUE;
  525. }
  526. static jobject android_media_MediaExtractor_getAudioPresentations(
  527. JNIEnv *env, jobject thiz, jint trackIdx) {
  528. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  529. jobject presentationsJObj = JAudioPresentationInfo::asJobject(env, gAudioPresentationFields);
  530. if (extractor == NULL) {
  531. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  532. return presentationsJObj;
  533. }
  534. AudioPresentationCollection presentations;
  535. status_t err = extractor->getAudioPresentations(trackIdx, &presentations);
  536. if (err == ERROR_END_OF_STREAM || err == ERROR_UNSUPPORTED) {
  537. return presentationsJObj;
  538. } else if (err != OK) {
  539. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  540. return presentationsJObj;
  541. }
  542. JAudioPresentationInfo::addPresentations(
  543. env, gAudioPresentationFields, presentations, presentationsJObj);
  544. return presentationsJObj;
  545. }
  546. static void android_media_MediaExtractor_native_init(JNIEnv *env) {
  547. jclass clazz = env->FindClass("android/media/MediaExtractor");
  548. CHECK(clazz != NULL);
  549. gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
  550. CHECK(gFields.context != NULL);
  551. clazz = env->FindClass("android/media/MediaCodec$CryptoInfo");
  552. CHECK(clazz != NULL);
  553. gFields.cryptoInfoSetID =
  554. env->GetMethodID(clazz, "set", "(I[I[I[B[BI)V");
  555. gFields.cryptoInfoSetPatternID =
  556. env->GetMethodID(clazz, "setPattern", "(II)V");
  557. gAudioPresentationFields.init(env);
  558. }
  559. static void android_media_MediaExtractor_native_setup(
  560. JNIEnv *env, jobject thiz) {
  561. sp<JMediaExtractor> extractor = new JMediaExtractor(env, thiz);
  562. setMediaExtractor(env,thiz, extractor);
  563. }
  564. static void android_media_MediaExtractor_setDataSource(
  565. JNIEnv *env, jobject thiz,
  566. jobject httpServiceBinderObj,
  567. jstring pathObj,
  568. jobjectArray keysArray,
  569. jobjectArray valuesArray) {
  570. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  571. if (extractor == NULL) {
  572. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  573. return;
  574. }
  575. if (pathObj == NULL) {
  576. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  577. return;
  578. }
  579. KeyedVector<String8, String8> headers;
  580. if (!ConvertKeyValueArraysToKeyedVector(
  581. env, keysArray, valuesArray, &headers)) {
  582. return;
  583. }
  584. const char *path = env->GetStringUTFChars(pathObj, NULL);
  585. if (path == NULL) {
  586. return;
  587. }
  588. sp<IMediaHTTPService> httpService;
  589. if (httpServiceBinderObj != NULL) {
  590. sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj);
  591. httpService = interface_cast<IMediaHTTPService>(binder);
  592. }
  593. status_t err = extractor->setDataSource(httpService, path, &headers);
  594. env->ReleaseStringUTFChars(pathObj, path);
  595. path = NULL;
  596. if (err != OK) {
  597. jniThrowException(
  598. env,
  599. "java/io/IOException",
  600. "Failed to instantiate extractor.");
  601. return;
  602. }
  603. }
  604. static void android_media_MediaExtractor_setDataSourceFd(
  605. JNIEnv *env, jobject thiz,
  606. jobject fileDescObj, jlong offset, jlong length) {
  607. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  608. if (extractor == NULL) {
  609. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  610. return;
  611. }
  612. if (fileDescObj == NULL) {
  613. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  614. return;
  615. }
  616. int fd = jniGetFDFromFileDescriptor(env, fileDescObj);
  617. status_t err = extractor->setDataSource(fd, offset, length);
  618. if (err != OK) {
  619. jniThrowException(
  620. env,
  621. "java/io/IOException",
  622. "Failed to instantiate extractor.");
  623. return;
  624. }
  625. }
  626. static void android_media_MediaExtractor_setDataSourceCallback(
  627. JNIEnv *env, jobject thiz,
  628. jobject callbackObj) {
  629. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  630. if (extractor == NULL) {
  631. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  632. return;
  633. }
  634. if (callbackObj == NULL) {
  635. jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
  636. return;
  637. }
  638. sp<DataSource> bridge =
  639. CreateDataSourceFromIDataSource(new JMediaDataSource(env, callbackObj));
  640. status_t err = extractor->setDataSource(bridge);
  641. if (err != OK) {
  642. // Clear bridge so that JMediaDataSource::close() is called _before_
  643. // we throw the IOException.
  644. // Otherwise close() gets called when we go out of scope, it calls
  645. // Java with a pending exception and crashes the process.
  646. bridge.clear();
  647. jniThrowException(
  648. env,
  649. "java/io/IOException",
  650. "Failed to instantiate extractor.");
  651. return;
  652. }
  653. }
  654. static void android_media_MediaExtractor_setMediaCas(
  655. JNIEnv *env, jobject thiz, jobject casBinderObj) {
  656. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  657. if (extractor == NULL) {
  658. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  659. return;
  660. }
  661. status_t err = extractor->setMediaCas(env, casBinderObj);
  662. if (err != OK) {
  663. extractor.clear();
  664. jniThrowException(
  665. env,
  666. "java/lang/IllegalArgumentException",
  667. "Failed to set MediaCas on extractor.");
  668. }
  669. }
  670. static jlong android_media_MediaExtractor_getCachedDurationUs(
  671. JNIEnv *env, jobject thiz) {
  672. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  673. if (extractor == NULL) {
  674. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  675. return -1LL;
  676. }
  677. int64_t cachedDurationUs;
  678. bool eos;
  679. if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
  680. return -1LL;
  681. }
  682. return (jlong) cachedDurationUs;
  683. }
  684. static jboolean android_media_MediaExtractor_hasCacheReachedEOS(
  685. JNIEnv *env, jobject thiz) {
  686. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  687. if (extractor == NULL) {
  688. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  689. return JNI_TRUE;
  690. }
  691. int64_t cachedDurationUs;
  692. bool eos;
  693. if (!extractor->getCachedDuration(&cachedDurationUs, &eos)) {
  694. return JNI_TRUE;
  695. }
  696. return eos ? JNI_TRUE : JNI_FALSE;
  697. }
  698. static void android_media_MediaExtractor_native_finalize(
  699. JNIEnv *env, jobject thiz) {
  700. android_media_MediaExtractor_release(env, thiz);
  701. }
  702. static jobject
  703. android_media_MediaExtractor_native_getMetrics(JNIEnv * env, jobject thiz)
  704. {
  705. ALOGV("android_media_MediaExtractor_native_getMetrics");
  706. sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
  707. if (extractor == NULL ) {
  708. jniThrowException(env, "java/lang/IllegalStateException", NULL);
  709. return NULL;
  710. }
  711. // get what we have for the metrics from the codec
  712. Parcel reply;
  713. status_t err = extractor->getMetrics(&reply);
  714. if (err != OK) {
  715. ALOGE("getMetrics failed");
  716. return (jobject) NULL;
  717. }
  718. // build and return the Bundle
  719. std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create());
  720. item->readFromParcel(reply);
  721. jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item.get(), NULL);
  722. return mybundle;
  723. }
  724. static const JNINativeMethod gMethods[] = {
  725. { "release", "()V", (void *)android_media_MediaExtractor_release },
  726. { "getTrackCount", "()I", (void *)android_media_MediaExtractor_getTrackCount },
  727. { "getFileFormatNative", "()Ljava/util/Map;",
  728. (void *)android_media_MediaExtractor_getFileFormatNative },
  729. { "getTrackFormatNative", "(I)Ljava/util/Map;",
  730. (void *)android_media_MediaExtractor_getTrackFormatNative },
  731. { "selectTrack", "(I)V", (void *)android_media_MediaExtractor_selectTrack },
  732. { "unselectTrack", "(I)V",
  733. (void *)android_media_MediaExtractor_unselectTrack },
  734. { "seekTo", "(JI)V", (void *)android_media_MediaExtractor_seekTo },
  735. { "advance", "()Z", (void *)android_media_MediaExtractor_advance },
  736. { "readSampleData", "(Ljava/nio/ByteBuffer;I)I",
  737. (void *)android_media_MediaExtractor_readSampleData },
  738. { "getSampleTrackIndex", "()I",
  739. (void *)android_media_MediaExtractor_getSampleTrackIndex },
  740. { "getSampleTime", "()J",
  741. (void *)android_media_MediaExtractor_getSampleTime },
  742. { "getSampleSize", "()J",
  743. (void *)android_media_MediaExtractor_getSampleSize },
  744. { "getSampleFlags", "()I",
  745. (void *)android_media_MediaExtractor_getSampleFlags },
  746. { "getSampleCryptoInfo", "(Landroid/media/MediaCodec$CryptoInfo;)Z",
  747. (void *)android_media_MediaExtractor_getSampleCryptoInfo },
  748. { "native_init", "()V", (void *)android_media_MediaExtractor_native_init },
  749. { "native_setup", "()V",
  750. (void *)android_media_MediaExtractor_native_setup },
  751. { "native_finalize", "()V",
  752. (void *)android_media_MediaExtractor_native_finalize },
  753. { "nativeSetDataSource",
  754. "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;"
  755. "[Ljava/lang/String;)V",
  756. (void *)android_media_MediaExtractor_setDataSource },
  757. { "setDataSource", "(Ljava/io/FileDescriptor;JJ)V",
  758. (void *)android_media_MediaExtractor_setDataSourceFd },
  759. { "setDataSource", "(Landroid/media/MediaDataSource;)V",
  760. (void *)android_media_MediaExtractor_setDataSourceCallback },
  761. { "nativeSetMediaCas", "(Landroid/os/IHwBinder;)V",
  762. (void *)android_media_MediaExtractor_setMediaCas },
  763. { "getCachedDuration", "()J",
  764. (void *)android_media_MediaExtractor_getCachedDurationUs },
  765. { "hasCacheReachedEndOfStream", "()Z",
  766. (void *)android_media_MediaExtractor_hasCacheReachedEOS },
  767. {"native_getMetrics", "()Landroid/os/PersistableBundle;",
  768. (void *)android_media_MediaExtractor_native_getMetrics},
  769. { "native_getAudioPresentations", "(I)Ljava/util/List;",
  770. (void *)android_media_MediaExtractor_getAudioPresentations },
  771. };
  772. int register_android_media_MediaExtractor(JNIEnv *env) {
  773. return AndroidRuntime::registerNativeMethods(env,
  774. "android/media/MediaExtractor", gMethods, NELEM(gMethods));
  775. }