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