1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.media.MediaCodec;
23 import android.media.MediaCodec.BufferInfo;
24 import dalvik.system.CloseGuard;
25 
26 import java.io.FileDescriptor;
27 import java.io.IOException;
28 import java.io.RandomAccessFile;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.nio.ByteBuffer;
32 import java.util.Map;
33 
34 /**
35  * MediaMuxer facilitates muxing elementary streams. Currently MediaMuxer supports MP4, Webm
36  * and 3GP file as the output. It also supports muxing B-frames in MP4 since Android Nougat.
37  * <p>
38  * It is generally used like this:
39  *
40  * <pre>
41  * MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
42  * // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
43  * // or MediaExtractor.getTrackFormat().
44  * MediaFormat audioFormat = new MediaFormat(...);
45  * MediaFormat videoFormat = new MediaFormat(...);
46  * int audioTrackIndex = muxer.addTrack(audioFormat);
47  * int videoTrackIndex = muxer.addTrack(videoFormat);
48  * ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
49  * boolean finished = false;
50  * BufferInfo bufferInfo = new BufferInfo();
51  *
52  * muxer.start();
53  * while(!finished) {
54  *   // getInputBuffer() will fill the inputBuffer with one frame of encoded
55  *   // sample from either MediaCodec or MediaExtractor, set isAudioSample to
56  *   // true when the sample is audio data, set up all the fields of bufferInfo,
57  *   // and return true if there are no more samples.
58  *   finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
59  *   if (!finished) {
60  *     int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
61  *     muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
62  *   }
63  * };
64  * muxer.stop();
65  * muxer.release();
66  * </pre>
67  *
68 
69  <h4>Metadata Track</h4>
70  <p>
71   Per-frame metadata is useful in carrying extra information that correlated with video or audio to
72   facilitate offline processing, e.g. gyro signals from the sensor could help video stabilization when
73   doing offline processing. Metaadata track is only supported in MP4 container. When adding a new
74   metadata track, track's mime format must start with prefix "application/", e.g. "applicaton/gyro".
75   Metadata's format/layout will be defined by the application. Writing metadata is nearly the same as
76   writing video/audio data except that the data will not be from mediacodec. Application just needs
77   to pass the bytebuffer that contains the metadata and also the associated timestamp to the
78   {@link #writeSampleData} api. The timestamp must be in the same time base as video and audio. The
79   generated MP4 file uses TextMetaDataSampleEntry defined in section 12.3.3.2 of the ISOBMFF to signal
80   the metadata's mime format. When using{@link android.media.MediaExtractor} to extract the file with
81   metadata track, the mime format of the metadata will be extracted into {@link android.media.MediaFormat}.
82 
83  <pre class=prettyprint>
84    MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
85    // SetUp Video/Audio Tracks.
86    MediaFormat audioFormat = new MediaFormat(...);
87    MediaFormat videoFormat = new MediaFormat(...);
88    int audioTrackIndex = muxer.addTrack(audioFormat);
89    int videoTrackIndex = muxer.addTrack(videoFormat);
90 
91    // Setup Metadata Track
92    MediaFormat metadataFormat = new MediaFormat(...);
93    metadataFormat.setString(KEY_MIME, "application/gyro");
94    int metadataTrackIndex = muxer.addTrack(metadataFormat);
95 
96    muxer.start();
97    while(..) {
98        // Allocate bytebuffer and write gyro data(x,y,z) into it.
99        ByteBuffer metaData = ByteBuffer.allocate(bufferSize);
100        metaData.putFloat(x);
101        metaData.putFloat(y);
102        metaData.putFloat(z);
103        BufferInfo metaInfo = new BufferInfo();
104        // Associate this metadata with the video frame by setting
105        // the same timestamp as the video frame.
106        metaInfo.presentationTimeUs = currentVideoTrackTimeUs;
107        metaInfo.offset = 0;
108        metaInfo.flags = 0;
109        metaInfo.size = bufferSize;
110        muxer.writeSampleData(metadataTrackIndex, metaData, metaInfo);
111    };
112    muxer.stop();
113    muxer.release();
114  }</pre>
115 
116  <h2 id=History><a name="History"></a>Features and API History</h2>
117  <p>
118  The following table summarizes the feature support in different API version and containers.
119  For API version numbers, see {@link android.os.Build.VERSION_CODES}.
120 
121  <style>
122  .api > tr > th, .api > tr > td { text-align: center; padding: 4px 4px; }
123  .api > tr > th     { vertical-align: bottom; }
124  .api > tr > td     { vertical-align: middle; }
125  .sml > tr > th, .sml > tr > td { text-align: center; padding: 2px 4px; }
126  .fn { text-align: center; }
127  </style>
128 
129  <table align="right" style="width: 0%">
130   <thead>
131    <tbody class=api>
132     <tr><th>Symbol</th>
133     <th>Meaning</th></tr>
134    </tbody>
135   </thead>
136   <tbody class=sml>
137    <tr><td>&#9679;</td><td>Supported</td></tr>
138    <tr><td>&#9675;</td><td>Not supported</td></tr>
139    <tr><td>&#9639;</td><td>Supported in MP4/WebM/3GP</td></tr>
140    <tr><td>&#8277;</td><td>Only Supported in MP4</td></tr>
141   </tbody>
142  </table>
143 <table align="center" style="width: 100%;">
144   <thead class=api>
145    <tr>
146     <th rowspan=2>Feature</th>
147     <th colspan="24">SDK Version</th>
148    </tr>
149    <tr>
150     <th>18</th>
151     <th>19</th>
152     <th>20</th>
153     <th>21</th>
154     <th>22</th>
155     <th>23</th>
156     <th>24</th>
157     <th>25</th>
158     <th>26+</th>
159    </tr>
160   </thead>
161  <tbody class=api>
162    <tr>
163     <td align="center">MP4 container</td>
164     <td>&#9679;</td>
165     <td>&#9679;</td>
166     <td>&#9679;</td>
167     <td>&#9679;</td>
168     <td>&#9679;</td>
169     <td>&#9679;</td>
170     <td>&#9679;</td>
171     <td>&#9679;</td>
172     <td>&#9679;</td>
173    </tr>
174     <td align="center">WebM container</td>
175     <td>&#9675;</td>
176     <td>&#9675;</td>
177     <td>&#9675;</td>
178     <td>&#9679;</td>
179     <td>&#9679;</td>
180     <td>&#9679;</td>
181     <td>&#9679;</td>
182     <td>&#9679;</td>
183     <td>&#9679;</td>
184    </tr>
185     <td align="center">3GP container</td>
186     <td>&#9675;</td>
187     <td>&#9675;</td>
188     <td>&#9675;</td>
189     <td>&#9675;</td>
190     <td>&#9675;</td>
191     <td>&#9675;</td>
192     <td>&#9675;</td>
193     <td>&#9675;</td>
194     <td>&#9679;</td>
195    </tr>
196     <td align="center">Muxing B-Frames(bi-directional predicted frames)</td>
197     <td>&#9675;</td>
198     <td>&#9675;</td>
199     <td>&#9675;</td>
200     <td>&#9675;</td>
201     <td>&#9675;</td>
202     <td>&#9675;</td>
203     <td>&#8277;</td>
204     <td>&#8277;</td>
205     <td>&#8277;</td>
206    </tr>
207    </tr>
208     <td align="center">Muxing Single Video/Audio Track</td>
209     <td>&#9639;</td>
210     <td>&#9639;</td>
211     <td>&#9639;</td>
212     <td>&#9639;</td>
213     <td>&#9639;</td>
214     <td>&#9639;</td>
215     <td>&#9639;</td>
216     <td>&#9639;</td>
217     <td>&#9639;</td>
218    </tr>
219    </tr>
220     <td align="center">Muxing Multiple Video/Audio Tracks</td>
221     <td>&#9675;</td>
222     <td>&#9675;</td>
223     <td>&#9675;</td>
224     <td>&#9675;</td>
225     <td>&#9675;</td>
226     <td>&#9675;</td>
227     <td>&#9675;</td>
228     <td>&#9675;</td>
229     <td>&#8277;</td>
230    </tr>
231    </tr>
232     <td align="center">Muxing Metadata Tracks</td>
233     <td>&#9675;</td>
234     <td>&#9675;</td>
235     <td>&#9675;</td>
236     <td>&#9675;</td>
237     <td>&#9675;</td>
238     <td>&#9675;</td>
239     <td>&#9675;</td>
240     <td>&#9675;</td>
241     <td>&#8277;</td>
242    </tr>
243    </tbody>
244  </table>
245  */
246 
247 final public class MediaMuxer {
248 
249     static {
250         System.loadLibrary("media_jni");
251     }
252 
253     /**
254      * Defines the output format. These constants are used with constructor.
255      */
256     public static final class OutputFormat {
257         /* Do not change these values without updating their counterparts
258          * in include/media/stagefright/MediaMuxer.h!
259          */
OutputFormat()260         private OutputFormat() {}
261         /** MPEG4 media file format*/
262         public static final int MUXER_OUTPUT_MPEG_4 = 0;
263         /** WEBM media file format*/
264         public static final int MUXER_OUTPUT_WEBM   = 1;
265         /** 3GPP media file format*/
266         public static final int MUXER_OUTPUT_3GPP   = 2;
267     };
268 
269     /** @hide */
270     @IntDef({
271         OutputFormat.MUXER_OUTPUT_MPEG_4,
272         OutputFormat.MUXER_OUTPUT_WEBM,
273         OutputFormat.MUXER_OUTPUT_3GPP,
274     })
275     @Retention(RetentionPolicy.SOURCE)
276     public @interface Format {}
277 
278     // All the native functions are listed here.
nativeSetup(@onNull FileDescriptor fd, int format)279     private static native long nativeSetup(@NonNull FileDescriptor fd, int format)
280             throws IllegalArgumentException, IOException;
nativeRelease(long nativeObject)281     private static native void nativeRelease(long nativeObject);
nativeStart(long nativeObject)282     private static native void nativeStart(long nativeObject);
nativeStop(long nativeObject)283     private static native void nativeStop(long nativeObject);
nativeAddTrack( long nativeObject, @NonNull String[] keys, @NonNull Object[] values)284     private static native int nativeAddTrack(
285             long nativeObject, @NonNull String[] keys, @NonNull Object[] values);
nativeSetOrientationHint( long nativeObject, int degrees)286     private static native void nativeSetOrientationHint(
287             long nativeObject, int degrees);
nativeSetLocation(long nativeObject, int latitude, int longitude)288     private static native void nativeSetLocation(long nativeObject, int latitude, int longitude);
nativeWriteSampleData( long nativeObject, int trackIndex, @NonNull ByteBuffer byteBuf, int offset, int size, long presentationTimeUs, @MediaCodec.BufferFlag int flags)289     private static native void nativeWriteSampleData(
290             long nativeObject, int trackIndex, @NonNull ByteBuffer byteBuf,
291             int offset, int size, long presentationTimeUs, @MediaCodec.BufferFlag int flags);
292 
293     // Muxer internal states.
294     private static final int MUXER_STATE_UNINITIALIZED  = -1;
295     private static final int MUXER_STATE_INITIALIZED    = 0;
296     private static final int MUXER_STATE_STARTED        = 1;
297     private static final int MUXER_STATE_STOPPED        = 2;
298 
299     private int mState = MUXER_STATE_UNINITIALIZED;
300 
301     private final CloseGuard mCloseGuard = CloseGuard.get();
302     private int mLastTrackIndex = -1;
303 
304     private long mNativeObject;
305 
306     /**
307      * Constructor.
308      * Creates a media muxer that writes to the specified path.
309      * @param path The path of the output media file.
310      * @param format The format of the output media file.
311      * @see android.media.MediaMuxer.OutputFormat
312      * @throws IllegalArgumentException if path is invalid or format is not supported.
313      * @throws IOException if failed to open the file for write.
314      */
MediaMuxer(@onNull String path, @Format int format)315     public MediaMuxer(@NonNull String path, @Format int format) throws IOException {
316         if (path == null) {
317             throw new IllegalArgumentException("path must not be null");
318         }
319         // Use RandomAccessFile so we can open the file with RW access;
320         // RW access allows the native writer to memory map the output file.
321         RandomAccessFile file = null;
322         try {
323             file = new RandomAccessFile(path, "rws");
324             FileDescriptor fd = file.getFD();
325             setUpMediaMuxer(fd, format);
326         } finally {
327             if (file != null) {
328                 file.close();
329             }
330         }
331     }
332 
333     /**
334      * Constructor.
335      * Creates a media muxer that writes to the specified FileDescriptor. File descriptor
336      * must be seekable and writable. Application should not use the file referenced
337      * by this file descriptor until {@link #stop}. It is the application's responsibility
338      * to close the file descriptor. It is safe to do so as soon as this call returns.
339      * @param fd The FileDescriptor of the output media file.
340      * @param format The format of the output media file.
341      * @see android.media.MediaMuxer.OutputFormat
342      * @throws IllegalArgumentException if fd is invalid or format is not supported.
343      * @throws IOException if failed to open the file for write.
344      */
MediaMuxer(@onNull FileDescriptor fd, @Format int format)345     public MediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException {
346         setUpMediaMuxer(fd, format);
347     }
348 
setUpMediaMuxer(@onNull FileDescriptor fd, @Format int format)349     private void setUpMediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException {
350         if (format != OutputFormat.MUXER_OUTPUT_MPEG_4 && format != OutputFormat.MUXER_OUTPUT_WEBM
351                 && format != OutputFormat.MUXER_OUTPUT_3GPP) {
352             throw new IllegalArgumentException("format: " + format + " is invalid");
353         }
354         mNativeObject = nativeSetup(fd, format);
355         mState = MUXER_STATE_INITIALIZED;
356         mCloseGuard.open("release");
357     }
358 
359     /**
360      * Sets the orientation hint for output video playback.
361      * <p>This method should be called before {@link #start}. Calling this
362      * method will not rotate the video frame when muxer is generating the file,
363      * but add a composition matrix containing the rotation angle in the output
364      * video if the output format is
365      * {@link OutputFormat#MUXER_OUTPUT_MPEG_4} so that a video player can
366      * choose the proper orientation for playback. Note that some video players
367      * may choose to ignore the composition matrix in a video during playback.
368      * By default, the rotation degree is 0.</p>
369      * @param degrees the angle to be rotated clockwise in degrees.
370      * The supported angles are 0, 90, 180, and 270 degrees.
371      * @throws IllegalArgumentException if degree is not supported.
372      * @throws IllegalStateException If this method is called after {@link #start}.
373      */
setOrientationHint(int degrees)374     public void setOrientationHint(int degrees) {
375         if (degrees != 0 && degrees != 90  && degrees != 180 && degrees != 270) {
376             throw new IllegalArgumentException("Unsupported angle: " + degrees);
377         }
378         if (mState == MUXER_STATE_INITIALIZED) {
379             nativeSetOrientationHint(mNativeObject, degrees);
380         } else {
381             throw new IllegalStateException("Can't set rotation degrees due" +
382                     " to wrong state.");
383         }
384     }
385 
386     /**
387      * Set and store the geodata (latitude and longitude) in the output file.
388      * This method should be called before {@link #start}. The geodata is stored
389      * in udta box if the output format is
390      * {@link OutputFormat#MUXER_OUTPUT_MPEG_4}, and is ignored for other output
391      * formats. The geodata is stored according to ISO-6709 standard.
392      *
393      * @param latitude Latitude in degrees. Its value must be in the range [-90,
394      * 90].
395      * @param longitude Longitude in degrees. Its value must be in the range
396      * [-180, 180].
397      * @throws IllegalArgumentException If the given latitude or longitude is out
398      * of range.
399      * @throws IllegalStateException If this method is called after {@link #start}.
400      */
setLocation(float latitude, float longitude)401     public void setLocation(float latitude, float longitude) {
402         int latitudex10000  = (int) (latitude * 10000 + 0.5);
403         int longitudex10000 = (int) (longitude * 10000 + 0.5);
404 
405         if (latitudex10000 > 900000 || latitudex10000 < -900000) {
406             String msg = "Latitude: " + latitude + " out of range.";
407             throw new IllegalArgumentException(msg);
408         }
409         if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
410             String msg = "Longitude: " + longitude + " out of range";
411             throw new IllegalArgumentException(msg);
412         }
413 
414         if (mState == MUXER_STATE_INITIALIZED && mNativeObject != 0) {
415             nativeSetLocation(mNativeObject, latitudex10000, longitudex10000);
416         } else {
417             throw new IllegalStateException("Can't set location due to wrong state.");
418         }
419     }
420 
421     /**
422      * Starts the muxer.
423      * <p>Make sure this is called after {@link #addTrack} and before
424      * {@link #writeSampleData}.</p>
425      * @throws IllegalStateException If this method is called after {@link #start}
426      * or Muxer is released
427      */
start()428     public void start() {
429         if (mNativeObject == 0) {
430             throw new IllegalStateException("Muxer has been released!");
431         }
432         if (mState == MUXER_STATE_INITIALIZED) {
433             nativeStart(mNativeObject);
434             mState = MUXER_STATE_STARTED;
435         } else {
436             throw new IllegalStateException("Can't start due to wrong state.");
437         }
438     }
439 
440     /**
441      * Stops the muxer.
442      * <p>Once the muxer stops, it can not be restarted.</p>
443      * @throws IllegalStateException if muxer is in the wrong state.
444      */
stop()445     public void stop() {
446         if (mState == MUXER_STATE_STARTED) {
447             nativeStop(mNativeObject);
448             mState = MUXER_STATE_STOPPED;
449         } else {
450             throw new IllegalStateException("Can't stop due to wrong state.");
451         }
452     }
453 
454     @Override
finalize()455     protected void finalize() throws Throwable {
456         try {
457             if (mCloseGuard != null) {
458                 mCloseGuard.warnIfOpen();
459             }
460             if (mNativeObject != 0) {
461                 nativeRelease(mNativeObject);
462                 mNativeObject = 0;
463             }
464         } finally {
465             super.finalize();
466         }
467     }
468 
469     /**
470      * Adds a track with the specified format.
471      * <p>
472      * The following table summarizes support for specific format keys across android releases.
473      * Keys marked with '+:' are required.
474      *
475      * <table style="width: 0%">
476      *  <thead>
477      *   <tr>
478      *    <th rowspan=2>OS Version(s)</th>
479      *    <td colspan=3>{@code MediaFormat} keys used for</th>
480      *   </tr><tr>
481      *    <th>All Tracks</th>
482      *    <th>Audio Tracks</th>
483      *    <th>Video Tracks</th>
484      *   </tr>
485      *  </thead>
486      *  <tbody>
487      *   <tr>
488      *    <td>{@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}</td>
489      *    <td rowspan=7>+: {@link MediaFormat#KEY_MIME}</td>
490      *    <td rowspan=3>+: {@link MediaFormat#KEY_SAMPLE_RATE},<br>
491      *        +: {@link MediaFormat#KEY_CHANNEL_COUNT},<br>
492      *        +: <strong>codec-specific data<sup>AAC</sup></strong></td>
493      *    <td rowspan=5>+: {@link MediaFormat#KEY_WIDTH},<br>
494      *        +: {@link MediaFormat#KEY_HEIGHT},<br>
495      *        no {@code KEY_ROTATION},
496      *        use {@link #setOrientationHint setOrientationHint()}<sup>.mp4</sup>,<br>
497      *        +: <strong>codec-specific data<sup>AVC, MPEG4</sup></strong></td>
498      *   </tr><tr>
499      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT}</td>
500      *   </tr><tr>
501      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT_WATCH}</td>
502      *   </tr><tr>
503      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP}</td>
504      *    <td rowspan=4>as above, plus<br>
505      *        +: <strong>codec-specific data<sup>Vorbis & .webm</sup></strong></td>
506      *   </tr><tr>
507      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}</td>
508      *   </tr><tr>
509      *    <td>{@link android.os.Build.VERSION_CODES#M}</td>
510      *    <td>as above, plus<br>
511      *        {@link MediaFormat#KEY_BIT_RATE}<sup>AAC</sup></td>
512      *   </tr><tr>
513      *    <td>{@link android.os.Build.VERSION_CODES#N}</td>
514      *    <td>as above, plus<br>
515      *        <!-- {link MediaFormat#KEY_MAX_BIT_RATE}<sup>AAC, MPEG4</sup>,<br> -->
516      *        {@link MediaFormat#KEY_BIT_RATE}<sup>MPEG4</sup>,<br>
517      *        {@link MediaFormat#KEY_HDR_STATIC_INFO}<sup>#, .webm</sup>,<br>
518      *        {@link MediaFormat#KEY_COLOR_STANDARD}<sup>#</sup>,<br>
519      *        {@link MediaFormat#KEY_COLOR_TRANSFER}<sup>#</sup>,<br>
520      *        {@link MediaFormat#KEY_COLOR_RANGE}<sup>#</sup>,<br>
521      *        +: <strong>codec-specific data<sup>HEVC</sup></strong>,<br>
522      *        codec-specific data<sup>VP9</sup></td>
523      *   </tr>
524      *   <tr>
525      *    <td colspan=4>
526      *     <p class=note><strong>Notes:</strong><br>
527      *      #: storing into container metadata.<br>
528      *      .mp4, .webm&hellip;: for listed containers<br>
529      *      MPEG4, AAC&hellip;: for listed codecs
530      *    </td>
531      *   </tr><tr>
532      *    <td colspan=4>
533      *     <p class=note>Note that the codec-specific data for the track must be specified using
534      *     this method. Furthermore, codec-specific data must not be passed/specified via the
535      *     {@link #writeSampleData writeSampleData()} call.
536      *    </td>
537      *   </tr>
538      *  </tbody>
539      * </table>
540      *
541      * <p>
542      * The following table summarizes codec support for containers across android releases:
543      *
544      * <table style="width: 0%">
545      *  <thead>
546      *   <tr>
547      *    <th rowspan=2>OS Version(s)</th>
548      *    <td colspan=3>Codec support</th>
549      *   </tr><tr>
550      *    <th>{@linkplain OutputFormat#MUXER_OUTPUT_MPEG_4 MP4}</th>
551      *    <th>{@linkplain OutputFormat#MUXER_OUTPUT_WEBM WEBM}</th>
552      *   </tr>
553      *  </thead>
554      *  <tbody>
555      *   <tr>
556      *    <td>{@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}</td>
557      *    <td rowspan=6>{@link MediaFormat#MIMETYPE_AUDIO_AAC AAC},<br>
558      *        {@link MediaFormat#MIMETYPE_AUDIO_AMR_NB NB-AMR},<br>
559      *        {@link MediaFormat#MIMETYPE_AUDIO_AMR_WB WB-AMR},<br>
560      *        {@link MediaFormat#MIMETYPE_VIDEO_H263 H.263},<br>
561      *        {@link MediaFormat#MIMETYPE_VIDEO_MPEG4 MPEG-4},<br>
562      *        {@link MediaFormat#MIMETYPE_VIDEO_AVC AVC} (H.264)</td>
563      *    <td rowspan=3>Not supported</td>
564      *   </tr><tr>
565      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT}</td>
566      *   </tr><tr>
567      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT_WATCH}</td>
568      *   </tr><tr>
569      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP}</td>
570      *    <td rowspan=3>{@link MediaFormat#MIMETYPE_AUDIO_VORBIS Vorbis},<br>
571      *        {@link MediaFormat#MIMETYPE_VIDEO_VP8 VP8}</td>
572      *   </tr><tr>
573      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}</td>
574      *   </tr><tr>
575      *    <td>{@link android.os.Build.VERSION_CODES#M}</td>
576      *   </tr><tr>
577      *    <td>{@link android.os.Build.VERSION_CODES#N}</td>
578      *    <td>as above, plus<br>
579      *        {@link MediaFormat#MIMETYPE_VIDEO_HEVC HEVC} (H.265)</td>
580      *    <td>as above, plus<br>
581      *        {@link MediaFormat#MIMETYPE_VIDEO_VP9 VP9}</td>
582      *   </tr>
583      *  </tbody>
584      * </table>
585      *
586      * @param format The media format for the track.  This must not be an empty
587      *               MediaFormat.
588      * @return The track index for this newly added track, and it should be used
589      * in the {@link #writeSampleData}.
590      * @throws IllegalArgumentException if format is invalid.
591      * @throws IllegalStateException if muxer is in the wrong state.
592      */
addTrack(@onNull MediaFormat format)593     public int addTrack(@NonNull MediaFormat format) {
594         if (format == null) {
595             throw new IllegalArgumentException("format must not be null.");
596         }
597         if (mState != MUXER_STATE_INITIALIZED) {
598             throw new IllegalStateException("Muxer is not initialized.");
599         }
600         if (mNativeObject == 0) {
601             throw new IllegalStateException("Muxer has been released!");
602         }
603         int trackIndex = -1;
604         // Convert the MediaFormat into key-value pairs and send to the native.
605         Map<String, Object> formatMap = format.getMap();
606 
607         String[] keys = null;
608         Object[] values = null;
609         int mapSize = formatMap.size();
610         if (mapSize > 0) {
611             keys = new String[mapSize];
612             values = new Object[mapSize];
613             int i = 0;
614             for (Map.Entry<String, Object> entry : formatMap.entrySet()) {
615                 keys[i] = entry.getKey();
616                 values[i] = entry.getValue();
617                 ++i;
618             }
619             trackIndex = nativeAddTrack(mNativeObject, keys, values);
620         } else {
621             throw new IllegalArgumentException("format must not be empty.");
622         }
623 
624         // Track index number is expected to incremented as addTrack succeed.
625         // However, if format is invalid, it will get a negative trackIndex.
626         if (mLastTrackIndex >= trackIndex) {
627             throw new IllegalArgumentException("Invalid format.");
628         }
629         mLastTrackIndex = trackIndex;
630         return trackIndex;
631     }
632 
633     /**
634      * Writes an encoded sample into the muxer.
635      * <p>The application needs to make sure that the samples are written into
636      * the right tracks. Also, it needs to make sure the samples for each track
637      * are written in chronological order (e.g. in the order they are provided
638      * by the encoder.)</p>
639      * @param byteBuf The encoded sample.
640      * @param trackIndex The track index for this sample.
641      * @param bufferInfo The buffer information related to this sample.
642      * @throws IllegalArgumentException if trackIndex, byteBuf or bufferInfo is  invalid.
643      * @throws IllegalStateException if muxer is in wrong state.
644      * MediaMuxer uses the flags provided in {@link MediaCodec.BufferInfo},
645      * to signal sync frames.
646      */
writeSampleData(int trackIndex, @NonNull ByteBuffer byteBuf, @NonNull BufferInfo bufferInfo)647     public void writeSampleData(int trackIndex, @NonNull ByteBuffer byteBuf,
648             @NonNull BufferInfo bufferInfo) {
649         if (trackIndex < 0 || trackIndex > mLastTrackIndex) {
650             throw new IllegalArgumentException("trackIndex is invalid");
651         }
652 
653         if (byteBuf == null) {
654             throw new IllegalArgumentException("byteBuffer must not be null");
655         }
656 
657         if (bufferInfo == null) {
658             throw new IllegalArgumentException("bufferInfo must not be null");
659         }
660         if (bufferInfo.size < 0 || bufferInfo.offset < 0
661                 || (bufferInfo.offset + bufferInfo.size) > byteBuf.capacity()
662                 || bufferInfo.presentationTimeUs < 0) {
663             throw new IllegalArgumentException("bufferInfo must specify a" +
664                     " valid buffer offset, size and presentation time");
665         }
666 
667         if (mNativeObject == 0) {
668             throw new IllegalStateException("Muxer has been released!");
669         }
670 
671         if (mState != MUXER_STATE_STARTED) {
672             throw new IllegalStateException("Can't write, muxer is not started");
673         }
674 
675         nativeWriteSampleData(mNativeObject, trackIndex, byteBuf,
676                 bufferInfo.offset, bufferInfo.size,
677                 bufferInfo.presentationTimeUs, bufferInfo.flags);
678     }
679 
680     /**
681      * Make sure you call this when you're done to free up any resources
682      * instead of relying on the garbage collector to do this for you at
683      * some point in the future.
684      */
release()685     public void release() {
686         if (mState == MUXER_STATE_STARTED) {
687             stop();
688         }
689         if (mNativeObject != 0) {
690             nativeRelease(mNativeObject);
691             mNativeObject = 0;
692             mCloseGuard.close();
693         }
694         mState = MUXER_STATE_UNINITIALIZED;
695     }
696 }
697