MediaMuxer.h revision 3db62dfc5102247d415df4667bd9609e669fc022
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
25namespace android {
26
27struct ABuffer;
28struct AMessage;
29struct MediaAdapter;
30struct MediaBuffer;
31struct MediaSource;
32struct MetaData;
33struct 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.
41struct MediaMuxer : public RefBase {
42public:
43    // Please update media/java/android/media/MediaMuxer.java if the
44    // SampleFlags is updated.
45    enum SampleFlags {
46        SAMPLE_FLAG_SYNC = 1,
47    };
48
49    // Please update media/java/android/media/MediaMuxer.java if the
50    // OutputFormat is updated.
51    enum OutputFormat {
52        OUTPUT_FORMAT_MPEG_4 = 0,
53        OUTPUT_FORMAT_LIST_END // must be last - used to validate format type
54    };
55
56    // Construct the muxer with the output file path.
57    MediaMuxer(const char *path, OutputFormat format);
58
59    // Construct the muxer with the file descriptor. Note that the MediaMuxer
60    // will close this file at stop().
61    MediaMuxer(int fd, OutputFormat format);
62
63    virtual ~MediaMuxer();
64
65    /**
66     * Add a track with its format information. This should be
67     * called before start().
68     * @param format the track's format.
69     * @return the track's index or negative number if error.
70     */
71    ssize_t addTrack(const sp<AMessage> &format);
72
73    /**
74     * Start muxing. Make sure all the tracks have been added before
75     * calling this.
76     */
77    status_t start();
78
79    /**
80     * Stop muxing.
81     * This method is a blocking call. Depending on how
82     * much data is bufferred internally, the time needed for stopping
83     * the muxer may be time consuming. UI thread is
84     * not recommended for launching this call.
85     */
86    status_t stop();
87
88    /**
89     * Send a sample buffer for muxing.
90     * The buffer can be reused once this method returns. Typically,
91     * this function won't be blocked for very long, and thus there
92     * is no need to use a separate thread calling this method to
93     * push a buffer.
94     * @param buffer the incoming sample buffer.
95     * @param trackIndex the buffer's track index number.
96     * @param timeUs the buffer's time stamp.
97     * @param flags the only supported flag for now is
98     *              MediaCodec::BUFFER_FLAG_SYNCFRAME.
99     * @return OK if no error.
100     */
101    status_t writeSampleData(const sp<ABuffer> &buffer, size_t trackIndex,
102                             int64_t timeUs, uint32_t flags) ;
103
104private:
105    sp<MPEG4Writer> mWriter;
106    Vector< sp<MediaAdapter> > mTrackList;  // Each track has its MediaAdapter.
107
108    Mutex mMuxerLock;
109
110    enum State {
111        UNINITED,
112        INITED,
113        STARTED,
114        STOPPED
115    };
116    State mState;
117
118    DISALLOW_EVIL_CONSTRUCTORS(MediaMuxer);
119};
120
121}  // namespace android
122
123#endif  // MEDIA_MUXER_H_
124
125