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