Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2017, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaPlayer2-JNI" |
| 20 | #include "utils/Log.h" |
| 21 | |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 24 | #include <media/AudioResamplerPublic.h> |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 25 | #include <media/DataSourceDesc.h> |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 26 | #include <media/MediaHTTPService.h> |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 27 | #include <media/MediaAnalyticsItem.h> |
| 28 | #include <media/NdkWrapper.h> |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 29 | #include <media/stagefright/Utils.h> |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 30 | #include <media/stagefright/foundation/ByteUtils.h> // for FOURCC definition |
Hyundo Moon | 8e5ef90 | 2018-02-07 11:53:37 +0900 | [diff] [blame] | 31 | #include <mediaplayer2/JAudioTrack.h> |
Wei Jia | c3c31a53 | 2018-02-05 16:18:27 -0800 | [diff] [blame] | 32 | #include <mediaplayer2/mediaplayer2.h> |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <assert.h> |
| 35 | #include <limits.h> |
| 36 | #include <unistd.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <utils/threads.h> |
| 39 | #include "jni.h" |
| 40 | #include <nativehelper/JNIHelp.h> |
| 41 | #include "android/native_window_jni.h" |
| 42 | #include "android_runtime/Log.h" |
| 43 | #include "utils/Errors.h" // for status_t |
| 44 | #include "utils/KeyedVector.h" |
| 45 | #include "utils/String8.h" |
| 46 | #include "android_media_BufferingParams.h" |
| 47 | #include "android_media_Media2HTTPService.h" |
| 48 | #include "android_media_Media2DataSource.h" |
| 49 | #include "android_media_MediaMetricsJNI.h" |
| 50 | #include "android_media_PlaybackParams.h" |
| 51 | #include "android_media_SyncParams.h" |
| 52 | #include "android_media_VolumeShaper.h" |
| 53 | |
| 54 | #include "android_os_Parcel.h" |
| 55 | #include "android_util_Binder.h" |
| 56 | #include <binder/Parcel.h> |
| 57 | |
| 58 | // Modular DRM begin |
| 59 | #define FIND_CLASS(var, className) \ |
| 60 | var = env->FindClass(className); \ |
| 61 | LOG_FATAL_IF(! (var), "Unable to find class " className); |
| 62 | |
| 63 | #define GET_METHOD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 64 | var = env->GetMethodID(clazz, fieldName, fieldDescriptor); \ |
| 65 | LOG_FATAL_IF(! (var), "Unable to find method " fieldName); |
| 66 | |
| 67 | struct StateExceptionFields { |
| 68 | jmethodID init; |
| 69 | jclass classId; |
| 70 | }; |
| 71 | |
| 72 | static StateExceptionFields gStateExceptionFields; |
| 73 | // Modular DRM end |
| 74 | |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | using namespace android; |
| 78 | |
| 79 | using media::VolumeShaper; |
| 80 | |
| 81 | // ---------------------------------------------------------------------------- |
| 82 | |
| 83 | struct fields_t { |
| 84 | jfieldID context; |
| 85 | jfieldID surface_texture; |
| 86 | |
| 87 | jmethodID post_event; |
| 88 | |
| 89 | jmethodID proxyConfigGetHost; |
| 90 | jmethodID proxyConfigGetPort; |
| 91 | jmethodID proxyConfigGetExclusionList; |
| 92 | }; |
| 93 | static fields_t fields; |
| 94 | |
| 95 | static BufferingParams::fields_t gBufferingParamsFields; |
| 96 | static PlaybackParams::fields_t gPlaybackParamsFields; |
| 97 | static SyncParams::fields_t gSyncParamsFields; |
| 98 | static VolumeShaperHelper::fields_t gVolumeShaperFields; |
| 99 | |
| 100 | static Mutex sLock; |
| 101 | |
| 102 | static bool ConvertKeyValueArraysToKeyedVector( |
| 103 | JNIEnv *env, jobjectArray keys, jobjectArray values, |
| 104 | KeyedVector<String8, String8>* keyedVector) { |
| 105 | |
| 106 | int nKeyValuePairs = 0; |
| 107 | bool failed = false; |
| 108 | if (keys != NULL && values != NULL) { |
| 109 | nKeyValuePairs = env->GetArrayLength(keys); |
| 110 | failed = (nKeyValuePairs != env->GetArrayLength(values)); |
| 111 | } |
| 112 | |
| 113 | if (!failed) { |
| 114 | failed = ((keys != NULL && values == NULL) || |
| 115 | (keys == NULL && values != NULL)); |
| 116 | } |
| 117 | |
| 118 | if (failed) { |
| 119 | ALOGE("keys and values arrays have different length"); |
| 120 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | for (int i = 0; i < nKeyValuePairs; ++i) { |
| 125 | // No need to check on the ArrayIndexOutOfBoundsException, since |
| 126 | // it won't happen here. |
| 127 | jstring key = (jstring) env->GetObjectArrayElement(keys, i); |
| 128 | jstring value = (jstring) env->GetObjectArrayElement(values, i); |
| 129 | |
| 130 | const char* keyStr = env->GetStringUTFChars(key, NULL); |
| 131 | if (!keyStr) { // OutOfMemoryError |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | const char* valueStr = env->GetStringUTFChars(value, NULL); |
| 136 | if (!valueStr) { // OutOfMemoryError |
| 137 | env->ReleaseStringUTFChars(key, keyStr); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | keyedVector->add(String8(keyStr), String8(valueStr)); |
| 142 | |
| 143 | env->ReleaseStringUTFChars(key, keyStr); |
| 144 | env->ReleaseStringUTFChars(value, valueStr); |
| 145 | env->DeleteLocalRef(key); |
| 146 | env->DeleteLocalRef(value); |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | // ---------------------------------------------------------------------------- |
| 152 | // ref-counted object for callbacks |
| 153 | class JNIMediaPlayer2Listener: public MediaPlayer2Listener |
| 154 | { |
| 155 | public: |
| 156 | JNIMediaPlayer2Listener(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 157 | ~JNIMediaPlayer2Listener(); |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 158 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, |
| 159 | const Parcel *obj = NULL) override; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 160 | private: |
| 161 | JNIMediaPlayer2Listener(); |
| 162 | jclass mClass; // Reference to MediaPlayer2 class |
| 163 | jobject mObject; // Weak ref to MediaPlayer2 Java object to call on |
| 164 | }; |
| 165 | |
| 166 | JNIMediaPlayer2Listener::JNIMediaPlayer2Listener(JNIEnv* env, jobject thiz, jobject weak_thiz) |
| 167 | { |
| 168 | |
| 169 | // Hold onto the MediaPlayer2 class for use in calling the static method |
| 170 | // that posts events to the application thread. |
| 171 | jclass clazz = env->GetObjectClass(thiz); |
| 172 | if (clazz == NULL) { |
| 173 | ALOGE("Can't find android/media/MediaPlayer2Impl"); |
| 174 | jniThrowException(env, "java/lang/Exception", NULL); |
| 175 | return; |
| 176 | } |
| 177 | mClass = (jclass)env->NewGlobalRef(clazz); |
| 178 | |
| 179 | // We use a weak reference so the MediaPlayer2 object can be garbage collected. |
| 180 | // The reference is only used as a proxy for callbacks. |
| 181 | mObject = env->NewGlobalRef(weak_thiz); |
| 182 | } |
| 183 | |
| 184 | JNIMediaPlayer2Listener::~JNIMediaPlayer2Listener() |
| 185 | { |
| 186 | // remove global references |
| 187 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 188 | env->DeleteGlobalRef(mObject); |
| 189 | env->DeleteGlobalRef(mClass); |
| 190 | } |
| 191 | |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 192 | void JNIMediaPlayer2Listener::notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 193 | { |
| 194 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 195 | if (obj && obj->dataSize() > 0) { |
| 196 | jobject jParcel = createJavaParcelObject(env); |
| 197 | if (jParcel != NULL) { |
| 198 | Parcel* nativeParcel = parcelForJavaObject(env, jParcel); |
| 199 | nativeParcel->setData(obj->data(), obj->dataSize()); |
| 200 | env->CallStaticVoidMethod(mClass, fields.post_event, mObject, |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 201 | srcId, msg, ext1, ext2, jParcel); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 202 | env->DeleteLocalRef(jParcel); |
| 203 | } |
| 204 | } else { |
| 205 | env->CallStaticVoidMethod(mClass, fields.post_event, mObject, |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 206 | srcId, msg, ext1, ext2, NULL); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 207 | } |
| 208 | if (env->ExceptionCheck()) { |
| 209 | ALOGW("An exception occurred while notifying an event."); |
| 210 | LOGW_EX(env); |
| 211 | env->ExceptionClear(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | |
| 217 | static sp<MediaPlayer2> getMediaPlayer(JNIEnv* env, jobject thiz) |
| 218 | { |
| 219 | Mutex::Autolock l(sLock); |
| 220 | MediaPlayer2* const p = (MediaPlayer2*)env->GetLongField(thiz, fields.context); |
| 221 | return sp<MediaPlayer2>(p); |
| 222 | } |
| 223 | |
| 224 | static sp<MediaPlayer2> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer2>& player) |
| 225 | { |
| 226 | Mutex::Autolock l(sLock); |
| 227 | sp<MediaPlayer2> old = (MediaPlayer2*)env->GetLongField(thiz, fields.context); |
| 228 | if (player.get()) { |
| 229 | player->incStrong((void*)setMediaPlayer); |
| 230 | } |
| 231 | if (old != 0) { |
| 232 | old->decStrong((void*)setMediaPlayer); |
| 233 | } |
| 234 | env->SetLongField(thiz, fields.context, (jlong)player.get()); |
| 235 | return old; |
| 236 | } |
| 237 | |
| 238 | // If exception is NULL and opStatus is not OK, this method sends an error |
| 239 | // event to the client application; otherwise, if exception is not NULL and |
| 240 | // opStatus is not OK, this method throws the given exception to the client |
| 241 | // application. |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 242 | static void process_media_player_call( |
| 243 | JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 244 | { |
| 245 | if (exception == NULL) { // Don't throw exception. Instead, send an event. |
| 246 | if (opStatus != (status_t) OK) { |
| 247 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 248 | if (mp != 0) { |
| 249 | int64_t srcId = 0; |
| 250 | mp->getSrcId(&srcId); |
| 251 | mp->notify(srcId, MEDIA2_ERROR, opStatus, 0); |
| 252 | } |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 253 | } |
| 254 | } else { // Throw exception! |
| 255 | if ( opStatus == (status_t) INVALID_OPERATION ) { |
| 256 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 257 | } else if ( opStatus == (status_t) BAD_VALUE ) { |
| 258 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 259 | } else if ( opStatus == (status_t) PERMISSION_DENIED ) { |
| 260 | jniThrowException(env, "java/lang/SecurityException", NULL); |
| 261 | } else if ( opStatus != (status_t) OK ) { |
| 262 | if (strlen(message) > 230) { |
| 263 | // if the message is too long, don't bother displaying the status code |
| 264 | jniThrowException( env, exception, message); |
| 265 | } else { |
| 266 | char msg[256]; |
| 267 | // append the status code to the message |
| 268 | sprintf(msg, "%s: status=0x%X", message, opStatus); |
| 269 | jniThrowException( env, exception, msg); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 276 | android_media_MediaPlayer2_handleDataSourceUrl( |
| 277 | JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId, |
| 278 | jobject httpServiceObj, jstring path, jobjectArray keys, jobjectArray values) { |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 279 | |
| 280 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 281 | if (mp == NULL) { |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 282 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (path == NULL) { |
| 287 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | const char *tmp = env->GetStringUTFChars(path, NULL); |
| 292 | if (tmp == NULL) { // Out of memory |
| 293 | return; |
| 294 | } |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 295 | ALOGV("handleDataSourceUrl: path %s, srcId %lld", tmp, (long long)srcId); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 296 | |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 297 | if (strncmp(tmp, "content://", 10) == 0) { |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 298 | ALOGE("handleDataSourceUrl: content scheme is not supported in native code"); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 299 | jniThrowException(env, "java/io/IOException", |
| 300 | "content scheme is not supported in native code"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | sp<DataSourceDesc> dsd = new DataSourceDesc(); |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 305 | dsd->mId = srcId; |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 306 | dsd->mType = DataSourceDesc::TYPE_URL; |
| 307 | dsd->mUrl = tmp; |
| 308 | |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 309 | env->ReleaseStringUTFChars(path, tmp); |
| 310 | tmp = NULL; |
| 311 | |
| 312 | // We build a KeyedVector out of the key and val arrays |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 313 | if (!ConvertKeyValueArraysToKeyedVector( |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 314 | env, keys, values, &dsd->mHeaders)) { |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
| 318 | sp<MediaHTTPService> httpService; |
| 319 | if (httpServiceObj != NULL) { |
| 320 | httpService = new JMedia2HTTPService(env, httpServiceObj); |
| 321 | } |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 322 | dsd->mHttpService = httpService; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 323 | |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 324 | status_t err; |
| 325 | if (isCurrent) { |
| 326 | err = mp->setDataSource(dsd); |
| 327 | } else { |
| 328 | err = mp->prepareNextDataSource(dsd); |
| 329 | } |
| 330 | process_media_player_call(env, thiz, err, |
| 331 | "java/io/IOException", "handleDataSourceUrl failed." ); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 335 | android_media_MediaPlayer2_handleDataSourceFD( |
| 336 | JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId, |
| 337 | jobject fileDescriptor, jlong offset, jlong length) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 338 | { |
| 339 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 340 | if (mp == NULL ) { |
| 341 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (fileDescriptor == NULL) { |
| 346 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 347 | return; |
| 348 | } |
| 349 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 350 | ALOGV("handleDataSourceFD: srcId=%lld, fd=%d (%s), offset=%lld, length=%lld", |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 351 | (long long)srcId, fd, nameForFd(fd).c_str(), (long long)offset, (long long)length); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 352 | |
| 353 | struct stat sb; |
| 354 | int ret = fstat(fd, &sb); |
| 355 | if (ret != 0) { |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 356 | ALOGE("handleDataSourceFD: fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
| 357 | jniThrowException(env, "java/io/IOException", "handleDataSourceFD failed fstat"); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev)); |
| 362 | ALOGV("st_mode = %u", sb.st_mode); |
| 363 | ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid)); |
| 364 | ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid)); |
| 365 | ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size)); |
| 366 | |
| 367 | if (offset >= sb.st_size) { |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 368 | ALOGE("handleDataSourceFD: offset is out of range"); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 369 | jniThrowException(env, "java/lang/IllegalArgumentException", |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 370 | "handleDataSourceFD failed, offset is out of range."); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | if (offset + length > sb.st_size) { |
| 374 | length = sb.st_size - offset; |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 375 | ALOGV("handleDataSourceFD: adjusted length = %lld", (long long)length); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | sp<DataSourceDesc> dsd = new DataSourceDesc(); |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 379 | dsd->mId = srcId; |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 380 | dsd->mType = DataSourceDesc::TYPE_FD; |
| 381 | dsd->mFD = fd; |
| 382 | dsd->mFDOffset = offset; |
| 383 | dsd->mFDLength = length; |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 384 | |
| 385 | status_t err; |
| 386 | if (isCurrent) { |
| 387 | err = mp->setDataSource(dsd); |
| 388 | } else { |
| 389 | err = mp->prepareNextDataSource(dsd); |
| 390 | } |
| 391 | process_media_player_call(env, thiz, err, |
| 392 | "java/io/IOException", "handleDataSourceFD failed." ); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 396 | android_media_MediaPlayer2_handleDataSourceCallback( |
| 397 | JNIEnv *env, jobject thiz, jboolean isCurrent, jlong srcId, jobject dataSource) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 398 | { |
| 399 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 400 | if (mp == NULL ) { |
| 401 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | if (dataSource == NULL) { |
| 406 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 407 | return; |
| 408 | } |
| 409 | sp<DataSource> callbackDataSource = new JMedia2DataSource(env, dataSource); |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 410 | sp<DataSourceDesc> dsd = new DataSourceDesc(); |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 411 | dsd->mId = srcId; |
Wei Jia | 913074c | 2018-02-01 10:42:58 -0800 | [diff] [blame] | 412 | dsd->mType = DataSourceDesc::TYPE_CALLBACK; |
| 413 | dsd->mCallbackSource = callbackDataSource; |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 414 | |
| 415 | status_t err; |
| 416 | if (isCurrent) { |
| 417 | err = mp->setDataSource(dsd); |
| 418 | } else { |
| 419 | err = mp->prepareNextDataSource(dsd); |
| 420 | } |
| 421 | process_media_player_call(env, thiz, err, |
| 422 | "java/lang/RuntimeException", "handleDataSourceCallback failed." ); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static sp<ANativeWindowWrapper> |
| 426 | getVideoSurfaceTexture(JNIEnv* env, jobject thiz) { |
| 427 | ANativeWindow * const p = (ANativeWindow*)env->GetLongField(thiz, fields.surface_texture); |
| 428 | return new ANativeWindowWrapper(p); |
| 429 | } |
| 430 | |
| 431 | static void |
| 432 | decVideoSurfaceRef(JNIEnv *env, jobject thiz) |
| 433 | { |
| 434 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 435 | if (mp == NULL) { |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | ANativeWindow * const old_anw = (ANativeWindow*)env->GetLongField(thiz, fields.surface_texture); |
| 440 | if (old_anw != NULL) { |
| 441 | ANativeWindow_release(old_anw); |
| 442 | env->SetLongField(thiz, fields.surface_texture, (jlong)NULL); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | static void |
| 447 | setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive) |
| 448 | { |
| 449 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 450 | if (mp == NULL) { |
| 451 | if (mediaPlayerMustBeAlive) { |
| 452 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 453 | } |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | decVideoSurfaceRef(env, thiz); |
| 458 | |
| 459 | ANativeWindow* anw = NULL; |
| 460 | if (jsurface) { |
| 461 | anw = ANativeWindow_fromSurface(env, jsurface); |
| 462 | if (anw == NULL) { |
| 463 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 464 | "The surface has been released"); |
| 465 | return; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | env->SetLongField(thiz, fields.surface_texture, (jlong)anw); |
| 470 | |
| 471 | // This will fail if the media player has not been initialized yet. This |
| 472 | // can be the case if setDisplay() on MediaPlayer2Impl.java has been called |
| 473 | // before setDataSource(). The redundant call to setVideoSurfaceTexture() |
Wei Jia | 1789cc7 | 2018-02-23 09:16:08 -0800 | [diff] [blame^] | 474 | // in prepare/prepare covers for this case. |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 475 | mp->setVideoSurfaceTexture(new ANativeWindowWrapper(anw)); |
| 476 | } |
| 477 | |
| 478 | static void |
| 479 | android_media_MediaPlayer2_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface) |
| 480 | { |
| 481 | setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */); |
| 482 | } |
| 483 | |
| 484 | static jobject |
| 485 | android_media_MediaPlayer2_getBufferingParams(JNIEnv *env, jobject thiz) |
| 486 | { |
| 487 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 488 | if (mp == NULL) { |
| 489 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | BufferingParams bp; |
| 494 | BufferingSettings &settings = bp.settings; |
| 495 | process_media_player_call( |
| 496 | env, thiz, mp->getBufferingSettings(&settings), |
| 497 | "java/lang/IllegalStateException", "unexpected error"); |
| 498 | ALOGV("getBufferingSettings:{%s}", settings.toString().string()); |
| 499 | |
| 500 | return bp.asJobject(env, gBufferingParamsFields); |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | android_media_MediaPlayer2_setBufferingParams(JNIEnv *env, jobject thiz, jobject params) |
| 505 | { |
| 506 | if (params == NULL) { |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 511 | if (mp == NULL) { |
| 512 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | BufferingParams bp; |
| 517 | bp.fillFromJobject(env, gBufferingParamsFields, params); |
| 518 | ALOGV("setBufferingParams:{%s}", bp.settings.toString().string()); |
| 519 | |
| 520 | process_media_player_call( |
| 521 | env, thiz, mp->setBufferingSettings(bp.settings), |
| 522 | "java/lang/IllegalStateException", "unexpected error"); |
| 523 | } |
| 524 | |
| 525 | static void |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 526 | android_media_MediaPlayer2_playNextDataSource(JNIEnv *env, jobject thiz, jlong srcId) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 527 | { |
| 528 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 529 | if (mp == NULL) { |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 530 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 531 | return; |
| 532 | } |
| 533 | |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 534 | process_media_player_call(env, thiz, mp->playNextDataSource((int64_t)srcId), |
| 535 | "java/io/IOException", "playNextDataSource failed." ); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static void |
Wei Jia | 1789cc7 | 2018-02-23 09:16:08 -0800 | [diff] [blame^] | 539 | android_media_MediaPlayer2_prepare(JNIEnv *env, jobject thiz) |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 540 | { |
| 541 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 542 | if (mp == NULL ) { |
| 543 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | // Handle the case where the display surface was set before the mp was |
| 548 | // initialized. We try again to make it stick. |
| 549 | sp<ANativeWindowWrapper> st = getVideoSurfaceTexture(env, thiz); |
| 550 | mp->setVideoSurfaceTexture(st); |
| 551 | |
| 552 | process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." ); |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | android_media_MediaPlayer2_start(JNIEnv *env, jobject thiz) |
| 557 | { |
| 558 | ALOGV("start"); |
| 559 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 560 | if (mp == NULL ) { |
| 561 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 562 | return; |
| 563 | } |
| 564 | process_media_player_call( env, thiz, mp->start(), NULL, NULL ); |
| 565 | } |
| 566 | |
| 567 | static void |
| 568 | android_media_MediaPlayer2_stop(JNIEnv *env, jobject thiz) |
| 569 | { |
| 570 | ALOGV("stop"); |
| 571 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 572 | if (mp == NULL ) { |
| 573 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 574 | return; |
| 575 | } |
| 576 | process_media_player_call( env, thiz, mp->stop(), NULL, NULL ); |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | android_media_MediaPlayer2_pause(JNIEnv *env, jobject thiz) |
| 581 | { |
| 582 | ALOGV("pause"); |
| 583 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 584 | if (mp == NULL ) { |
| 585 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 586 | return; |
| 587 | } |
| 588 | process_media_player_call( env, thiz, mp->pause(), NULL, NULL ); |
| 589 | } |
| 590 | |
| 591 | static jboolean |
| 592 | android_media_MediaPlayer2_isPlaying(JNIEnv *env, jobject thiz) |
| 593 | { |
| 594 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 595 | if (mp == NULL ) { |
| 596 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 597 | return JNI_FALSE; |
| 598 | } |
| 599 | const jboolean is_playing = mp->isPlaying(); |
| 600 | |
| 601 | ALOGV("isPlaying: %d", is_playing); |
| 602 | return is_playing; |
| 603 | } |
| 604 | |
| 605 | static void |
| 606 | android_media_MediaPlayer2_setPlaybackParams(JNIEnv *env, jobject thiz, jobject params) |
| 607 | { |
| 608 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 609 | if (mp == NULL) { |
| 610 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | PlaybackParams pbp; |
| 615 | pbp.fillFromJobject(env, gPlaybackParamsFields, params); |
| 616 | ALOGV("setPlaybackParams: %d:%f %d:%f %d:%u %d:%u", |
| 617 | pbp.speedSet, pbp.audioRate.mSpeed, |
| 618 | pbp.pitchSet, pbp.audioRate.mPitch, |
| 619 | pbp.audioFallbackModeSet, pbp.audioRate.mFallbackMode, |
| 620 | pbp.audioStretchModeSet, pbp.audioRate.mStretchMode); |
| 621 | |
| 622 | AudioPlaybackRate rate; |
| 623 | status_t err = mp->getPlaybackSettings(&rate); |
| 624 | if (err == OK) { |
| 625 | bool updatedRate = false; |
| 626 | if (pbp.speedSet) { |
| 627 | rate.mSpeed = pbp.audioRate.mSpeed; |
| 628 | updatedRate = true; |
| 629 | } |
| 630 | if (pbp.pitchSet) { |
| 631 | rate.mPitch = pbp.audioRate.mPitch; |
| 632 | updatedRate = true; |
| 633 | } |
| 634 | if (pbp.audioFallbackModeSet) { |
| 635 | rate.mFallbackMode = pbp.audioRate.mFallbackMode; |
| 636 | updatedRate = true; |
| 637 | } |
| 638 | if (pbp.audioStretchModeSet) { |
| 639 | rate.mStretchMode = pbp.audioRate.mStretchMode; |
| 640 | updatedRate = true; |
| 641 | } |
| 642 | if (updatedRate) { |
| 643 | err = mp->setPlaybackSettings(rate); |
| 644 | } |
| 645 | } |
| 646 | process_media_player_call( |
| 647 | env, thiz, err, |
| 648 | "java/lang/IllegalStateException", "unexpected error"); |
| 649 | } |
| 650 | |
| 651 | static jobject |
| 652 | android_media_MediaPlayer2_getPlaybackParams(JNIEnv *env, jobject thiz) |
| 653 | { |
| 654 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 655 | if (mp == NULL) { |
| 656 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | PlaybackParams pbp; |
| 661 | AudioPlaybackRate &audioRate = pbp.audioRate; |
| 662 | process_media_player_call( |
| 663 | env, thiz, mp->getPlaybackSettings(&audioRate), |
| 664 | "java/lang/IllegalStateException", "unexpected error"); |
| 665 | ALOGV("getPlaybackSettings: %f %f %d %d", |
| 666 | audioRate.mSpeed, audioRate.mPitch, audioRate.mFallbackMode, audioRate.mStretchMode); |
| 667 | |
| 668 | pbp.speedSet = true; |
| 669 | pbp.pitchSet = true; |
| 670 | pbp.audioFallbackModeSet = true; |
| 671 | pbp.audioStretchModeSet = true; |
| 672 | |
| 673 | return pbp.asJobject(env, gPlaybackParamsFields); |
| 674 | } |
| 675 | |
| 676 | static void |
| 677 | android_media_MediaPlayer2_setSyncParams(JNIEnv *env, jobject thiz, jobject params) |
| 678 | { |
| 679 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 680 | if (mp == NULL) { |
| 681 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | SyncParams scp; |
| 686 | scp.fillFromJobject(env, gSyncParamsFields, params); |
| 687 | ALOGV("setSyncParams: %d:%d %d:%d %d:%f %d:%f", |
| 688 | scp.syncSourceSet, scp.sync.mSource, |
| 689 | scp.audioAdjustModeSet, scp.sync.mAudioAdjustMode, |
| 690 | scp.toleranceSet, scp.sync.mTolerance, |
| 691 | scp.frameRateSet, scp.frameRate); |
| 692 | |
| 693 | AVSyncSettings avsync; |
| 694 | float videoFrameRate; |
| 695 | status_t err = mp->getSyncSettings(&avsync, &videoFrameRate); |
| 696 | if (err == OK) { |
| 697 | bool updatedSync = scp.frameRateSet; |
| 698 | if (scp.syncSourceSet) { |
| 699 | avsync.mSource = scp.sync.mSource; |
| 700 | updatedSync = true; |
| 701 | } |
| 702 | if (scp.audioAdjustModeSet) { |
| 703 | avsync.mAudioAdjustMode = scp.sync.mAudioAdjustMode; |
| 704 | updatedSync = true; |
| 705 | } |
| 706 | if (scp.toleranceSet) { |
| 707 | avsync.mTolerance = scp.sync.mTolerance; |
| 708 | updatedSync = true; |
| 709 | } |
| 710 | if (updatedSync) { |
| 711 | err = mp->setSyncSettings(avsync, scp.frameRateSet ? scp.frameRate : -1.f); |
| 712 | } |
| 713 | } |
| 714 | process_media_player_call( |
| 715 | env, thiz, err, |
| 716 | "java/lang/IllegalStateException", "unexpected error"); |
| 717 | } |
| 718 | |
| 719 | static jobject |
| 720 | android_media_MediaPlayer2_getSyncParams(JNIEnv *env, jobject thiz) |
| 721 | { |
| 722 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 723 | if (mp == NULL) { |
| 724 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | SyncParams scp; |
| 729 | scp.frameRate = -1.f; |
| 730 | process_media_player_call( |
| 731 | env, thiz, mp->getSyncSettings(&scp.sync, &scp.frameRate), |
| 732 | "java/lang/IllegalStateException", "unexpected error"); |
| 733 | |
| 734 | ALOGV("getSyncSettings: %d %d %f %f", |
| 735 | scp.sync.mSource, scp.sync.mAudioAdjustMode, scp.sync.mTolerance, scp.frameRate); |
| 736 | |
| 737 | // sanity check params |
| 738 | if (scp.sync.mSource >= AVSYNC_SOURCE_MAX |
| 739 | || scp.sync.mAudioAdjustMode >= AVSYNC_AUDIO_ADJUST_MODE_MAX |
| 740 | || scp.sync.mTolerance < 0.f |
| 741 | || scp.sync.mTolerance >= AVSYNC_TOLERANCE_MAX) { |
| 742 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 743 | return NULL; |
| 744 | } |
| 745 | |
| 746 | scp.syncSourceSet = true; |
| 747 | scp.audioAdjustModeSet = true; |
| 748 | scp.toleranceSet = true; |
| 749 | scp.frameRateSet = scp.frameRate >= 0.f; |
| 750 | |
| 751 | return scp.asJobject(env, gSyncParamsFields); |
| 752 | } |
| 753 | |
| 754 | static void |
| 755 | android_media_MediaPlayer2_seekTo(JNIEnv *env, jobject thiz, jlong msec, jint mode) |
| 756 | { |
| 757 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 758 | if (mp == NULL ) { |
| 759 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 760 | return; |
| 761 | } |
| 762 | ALOGV("seekTo: %lld(msec), mode=%d", (long long)msec, mode); |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 763 | process_media_player_call(env, thiz, mp->seekTo((int64_t)msec, (MediaPlayer2SeekMode)mode), |
| 764 | NULL, NULL); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static void |
| 768 | android_media_MediaPlayer2_notifyAt(JNIEnv *env, jobject thiz, jlong mediaTimeUs) |
| 769 | { |
| 770 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 771 | if (mp == NULL) { |
| 772 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 773 | return; |
| 774 | } |
| 775 | ALOGV("notifyAt: %lld", (long long)mediaTimeUs); |
| 776 | process_media_player_call( env, thiz, mp->notifyAt((int64_t)mediaTimeUs), NULL, NULL ); |
| 777 | } |
| 778 | |
| 779 | static jint |
| 780 | android_media_MediaPlayer2_getVideoWidth(JNIEnv *env, jobject thiz) |
| 781 | { |
| 782 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 783 | if (mp == NULL ) { |
| 784 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 785 | return 0; |
| 786 | } |
| 787 | int w; |
| 788 | if (0 != mp->getVideoWidth(&w)) { |
| 789 | ALOGE("getVideoWidth failed"); |
| 790 | w = 0; |
| 791 | } |
| 792 | ALOGV("getVideoWidth: %d", w); |
| 793 | return (jint) w; |
| 794 | } |
| 795 | |
| 796 | static jint |
| 797 | android_media_MediaPlayer2_getVideoHeight(JNIEnv *env, jobject thiz) |
| 798 | { |
| 799 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 800 | if (mp == NULL ) { |
| 801 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 802 | return 0; |
| 803 | } |
| 804 | int h; |
| 805 | if (0 != mp->getVideoHeight(&h)) { |
| 806 | ALOGE("getVideoHeight failed"); |
| 807 | h = 0; |
| 808 | } |
| 809 | ALOGV("getVideoHeight: %d", h); |
| 810 | return (jint) h; |
| 811 | } |
| 812 | |
| 813 | static jobject |
| 814 | android_media_MediaPlayer2_native_getMetrics(JNIEnv *env, jobject thiz) |
| 815 | { |
| 816 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 817 | if (mp == NULL ) { |
| 818 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | Parcel p; |
| 823 | int key = FOURCC('m','t','r','X'); |
| 824 | status_t status = mp->getParameter(key, &p); |
| 825 | if (status != OK) { |
| 826 | ALOGD("getMetrics() failed: %d", status); |
| 827 | return (jobject) NULL; |
| 828 | } |
| 829 | |
| 830 | p.setDataPosition(0); |
| 831 | MediaAnalyticsItem *item = new MediaAnalyticsItem; |
| 832 | item->readFromParcel(p); |
| 833 | jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL); |
| 834 | |
| 835 | // housekeeping |
| 836 | delete item; |
| 837 | item = NULL; |
| 838 | |
| 839 | return mybundle; |
| 840 | } |
| 841 | |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 842 | static jlong |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 843 | android_media_MediaPlayer2_getCurrentPosition(JNIEnv *env, jobject thiz) |
| 844 | { |
| 845 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 846 | if (mp == NULL ) { |
| 847 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 848 | return 0; |
| 849 | } |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 850 | int64_t msec; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 851 | process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL ); |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 852 | ALOGV("getCurrentPosition: %lld (msec)", (long long)msec); |
| 853 | return (jlong) msec; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 856 | static jlong |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 857 | android_media_MediaPlayer2_getDuration(JNIEnv *env, jobject thiz) |
| 858 | { |
| 859 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 860 | if (mp == NULL ) { |
| 861 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 862 | return 0; |
| 863 | } |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 864 | int64_t msec; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 865 | process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL ); |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 866 | ALOGV("getDuration: %lld (msec)", (long long)msec); |
| 867 | return (jlong) msec; |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static void |
| 871 | android_media_MediaPlayer2_reset(JNIEnv *env, jobject thiz) |
| 872 | { |
| 873 | ALOGV("reset"); |
| 874 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 875 | if (mp == NULL ) { |
| 876 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 877 | return; |
| 878 | } |
| 879 | process_media_player_call( env, thiz, mp->reset(), NULL, NULL ); |
| 880 | } |
| 881 | |
| 882 | static jint |
| 883 | android_media_MediaPlayer2_getAudioStreamType(JNIEnv *env, jobject thiz) |
| 884 | { |
| 885 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 886 | if (mp == NULL ) { |
| 887 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 888 | return 0; |
| 889 | } |
| 890 | audio_stream_type_t streamtype; |
| 891 | process_media_player_call( env, thiz, mp->getAudioStreamType(&streamtype), NULL, NULL ); |
| 892 | ALOGV("getAudioStreamType: %d (streamtype)", streamtype); |
| 893 | return (jint) streamtype; |
| 894 | } |
| 895 | |
| 896 | static jboolean |
| 897 | android_media_MediaPlayer2_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request) |
| 898 | { |
| 899 | ALOGV("setParameter: key %d", key); |
| 900 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 901 | if (mp == NULL ) { |
| 902 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | Parcel *request = parcelForJavaObject(env, java_request); |
| 907 | status_t err = mp->setParameter(key, *request); |
| 908 | if (err == OK) { |
| 909 | return true; |
| 910 | } else { |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 915 | static jobject |
| 916 | android_media_MediaPlayer2_getParameter(JNIEnv *env, jobject thiz, jint key) |
| 917 | { |
| 918 | ALOGV("getParameter: key %d", key); |
| 919 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 920 | if (mp == NULL) { |
| 921 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
| 925 | jobject jParcel = createJavaParcelObject(env); |
| 926 | if (jParcel != NULL) { |
| 927 | Parcel* nativeParcel = parcelForJavaObject(env, jParcel); |
| 928 | status_t err = mp->getParameter(key, nativeParcel); |
| 929 | if (err != OK) { |
| 930 | env->DeleteLocalRef(jParcel); |
| 931 | return NULL; |
| 932 | } |
| 933 | } |
| 934 | return jParcel; |
| 935 | } |
| 936 | |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 937 | static void |
| 938 | android_media_MediaPlayer2_setLooping(JNIEnv *env, jobject thiz, jboolean looping) |
| 939 | { |
| 940 | ALOGV("setLooping: %d", looping); |
| 941 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 942 | if (mp == NULL ) { |
| 943 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 944 | return; |
| 945 | } |
| 946 | process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL ); |
| 947 | } |
| 948 | |
| 949 | static jboolean |
| 950 | android_media_MediaPlayer2_isLooping(JNIEnv *env, jobject thiz) |
| 951 | { |
| 952 | ALOGV("isLooping"); |
| 953 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 954 | if (mp == NULL ) { |
| 955 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 956 | return JNI_FALSE; |
| 957 | } |
| 958 | return mp->isLooping() ? JNI_TRUE : JNI_FALSE; |
| 959 | } |
| 960 | |
| 961 | static void |
| 962 | android_media_MediaPlayer2_setVolume(JNIEnv *env, jobject thiz, jfloat leftVolume, jfloat rightVolume) |
| 963 | { |
| 964 | ALOGV("setVolume: left %f right %f", (float) leftVolume, (float) rightVolume); |
| 965 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 966 | if (mp == NULL ) { |
| 967 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 968 | return; |
| 969 | } |
| 970 | process_media_player_call( env, thiz, mp->setVolume((float) leftVolume, (float) rightVolume), NULL, NULL ); |
| 971 | } |
| 972 | |
| 973 | // Sends the request and reply parcels to the media player via the |
| 974 | // binder interface. |
| 975 | static jint |
| 976 | android_media_MediaPlayer2_invoke(JNIEnv *env, jobject thiz, |
| 977 | jobject java_request, jobject java_reply) |
| 978 | { |
| 979 | sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); |
| 980 | if (media_player == NULL ) { |
| 981 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 982 | return UNKNOWN_ERROR; |
| 983 | } |
| 984 | |
| 985 | Parcel *request = parcelForJavaObject(env, java_request); |
| 986 | Parcel *reply = parcelForJavaObject(env, java_reply); |
| 987 | |
| 988 | request->setDataPosition(0); |
| 989 | |
| 990 | // Don't use process_media_player_call which use the async loop to |
| 991 | // report errors, instead returns the status. |
| 992 | return (jint) media_player->invoke(*request, reply); |
| 993 | } |
| 994 | |
| 995 | // Sends the new filter to the client. |
| 996 | static jint |
| 997 | android_media_MediaPlayer2_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request) |
| 998 | { |
| 999 | sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); |
| 1000 | if (media_player == NULL ) { |
| 1001 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1002 | return UNKNOWN_ERROR; |
| 1003 | } |
| 1004 | |
| 1005 | Parcel *filter = parcelForJavaObject(env, request); |
| 1006 | |
| 1007 | if (filter == NULL ) { |
| 1008 | jniThrowException(env, "java/lang/RuntimeException", "Filter is null"); |
| 1009 | return UNKNOWN_ERROR; |
| 1010 | } |
| 1011 | |
| 1012 | return (jint) media_player->setMetadataFilter(*filter); |
| 1013 | } |
| 1014 | |
| 1015 | static jboolean |
| 1016 | android_media_MediaPlayer2_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only, |
| 1017 | jboolean apply_filter, jobject reply) |
| 1018 | { |
| 1019 | sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); |
| 1020 | if (media_player == NULL ) { |
| 1021 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1022 | return JNI_FALSE; |
| 1023 | } |
| 1024 | |
| 1025 | Parcel *metadata = parcelForJavaObject(env, reply); |
| 1026 | |
| 1027 | if (metadata == NULL ) { |
| 1028 | jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null"); |
| 1029 | return JNI_FALSE; |
| 1030 | } |
| 1031 | |
| 1032 | metadata->freeData(); |
| 1033 | // On return metadata is positioned at the beginning of the |
| 1034 | // metadata. Note however that the parcel actually starts with the |
| 1035 | // return code so you should not rewind the parcel using |
| 1036 | // setDataPosition(0). |
| 1037 | if (media_player->getMetadata(update_only, apply_filter, metadata) == OK) { |
| 1038 | return JNI_TRUE; |
| 1039 | } else { |
| 1040 | return JNI_FALSE; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | // This function gets some field IDs, which in turn causes class initialization. |
| 1045 | // It is called from a static block in MediaPlayer2, which won't run until the |
| 1046 | // first time an instance of this class is used. |
| 1047 | static void |
| 1048 | android_media_MediaPlayer2_native_init(JNIEnv *env) |
| 1049 | { |
| 1050 | jclass clazz; |
| 1051 | |
| 1052 | clazz = env->FindClass("android/media/MediaPlayer2Impl"); |
| 1053 | if (clazz == NULL) { |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); |
| 1058 | if (fields.context == NULL) { |
| 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
Wei Jia | 34c5bb1 | 2018-02-08 09:57:23 -0800 | [diff] [blame] | 1063 | "(Ljava/lang/Object;JIIILjava/lang/Object;)V"); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1064 | if (fields.post_event == NULL) { |
| 1065 | return; |
| 1066 | } |
| 1067 | |
| 1068 | fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "J"); |
| 1069 | if (fields.surface_texture == NULL) { |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | env->DeleteLocalRef(clazz); |
| 1074 | |
| 1075 | clazz = env->FindClass("android/net/ProxyInfo"); |
| 1076 | if (clazz == NULL) { |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | fields.proxyConfigGetHost = |
| 1081 | env->GetMethodID(clazz, "getHost", "()Ljava/lang/String;"); |
| 1082 | |
| 1083 | fields.proxyConfigGetPort = |
| 1084 | env->GetMethodID(clazz, "getPort", "()I"); |
| 1085 | |
| 1086 | fields.proxyConfigGetExclusionList = |
| 1087 | env->GetMethodID(clazz, "getExclusionListAsString", "()Ljava/lang/String;"); |
| 1088 | |
| 1089 | env->DeleteLocalRef(clazz); |
| 1090 | |
| 1091 | gBufferingParamsFields.init(env); |
| 1092 | |
| 1093 | // Modular DRM |
| 1094 | FIND_CLASS(clazz, "android/media/MediaDrm$MediaDrmStateException"); |
| 1095 | if (clazz) { |
| 1096 | GET_METHOD_ID(gStateExceptionFields.init, clazz, "<init>", "(ILjava/lang/String;)V"); |
| 1097 | gStateExceptionFields.classId = static_cast<jclass>(env->NewGlobalRef(clazz)); |
| 1098 | |
| 1099 | env->DeleteLocalRef(clazz); |
| 1100 | } else { |
| 1101 | ALOGE("JNI android_media_MediaPlayer2_native_init couldn't " |
| 1102 | "get clazz android/media/MediaDrm$MediaDrmStateException"); |
| 1103 | } |
| 1104 | |
| 1105 | gPlaybackParamsFields.init(env); |
| 1106 | gSyncParamsFields.init(env); |
| 1107 | gVolumeShaperFields.init(env); |
| 1108 | } |
| 1109 | |
| 1110 | static void |
| 1111 | android_media_MediaPlayer2_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 1112 | { |
| 1113 | ALOGV("native_setup"); |
Wei Jia | 1c2b64d | 2018-02-20 10:20:08 -0800 | [diff] [blame] | 1114 | sp<MediaPlayer2> mp = MediaPlayer2::Create(); |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1115 | if (mp == NULL) { |
| 1116 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | // create new listener and give it to MediaPlayer2 |
| 1121 | sp<JNIMediaPlayer2Listener> listener = new JNIMediaPlayer2Listener(env, thiz, weak_this); |
| 1122 | mp->setListener(listener); |
| 1123 | |
| 1124 | // Stow our new C++ MediaPlayer2 in an opaque field in the Java object. |
| 1125 | setMediaPlayer(env, thiz, mp); |
| 1126 | } |
| 1127 | |
| 1128 | static void |
| 1129 | android_media_MediaPlayer2_release(JNIEnv *env, jobject thiz) |
| 1130 | { |
| 1131 | ALOGV("release"); |
| 1132 | decVideoSurfaceRef(env, thiz); |
| 1133 | sp<MediaPlayer2> mp = setMediaPlayer(env, thiz, 0); |
| 1134 | if (mp != NULL) { |
| 1135 | // this prevents native callbacks after the object is released |
| 1136 | mp->setListener(0); |
| 1137 | mp->disconnect(); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | static void |
| 1142 | android_media_MediaPlayer2_native_finalize(JNIEnv *env, jobject thiz) |
| 1143 | { |
| 1144 | ALOGV("native_finalize"); |
| 1145 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1146 | if (mp != NULL) { |
| 1147 | ALOGW("MediaPlayer2 finalized without being released"); |
| 1148 | } |
| 1149 | android_media_MediaPlayer2_release(env, thiz); |
| 1150 | } |
| 1151 | |
| 1152 | static void android_media_MediaPlayer2_set_audio_session_id(JNIEnv *env, jobject thiz, |
| 1153 | jint sessionId) { |
| 1154 | ALOGV("set_session_id(): %d", sessionId); |
| 1155 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1156 | if (mp == NULL ) { |
| 1157 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1158 | return; |
| 1159 | } |
| 1160 | process_media_player_call( env, thiz, mp->setAudioSessionId((audio_session_t) sessionId), NULL, |
| 1161 | NULL); |
| 1162 | } |
| 1163 | |
| 1164 | static jint android_media_MediaPlayer2_get_audio_session_id(JNIEnv *env, jobject thiz) { |
| 1165 | ALOGV("get_session_id()"); |
| 1166 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1167 | if (mp == NULL ) { |
| 1168 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | return (jint) mp->getAudioSessionId(); |
| 1173 | } |
| 1174 | |
| 1175 | static void |
| 1176 | android_media_MediaPlayer2_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level) |
| 1177 | { |
| 1178 | ALOGV("setAuxEffectSendLevel: level %f", level); |
| 1179 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1180 | if (mp == NULL ) { |
| 1181 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1182 | return; |
| 1183 | } |
| 1184 | process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL ); |
| 1185 | } |
| 1186 | |
| 1187 | static void android_media_MediaPlayer2_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) { |
| 1188 | ALOGV("attachAuxEffect(): %d", effectId); |
| 1189 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1190 | if (mp == NULL ) { |
| 1191 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1192 | return; |
| 1193 | } |
| 1194 | process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL ); |
| 1195 | } |
| 1196 | |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1197 | ///////////////////////////////////////////////////////////////////////////////////// |
| 1198 | // Modular DRM begin |
| 1199 | |
| 1200 | // TODO: investigate if these can be shared with their MediaDrm counterparts |
| 1201 | static void throwDrmStateException(JNIEnv *env, const char *msg, status_t err) |
| 1202 | { |
| 1203 | ALOGE("Illegal DRM state exception: %s (%d)", msg, err); |
| 1204 | |
| 1205 | jobject exception = env->NewObject(gStateExceptionFields.classId, |
| 1206 | gStateExceptionFields.init, static_cast<int>(err), |
| 1207 | env->NewStringUTF(msg)); |
| 1208 | env->Throw(static_cast<jthrowable>(exception)); |
| 1209 | } |
| 1210 | |
| 1211 | // TODO: investigate if these can be shared with their MediaDrm counterparts |
| 1212 | static bool throwDrmExceptionAsNecessary(JNIEnv *env, status_t err, const char *msg = NULL) |
| 1213 | { |
| 1214 | const char *drmMessage = "Unknown DRM Msg"; |
| 1215 | |
| 1216 | switch (err) { |
| 1217 | case ERROR_DRM_UNKNOWN: |
| 1218 | drmMessage = "General DRM error"; |
| 1219 | break; |
| 1220 | case ERROR_DRM_NO_LICENSE: |
| 1221 | drmMessage = "No license"; |
| 1222 | break; |
| 1223 | case ERROR_DRM_LICENSE_EXPIRED: |
| 1224 | drmMessage = "License expired"; |
| 1225 | break; |
| 1226 | case ERROR_DRM_SESSION_NOT_OPENED: |
| 1227 | drmMessage = "Session not opened"; |
| 1228 | break; |
| 1229 | case ERROR_DRM_DECRYPT_UNIT_NOT_INITIALIZED: |
| 1230 | drmMessage = "Not initialized"; |
| 1231 | break; |
| 1232 | case ERROR_DRM_DECRYPT: |
| 1233 | drmMessage = "Decrypt error"; |
| 1234 | break; |
| 1235 | case ERROR_DRM_CANNOT_HANDLE: |
| 1236 | drmMessage = "Unsupported scheme or data format"; |
| 1237 | break; |
| 1238 | case ERROR_DRM_TAMPER_DETECTED: |
| 1239 | drmMessage = "Invalid state"; |
| 1240 | break; |
| 1241 | default: |
| 1242 | break; |
| 1243 | } |
| 1244 | |
| 1245 | String8 vendorMessage; |
| 1246 | if (err >= ERROR_DRM_VENDOR_MIN && err <= ERROR_DRM_VENDOR_MAX) { |
| 1247 | vendorMessage = String8::format("DRM vendor-defined error: %d", err); |
| 1248 | drmMessage = vendorMessage.string(); |
| 1249 | } |
| 1250 | |
| 1251 | if (err == BAD_VALUE) { |
| 1252 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
| 1253 | return true; |
| 1254 | } else if (err == ERROR_DRM_NOT_PROVISIONED) { |
| 1255 | jniThrowException(env, "android/media/NotProvisionedException", msg); |
| 1256 | return true; |
| 1257 | } else if (err == ERROR_DRM_RESOURCE_BUSY) { |
| 1258 | jniThrowException(env, "android/media/ResourceBusyException", msg); |
| 1259 | return true; |
| 1260 | } else if (err == ERROR_DRM_DEVICE_REVOKED) { |
| 1261 | jniThrowException(env, "android/media/DeniedByServerException", msg); |
| 1262 | return true; |
| 1263 | } else if (err == DEAD_OBJECT) { |
| 1264 | jniThrowException(env, "android/media/MediaDrmResetException", |
| 1265 | "mediaserver died"); |
| 1266 | return true; |
| 1267 | } else if (err != OK) { |
| 1268 | String8 errbuf; |
| 1269 | if (drmMessage != NULL) { |
| 1270 | if (msg == NULL) { |
| 1271 | msg = drmMessage; |
| 1272 | } else { |
| 1273 | errbuf = String8::format("%s: %s", msg, drmMessage); |
| 1274 | msg = errbuf.string(); |
| 1275 | } |
| 1276 | } |
| 1277 | throwDrmStateException(env, msg, err); |
| 1278 | return true; |
| 1279 | } |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
| 1283 | static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) |
| 1284 | { |
| 1285 | Vector<uint8_t> vector; |
| 1286 | size_t length = env->GetArrayLength(byteArray); |
| 1287 | vector.insertAt((size_t)0, length); |
| 1288 | env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray()); |
| 1289 | return vector; |
| 1290 | } |
| 1291 | |
| 1292 | static void android_media_MediaPlayer2_prepareDrm(JNIEnv *env, jobject thiz, |
| 1293 | jbyteArray uuidObj, jbyteArray drmSessionIdObj) |
| 1294 | { |
| 1295 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1296 | if (mp == NULL) { |
| 1297 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | if (uuidObj == NULL) { |
| 1302 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | Vector<uint8_t> uuid = JByteArrayToVector(env, uuidObj); |
| 1307 | |
| 1308 | if (uuid.size() != 16) { |
| 1309 | jniThrowException( |
| 1310 | env, |
| 1311 | "java/lang/IllegalArgumentException", |
| 1312 | "invalid UUID size, expected 16 bytes"); |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | Vector<uint8_t> drmSessionId = JByteArrayToVector(env, drmSessionIdObj); |
| 1317 | |
| 1318 | if (drmSessionId.size() == 0) { |
| 1319 | jniThrowException( |
| 1320 | env, |
| 1321 | "java/lang/IllegalArgumentException", |
| 1322 | "empty drmSessionId"); |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | status_t err = mp->prepareDrm(uuid.array(), drmSessionId); |
| 1327 | if (err != OK) { |
| 1328 | if (err == INVALID_OPERATION) { |
| 1329 | jniThrowException( |
| 1330 | env, |
| 1331 | "java/lang/IllegalStateException", |
| 1332 | "The player must be in prepared state."); |
| 1333 | } else if (err == ERROR_DRM_CANNOT_HANDLE) { |
| 1334 | jniThrowException( |
| 1335 | env, |
| 1336 | "android/media/UnsupportedSchemeException", |
| 1337 | "Failed to instantiate drm object."); |
| 1338 | } else { |
| 1339 | throwDrmExceptionAsNecessary(env, err, "Failed to prepare DRM scheme"); |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | static void android_media_MediaPlayer2_releaseDrm(JNIEnv *env, jobject thiz) |
| 1345 | { |
| 1346 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1347 | if (mp == NULL ) { |
| 1348 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | status_t err = mp->releaseDrm(); |
| 1353 | if (err != OK) { |
| 1354 | if (err == INVALID_OPERATION) { |
| 1355 | jniThrowException( |
| 1356 | env, |
| 1357 | "java/lang/IllegalStateException", |
| 1358 | "Can not release DRM in an active player state."); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | // Modular DRM end |
| 1363 | // ---------------------------------------------------------------------------- |
| 1364 | |
| 1365 | ///////////////////////////////////////////////////////////////////////////////////// |
| 1366 | // AudioRouting begin |
| 1367 | static jboolean android_media_MediaPlayer2_setOutputDevice(JNIEnv *env, jobject thiz, jint device_id) |
| 1368 | { |
| 1369 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1370 | if (mp == NULL) { |
| 1371 | return false; |
| 1372 | } |
| 1373 | return mp->setOutputDevice(device_id) == NO_ERROR; |
| 1374 | } |
| 1375 | |
| 1376 | static jint android_media_MediaPlayer2_getRoutedDeviceId(JNIEnv *env, jobject thiz) |
| 1377 | { |
| 1378 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1379 | if (mp == NULL) { |
| 1380 | return AUDIO_PORT_HANDLE_NONE; |
| 1381 | } |
| 1382 | return mp->getRoutedDeviceId(); |
| 1383 | } |
| 1384 | |
| 1385 | static void android_media_MediaPlayer2_enableDeviceCallback( |
| 1386 | JNIEnv* env, jobject thiz, jboolean enabled) |
| 1387 | { |
| 1388 | sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); |
| 1389 | if (mp == NULL) { |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | status_t status = mp->enableAudioDeviceCallback(enabled); |
| 1394 | if (status != NO_ERROR) { |
| 1395 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1396 | ALOGE("enable device callback failed: %d", status); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | // AudioRouting end |
| 1401 | // ---------------------------------------------------------------------------- |
| 1402 | |
Hyundo Moon | 8e5ef90 | 2018-02-07 11:53:37 +0900 | [diff] [blame] | 1403 | ///////////////////////////////////////////////////////////////////////////////////// |
| 1404 | // AudioTrack.StreamEventCallback begin |
| 1405 | static void android_media_MediaPlayer2_native_on_tear_down(JNIEnv *env __unused, |
| 1406 | jobject thiz __unused, jlong callbackPtr, jlong userDataPtr) |
| 1407 | { |
| 1408 | JAudioTrack::callback_t callback = (JAudioTrack::callback_t) callbackPtr; |
| 1409 | if (callback != NULL) { |
| 1410 | callback(JAudioTrack::EVENT_NEW_IAUDIOTRACK, (void *) userDataPtr, NULL); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | static void android_media_MediaPlayer2_native_on_stream_presentation_end(JNIEnv *env __unused, |
| 1415 | jobject thiz __unused, jlong callbackPtr, jlong userDataPtr) |
| 1416 | { |
| 1417 | JAudioTrack::callback_t callback = (JAudioTrack::callback_t) callbackPtr; |
| 1418 | if (callback != NULL) { |
| 1419 | callback(JAudioTrack::EVENT_STREAM_END, (void *) userDataPtr, NULL); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | static void android_media_MediaPlayer2_native_on_stream_data_request(JNIEnv *env __unused, |
| 1424 | jobject thiz __unused, jlong jAudioTrackPtr, jlong callbackPtr, jlong userDataPtr) |
| 1425 | { |
| 1426 | JAudioTrack::callback_t callback = (JAudioTrack::callback_t) callbackPtr; |
| 1427 | JAudioTrack* track = (JAudioTrack *) jAudioTrackPtr; |
| 1428 | if (callback != NULL && track != NULL) { |
| 1429 | JAudioTrack::Buffer* buffer = new JAudioTrack::Buffer(); |
| 1430 | |
| 1431 | size_t bufferSizeInFrames = track->frameCount(); |
| 1432 | audio_format_t format = track->format(); |
| 1433 | |
| 1434 | size_t bufferSizeInBytes; |
| 1435 | if (audio_has_proportional_frames(format)) { |
| 1436 | bufferSizeInBytes = |
| 1437 | bufferSizeInFrames * audio_bytes_per_sample(format) * track->channelCount(); |
| 1438 | } else { |
| 1439 | // See Javadoc of AudioTrack::getBufferSizeInFrames(). |
| 1440 | bufferSizeInBytes = bufferSizeInFrames; |
| 1441 | } |
| 1442 | |
| 1443 | uint8_t* byteBuffer = new uint8_t[bufferSizeInBytes]; |
| 1444 | buffer->mSize = bufferSizeInBytes; |
| 1445 | buffer->mData = (void *) byteBuffer; |
| 1446 | |
| 1447 | callback(JAudioTrack::EVENT_MORE_DATA, (void *) userDataPtr, buffer); |
| 1448 | |
| 1449 | if (buffer->mSize > 0 && buffer->mData == byteBuffer) { |
| 1450 | track->write(buffer->mData, buffer->mSize, true /* Blocking */); |
| 1451 | } |
| 1452 | |
| 1453 | delete[] byteBuffer; |
| 1454 | delete buffer; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | // AudioTrack.StreamEventCallback end |
| 1460 | // ---------------------------------------------------------------------------- |
| 1461 | |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1462 | static const JNINativeMethod gMethods[] = { |
| 1463 | { |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 1464 | "nativeHandleDataSourceUrl", |
| 1465 | "(ZJLandroid/media/Media2HTTPService;Ljava/lang/String;[Ljava/lang/String;" |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1466 | "[Ljava/lang/String;)V", |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 1467 | (void *)android_media_MediaPlayer2_handleDataSourceUrl |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1468 | }, |
Wei Jia | de0c397 | 2018-02-15 16:53:18 -0800 | [diff] [blame] | 1469 | { |
| 1470 | "nativeHandleDataSourceFD", |
| 1471 | "(ZJLjava/io/FileDescriptor;JJ)V", |
| 1472 | (void *)android_media_MediaPlayer2_handleDataSourceFD |
| 1473 | }, |
| 1474 | { |
| 1475 | "nativeHandleDataSourceCallback", |
| 1476 | "(ZJLandroid/media/Media2DataSource;)V", |
| 1477 | (void *)android_media_MediaPlayer2_handleDataSourceCallback |
| 1478 | }, |
| 1479 | {"nativePlayNextDataSource", "(J)V", (void *)android_media_MediaPlayer2_playNextDataSource}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1480 | {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer2_setVideoSurface}, |
| 1481 | {"getBufferingParams", "()Landroid/media/BufferingParams;", (void *)android_media_MediaPlayer2_getBufferingParams}, |
| 1482 | {"setBufferingParams", "(Landroid/media/BufferingParams;)V", (void *)android_media_MediaPlayer2_setBufferingParams}, |
Wei Jia | 1789cc7 | 2018-02-23 09:16:08 -0800 | [diff] [blame^] | 1483 | {"prepare", "()V", (void *)android_media_MediaPlayer2_prepare}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1484 | {"_start", "()V", (void *)android_media_MediaPlayer2_start}, |
| 1485 | {"_stop", "()V", (void *)android_media_MediaPlayer2_stop}, |
| 1486 | {"getVideoWidth", "()I", (void *)android_media_MediaPlayer2_getVideoWidth}, |
| 1487 | {"getVideoHeight", "()I", (void *)android_media_MediaPlayer2_getVideoHeight}, |
| 1488 | {"native_getMetrics", "()Landroid/os/PersistableBundle;", (void *)android_media_MediaPlayer2_native_getMetrics}, |
| 1489 | {"setPlaybackParams", "(Landroid/media/PlaybackParams;)V", (void *)android_media_MediaPlayer2_setPlaybackParams}, |
| 1490 | {"getPlaybackParams", "()Landroid/media/PlaybackParams;", (void *)android_media_MediaPlayer2_getPlaybackParams}, |
| 1491 | {"setSyncParams", "(Landroid/media/SyncParams;)V", (void *)android_media_MediaPlayer2_setSyncParams}, |
| 1492 | {"getSyncParams", "()Landroid/media/SyncParams;", (void *)android_media_MediaPlayer2_getSyncParams}, |
| 1493 | {"_seekTo", "(JI)V", (void *)android_media_MediaPlayer2_seekTo}, |
| 1494 | {"_notifyAt", "(J)V", (void *)android_media_MediaPlayer2_notifyAt}, |
| 1495 | {"_pause", "()V", (void *)android_media_MediaPlayer2_pause}, |
| 1496 | {"isPlaying", "()Z", (void *)android_media_MediaPlayer2_isPlaying}, |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 1497 | {"getCurrentPosition", "()J", (void *)android_media_MediaPlayer2_getCurrentPosition}, |
| 1498 | {"getDuration", "()J", (void *)android_media_MediaPlayer2_getDuration}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1499 | {"_release", "()V", (void *)android_media_MediaPlayer2_release}, |
| 1500 | {"_reset", "()V", (void *)android_media_MediaPlayer2_reset}, |
| 1501 | {"_getAudioStreamType", "()I", (void *)android_media_MediaPlayer2_getAudioStreamType}, |
| 1502 | {"setParameter", "(ILandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer2_setParameter}, |
Wei Jia | 1288759 | 2018-02-20 15:01:52 -0800 | [diff] [blame] | 1503 | {"getParameter", "(I)Landroid/os/Parcel;", (void *)android_media_MediaPlayer2_getParameter}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1504 | {"setLooping", "(Z)V", (void *)android_media_MediaPlayer2_setLooping}, |
| 1505 | {"isLooping", "()Z", (void *)android_media_MediaPlayer2_isLooping}, |
| 1506 | {"_setVolume", "(FF)V", (void *)android_media_MediaPlayer2_setVolume}, |
| 1507 | {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer2_invoke}, |
| 1508 | {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer2_setMetadataFilter}, |
| 1509 | {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer2_getMetadata}, |
| 1510 | {"native_init", "()V", (void *)android_media_MediaPlayer2_native_init}, |
| 1511 | {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer2_native_setup}, |
| 1512 | {"native_finalize", "()V", (void *)android_media_MediaPlayer2_native_finalize}, |
| 1513 | {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer2_get_audio_session_id}, |
| 1514 | {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer2_set_audio_session_id}, |
| 1515 | {"_setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer2_setAuxEffectSendLevel}, |
| 1516 | {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer2_attachAuxEffect}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1517 | // Modular DRM |
| 1518 | { "_prepareDrm", "([B[B)V", (void *)android_media_MediaPlayer2_prepareDrm }, |
| 1519 | { "_releaseDrm", "()V", (void *)android_media_MediaPlayer2_releaseDrm }, |
| 1520 | |
| 1521 | // AudioRouting |
| 1522 | {"native_setOutputDevice", "(I)Z", (void *)android_media_MediaPlayer2_setOutputDevice}, |
| 1523 | {"native_getRoutedDeviceId", "()I", (void *)android_media_MediaPlayer2_getRoutedDeviceId}, |
| 1524 | {"native_enableDeviceCallback", "(Z)V", (void *)android_media_MediaPlayer2_enableDeviceCallback}, |
Hyundo Moon | 8e5ef90 | 2018-02-07 11:53:37 +0900 | [diff] [blame] | 1525 | |
| 1526 | // StreamEventCallback for JAudioTrack |
| 1527 | {"native_stream_event_onTearDown", "(JJ)V", (void *)android_media_MediaPlayer2_native_on_tear_down}, |
| 1528 | {"native_stream_event_onStreamPresentationEnd", "(JJ)V", (void *)android_media_MediaPlayer2_native_on_stream_presentation_end}, |
| 1529 | {"native_stream_event_onStreamDataRequest", "(JJJ)V", (void *)android_media_MediaPlayer2_native_on_stream_data_request}, |
Wei Jia | 0a8a8f0 | 2017-12-05 17:05:29 -0800 | [diff] [blame] | 1530 | }; |
| 1531 | |
| 1532 | // This function only registers the native methods |
| 1533 | static int register_android_media_MediaPlayer2Impl(JNIEnv *env) |
| 1534 | { |
| 1535 | return AndroidRuntime::registerNativeMethods(env, |
| 1536 | "android/media/MediaPlayer2Impl", gMethods, NELEM(gMethods)); |
| 1537 | } |
| 1538 | |
| 1539 | jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) |
| 1540 | { |
| 1541 | JNIEnv* env = NULL; |
| 1542 | jint result = -1; |
| 1543 | |
| 1544 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 1545 | ALOGE("ERROR: GetEnv failed\n"); |
| 1546 | goto bail; |
| 1547 | } |
| 1548 | assert(env != NULL); |
| 1549 | |
| 1550 | if (register_android_media_MediaPlayer2Impl(env) < 0) { |
| 1551 | ALOGE("ERROR: MediaPlayer2 native registration failed\n"); |
| 1552 | goto bail; |
| 1553 | } |
| 1554 | |
| 1555 | /* success -- return valid version number */ |
| 1556 | result = JNI_VERSION_1_4; |
| 1557 | |
| 1558 | bail: |
| 1559 | return result; |
| 1560 | } |
| 1561 | |
| 1562 | // KTHXBYE |