MPEG2TSExtractor: support m2ts container
m2ts is based on the MPEG-2 transport stream container.
The standard MPEG2-2 TS 188-byte packet is prefixed with a
4-byte extra header to a total size of 192 bytes.
This patch makes MPEG2TSextractor support both
MPEG-2 transport stream and m2ts.
mts file use the same container as ts file
Bug: 123261411
Test: play m2ts file and check if it's normal
Change-Id: I7798a6bf3fcddf064d6bb6e39cd303bf83c3489e
(cherry picked from commit c1a464670859ecacf6c84aff855e2686fd18302b)
diff --git a/media/extractors/mpeg2/ExtractorBundle.cpp b/media/extractors/mpeg2/ExtractorBundle.cpp
index 946a2a9..a7c756b 100644
--- a/media/extractors/mpeg2/ExtractorBundle.cpp
+++ b/media/extractors/mpeg2/ExtractorBundle.cpp
@@ -29,6 +29,8 @@
static const char *extensions[] = {
"m2p",
+ "m2ts",
+ "mts",
"ts",
NULL
};
diff --git a/media/extractors/mpeg2/MPEG2TSExtractor.cpp b/media/extractors/mpeg2/MPEG2TSExtractor.cpp
index 49dd0b4..50ce657 100644
--- a/media/extractors/mpeg2/MPEG2TSExtractor.cpp
+++ b/media/extractors/mpeg2/MPEG2TSExtractor.cpp
@@ -238,6 +238,12 @@
mParser(new ATSParser),
mLastSyncEvent(0),
mOffset(0) {
+ char header;
+ if (source->readAt(0, &header, 1) == 1 && header == 0x47) {
+ mHeaderSkip = 0;
+ } else {
+ mHeaderSkip = 4;
+ }
init();
}
@@ -460,7 +466,7 @@
Mutex::Autolock autoLock(mLock);
uint8_t packet[kTSPacketSize];
- ssize_t n = mDataSource->readAt(mOffset, packet, kTSPacketSize);
+ ssize_t n = mDataSource->readAt(mOffset + mHeaderSkip, packet, kTSPacketSize);
if (n < (ssize_t)kTSPacketSize) {
if (n >= 0) {
@@ -470,7 +476,7 @@
}
ATSParser::SyncEvent event(mOffset);
- mOffset += n;
+ mOffset += mHeaderSkip + n;
status_t err = mParser->feedTSPacket(packet, kTSPacketSize, &event);
if (event.hasReturnedData()) {
if (isInit) {
@@ -539,15 +545,15 @@
break;
}
- ssize_t n = mDataSource->readAt(offset, packet, kTSPacketSize);
+ ssize_t n = mDataSource->readAt(offset+mHeaderSkip, packet, kTSPacketSize);
if (n < 0) {
return n;
} else if (n < (ssize_t)kTSPacketSize) {
break;
}
- offset += kTSPacketSize;
- bytesRead += kTSPacketSize;
+ offset += kTSPacketSize + mHeaderSkip;
+ bytesRead += kTSPacketSize + mHeaderSkip;
err = parser->feedTSPacket(packet, kTSPacketSize, &ev);
if (err != OK) {
return err;
@@ -791,7 +797,17 @@
char header;
if (source->readAt(kTSPacketSize * i, &header, 1) != 1
|| header != 0x47) {
- return false;
+ // not ts file, check if m2ts file
+ for (int j = 0; j < 5; ++j) {
+ char headers[5];
+ if (source->readAt((kTSPacketSize + 4) * j, &headers, 5) != 5
+ || headers[4] != 0x47) {
+ // not m2ts file too, return
+ return false;
+ }
+ }
+ ALOGV("this is m2ts file\n");
+ break;
}
}
diff --git a/media/extractors/mpeg2/MPEG2TSExtractor.h b/media/extractors/mpeg2/MPEG2TSExtractor.h
index 2537d3b..dcd1e7b 100644
--- a/media/extractors/mpeg2/MPEG2TSExtractor.h
+++ b/media/extractors/mpeg2/MPEG2TSExtractor.h
@@ -102,6 +102,7 @@
status_t estimateDurationsFromTimesUsAtEnd();
+ size_t mHeaderSkip;
DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSExtractor);
};