android_media_MediaDataSource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2015, 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 "JMediaDataSource-JNI"
  18. #include <utils/Log.h>
  19. #include "android_media_MediaDataSource.h"
  20. #include "android_runtime/AndroidRuntime.h"
  21. #include "android_runtime/Log.h"
  22. #include "jni.h"
  23. #include <nativehelper/JNIHelp.h>
  24. #include <binder/MemoryDealer.h>
  25. #include <drm/drm_framework_common.h>
  26. #include <media/stagefright/foundation/ADebug.h>
  27. #include <nativehelper/ScopedLocalRef.h>
  28. namespace android {
  29. JMediaDataSource::JMediaDataSource(JNIEnv* env, jobject source)
  30. : mJavaObjStatus(OK), mSizeIsCached(false), mCachedSize(0), mMemory(NULL) {
  31. mMediaDataSourceObj = env->NewGlobalRef(source);
  32. CHECK(mMediaDataSourceObj != NULL);
  33. ScopedLocalRef<jclass> mediaDataSourceClass(env, env->GetObjectClass(mMediaDataSourceObj));
  34. CHECK(mediaDataSourceClass.get() != NULL);
  35. mReadMethod = env->GetMethodID(mediaDataSourceClass.get(), "readAt", "(J[BII)I");
  36. CHECK(mReadMethod != NULL);
  37. mGetSizeMethod = env->GetMethodID(mediaDataSourceClass.get(), "getSize", "()J");
  38. CHECK(mGetSizeMethod != NULL);
  39. mCloseMethod = env->GetMethodID(mediaDataSourceClass.get(), "close", "()V");
  40. CHECK(mCloseMethod != NULL);
  41. ScopedLocalRef<jbyteArray> tmp(env, env->NewByteArray(kBufferSize));
  42. mByteArrayObj = (jbyteArray)env->NewGlobalRef(tmp.get());
  43. CHECK(mByteArrayObj != NULL);
  44. sp<MemoryDealer> memoryDealer = new MemoryDealer(kBufferSize, "JMediaDataSource");
  45. mMemory = memoryDealer->allocate(kBufferSize);
  46. if (mMemory == NULL) {
  47. ALOGE("Failed to allocate memory!");
  48. }
  49. }
  50. JMediaDataSource::~JMediaDataSource() {
  51. JNIEnv* env = AndroidRuntime::getJNIEnv();
  52. env->DeleteGlobalRef(mMediaDataSourceObj);
  53. env->DeleteGlobalRef(mByteArrayObj);
  54. }
  55. sp<IMemory> JMediaDataSource::getIMemory() {
  56. Mutex::Autolock lock(mLock);
  57. return mMemory;
  58. }
  59. ssize_t JMediaDataSource::readAt(off64_t offset, size_t size) {
  60. Mutex::Autolock lock(mLock);
  61. if (mJavaObjStatus != OK || mMemory == NULL) {
  62. return -1;
  63. }
  64. if (size > kBufferSize) {
  65. size = kBufferSize;
  66. }
  67. JNIEnv* env = AndroidRuntime::getJNIEnv();
  68. jint numread = env->CallIntMethod(mMediaDataSourceObj, mReadMethod,
  69. (jlong)offset, mByteArrayObj, (jint)0, (jint)size);
  70. if (env->ExceptionCheck()) {
  71. ALOGW("An exception occurred in readAt()");
  72. LOGW_EX(env);
  73. env->ExceptionClear();
  74. mJavaObjStatus = UNKNOWN_ERROR;
  75. return -1;
  76. }
  77. if (numread < 0) {
  78. if (numread != -1) {
  79. ALOGW("An error occurred in readAt()");
  80. mJavaObjStatus = UNKNOWN_ERROR;
  81. return -1;
  82. } else {
  83. // numread == -1 indicates EOF
  84. return 0;
  85. }
  86. }
  87. if ((size_t)numread > size) {
  88. ALOGE("readAt read too many bytes.");
  89. mJavaObjStatus = UNKNOWN_ERROR;
  90. return -1;
  91. }
  92. ALOGV("readAt %lld / %zu => %d.", (long long)offset, size, numread);
  93. env->GetByteArrayRegion(mByteArrayObj, 0, numread, (jbyte*)mMemory->pointer());
  94. return numread;
  95. }
  96. status_t JMediaDataSource::getSize(off64_t* size) {
  97. Mutex::Autolock lock(mLock);
  98. if (mJavaObjStatus != OK) {
  99. return UNKNOWN_ERROR;
  100. }
  101. if (mSizeIsCached) {
  102. *size = mCachedSize;
  103. return OK;
  104. }
  105. JNIEnv* env = AndroidRuntime::getJNIEnv();
  106. *size = env->CallLongMethod(mMediaDataSourceObj, mGetSizeMethod);
  107. if (env->ExceptionCheck()) {
  108. ALOGW("An exception occurred in getSize()");
  109. LOGW_EX(env);
  110. env->ExceptionClear();
  111. // After returning an error, size shouldn't be used by callers.
  112. *size = UNKNOWN_ERROR;
  113. mJavaObjStatus = UNKNOWN_ERROR;
  114. return UNKNOWN_ERROR;
  115. }
  116. // The minimum size should be -1, which indicates unknown size.
  117. if (*size < 0) {
  118. *size = -1;
  119. }
  120. mCachedSize = *size;
  121. mSizeIsCached = true;
  122. return OK;
  123. }
  124. void JMediaDataSource::close() {
  125. Mutex::Autolock lock(mLock);
  126. JNIEnv* env = AndroidRuntime::getJNIEnv();
  127. env->CallVoidMethod(mMediaDataSourceObj, mCloseMethod);
  128. // The closed state is effectively the same as an error state.
  129. mJavaObjStatus = UNKNOWN_ERROR;
  130. }
  131. uint32_t JMediaDataSource::getFlags() {
  132. return 0;
  133. }
  134. String8 JMediaDataSource::toString() {
  135. return String8::format("JMediaDataSource(pid %d, uid %d)", getpid(), getuid());
  136. }
  137. sp<DecryptHandle> JMediaDataSource::DrmInitialization(const char * /* mime */) {
  138. return NULL;
  139. }
  140. } // namespace android