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