1 /* 2 * Copyright 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 #ifndef MEDIA_MUXER_H_ 18 #define MEDIA_MUXER_H_ 19 20 #include <utils/Errors.h> 21 #include <utils/RefBase.h> 22 #include <utils/Vector.h> 23 #include <utils/threads.h> 24 25 #include "foundation/ABase.h" 26 27 namespace android { 28 29 struct ABuffer; 30 struct AMessage; 31 struct MediaAdapter; 32 class MediaBuffer; 33 struct MediaSource; 34 class MetaData; 35 struct MediaWriter; 36 37 // MediaMuxer is used to mux multiple tracks into a video. Currently, we only 38 // support a mp4 file as the output. 39 // The expected calling order of the functions is: 40 // Constructor -> addTrack+ -> start -> writeSampleData+ -> stop 41 // If muxing operation need to be cancelled, the app is responsible for 42 // deleting the output file after stop. 43 struct MediaMuxer : public RefBase { 44 public: 45 // Please update media/java/android/media/MediaMuxer.java if the 46 // OutputFormat is updated. 47 enum OutputFormat { 48 OUTPUT_FORMAT_MPEG_4 = 0, 49 OUTPUT_FORMAT_WEBM = 1, 50 OUTPUT_FORMAT_THREE_GPP = 2, 51 OUTPUT_FORMAT_HEIF = 3, 52 OUTPUT_FORMAT_OGG = 4, 53 OUTPUT_FORMAT_LIST_END // must be last - used to validate format type 54 }; 55 56 // Construct the muxer with the file descriptor. Note that the MediaMuxer 57 // will close this file at stop(). 58 MediaMuxer(int fd, OutputFormat format); 59 60 virtual ~MediaMuxer(); 61 62 /** 63 * Add a track with its format information. This should be 64 * called before start(). 65 * @param format the track's format. 66 * @return the track's index or negative number if error. 67 */ 68 ssize_t addTrack(const sp<AMessage> &format); 69 70 /** 71 * Start muxing. Make sure all the tracks have been added before 72 * calling this. 73 */ 74 status_t start(); 75 76 /** 77 * Set the orientation hint. 78 * @param degrees The rotation degrees. It has to be either 0, 79 * 90, 180 or 270. 80 * @return OK if no error. 81 */ 82 status_t setOrientationHint(int degrees); 83 84 /** 85 * Set the location. 86 * @param latitude The latitude in degree x 1000. Its value must be in the range 87 * [-900000, 900000]. 88 * @param longitude The longitude in degree x 1000. Its value must be in the range 89 * [-1800000, 1800000]. 90 * @return OK if no error. 91 */ 92 status_t setLocation(int latitude, int longitude); 93 94 /** 95 * Stop muxing. 96 * This method is a blocking call. Depending on how 97 * much data is bufferred internally, the time needed for stopping 98 * the muxer may be time consuming. UI thread is 99 * not recommended for launching this call. 100 * @return OK if no error. 101 */ 102 status_t stop(); 103 104 /** 105 * Send a sample buffer for muxing. 106 * The buffer can be reused once this method returns. Typically, 107 * this function won't be blocked for very long, and thus there 108 * is no need to use a separate thread calling this method to 109 * push a buffer. 110 * @param buffer the incoming sample buffer. 111 * @param trackIndex the buffer's track index number. 112 * @param timeUs the buffer's time stamp. 113 * @param flags the only supported flag for now is 114 * MediaCodec::BUFFER_FLAG_SYNCFRAME. 115 * @return OK if no error. 116 */ 117 status_t writeSampleData(const sp<ABuffer> &buffer, size_t trackIndex, 118 int64_t timeUs, uint32_t flags) ; 119 120 private: 121 const OutputFormat mFormat; 122 sp<MediaWriter> mWriter; 123 Vector< sp<MediaAdapter> > mTrackList; // Each track has its MediaAdapter. 124 sp<MetaData> mFileMeta; // Metadata for the whole file. 125 126 Mutex mMuxerLock; 127 128 enum State { 129 UNINITIALIZED, 130 INITIALIZED, 131 STARTED, 132 STOPPED 133 }; 134 State mState; 135 136 DISALLOW_EVIL_CONSTRUCTORS(MediaMuxer); 137 }; 138 139 } // namespace android 140 141 #endif // MEDIA_MUXER_H_ 142 143