MPEG4Writer.h revision 58ae9c530247668f8af36e30d228c716c226b3d4
1bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell/*
2bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Copyright (C) 2009 The Android Open Source Project
3bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
4bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * you may not use this file except in compliance with the License.
6bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * You may obtain a copy of the License at
7bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
8bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell *
10bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * Unless required by applicable law or agreed to in writing, software
11bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * See the License for the specific language governing permissions and
14bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell * limitations under the License.
15bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell */
16bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
17bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell#ifndef MPEG4_WRITER_H_
18bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
198a0c51a3d438802df28612e02fdf0cc8fab06ddbAdam Powell#define MPEG4_WRITER_H_
20c029e15f5a4709214cb433a562256586824a0f33Adam Powell
210574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov#include <stdio.h>
22c029e15f5a4709214cb433a562256586824a0f33Adam Powell
230d4ffdf13a16b1ce2f3bc458002374cdd25663c3alanv#include <media/stagefright/MediaWriter.h>
24a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye#include <utils/List.h>
25a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye#include <utils/threads.h>
269648c538bac4f04145c118cc41168d1d7a536312Svetoslav Ganov
2725121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganovnamespace android {
28a6613ed42e0dee0cac3da01f5450e2d61c270b52Adam Powell
29bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powellclass MediaBuffer;
30d7d27e9ebe5c7325e67e1a8af265378bd2056cadChet Haaseclass MediaSource;
313cf6141d516a0a6170823b1bda5c0b6d7500e76cAdam Powellclass MetaData;
329648c538bac4f04145c118cc41168d1d7a536312Svetoslav Ganov
33bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powellclass MPEG4Writer : public MediaWriter {
34a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyepublic:
35a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    MPEG4Writer(const char *filename);
36a6613ed42e0dee0cac3da01f5450e2d61c270b52Adam Powell    MPEG4Writer(int fd);
37d7d27e9ebe5c7325e67e1a8af265378bd2056cadChet Haase
38a6613ed42e0dee0cac3da01f5450e2d61c270b52Adam Powell    virtual status_t addSource(const sp<MediaSource> &source);
39bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    virtual status_t start();
400574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov    virtual bool reachedEOS();
410574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov    virtual void stop();
42bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell    virtual void pause();
43bc889e39e279fcf8c3d35fc11d8052c002eddf38Adam Powell
44a6613ed42e0dee0cac3da01f5450e2d61c270b52Adam Powell    void beginBox(const char *fourcc);
45a6613ed42e0dee0cac3da01f5450e2d61c270b52Adam Powell    void writeInt8(int8_t x);
46a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    void writeInt16(int16_t x);
47a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    void writeInt32(int32_t x);
48a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    void writeInt64(int64_t x);
49a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    void writeCString(const char *s);
50a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    void writeFourcc(const char *fourcc);
51560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    void write(const void *data, size_t size);
52560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    void endBox();
53560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    uint32_t interleaveDuration() const { return mInterleaveDurationUs; }
54560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    status_t setInterleaveDuration(uint32_t duration);
55560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
56560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powellprotected:
57560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    virtual ~MPEG4Writer();
58560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
59560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powellprivate:
60560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    class Track;
61560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell
62560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    FILE *mFile;
63560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    bool mPaused;
64560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    bool mStarted;
65560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    off_t mOffset;
66560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    off_t mMdatOffset;
67560114f591be31d0fb73c26a1ee1cc0a15184abaAdam Powell    uint8_t *mMoovBoxBuffer;
68ec03704fbb4f0217d4c274d1c6cf56e6ea4dcfbdAdam Powell    off_t mMoovBoxBufferOffset;
69ec03704fbb4f0217d4c274d1c6cf56e6ea4dcfbdAdam Powell    bool  mWriteMoovBoxToMemory;
70a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    off_t mFreeBoxOffset;
71a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    bool mStreamableFile;
72a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    off_t mEstimatedMoovBoxSize;
73a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    uint32_t mInterleaveDurationUs;
74a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    int64_t mStartTimestampUs;
75a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    Mutex mLock;
76a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
77a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    List<Track *> mTracks;
78a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
79a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    List<off_t> mBoxes;
8025121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov
8125121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    void setStartTimestampUs(int64_t timeUs);
8225121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    int64_t getStartTimestampUs();  // Not const
8325121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    status_t startTracks();
8425121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    size_t numTracks();
8525121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov
8625121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    void lock();
8725121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    void unlock();
8825121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov
8925121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    // Acquire lock before calling these methods
9025121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    off_t addSample_l(MediaBuffer *buffer);
9125121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    off_t addLengthPrefixedSample_l(MediaBuffer *buffer);
9225121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov
9325121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream);
9425121559b53b9f6c7ef7159203d42e11b9aee281Svetoslav Ganov    bool exceedsFileSizeLimit();
958a0c51a3d438802df28612e02fdf0cc8fab06ddbAdam Powell    bool exceedsFileDurationLimit();
96b5b909e4544fd4d1d0bdf445b8588a7233d5c71fAlan Viverette
97b5b909e4544fd4d1d0bdf445b8588a7233d5c71fAlan Viverette    MPEG4Writer(const MPEG4Writer &);
98b5b909e4544fd4d1d0bdf445b8588a7233d5c71fAlan Viverette    MPEG4Writer &operator=(const MPEG4Writer &);
99b5b909e4544fd4d1d0bdf445b8588a7233d5c71fAlan Viverette};
100b5b909e4544fd4d1d0bdf445b8588a7233d5c71fAlan Viverette
101a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye}  // namespace android
102a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
103a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye#endif  // MPEG4_WRITER_H_
104a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye