JMedia2HTTPConnection.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2017, 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 "JMedia2HTTPConnection"
  18. #include <utils/Log.h>
  19. #include <mediaplayer2/JavaVMHelper.h>
  20. #include <mediaplayer2/JMedia2HTTPConnection.h>
  21. #include <media/stagefright/foundation/ADebug.h>
  22. #include <nativehelper/scoped_local_ref.h>
  23. #include "log/log.h"
  24. #include "jni.h"
  25. namespace android {
  26. static const size_t kBufferSize = 32768;
  27. JMedia2HTTPConnection::JMedia2HTTPConnection(JNIEnv *env, jobject thiz) {
  28. mMedia2HTTPConnectionObj = env->NewGlobalRef(thiz);
  29. CHECK(mMedia2HTTPConnectionObj != NULL);
  30. ScopedLocalRef<jclass> media2HTTPConnectionClass(
  31. env, env->GetObjectClass(mMedia2HTTPConnectionObj));
  32. CHECK(media2HTTPConnectionClass.get() != NULL);
  33. mConnectMethod = env->GetMethodID(
  34. media2HTTPConnectionClass.get(),
  35. "connect",
  36. "(Ljava/lang/String;Ljava/lang/String;)Z");
  37. CHECK(mConnectMethod != NULL);
  38. mDisconnectMethod = env->GetMethodID(
  39. media2HTTPConnectionClass.get(),
  40. "disconnect",
  41. "()V");
  42. CHECK(mDisconnectMethod != NULL);
  43. mReadAtMethod = env->GetMethodID(
  44. media2HTTPConnectionClass.get(),
  45. "readAt",
  46. "(J[BI)I");
  47. CHECK(mReadAtMethod != NULL);
  48. mGetSizeMethod = env->GetMethodID(
  49. media2HTTPConnectionClass.get(),
  50. "getSize",
  51. "()J");
  52. CHECK(mGetSizeMethod != NULL);
  53. mGetMIMETypeMethod = env->GetMethodID(
  54. media2HTTPConnectionClass.get(),
  55. "getMIMEType",
  56. "()Ljava/lang/String;");
  57. CHECK(mGetMIMETypeMethod != NULL);
  58. mGetUriMethod = env->GetMethodID(
  59. media2HTTPConnectionClass.get(),
  60. "getUri",
  61. "()Ljava/lang/String;");
  62. CHECK(mGetUriMethod != NULL);
  63. ScopedLocalRef<jbyteArray> tmp(
  64. env, env->NewByteArray(kBufferSize));
  65. mByteArrayObj = (jbyteArray)env->NewGlobalRef(tmp.get());
  66. CHECK(mByteArrayObj != NULL);
  67. }
  68. JMedia2HTTPConnection::~JMedia2HTTPConnection() {
  69. JNIEnv *env = JavaVMHelper::getJNIEnv();
  70. env->DeleteGlobalRef(mMedia2HTTPConnectionObj);
  71. env->DeleteGlobalRef(mByteArrayObj);
  72. }
  73. bool JMedia2HTTPConnection::connect(
  74. const char *uri, const KeyedVector<String8, String8> *headers) {
  75. String8 tmp("");
  76. if (headers != NULL) {
  77. for (size_t i = 0; i < headers->size(); ++i) {
  78. tmp.append(headers->keyAt(i));
  79. tmp.append(String8(": "));
  80. tmp.append(headers->valueAt(i));
  81. tmp.append(String8("\r\n"));
  82. }
  83. }
  84. JNIEnv* env = JavaVMHelper::getJNIEnv();
  85. jstring juri = env->NewStringUTF(uri);
  86. jstring jheaders = env->NewStringUTF(tmp.string());
  87. jboolean ret =
  88. env->CallBooleanMethod(mMedia2HTTPConnectionObj, mConnectMethod, juri, jheaders);
  89. env->DeleteLocalRef(juri);
  90. env->DeleteLocalRef(jheaders);
  91. return (bool)ret;
  92. }
  93. void JMedia2HTTPConnection::disconnect() {
  94. JNIEnv* env = JavaVMHelper::getJNIEnv();
  95. env->CallVoidMethod(mMedia2HTTPConnectionObj, mDisconnectMethod);
  96. }
  97. ssize_t JMedia2HTTPConnection::readAt(off64_t offset, void *data, size_t size) {
  98. JNIEnv* env = JavaVMHelper::getJNIEnv();
  99. if (size > kBufferSize) {
  100. size = kBufferSize;
  101. }
  102. jint n = env->CallIntMethod(
  103. mMedia2HTTPConnectionObj, mReadAtMethod, (jlong)offset, mByteArrayObj, (jint)size);
  104. if (n > 0) {
  105. env->GetByteArrayRegion(
  106. mByteArrayObj,
  107. 0,
  108. n,
  109. (jbyte *)data);
  110. }
  111. return n;
  112. }
  113. off64_t JMedia2HTTPConnection::getSize() {
  114. JNIEnv* env = JavaVMHelper::getJNIEnv();
  115. return (off64_t)(env->CallLongMethod(mMedia2HTTPConnectionObj, mGetSizeMethod));
  116. }
  117. status_t JMedia2HTTPConnection::getMIMEType(String8 *mimeType) {
  118. JNIEnv* env = JavaVMHelper::getJNIEnv();
  119. jstring jmime = (jstring)env->CallObjectMethod(mMedia2HTTPConnectionObj, mGetMIMETypeMethod);
  120. jboolean flag = env->ExceptionCheck();
  121. if (flag) {
  122. env->ExceptionClear();
  123. return UNKNOWN_ERROR;
  124. }
  125. const char *str = env->GetStringUTFChars(jmime, 0);
  126. if (str != NULL) {
  127. *mimeType = String8(str);
  128. } else {
  129. *mimeType = "application/octet-stream";
  130. }
  131. env->ReleaseStringUTFChars(jmime, str);
  132. return OK;
  133. }
  134. status_t JMedia2HTTPConnection::getUri(String8 *uri) {
  135. JNIEnv* env = JavaVMHelper::getJNIEnv();
  136. jstring juri = (jstring)env->CallObjectMethod(mMedia2HTTPConnectionObj, mGetUriMethod);
  137. jboolean flag = env->ExceptionCheck();
  138. if (flag) {
  139. env->ExceptionClear();
  140. return UNKNOWN_ERROR;
  141. }
  142. const char *str = env->GetStringUTFChars(juri, 0);
  143. *uri = String8(str);
  144. env->ReleaseStringUTFChars(juri, str);
  145. return OK;
  146. }
  147. } // namespace android