MPEG4Writer.h revision 1824486e044f4f09640fbd7bef74a20e4efb35ae
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
43    void beginBox(const char *fourcc);
44    void writeInt8(int8_t x);
45    void writeInt16(int16_t x);
46    void writeInt32(int32_t x);
47    void writeInt64(int64_t x);
48    void writeCString(const char *s);
49    void writeFourcc(const char *fourcc);
50    void write(const void *data, size_t size);
51    void endBox();
52    uint32_t interleaveDuration() const { return mInterleaveDurationUs; }
53    status_t setInterleaveDuration(uint32_t duration);
54
55protected:
56    virtual ~MPEG4Writer();
57
58private:
59    class Track;
60
61    FILE *mFile;
62    off_t mOffset;
63    off_t mMdatOffset;
64    uint8_t *mMoovBoxBuffer;
65    off_t mMoovBoxBufferOffset;
66    bool  mWriteMoovBoxToMemory;
67    off_t mFreeBoxOffset;
68    bool mStreamableFile;
69    off_t mEstimatedMoovBoxSize;
70    uint32_t mInterleaveDurationUs;
71    Mutex mLock;
72
73    List<Track *> mTracks;
74
75    List<off_t> mBoxes;
76
77    void lock();
78    void unlock();
79
80    // Acquire lock before calling these methods
81    off_t addSample_l(MediaBuffer *buffer);
82    off_t addLengthPrefixedSample_l(MediaBuffer *buffer);
83
84    inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream);
85    bool exceedsFileSizeLimit();
86    bool exceedsFileDurationLimit();
87
88    MPEG4Writer(const MPEG4Writer &);
89    MPEG4Writer &operator=(const MPEG4Writer &);
90};
91
92}  // namespace android
93
94#endif  // MPEG4_WRITER_H_
95