1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
* Copyright 2012, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "MediaExtractor-JNI"
#include <utils/Log.h>
#include "android_media_MediaExtractor.h"
#include "android_media_Utils.h"
#include "android_runtime/AndroidRuntime.h"
#include "jni.h"
#include "JNIHelp.h"
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/DataSource.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/NuMediaExtractor.h>
namespace android {
struct fields_t {
jfieldID context;
};
static fields_t gFields;
////////////////////////////////////////////////////////////////////////////////
JMediaExtractor::JMediaExtractor(JNIEnv *env, jobject thiz)
: mClass(NULL),
mObject(NULL) {
jclass clazz = env->GetObjectClass(thiz);
CHECK(clazz != NULL);
mClass = (jclass)env->NewGlobalRef(clazz);
mObject = env->NewWeakGlobalRef(thiz);
mImpl = new NuMediaExtractor;
}
JMediaExtractor::~JMediaExtractor() {
JNIEnv *env = AndroidRuntime::getJNIEnv();
env->DeleteWeakGlobalRef(mObject);
mObject = NULL;
env->DeleteGlobalRef(mClass);
mClass = NULL;
}
status_t JMediaExtractor::setDataSource(const char *path) {
return mImpl->setDataSource(path);
}
size_t JMediaExtractor::countTracks() const {
return mImpl->countTracks();
}
status_t JMediaExtractor::getTrackFormat(size_t index, jobject *format) const {
sp<AMessage> msg;
status_t err;
if ((err = mImpl->getTrackFormat(index, &msg)) != OK) {
return err;
}
JNIEnv *env = AndroidRuntime::getJNIEnv();
return ConvertMessageToMap(env, msg, format);
}
status_t JMediaExtractor::selectTrack(size_t index) {
return mImpl->selectTrack(index);
}
status_t JMediaExtractor::seekTo(int64_t timeUs) {
return mImpl->seekTo(timeUs);
}
status_t JMediaExtractor::advance() {
return mImpl->advance();
}
status_t JMediaExtractor::readSampleData(
jobject byteBuf, size_t offset, size_t *sampleSize) {
JNIEnv *env = AndroidRuntime::getJNIEnv();
void *dst = env->GetDirectBufferAddress(byteBuf);
jlong dstSize;
jbyteArray byteArray = NULL;
if (dst == NULL) {
jclass byteBufClass = env->FindClass("java/nio/ByteBuffer");
CHECK(byteBufClass != NULL);
jmethodID arrayID =
env->GetMethodID(byteBufClass, "array", "()[B");
CHECK(arrayID != NULL);
byteArray =
(jbyteArray)env->CallObjectMethod(byteBuf, arrayID);
if (byteArray == NULL) {
return INVALID_OPERATION;
}
jboolean isCopy;
dst = env->GetByteArrayElements(byteArray, &isCopy);
dstSize = env->GetArrayLength(byteArray);
} else {
dstSize = env->GetDirectBufferCapacity(byteBuf);
}
if (dstSize < offset) {
if (byteArray != NULL) {
env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
}
return -ERANGE;
}
sp<ABuffer> buffer = new ABuffer((char *)dst + offset, dstSize - offset);
status_t err = mImpl->readSampleData(buffer);
if (byteArray != NULL) {
env->ReleaseByteArrayElements(byteArray, (jbyte *)dst, 0);
}
if (err != OK) {
return err;
}
*sampleSize = buffer->size();
return OK;
}
status_t JMediaExtractor::getSampleTrackIndex(size_t *trackIndex) {
return mImpl->getSampleTrackIndex(trackIndex);
}
status_t JMediaExtractor::getSampleTime(int64_t *sampleTimeUs) {
return mImpl->getSampleTime(sampleTimeUs);
}
status_t JMediaExtractor::getSampleFlags(uint32_t *sampleFlags) {
return mImpl->getSampleFlags(sampleFlags);
}
} // namespace android
////////////////////////////////////////////////////////////////////////////////
using namespace android;
static sp<JMediaExtractor> setMediaExtractor(
JNIEnv *env, jobject thiz, const sp<JMediaExtractor> &extractor) {
sp<JMediaExtractor> old =
(JMediaExtractor *)env->GetIntField(thiz, gFields.context);
if (extractor != NULL) {
extractor->incStrong(thiz);
}
if (old != NULL) {
old->decStrong(thiz);
}
env->SetIntField(thiz, gFields.context, (int)extractor.get());
return old;
}
static sp<JMediaExtractor> getMediaExtractor(JNIEnv *env, jobject thiz) {
return (JMediaExtractor *)env->GetIntField(thiz, gFields.context);
}
static void android_media_MediaExtractor_release(JNIEnv *env, jobject thiz) {
setMediaExtractor(env, thiz, NULL);
}
static jint android_media_MediaExtractor_countTracks(
JNIEnv *env, jobject thiz) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return NULL;
}
return extractor->countTracks();
}
static jobject android_media_MediaExtractor_getTrackFormat(
JNIEnv *env, jobject thiz, jint index) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return NULL;
}
jobject format;
status_t err = extractor->getTrackFormat(index, &format);
if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return NULL;
}
return format;
}
static void android_media_MediaExtractor_selectTrack(
JNIEnv *env, jobject thiz, jint index) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return;
}
status_t err = extractor->selectTrack(index);
if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return;
}
}
static void android_media_MediaExtractor_seekTo(
JNIEnv *env, jobject thiz, jlong timeUs) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return;
}
status_t err = extractor->seekTo(timeUs);
if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return;
}
}
static jboolean android_media_MediaExtractor_advance(
JNIEnv *env, jobject thiz) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return false;
}
status_t err = extractor->advance();
if (err == ERROR_END_OF_STREAM) {
return false;
} else if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return false;
}
return true;
}
static jint android_media_MediaExtractor_readSampleData(
JNIEnv *env, jobject thiz, jobject byteBuf, jint offset) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return -1;
}
size_t sampleSize;
status_t err = extractor->readSampleData(byteBuf, offset, &sampleSize);
if (err == ERROR_END_OF_STREAM) {
return -1;
} else if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return false;
}
return sampleSize;
}
static jint android_media_MediaExtractor_getSampleTrackIndex(
JNIEnv *env, jobject thiz) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return -1;
}
size_t trackIndex;
status_t err = extractor->getSampleTrackIndex(&trackIndex);
if (err == ERROR_END_OF_STREAM) {
return -1;
} else if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return false;
}
return trackIndex;
}
static jlong android_media_MediaExtractor_getSampleTime(
JNIEnv *env, jobject thiz) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return -1ll;
}
int64_t sampleTimeUs;
status_t err = extractor->getSampleTime(&sampleTimeUs);
if (err == ERROR_END_OF_STREAM) {
return -1ll;
} else if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return false;
}
return sampleTimeUs;
}
static jint android_media_MediaExtractor_getSampleFlags(
JNIEnv *env, jobject thiz) {
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
if (extractor == NULL) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return -1ll;
}
uint32_t sampleFlags;
status_t err = extractor->getSampleFlags(&sampleFlags);
if (err == ERROR_END_OF_STREAM) {
return -1ll;
} else if (err != OK) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return false;
}
return sampleFlags;
}
static void android_media_MediaExtractor_native_init(JNIEnv *env) {
jclass clazz = env->FindClass("android/media/MediaExtractor");
CHECK(clazz != NULL);
gFields.context = env->GetFieldID(clazz, "mNativeContext", "I");
CHECK(gFields.context != NULL);
DataSource::RegisterDefaultSniffers();
}
static void android_media_MediaExtractor_native_setup(
JNIEnv *env, jobject thiz, jstring path) {
sp<JMediaExtractor> extractor = new JMediaExtractor(env, thiz);
if (path == NULL) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return;
}
const char *tmp = env->GetStringUTFChars(path, NULL);
if (tmp == NULL) {
return;
}
status_t err = extractor->setDataSource(tmp);
env->ReleaseStringUTFChars(path, tmp);
tmp = NULL;
if (err != OK) {
jniThrowException(
env,
"java/io/IOException",
"Failed to instantiate extractor.");
return;
}
setMediaExtractor(env,thiz, extractor);
}
static void android_media_MediaExtractor_native_finalize(
JNIEnv *env, jobject thiz) {
android_media_MediaExtractor_release(env, thiz);
}
static JNINativeMethod gMethods[] = {
{ "release", "()V", (void *)android_media_MediaExtractor_release },
{ "countTracks", "()I", (void *)android_media_MediaExtractor_countTracks },
{ "getTrackFormat", "(I)Ljava/util/Map;",
(void *)android_media_MediaExtractor_getTrackFormat },
{ "selectTrack", "(I)V", (void *)android_media_MediaExtractor_selectTrack },
{ "seekTo", "(J)V", (void *)android_media_MediaExtractor_seekTo },
{ "advance", "()Z", (void *)android_media_MediaExtractor_advance },
{ "readSampleData", "(Ljava/nio/ByteBuffer;I)I",
(void *)android_media_MediaExtractor_readSampleData },
{ "getSampleTrackIndex", "()I",
(void *)android_media_MediaExtractor_getSampleTrackIndex },
{ "getSampleTime", "()J",
(void *)android_media_MediaExtractor_getSampleTime },
{ "getSampleFlags", "()I",
(void *)android_media_MediaExtractor_getSampleFlags },
{ "native_init", "()V", (void *)android_media_MediaExtractor_native_init },
{ "native_setup", "(Ljava/lang/String;)V",
(void *)android_media_MediaExtractor_native_setup },
{ "native_finalize", "()V",
(void *)android_media_MediaExtractor_native_finalize },
};
int register_android_media_MediaExtractor(JNIEnv *env) {
return AndroidRuntime::registerNativeMethods(env,
"android/media/MediaExtractor", gMethods, NELEM(gMethods));
}
|