change onData() to take byte array of raw data

conversion to UTF-8 if necessary will be handled by the renderer.

Bug: 15470448
Change-Id: I6d4a5e29e6af07f5c031197ea424f8f8e53cd05b
This commit is contained in:
Chong Zhang
2014-06-11 15:09:13 -07:00
parent 54f6e80d32
commit 079fa9683d
4 changed files with 86 additions and 52 deletions

View File

@@ -1649,8 +1649,8 @@ public class MediaPlayer implements SubtitleController.Listener
mFormat = MediaFormat.createSubtitleFormat(
MEDIA_MIMETYPE_TEXT_SUBRIP, language);
} else if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) {
mFormat = MediaFormat.createSubtitleFormat(
MEDIA_MIMETYPE_TEXT_VTT, language);
String mime = in.readString();
mFormat = MediaFormat.createSubtitleFormat(mime, language);
mFormat.setInteger(MediaFormat.KEY_IS_AUTOSELECT, in.readInt());
mFormat.setInteger(MediaFormat.KEY_IS_DEFAULT, in.readInt());
mFormat.setInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE, in.readInt());
@@ -1683,12 +1683,40 @@ public class MediaPlayer implements SubtitleController.Listener
dest.writeString(getLanguage());
if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) {
dest.writeString(mFormat.getString(MediaFormat.KEY_MIME));
dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_AUTOSELECT));
dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_DEFAULT));
dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE));
}
}
@Override
public String toString() {
StringBuilder out = new StringBuilder(128);
out.append(getClass().getName());
out.append('{');
switch (mTrackType) {
case MEDIA_TRACK_TYPE_VIDEO:
out.append("VIDEO");
break;
case MEDIA_TRACK_TYPE_AUDIO:
out.append("AUDIO");
break;
case MEDIA_TRACK_TYPE_TIMEDTEXT:
out.append("TIMEDTEXT");
break;
case MEDIA_TRACK_TYPE_SUBTITLE:
out.append("SUBTITLE");
break;
default:
out.append("UNKNOWN");
break;
}
out.append(", " + mFormat.toString());
out.append("}");
return out.toString();
}
/**
* Used to read a TrackInfo from a Parcel.
*/
@@ -1792,16 +1820,11 @@ public class MediaPlayer implements SubtitleController.Listener
}
SubtitleTrack track = mInbandSubtitleTracks[index];
if (track != null) {
try {
long runID = data.getStartTimeUs() + 1;
// TODO: move conversion into track
track.onData(new String(data.getData(), "UTF-8"), true /* eos */, runID);
track.setRunDiscardTimeMs(
runID,
(data.getStartTimeUs() + data.getDurationUs()) / 1000);
} catch (java.io.UnsupportedEncodingException e) {
Log.w(TAG, "subtitle data for track " + index + " is not UTF-8 encoded: " + e);
}
long runID = data.getStartTimeUs() + 1;
track.onData(data.getData(), true /* eos */, runID);
track.setRunDiscardTimeMs(
runID,
(data.getStartTimeUs() + data.getDurationUs()) / 1000);
}
}
};
@@ -1872,7 +1895,7 @@ public class MediaPlayer implements SubtitleController.Listener
}
scanner.close();
mOutOfBandSubtitleTracks.add(track);
track.onData(contents, true /* eos */, ~0 /* runID: keep forever */);
track.onData(contents.getBytes(), true /* eos */, ~0 /* runID: keep forever */);
return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
}

View File

@@ -83,7 +83,7 @@ public abstract class SubtitleTrack implements MediaTimeProvider.OnMediaTimeList
* indicating the last section of the run. Calls from different
* runs must not be intermixed.
*
* @param data
* @param data subtitle data byte buffer
* @param eos true if this is the last section of the run.
* @param runID mostly-unique ID for this run of data. Subtitle cues
* with runID of 0 are discarded immediately after
@@ -92,10 +92,8 @@ public abstract class SubtitleTrack implements MediaTimeProvider.OnMediaTimeList
* with other runID-s are discarded at the end of the
* run, which defaults to the latest timestamp of
* any of its cues (with this runID).
*
* TODO use ByteBuffer
*/
public abstract void onData(String data, boolean eos, long runID);
public abstract void onData(byte[] data, boolean eos, long runID);
/**
* Called when adding the subtitle rendering widget to the view hierarchy,

View File

@@ -563,28 +563,35 @@ class TtmlTrack extends SubtitleTrack implements TtmlNodeListener {
}
@Override
public void onData(String data, boolean eos, long runID) {
// implement intermixing restriction for TTML.
synchronized(mParser) {
if (mCurrentRunID != null && runID != mCurrentRunID) {
throw new IllegalStateException(
"Run #" + mCurrentRunID +
" in progress. Cannot process run #" + runID);
}
mCurrentRunID = runID;
mParsingData += data;
if (eos) {
try {
mParser.parse(mParsingData, mCurrentRunID);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
public void onData(byte[] data, boolean eos, long runID) {
try {
// TODO: handle UTF-8 conversion properly
String str = new String(data, "UTF-8");
// implement intermixing restriction for TTML.
synchronized(mParser) {
if (mCurrentRunID != null && runID != mCurrentRunID) {
throw new IllegalStateException(
"Run #" + mCurrentRunID +
" in progress. Cannot process run #" + runID);
}
mCurrentRunID = runID;
mParsingData += str;
if (eos) {
try {
mParser.parse(mParsingData, mCurrentRunID);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finishedRun(runID);
mParsingData = "";
mCurrentRunID = null;
}
finishedRun(runID);
mParsingData = "";
mCurrentRunID = null;
}
} catch (java.io.UnsupportedEncodingException e) {
Log.w(TAG, "subtitle data is not UTF-8 encoded: " + e);
}
}

View File

@@ -1001,22 +1001,28 @@ class WebVttTrack extends SubtitleTrack implements WebVttCueListener {
}
@Override
public void onData(String data, boolean eos, long runID) {
// implement intermixing restriction for WebVTT only for now
synchronized(mParser) {
if (mCurrentRunID != null && runID != mCurrentRunID) {
throw new IllegalStateException(
"Run #" + mCurrentRunID +
" in progress. Cannot process run #" + runID);
}
mCurrentRunID = runID;
mParser.parse(data);
if (eos) {
finishedRun(runID);
mParser.eos();
mRegions.clear();
mCurrentRunID = null;
public void onData(byte[] data, boolean eos, long runID) {
try {
String str = new String(data, "UTF-8");
// implement intermixing restriction for WebVTT only for now
synchronized(mParser) {
if (mCurrentRunID != null && runID != mCurrentRunID) {
throw new IllegalStateException(
"Run #" + mCurrentRunID +
" in progress. Cannot process run #" + runID);
}
mCurrentRunID = runID;
mParser.parse(str);
if (eos) {
finishedRun(runID);
mParser.eos();
mRegions.clear();
mCurrentRunID = null;
}
}
} catch (java.io.UnsupportedEncodingException e) {
Log.w(TAG, "subtitle data is not UTF-8 encoded: " + e);
}
}