• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SuppressLint;
22 import android.annotation.TestApi;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.media.MediaCodec.BufferInfo;
25 import android.os.Build;
26 
27 import dalvik.system.CloseGuard;
28 
29 import java.io.FileDescriptor;
30 import java.io.IOException;
31 import java.io.RandomAccessFile;
32 import java.lang.annotation.Retention;
33 import java.lang.annotation.RetentionPolicy;
34 import java.nio.ByteBuffer;
35 import java.util.Map;
36 
37 /**
38  * MediaMuxer facilitates muxing elementary streams. Currently MediaMuxer supports MP4, Webm
39  * and 3GP file as the output. It also supports muxing B-frames in MP4 since Android Nougat MR1.
40  * <p>
41  * It is generally used like this:
42  *
43  * <pre>
44  * MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
45  * // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
46  * // or MediaExtractor.getTrackFormat().
47  * MediaFormat audioFormat = new MediaFormat(...);
48  * MediaFormat videoFormat = new MediaFormat(...);
49  * int audioTrackIndex = muxer.addTrack(audioFormat);
50  * int videoTrackIndex = muxer.addTrack(videoFormat);
51  * ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
52  * boolean finished = false;
53  * BufferInfo bufferInfo = new BufferInfo();
54  *
55  * muxer.start();
56  * while(!finished) {
57  *   // getInputBuffer() will fill the inputBuffer with one frame of encoded
58  *   // sample from either MediaCodec or MediaExtractor, set isAudioSample to
59  *   // true when the sample is audio data, set up all the fields of bufferInfo,
60  *   // and return true if there are no more samples.
61  *   finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
62  *   if (!finished) {
63  *     int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
64  *     muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
65  *   }
66  * };
67  * muxer.stop();
68  * muxer.release();
69  * </pre>
70  *
71 
72  <h4>Metadata Track</h4>
73  <p>
74   Per-frame metadata carries information that correlates with video or audio to facilitate offline
75   processing. For example, gyro signals from the sensor can help video stabilization when doing
76   offline processing. Metadata tracks are only supported when multiplexing to the MP4 container
77   format. When adding a new metadata track, the MIME type format must start with prefix
78   "application/" (for example, "application/gyro"). The format of the metadata is
79   application-defined. Metadata timestamps must be in the same time base as video and audio
80   timestamps. The generated MP4 file uses TextMetaDataSampleEntry (defined in section 12.3.3.2 of
81   the ISOBMFF specification) to signal the metadata's MIME type.
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>&#9675;</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         /** @hide */
262         @SuppressLint("UnflaggedApi")
263         @TestApi
264         public static final int MUXER_OUTPUT_FIRST   = 0;
265         /** MPEG4 media file format*/
266         public static final int MUXER_OUTPUT_MPEG_4 = MUXER_OUTPUT_FIRST;
267         /** WEBM media file format*/
268         public static final int MUXER_OUTPUT_WEBM   = MUXER_OUTPUT_FIRST + 1;
269         /** 3GPP media file format*/
270         public static final int MUXER_OUTPUT_3GPP   = MUXER_OUTPUT_FIRST + 2;
271         /** HEIF media file format*/
272         public static final int MUXER_OUTPUT_HEIF   = MUXER_OUTPUT_FIRST + 3;
273         /** Ogg media file format*/
274         public static final int MUXER_OUTPUT_OGG   = MUXER_OUTPUT_FIRST + 4;
275         /** @hide */
276         @SuppressLint("UnflaggedApi")
277         @TestApi
278         public static final int MUXER_OUTPUT_LAST   = MUXER_OUTPUT_OGG;
279     };
280 
281     /** @hide */
282     @IntDef({
283         OutputFormat.MUXER_OUTPUT_MPEG_4,
284         OutputFormat.MUXER_OUTPUT_WEBM,
285         OutputFormat.MUXER_OUTPUT_3GPP,
286         OutputFormat.MUXER_OUTPUT_HEIF,
287         OutputFormat.MUXER_OUTPUT_OGG,
288     })
289     @Retention(RetentionPolicy.SOURCE)
290     public @interface Format {}
291 
292     // All the native functions are listed here.
293     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
nativeSetup(@onNull FileDescriptor fd, int format)294     private static native long nativeSetup(@NonNull FileDescriptor fd, int format)
295             throws IllegalArgumentException, IOException;
296     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
nativeRelease(long nativeObject)297     private static native void nativeRelease(long nativeObject);
nativeStart(long nativeObject)298     private static native void nativeStart(long nativeObject);
nativeStop(long nativeObject)299     private static native void nativeStop(long nativeObject);
nativeAddTrack( long nativeObject, @NonNull String[] keys, @NonNull Object[] values)300     private static native int nativeAddTrack(
301             long nativeObject, @NonNull String[] keys, @NonNull Object[] values);
nativeSetOrientationHint( long nativeObject, int degrees)302     private static native void nativeSetOrientationHint(
303             long nativeObject, int degrees);
nativeSetLocation(long nativeObject, int latitude, int longitude)304     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)305     private static native void nativeWriteSampleData(
306             long nativeObject, int trackIndex, @NonNull ByteBuffer byteBuf,
307             int offset, int size, long presentationTimeUs, @MediaCodec.BufferFlag int flags);
308 
309     // Muxer internal states.
310     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
311     private static final int MUXER_STATE_UNINITIALIZED  = -1;
312     private static final int MUXER_STATE_INITIALIZED    = 0;
313     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
314     private static final int MUXER_STATE_STARTED        = 1;
315     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
316     private static final int MUXER_STATE_STOPPED        = 2;
317 
318     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
319     private int mState = MUXER_STATE_UNINITIALIZED;
320 
321     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
322     private final CloseGuard mCloseGuard = CloseGuard.get();
323     private int mLastTrackIndex = -1;
324 
325     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
326     private long mNativeObject;
327 
convertMuxerStateCodeToString(int aState)328     private String convertMuxerStateCodeToString(int aState) {
329         switch (aState) {
330             case MUXER_STATE_UNINITIALIZED:
331                 return "UNINITIALIZED";
332             case MUXER_STATE_INITIALIZED:
333                 return "INITIALIZED";
334             case MUXER_STATE_STARTED:
335                 return "STARTED";
336             case MUXER_STATE_STOPPED:
337                 return "STOPPED";
338             default:
339                 return "UNKNOWN";
340         }
341     }
342 
343     /**
344      * Creates a media muxer that writes to the specified path.
345      * <p>The caller must not use the file {@code path} before calling {@link #stop}.
346      * @param path The path of the output media file.
347      * @param format The format of the output media file.
348      * @see android.media.MediaMuxer.OutputFormat
349      * @throws IllegalArgumentException if path is invalid or format is not supported.
350      * @throws IOException if an error occurs while opening or creating the output file.
351      */
MediaMuxer(@onNull String path, @Format int format)352     public MediaMuxer(@NonNull String path, @Format int format) throws IOException {
353         if (path == null) {
354             throw new IllegalArgumentException("path must not be null");
355         }
356         // Use RandomAccessFile so we can open the file with RW access;
357         // RW access allows the native writer to memory map the output file.
358         RandomAccessFile file = null;
359         try {
360             file = new RandomAccessFile(path, "rws");
361             file.setLength(0);
362             FileDescriptor fd = file.getFD();
363             setUpMediaMuxer(fd, format);
364         } finally {
365             if (file != null) {
366                 file.close();
367             }
368         }
369     }
370 
371     /**
372      * Creates a media muxer that writes to the specified FileDescriptor.
373      * <p>The caller must not use the file referenced by the specified {@code fd} before calling
374      * {@link #stop}.
375      * <p>It is the caller's responsibility to close the file descriptor, which is safe to do so
376      * as soon as this call returns.
377      * @param fd The FileDescriptor of the output media file. If {@code format} is
378      * {@link OutputFormat#MUXER_OUTPUT_WEBM}, {@code fd} must be open in read-write mode.
379      * Otherwise, write mode is sufficient, but read-write is also accepted.
380      * @param format The format of the output media file.
381      * @see android.media.MediaMuxer.OutputFormat
382      * @throws IllegalArgumentException if {@code format} is not supported, or if {@code fd} is
383      * not open in the expected mode.
384      * @throws IOException if an error occurs while performing an IO operation.
385      */
MediaMuxer(@onNull FileDescriptor fd, @Format int format)386     public MediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException {
387         setUpMediaMuxer(fd, format);
388     }
389 
setUpMediaMuxer(@onNull FileDescriptor fd, @Format int format)390     private void setUpMediaMuxer(@NonNull FileDescriptor fd, @Format int format) throws IOException {
391         if (format < OutputFormat.MUXER_OUTPUT_FIRST || format > OutputFormat.MUXER_OUTPUT_LAST) {
392             throw new IllegalArgumentException("format: " + format + " is invalid");
393         }
394         mNativeObject = nativeSetup(fd, format);
395         mState = MUXER_STATE_INITIALIZED;
396         mCloseGuard.open("release");
397     }
398 
399     /**
400      * Sets the orientation hint for output video playback.
401      * <p>This method should be called before {@link #start}. Calling this
402      * method will not rotate the video frame when muxer is generating the file,
403      * but add a composition matrix containing the rotation angle in the output
404      * video if the output format is
405      * {@link OutputFormat#MUXER_OUTPUT_MPEG_4} so that a video player can
406      * choose the proper orientation for playback. Note that some video players
407      * may choose to ignore the composition matrix in a video during playback.
408      * By default, the rotation degree is 0.</p>
409      * @param degrees the angle to be rotated clockwise in degrees.
410      * The supported angles are 0, 90, 180, and 270 degrees.
411      * @throws IllegalArgumentException if degree is not supported.
412      * @throws IllegalStateException If this method is called after {@link #start}.
413      */
setOrientationHint(int degrees)414     public void setOrientationHint(int degrees) {
415         if (degrees != 0 && degrees != 90  && degrees != 180 && degrees != 270) {
416             throw new IllegalArgumentException("Unsupported angle: " + degrees);
417         }
418         if (mState == MUXER_STATE_INITIALIZED) {
419             nativeSetOrientationHint(mNativeObject, degrees);
420         } else {
421             throw new IllegalStateException("Can't set rotation degrees due" +
422                     " to wrong state(" + convertMuxerStateCodeToString(mState) + ")");
423         }
424     }
425 
426     /**
427      * Set and store the geodata (latitude and longitude) in the output file.
428      * This method should be called before {@link #start}. The geodata is stored
429      * in udta box if the output format is
430      * {@link OutputFormat#MUXER_OUTPUT_MPEG_4}, and is ignored for other output
431      * formats. The geodata is stored according to ISO-6709 standard.
432      *
433      * @param latitude Latitude in degrees. Its value must be in the range [-90,
434      * 90].
435      * @param longitude Longitude in degrees. Its value must be in the range
436      * [-180, 180].
437      * @throws IllegalArgumentException If the given latitude or longitude is out
438      * of range.
439      * @throws IllegalStateException If this method is called after {@link #start}.
440      */
setLocation(float latitude, float longitude)441     public void setLocation(float latitude, float longitude) {
442         int latitudex10000  = Math.round(latitude * 10000);
443         int longitudex10000 = Math.round(longitude * 10000);
444 
445         if (latitudex10000 > 900000 || latitudex10000 < -900000) {
446             String msg = "Latitude: " + latitude + " out of range.";
447             throw new IllegalArgumentException(msg);
448         }
449         if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
450             String msg = "Longitude: " + longitude + " out of range";
451             throw new IllegalArgumentException(msg);
452         }
453 
454         if (mState == MUXER_STATE_INITIALIZED && mNativeObject != 0) {
455             nativeSetLocation(mNativeObject, latitudex10000, longitudex10000);
456         } else {
457             throw new IllegalStateException("Can't set location due to wrong state("
458                                              + convertMuxerStateCodeToString(mState) + ")");
459         }
460     }
461 
462     /**
463      * Starts the muxer.
464      * <p>Make sure this is called after {@link #addTrack} and before
465      * {@link #writeSampleData}.</p>
466      * @throws IllegalStateException If this method is called after {@link #start}
467      * or Muxer is released
468      */
start()469     public void start() {
470         if (mNativeObject == 0) {
471             throw new IllegalStateException("Muxer has been released!");
472         }
473         if (mState == MUXER_STATE_INITIALIZED) {
474             nativeStart(mNativeObject);
475             mState = MUXER_STATE_STARTED;
476         } else {
477             throw new IllegalStateException("Can't start due to wrong state("
478                                              + convertMuxerStateCodeToString(mState) + ")");
479         }
480     }
481 
482     /**
483      * Stops the muxer.
484      * <p>Once the muxer stops, it can not be restarted.</p>
485      * @throws IllegalStateException if muxer is in the wrong state.
486      */
stop()487     public void stop() {
488         if (mState == MUXER_STATE_STARTED) {
489             try {
490                 nativeStop(mNativeObject);
491             } catch (Exception e) {
492                 throw e;
493             } finally {
494                 mState = MUXER_STATE_STOPPED;
495             }
496         } else {
497             throw new IllegalStateException("Can't stop due to wrong state("
498                                              + convertMuxerStateCodeToString(mState) + ")");
499         }
500     }
501 
502     @Override
finalize()503     protected void finalize() throws Throwable {
504         try {
505             if (mCloseGuard != null) {
506                 mCloseGuard.warnIfOpen();
507             }
508             if (mNativeObject != 0) {
509                 nativeRelease(mNativeObject);
510                 mNativeObject = 0;
511             }
512         } finally {
513             super.finalize();
514         }
515     }
516 
517     /**
518      * Adds a track with the specified format.
519      * <p>
520      * The following table summarizes support for specific format keys across android releases.
521      * Keys marked with '+:' are required.
522      *
523      * <table>
524      *  <thead>
525      *   <tr>
526      *    <th rowspan=2>OS Version(s)</th>
527      *    <td colspan=3>{@code MediaFormat} keys used for</th>
528      *   </tr><tr>
529      *    <th>All Tracks</th>
530      *    <th>Audio Tracks</th>
531      *    <th>Video Tracks</th>
532      *   </tr>
533      *  </thead>
534      *  <tbody>
535      *   <tr>
536      *    <td>{@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}</td>
537      *    <td rowspan=7>+: {@link MediaFormat#KEY_MIME}</td>
538      *    <td rowspan=3>+: {@link MediaFormat#KEY_SAMPLE_RATE},<br>
539      *        +: {@link MediaFormat#KEY_CHANNEL_COUNT},<br>
540      *        +: <strong>codec-specific data<sup>AAC</sup></strong></td>
541      *    <td rowspan=5>+: {@link MediaFormat#KEY_WIDTH},<br>
542      *        +: {@link MediaFormat#KEY_HEIGHT},<br>
543      *        no {@code KEY_ROTATION},
544      *        use {@link #setOrientationHint setOrientationHint()}<sup>.mp4</sup>,<br>
545      *        +: <strong>codec-specific data<sup>AVC, MPEG4</sup></strong></td>
546      *   </tr><tr>
547      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT}</td>
548      *   </tr><tr>
549      *    <td>{@link android.os.Build.VERSION_CODES#KITKAT_WATCH}</td>
550      *   </tr><tr>
551      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP}</td>
552      *    <td rowspan=4>as above, plus<br>
553      *        +: <strong>codec-specific data<sup>Vorbis & .webm</sup></strong></td>
554      *   </tr><tr>
555      *    <td>{@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}</td>
556      *   </tr><tr>
557      *    <td>{@link android.os.Build.VERSION_CODES#M}</td>
558      *    <td>as above, plus<br>
559      *        {@link MediaFormat#KEY_BIT_RATE}<sup>AAC</sup></td>
560      *   </tr><tr>
561      *    <td>{@link android.os.Build.VERSION_CODES#N}</td>
562      *    <td>as above, plus<br>
563      *        <!-- {link MediaFormat#KEY_MAX_BIT_RATE}<sup>AAC, MPEG4</sup>,<br> -->
564      *        {@link MediaFormat#KEY_BIT_RATE}<sup>MPEG4</sup>,<br>
565      *        {@link MediaFormat#KEY_HDR_STATIC_INFO}<sup>#, .webm</sup>,<br>
566      *        {@link MediaFormat#KEY_COLOR_STANDARD}<sup>#</sup>,<br>
567      *        {@link MediaFormat#KEY_COLOR_TRANSFER}<sup>#</sup>,<br>
568      *        {@link MediaFormat#KEY_COLOR_RANGE}<sup>#</sup>,<br>
569      *        +: <strong>codec-specific data<sup>HEVC</sup></strong>,<br>
570      *        codec-specific data<sup>VP9</sup></td>
571      *   </tr>
572      *   <tr>
573      *    <td colspan=4>
574      *     <p class=note><strong>Notes:</strong><br>
575      *      #: storing into container metadata.<br>
576      *      .mp4, .webm&hellip;: for listed containers<br>
577      *      MPEG4, AAC&hellip;: for listed codecs
578      *    </td>
579      *   </tr><tr>
580      *    <td colspan=4>
581      *     <p class=note>Note that the codec-specific data for the track must be specified using
582      *     this method. Furthermore, codec-specific data must not be passed/specified via the
583      *     {@link #writeSampleData writeSampleData()} call.
584      *    </td>
585      *   </tr>
586      *  </tbody>
587      * </table>
588      *
589      * <p>
590      * The following table summarizes codec support for containers across android releases:
591      *
592      * <table>
593      *   <thead>
594      *     <tr>
595      *       <th>Codec</th>
596      *       <th>{@linkplain OutputFormat#MUXER_OUTPUT_MPEG_4 MP4}</th>
597      *       <th>{@linkplain OutputFormat#MUXER_OUTPUT_WEBM WEBM}</th>
598      *       <th>{@linkplain OutputFormat#MUXER_OUTPUT_OGG OGG}</th>
599      *       <th>Supported From SDK version</th>
600      *     </tr>
601      *   </thead>
602      *   <tbody>
603      *     <tr>
604      *       <td>{@link MediaFormat#MIMETYPE_AUDIO_AAC AAC}</td>
605      *       <td>✓</td>
606      *       <td></td>
607      *       <td></td>
608      *       <td>17</td>
609      *     </tr>
610      *     <tr>
611      *       <td>{@link MediaFormat#MIMETYPE_AUDIO_AMR_NB NB-AMR}</td>
612      *       <td>✓</td>
613      *       <td></td>
614      *       <td></td>
615      *       <td>17</td>
616      *     </tr>
617      *     <tr>
618      *       <td>{@link MediaFormat#MIMETYPE_AUDIO_AMR_WB WB-AMR}</td>
619      *       <td>✓</td>
620      *       <td></td>
621      *       <td></td>
622      *       <td>17</td>
623      *     </tr>
624      *     <tr>
625      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_H263 H.263}</td>
626      *       <td>✓</td>
627      *       <td></td>
628      *       <td></td>
629      *       <td>17</td>
630      *     </tr>
631      *     <tr>
632      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_MPEG4 MPEG-4}</td>
633      *       <td>✓</td>
634      *       <td></td>
635      *       <td></td>
636      *       <td>17</td>
637      *     </tr>
638      *     <tr>
639      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_AVC AVC} (H.264)</td>
640      *       <td>✓</td>
641      *       <td></td>
642      *       <td></td>
643      *       <td>17</td>
644      *     </tr>
645      *     <tr>
646      *       <td>{@link MediaFormat#MIMETYPE_AUDIO_VORBIS Vorbis}</td>
647      *       <td></td>
648      *       <td>✓</td>
649      *       <td></td>
650      *       <td>21</td>
651      *     </tr>
652      *     <tr>
653      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_VP8 VP8}</td>
654      *       <td></td>
655      *       <td>✓</td>
656      *       <td></td>
657      *       <td>21</td>
658      *     </tr>
659      *     <tr>
660      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_VP9 VP9}</td>
661      *       <td></td>
662      *       <td>✓</td>
663      *       <td></td>
664      *       <td>24</td>
665      *     </tr>
666      *     <tr>
667      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_HEVC HEVC} (H.265)</td>
668      *       <td>✓</td>
669      *       <td></td>
670      *       <td></td>
671      *       <td>24</td>
672      *     </tr>
673      *     <tr>
674      *       <td>{@link MediaFormat#MIMETYPE_AUDIO_OPUS OPUS}</td>
675      *       <td></td>
676      *       <td>✓</td>
677      *       <td>✓</td>
678      *       <td>26</td>
679      *     </tr>
680      *     <tr>
681      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_AV1 AV1}</td>
682      *       <td>✓</td>
683      *       <td></td>
684      *       <td></td>
685      *       <td>31</td>
686      *     </tr>
687      *     <tr>
688      *       <td>{@link MediaFormat#MIMETYPE_VIDEO_DOLBY_VISION Dolby Vision}</td>
689      *       <td>✓</td>
690      *       <td></td>
691      *       <td></td>
692      *       <td>32</td>
693      *     </tr>
694      *   </tbody>
695      * </table>
696      *
697      * @param format The media format for the track.  This must not be an empty
698      *               MediaFormat.
699      * @return The track index for this newly added track, and it should be used
700      * in the {@link #writeSampleData}.
701      * @throws IllegalArgumentException if format is invalid.
702      * @throws IllegalStateException if muxer is in the wrong state.
703      */
addTrack(@onNull MediaFormat format)704     public int addTrack(@NonNull MediaFormat format) {
705         if (format == null) {
706             throw new IllegalArgumentException("format must not be null.");
707         }
708         if (mState != MUXER_STATE_INITIALIZED) {
709             throw new IllegalStateException("Muxer is not initialized.");
710         }
711         if (mNativeObject == 0) {
712             throw new IllegalStateException("Muxer has been released!");
713         }
714         int trackIndex = -1;
715         // Convert the MediaFormat into key-value pairs and send to the native.
716         Map<String, Object> formatMap = format.getMap();
717 
718         String[] keys = null;
719         Object[] values = null;
720         int mapSize = formatMap.size();
721         if (mapSize > 0) {
722             keys = new String[mapSize];
723             values = new Object[mapSize];
724             int i = 0;
725             for (Map.Entry<String, Object> entry : formatMap.entrySet()) {
726                 keys[i] = entry.getKey();
727                 values[i] = entry.getValue();
728                 ++i;
729             }
730             trackIndex = nativeAddTrack(mNativeObject, keys, values);
731         } else {
732             throw new IllegalArgumentException("format must not be empty.");
733         }
734 
735         // Track index number is expected to incremented as addTrack succeed.
736         // However, if format is invalid, it will get a negative trackIndex.
737         if (mLastTrackIndex >= trackIndex) {
738             throw new IllegalArgumentException("Invalid format.");
739         }
740         mLastTrackIndex = trackIndex;
741         return trackIndex;
742     }
743 
744     /**
745      * Writes an encoded sample into the muxer.
746      * <p>The application needs to make sure that the samples are written into
747      * the right tracks. Also, it needs to make sure the samples for each track
748      * are written in chronological order (e.g. in the order they are provided
749      * by the encoder.)</p>
750      * <p> For MPEG4 media format, the duration of the last sample in a track can be set by passing
751      * an additional empty buffer(bufferInfo.size = 0) with MediaCodec.BUFFER_FLAG_END_OF_STREAM
752      * flag and a suitable presentation timestamp set in bufferInfo parameter as the last sample of
753      * that track.  This last sample's presentation timestamp shall be a sum of the presentation
754      * timestamp and the duration preferred for the original last sample.  If no explicit
755      * END_OF_STREAM sample was passed, then the duration of the last sample would be the same as
756      * that of the sample before that.</p>
757      * @param byteBuf The encoded sample.
758      * @param trackIndex The track index for this sample.
759      * @param bufferInfo The buffer information related to this sample.
760      * @throws IllegalArgumentException if trackIndex, byteBuf or bufferInfo is  invalid.
761      * @throws IllegalStateException if muxer is in wrong state.
762      * MediaMuxer uses the flags provided in {@link MediaCodec.BufferInfo},
763      * to signal sync frames.
764      */
writeSampleData(int trackIndex, @NonNull ByteBuffer byteBuf, @NonNull BufferInfo bufferInfo)765     public void writeSampleData(int trackIndex, @NonNull ByteBuffer byteBuf,
766             @NonNull BufferInfo bufferInfo) {
767         if (trackIndex < 0 || trackIndex > mLastTrackIndex) {
768             throw new IllegalArgumentException("trackIndex is invalid");
769         }
770 
771         if (byteBuf == null) {
772             throw new IllegalArgumentException("byteBuffer must not be null");
773         }
774 
775         if (bufferInfo == null) {
776             throw new IllegalArgumentException("bufferInfo must not be null");
777         }
778         if (bufferInfo.size < 0 || bufferInfo.offset < 0
779                 || (bufferInfo.offset + bufferInfo.size) > byteBuf.capacity()) {
780             throw new IllegalArgumentException("bufferInfo must specify a" +
781                     " valid buffer offset and size");
782         }
783 
784         if (mNativeObject == 0) {
785             throw new IllegalStateException("Muxer has been released!");
786         }
787 
788         if (mState != MUXER_STATE_STARTED) {
789             throw new IllegalStateException("Can't write, muxer is not started");
790         }
791 
792         nativeWriteSampleData(mNativeObject, trackIndex, byteBuf,
793                 bufferInfo.offset, bufferInfo.size,
794                 bufferInfo.presentationTimeUs, bufferInfo.flags);
795     }
796 
797     /**
798      * Make sure you call this when you're done to free up any resources
799      * instead of relying on the garbage collector to do this for you at
800      * some point in the future.
801      */
release()802     public void release() {
803         if (mState == MUXER_STATE_STARTED) {
804             stop();
805         }
806         if (mNativeObject != 0) {
807             nativeRelease(mNativeObject);
808             mNativeObject = 0;
809             mCloseGuard.close();
810         }
811         mState = MUXER_STATE_UNINITIALIZED;
812     }
813 }
814