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