MPEG4Writer.h revision d707fcb3e29707ca4a5935c294ef0b38eb5aba5f
1/*
2 * Copyright (C) 2009 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 MPEG4_WRITER_H_
18
19#define MPEG4_WRITER_H_
20
21#include <stdio.h>
22
23#include <media/stagefright/MediaWriter.h>
24#include <utils/List.h>
25#include <utils/threads.h>
26
27namespace android {
28
29class MediaBuffer;
30class MediaSource;
31class MetaData;
32
33class MPEG4Writer : public MediaWriter {
34public:
35    MPEG4Writer(const char *filename);
36    MPEG4Writer(int fd);
37
38    virtual status_t addSource(const sp<MediaSource> &source);
39    virtual status_t start(MetaData *param = NULL);
40    virtual status_t stop();
41    virtual status_t pause();
42    virtual bool reachedEOS();
43    virtual status_t dump(int fd, const Vector<String16>& args);
44
45    void beginBox(const char *fourcc);
46    void writeInt8(int8_t x);
47    void writeInt16(int16_t x);
48    void writeInt32(int32_t x);
49    void writeInt64(int64_t x);
50    void writeCString(const char *s);
51    void writeFourcc(const char *fourcc);
52    void write(const void *data, size_t size);
53    void endBox();
54    uint32_t interleaveDuration() const { return mInterleaveDurationUs; }
55    status_t setInterleaveDuration(uint32_t duration);
56    int32_t getTimeScale() const { return mTimeScale; }
57
58protected:
59    virtual ~MPEG4Writer();
60
61private:
62    class Track;
63
64    FILE *mFile;
65    bool mUse32BitOffset;
66    bool mPaused;
67    bool mStarted;
68    off_t mOffset;
69    off_t mMdatOffset;
70    uint8_t *mMoovBoxBuffer;
71    off_t mMoovBoxBufferOffset;
72    bool  mWriteMoovBoxToMemory;
73    off_t mFreeBoxOffset;
74    bool mStreamableFile;
75    off_t mEstimatedMoovBoxSize;
76    uint32_t mInterleaveDurationUs;
77    int32_t mTimeScale;
78    int64_t mStartTimestampUs;
79
80    Mutex mLock;
81
82    List<Track *> mTracks;
83
84    List<off_t> mBoxes;
85
86    void setStartTimestampUs(int64_t timeUs);
87    int64_t getStartTimestampUs();  // Not const
88    status_t startTracks(MetaData *params);
89    size_t numTracks();
90    int64_t estimateMoovBoxSize(int32_t bitRate);
91
92    struct Chunk {
93        Track               *mTrack;        // Owner
94        int64_t             mTimeStampUs;   // Timestamp of the 1st sample
95        List<MediaBuffer *> mSamples;       // Sample data
96
97        // Convenient constructor
98        Chunk(Track *track, int64_t timeUs, List<MediaBuffer *> samples)
99            : mTrack(track), mTimeStampUs(timeUs), mSamples(samples) {
100        }
101
102    };
103    struct ChunkInfo {
104        Track               *mTrack;        // Owner
105        List<Chunk>         mChunks;        // Remaining chunks to be written
106    };
107
108    bool            mIsFirstChunk;
109    volatile bool   mDone;                  // Writer thread is done?
110    pthread_t       mThread;                // Thread id for the writer
111    List<ChunkInfo> mChunkInfos;            // Chunk infos
112    Condition       mChunkReadyCondition;   // Signal that chunks are available
113
114    // Writer thread handling
115    status_t startWriterThread();
116    void stopWriterThread();
117    static void *ThreadWrapper(void *me);
118    void threadFunc();
119
120    // Buffer a single chunk to be written out later.
121    void bufferChunk(const Chunk& chunk);
122
123    // Write all buffered chunks from all tracks
124    void writeChunks();
125
126    // Write a chunk if there is one
127    status_t writeOneChunk();
128
129    // Write the first chunk from the given ChunkInfo.
130    void writeFirstChunk(ChunkInfo* info);
131
132    // Adjust other track media clock (presumably wall clock)
133    // based on audio track media clock with the drift time.
134    int64_t mDriftTimeUs;
135    void setDriftTimeUs(int64_t driftTimeUs);
136    int64_t getDriftTimeUs();
137
138    void lock();
139    void unlock();
140
141    // Acquire lock before calling these methods
142    off_t addSample_l(MediaBuffer *buffer);
143    off_t addLengthPrefixedSample_l(MediaBuffer *buffer);
144
145    inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream);
146    bool exceedsFileSizeLimit();
147    bool exceedsFileDurationLimit();
148    void trackProgressStatus(const Track* track, int64_t timeUs, status_t err = OK);
149
150    MPEG4Writer(const MPEG4Writer &);
151    MPEG4Writer &operator=(const MPEG4Writer &);
152};
153
154}  // namespace android
155
156#endif  // MPEG4_WRITER_H_
157