MediaMuxerpublic final class MediaMuxer extends Object MediaMuxer facilitates muxing elementary streams. Currently only supports an
mp4 file as the output and at most one audio and/or one video elementary
stream.
It is generally used like this:
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
// More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
// or MediaExtractor.getTrackFormat().
MediaFormat audioFormat = new MediaFormat(...);
MediaFormat videoFormat = new MediaFormat(...);
int audioTrackIndex = muxer.addTrack(audioFormat);
int videoTrackIndex = muxer.addTrack(videoFormat);
ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
boolean finished = false;
BufferInfo bufferInfo = new BufferInfo();
muxer.start();
while(!finished) {
// getInputBuffer() will fill the inputBuffer with one frame of encoded
// sample from either MediaCodec or MediaExtractor, set isAudioSample to
// true when the sample is audio data, set up all the fields of bufferInfo,
// and return true if there are no more samples.
finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
if (!finished) {
int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
}
};
muxer.stop();
muxer.release();
|
Fields Summary |
---|
private static final int | MUXER_STATE_UNINITIALIZED | private static final int | MUXER_STATE_INITIALIZED | private static final int | MUXER_STATE_STARTED | private static final int | MUXER_STATE_STOPPED | private int | mState | private final dalvik.system.CloseGuard | mCloseGuard | private int | mLastTrackIndex | private long | mNativeObject |
Constructors Summary |
---|
public MediaMuxer(String path, int format)Constructor.
Creates a media muxer that writes to the specified path.
if (path == null) {
throw new IllegalArgumentException("path must not be null");
}
if (format != OutputFormat.MUXER_OUTPUT_MPEG_4 &&
format != OutputFormat.MUXER_OUTPUT_WEBM) {
throw new IllegalArgumentException("format is invalid");
}
// Use RandomAccessFile so we can open the file with RW access;
// RW access allows the native writer to memory map the output file.
RandomAccessFile file = null;
try {
file = new RandomAccessFile(path, "rws");
FileDescriptor fd = file.getFD();
mNativeObject = nativeSetup(fd, format);
mState = MUXER_STATE_INITIALIZED;
mCloseGuard.open("release");
} finally {
if (file != null) {
file.close();
}
}
|
Methods Summary |
---|
public int | addTrack(MediaFormat format)Adds a track with the specified format.
if (format == null) {
throw new IllegalArgumentException("format must not be null.");
}
if (mState != MUXER_STATE_INITIALIZED) {
throw new IllegalStateException("Muxer is not initialized.");
}
if (mNativeObject == 0) {
throw new IllegalStateException("Muxer has been released!");
}
int trackIndex = -1;
// Convert the MediaFormat into key-value pairs and send to the native.
Map<String, Object> formatMap = format.getMap();
String[] keys = null;
Object[] values = null;
int mapSize = formatMap.size();
if (mapSize > 0) {
keys = new String[mapSize];
values = new Object[mapSize];
int i = 0;
for (Map.Entry<String, Object> entry : formatMap.entrySet()) {
keys[i] = entry.getKey();
values[i] = entry.getValue();
++i;
}
trackIndex = nativeAddTrack(mNativeObject, keys, values);
} else {
throw new IllegalArgumentException("format must not be empty.");
}
// Track index number is expected to incremented as addTrack succeed.
// However, if format is invalid, it will get a negative trackIndex.
if (mLastTrackIndex >= trackIndex) {
throw new IllegalArgumentException("Invalid format.");
}
mLastTrackIndex = trackIndex;
return trackIndex;
| protected void | finalize()
try {
if (mCloseGuard != null) {
mCloseGuard.warnIfOpen();
}
if (mNativeObject != 0) {
nativeRelease(mNativeObject);
mNativeObject = 0;
}
} finally {
super.finalize();
}
| private static native int | nativeAddTrack(long nativeObject, java.lang.String[] keys, java.lang.Object[] values)
| private static native void | nativeRelease(long nativeObject)
| private static native void | nativeSetLocation(long nativeObject, int latitude, int longitude)
| private static native void | nativeSetOrientationHint(long nativeObject, int degrees)
| private static native long | nativeSetup(java.io.FileDescriptor fd, int format)
| private static native void | nativeStart(long nativeObject)
| private static native void | nativeStop(long nativeObject)
| private static native void | nativeWriteSampleData(long nativeObject, int trackIndex, java.nio.ByteBuffer byteBuf, int offset, int size, long presentationTimeUs, int flags)
| public void | release()Make sure you call this when you're done to free up any resources
instead of relying on the garbage collector to do this for you at
some point in the future.
if (mState == MUXER_STATE_STARTED) {
stop();
}
if (mNativeObject != 0) {
nativeRelease(mNativeObject);
mNativeObject = 0;
mCloseGuard.close();
}
mState = MUXER_STATE_UNINITIALIZED;
| public void | setLocation(float latitude, float longitude)Set and store the geodata (latitude and longitude) in the output file.
This method should be called before {@link #start}. The geodata is stored
in udta box if the output format is
{@link OutputFormat#MUXER_OUTPUT_MPEG_4}, and is ignored for other output
formats. The geodata is stored according to ISO-6709 standard.
int latitudex10000 = (int) (latitude * 10000 + 0.5);
int longitudex10000 = (int) (longitude * 10000 + 0.5);
if (latitudex10000 > 900000 || latitudex10000 < -900000) {
String msg = "Latitude: " + latitude + " out of range.";
throw new IllegalArgumentException(msg);
}
if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
String msg = "Longitude: " + longitude + " out of range";
throw new IllegalArgumentException(msg);
}
if (mState == MUXER_STATE_INITIALIZED && mNativeObject != 0) {
nativeSetLocation(mNativeObject, latitudex10000, longitudex10000);
} else {
throw new IllegalStateException("Can't set location due to wrong state.");
}
| public void | setOrientationHint(int degrees)Sets the orientation hint for output video playback.
This method should be called before {@link #start}. Calling this
method will not rotate the video frame when muxer is generating the file,
but add a composition matrix containing the rotation angle in the output
video if the output format is
{@link OutputFormat#MUXER_OUTPUT_MPEG_4} so that a video player can
choose the proper orientation for playback. Note that some video players
may choose to ignore the composition matrix in a video during playback.
By default, the rotation degree is 0.
if (degrees != 0 && degrees != 90 && degrees != 180 && degrees != 270) {
throw new IllegalArgumentException("Unsupported angle: " + degrees);
}
if (mState == MUXER_STATE_INITIALIZED) {
nativeSetOrientationHint(mNativeObject, degrees);
} else {
throw new IllegalStateException("Can't set rotation degrees due" +
" to wrong state.");
}
| public void | start()Starts the muxer.
Make sure this is called after {@link #addTrack} and before
{@link #writeSampleData}.
if (mNativeObject == 0) {
throw new IllegalStateException("Muxer has been released!");
}
if (mState == MUXER_STATE_INITIALIZED) {
nativeStart(mNativeObject);
mState = MUXER_STATE_STARTED;
} else {
throw new IllegalStateException("Can't start due to wrong state.");
}
| public void | stop()Stops the muxer.
Once the muxer stops, it can not be restarted.
if (mState == MUXER_STATE_STARTED) {
nativeStop(mNativeObject);
mState = MUXER_STATE_STOPPED;
} else {
throw new IllegalStateException("Can't stop due to wrong state.");
}
| public void | writeSampleData(int trackIndex, java.nio.ByteBuffer byteBuf, android.media.MediaCodec.BufferInfo bufferInfo)Writes an encoded sample into the muxer.
The application needs to make sure that the samples are written into
the right tracks. Also, it needs to make sure the samples for each track
are written in chronological order (e.g. in the order they are provided
by the encoder.)
if (trackIndex < 0 || trackIndex > mLastTrackIndex) {
throw new IllegalArgumentException("trackIndex is invalid");
}
if (byteBuf == null) {
throw new IllegalArgumentException("byteBuffer must not be null");
}
if (bufferInfo == null) {
throw new IllegalArgumentException("bufferInfo must not be null");
}
if (bufferInfo.size < 0 || bufferInfo.offset < 0
|| (bufferInfo.offset + bufferInfo.size) > byteBuf.capacity()
|| bufferInfo.presentationTimeUs < 0) {
throw new IllegalArgumentException("bufferInfo must specify a" +
" valid buffer offset, size and presentation time");
}
if (mNativeObject == 0) {
throw new IllegalStateException("Muxer has been released!");
}
if (mState != MUXER_STATE_STARTED) {
throw new IllegalStateException("Can't write, muxer is not started");
}
nativeWriteSampleData(mNativeObject, trackIndex, byteBuf,
bufferInfo.offset, bufferInfo.size,
bufferInfo.presentationTimeUs, bufferInfo.flags);
|
|