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