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