MPEG4Writer.h revision 1acfe8649f8169caf2ff098c2dc2de880d9a3760
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();
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
56protected:
57    virtual ~MPEG4Writer();
58
59private:
60    class Track;
61
62    FILE *mFile;
63    bool mUse32BitOffset;
64    bool mPaused;
65    bool mStarted;
66    off_t mOffset;
67    off_t mMdatOffset;
68    uint8_t *mMoovBoxBuffer;
69    off_t mMoovBoxBufferOffset;
70    bool  mWriteMoovBoxToMemory;
71    off_t mFreeBoxOffset;
72    bool mStreamableFile;
73    off_t mEstimatedMoovBoxSize;
74    uint32_t mInterleaveDurationUs;
75    int64_t mStartTimestampUs;
76    Mutex mLock;
77
78    List<Track *> mTracks;
79
80    List<off_t> mBoxes;
81
82    void setStartTimestampUs(int64_t timeUs);
83    int64_t getStartTimestampUs();  // Not const
84    status_t startTracks();
85    size_t numTracks();
86
87    void lock();
88    void unlock();
89
90    // Acquire lock before calling these methods
91    off_t addSample_l(MediaBuffer *buffer);
92    off_t addLengthPrefixedSample_l(MediaBuffer *buffer);
93
94    inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream);
95    bool exceedsFileSizeLimit();
96    bool exceedsFileDurationLimit();
97
98    MPEG4Writer(const MPEG4Writer &);
99    MPEG4Writer &operator=(const MPEG4Writer &);
100};
101
102}  // namespace android
103
104#endif  // MPEG4_WRITER_H_
105