@@ -93,7 +93,7 @@ struct BlockIterator {
9393
9494 void advance ();
9595 void reset ();
96- void seek (int64_t seekTimeUs);
96+ void seek (int64_t seekTimeUs, bool seekToKeyFrame );
9797
9898 const mkvparser::Block *block () const ;
9999 int64_t blockTimeUs () const ;
@@ -137,6 +137,7 @@ struct MatroskaSource : public MediaSource {
137137 sp<MatroskaExtractor> mExtractor ;
138138 size_t mTrackIndex ;
139139 Type mType ;
140+ bool mIsAudio ;
140141 BlockIterator mBlockIter ;
141142 size_t mNALSizeLen ; // for type AVC
142143
@@ -156,6 +157,7 @@ MatroskaSource::MatroskaSource(
156157 : mExtractor (extractor),
157158 mTrackIndex (index),
158159 mType(OTHER),
160+ mIsAudio(false ),
159161 mBlockIter(mExtractor .get(),
160162 mExtractor->mTracks.itemAt(index).mTrackNum),
161163 mNALSizeLen(0 ) {
@@ -164,6 +166,8 @@ MatroskaSource::MatroskaSource(
164166 const char *mime;
165167 CHECK (meta->findCString (kKeyMIMEType , &mime));
166168
169+ mIsAudio = !strncasecmp (" audio/" , mime, 6 );
170+
167171 if (!strcasecmp (mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
168172 mType = AVC;
169173
@@ -299,7 +303,7 @@ void BlockIterator::reset() {
299303 } while (!eos () && block ()->GetTrackNumber () != mTrackNum );
300304}
301305
302- void BlockIterator::seek (int64_t seekTimeUs) {
306+ void BlockIterator::seek (int64_t seekTimeUs, bool seekToKeyFrame ) {
303307 Mutex::Autolock autoLock (mExtractor ->mLock );
304308
305309 mCluster = mExtractor ->mSegment ->FindCluster (seekTimeUs * 1000ll );
@@ -311,8 +315,10 @@ void BlockIterator::seek(int64_t seekTimeUs) {
311315 }
312316 while (!eos () && block ()->GetTrackNumber () != mTrackNum );
313317
314- while (!eos () && !mBlockEntry ->GetBlock ()->IsKey ()) {
315- advance_l ();
318+ if (seekToKeyFrame) {
319+ while (!eos () && !mBlockEntry ->GetBlock ()->IsKey ()) {
320+ advance_l ();
321+ }
316322 }
317323}
318324
@@ -396,7 +402,11 @@ status_t MatroskaSource::read(
396402 if (options && options->getSeekTo (&seekTimeUs, &mode)
397403 && !mExtractor ->isLiveStreaming ()) {
398404 clearPendingFrames ();
399- mBlockIter .seek (seekTimeUs);
405+
406+ // Apparently keyframe indication in audio tracks is unreliable,
407+ // fortunately in all our currently supported audio encodings every
408+ // frame is effectively a keyframe.
409+ mBlockIter .seek (seekTimeUs, !mIsAudio );
400410 }
401411
402412again:
@@ -537,6 +547,13 @@ MatroskaExtractor::MatroskaExtractor(const sp<DataSource> &source)
537547 return ;
538548 }
539549
550+ #if 0
551+ const mkvparser::SegmentInfo *info = mSegment->GetInfo();
552+ LOGI("muxing app: %s, writing app: %s",
553+ info->GetMuxingAppAsUTF8(),
554+ info->GetWritingAppAsUTF8());
555+ #endif
556+
540557 addTracks ();
541558}
542559
0 commit comments