Skip to content

Commit 129a6cc

Browse files
theandi666Android (Google) Code Review
authored andcommitted
Merge "The MediaExtractor can now unselect tracks and has more control over seeking." into jb-dev
2 parents 7797416 + f2855b3 commit 129a6cc

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

api/current.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11076,15 +11076,20 @@ package android.media {
1107611076
method public boolean hasCacheReachedEndOfStream();
1107711077
method public int readSampleData(java.nio.ByteBuffer, int);
1107811078
method public final void release();
11079-
method public void seekTo(long);
11079+
method public void seekTo(long, int);
1108011080
method public void selectTrack(int);
1108111081
method public final void setDataSource(android.content.Context, android.net.Uri, java.util.Map<java.lang.String, java.lang.String>) throws java.io.IOException;
1108211082
method public final void setDataSource(java.lang.String, java.util.Map<java.lang.String, java.lang.String>);
1108311083
method public final void setDataSource(java.lang.String);
1108411084
method public final void setDataSource(java.io.FileDescriptor);
1108511085
method public final void setDataSource(java.io.FileDescriptor, long, long);
11086+
method public void unselectTrack(int);
1108611087
field public static final int SAMPLE_FLAG_ENCRYPTED = 2; // 0x2
1108711088
field public static final int SAMPLE_FLAG_SYNC = 1; // 0x1
11089+
field public static final int SEEK_TO_CLOSEST = 3; // 0x3
11090+
field public static final int SEEK_TO_CLOSEST_SYNC = 2; // 0x2
11091+
field public static final int SEEK_TO_NEXT_SYNC = 1; // 0x1
11092+
field public static final int SEEK_TO_PREVIOUS_SYNC = 0; // 0x0
1108811093
}
1108911094

1109011095
public class MediaMetadataRetriever {

media/java/android/media/MediaExtractor.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,33 @@ protected void finalize() {
191191

192192
/** Subsequent calls to {@link #readSampleData}, {@link #getSampleTrackIndex} and
193193
* {@link #getSampleTime} only retrieve information for the subset of tracks
194-
* selected by the call below.
195-
* Selecting the same track multiple times has no effect, the track
194+
* selected.
195+
* Selecting the same track multiple times has no effect, the track is
196196
* only selected once.
197-
* Media data will be returned in the order of their timestamps.
198197
*/
199198
public native void selectTrack(int index);
200199

201-
/** All selected tracks seek near the requested time. The next sample
202-
* returned for each selected track will be a sync sample.
200+
/** Subsequent calls to {@link #readSampleData}, {@link #getSampleTrackIndex} and
201+
* {@link #getSampleTime} only retrieve information for the subset of tracks
202+
* selected.
203203
*/
204-
public native void seekTo(long timeUs);
204+
public native void unselectTrack(int index);
205+
206+
/** If possible, seek to a sync sample at or before the specified time */
207+
public static final int SEEK_TO_PREVIOUS_SYNC = 0;
208+
/** If possible, seek to a sync sample at or after the specified time */
209+
public static final int SEEK_TO_NEXT_SYNC = 1;
210+
/** If possible, seek to the sync sample closest to the specified time */
211+
public static final int SEEK_TO_CLOSEST_SYNC = 2;
212+
/** If possible, seek to a sample closest to the specified time, which may
213+
* NOT be a sync sample!
214+
*/
215+
public static final int SEEK_TO_CLOSEST = 3;
216+
217+
/** All selected tracks seek near the requested time according to the
218+
* specified mode.
219+
*/
220+
public native void seekTo(long timeUs, int mode);
205221

206222
/** Advance to the next sample. Returns false if no more sample data
207223
* is available (end of stream).

media/jni/android_media_MediaExtractor.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,13 @@ status_t JMediaExtractor::selectTrack(size_t index) {
9696
return mImpl->selectTrack(index);
9797
}
9898

99-
status_t JMediaExtractor::seekTo(int64_t timeUs) {
100-
return mImpl->seekTo(timeUs);
99+
status_t JMediaExtractor::unselectTrack(size_t index) {
100+
return mImpl->unselectTrack(index);
101+
}
102+
103+
status_t JMediaExtractor::seekTo(
104+
int64_t timeUs, MediaSource::ReadOptions::SeekMode mode) {
105+
return mImpl->seekTo(timeUs, mode);
101106
}
102107

103108
status_t JMediaExtractor::advance() {
@@ -281,16 +286,39 @@ static void android_media_MediaExtractor_selectTrack(
281286
}
282287
}
283288

289+
static void android_media_MediaExtractor_unselectTrack(
290+
JNIEnv *env, jobject thiz, jint index) {
291+
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
292+
293+
if (extractor == NULL) {
294+
jniThrowException(env, "java/lang/IllegalStateException", NULL);
295+
return;
296+
}
297+
298+
status_t err = extractor->unselectTrack(index);
299+
300+
if (err != OK) {
301+
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
302+
return;
303+
}
304+
}
305+
284306
static void android_media_MediaExtractor_seekTo(
285-
JNIEnv *env, jobject thiz, jlong timeUs) {
307+
JNIEnv *env, jobject thiz, jlong timeUs, jint mode) {
286308
sp<JMediaExtractor> extractor = getMediaExtractor(env, thiz);
287309

288310
if (extractor == NULL) {
289311
jniThrowException(env, "java/lang/IllegalStateException", NULL);
290312
return;
291313
}
292314

293-
extractor->seekTo(timeUs);
315+
if (mode < MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC
316+
|| mode > MediaSource::ReadOptions::SEEK_CLOSEST) {
317+
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
318+
return;
319+
}
320+
321+
extractor->seekTo(timeUs, (MediaSource::ReadOptions::SeekMode)mode);
294322
}
295323

296324
static jboolean android_media_MediaExtractor_advance(
@@ -648,7 +676,10 @@ static JNINativeMethod gMethods[] = {
648676

649677
{ "selectTrack", "(I)V", (void *)android_media_MediaExtractor_selectTrack },
650678

651-
{ "seekTo", "(J)V", (void *)android_media_MediaExtractor_seekTo },
679+
{ "unselectTrack", "(I)V",
680+
(void *)android_media_MediaExtractor_unselectTrack },
681+
682+
{ "seekTo", "(JI)V", (void *)android_media_MediaExtractor_seekTo },
652683

653684
{ "advance", "()Z", (void *)android_media_MediaExtractor_advance },
654685

media/jni/android_media_MediaExtractor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define _ANDROID_MEDIA_MEDIAEXTRACTOR_H_
1919

2020
#include <media/stagefright/foundation/ABase.h>
21+
#include <media/stagefright/MediaSource.h>
2122
#include <utils/Errors.h>
2223
#include <utils/KeyedVector.h>
2324
#include <utils/RefBase.h>
@@ -43,8 +44,9 @@ struct JMediaExtractor : public RefBase {
4344
status_t getTrackFormat(size_t index, jobject *format) const;
4445

4546
status_t selectTrack(size_t index);
47+
status_t unselectTrack(size_t index);
4648

47-
status_t seekTo(int64_t timeUs);
49+
status_t seekTo(int64_t timeUs, MediaSource::ReadOptions::SeekMode mode);
4850

4951
status_t advance();
5052
status_t readSampleData(jobject byteBuf, size_t offset, size_t *sampleSize);

0 commit comments

Comments
 (0)