MPEG4Writer.h revision b4d5320bda29cd9694461c9b61d0211f801ff0af
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/* 220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project 320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License"); 520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License. 620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at 720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * http://www.apache.org/licenses/LICENSE-2.0 920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software 1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS, 1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and 1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License. 1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */ 1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#ifndef MPEG4_WRITER_H_ 1820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#define MPEG4_WRITER_H_ 2020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <stdio.h> 2220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 232dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber#include <media/stagefright/MediaWriter.h> 2420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/List.h> 2520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <utils/threads.h> 2620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2720111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android { 2820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass MediaBuffer; 3020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass MediaSource; 3120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass MetaData; 3220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 332dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huberclass MPEG4Writer : public MediaWriter { 3420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberpublic: 3520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MPEG4Writer(const char *filename); 3630ab66297501757d745b9ae10da61adcd891f497Andreas Huber MPEG4Writer(int fd); 3720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 382dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber virtual status_t addSource(const sp<MediaSource> &source); 392dec2b5be2056c6d9428897dc672185872d30d17James Dong virtual status_t start(MetaData *param = NULL); 4037187916a486504acaf83bea30147eb5fbf46ae5James Dong virtual status_t stop(); 4137187916a486504acaf83bea30147eb5fbf46ae5James Dong virtual status_t pause(); 422dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber virtual bool reachedEOS(); 43dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong virtual status_t dump(int fd, const Vector<String16>& args); 4420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 4520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void beginBox(const char *fourcc); 4620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeInt8(int8_t x); 4720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeInt16(int16_t x); 4820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeInt32(int32_t x); 4920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeInt64(int64_t x); 5020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeCString(const char *s); 5120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void writeFourcc(const char *fourcc); 5220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void write(const void *data, size_t size); 5320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void endBox(); 5413aec890216948b0c364f8f92792129d0335f506James Dong uint32_t interleaveDuration() const { return mInterleaveDurationUs; } 5513aec890216948b0c364f8f92792129d0335f506James Dong status_t setInterleaveDuration(uint32_t duration); 568f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int32_t getTimeScale() const { return mTimeScale; } 5720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 58693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huberprotected: 59693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber virtual ~MPEG4Writer(); 60693d271e62a3726689ff68f4505ba49228eb94b2Andreas Huber 6120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberprivate: 6220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber class Track; 6320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 6420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber FILE *mFile; 65b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong bool mUse4ByteNalLength; 661acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong bool mUse32BitOffset; 67a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong bool mPaused; 68a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong bool mStarted; 6920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber off_t mOffset; 7020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber off_t mMdatOffset; 717837c17063a4c50bc856ba59418516fdab731de7James Dong uint8_t *mMoovBoxBuffer; 727837c17063a4c50bc856ba59418516fdab731de7James Dong off_t mMoovBoxBufferOffset; 737837c17063a4c50bc856ba59418516fdab731de7James Dong bool mWriteMoovBoxToMemory; 747837c17063a4c50bc856ba59418516fdab731de7James Dong off_t mFreeBoxOffset; 757837c17063a4c50bc856ba59418516fdab731de7James Dong bool mStreamableFile; 767837c17063a4c50bc856ba59418516fdab731de7James Dong off_t mEstimatedMoovBoxSize; 7713aec890216948b0c364f8f92792129d0335f506James Dong uint32_t mInterleaveDurationUs; 788f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int32_t mTimeScale; 793c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong int64_t mStartTimestampUs; 801c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Mutex mLock; 8220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 8320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber List<Track *> mTracks; 8420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 8520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber List<off_t> mBoxes; 8620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 87f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong void setStartTimestampUs(int64_t timeUs); 88f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong int64_t getStartTimestampUs(); // Not const 8993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong status_t startTracks(MetaData *params); 9058ae9c530247668f8af36e30d228c716c226b3d4James Dong size_t numTracks(); 912dec2b5be2056c6d9428897dc672185872d30d17James Dong int64_t estimateMoovBoxSize(int32_t bitRate); 923c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong 931c9747a4653aec1395c2bd6896c9b87cb5447837James Dong struct Chunk { 941c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Track *mTrack; // Owner 951c9747a4653aec1395c2bd6896c9b87cb5447837James Dong int64_t mTimeStampUs; // Timestamp of the 1st sample 961c9747a4653aec1395c2bd6896c9b87cb5447837James Dong List<MediaBuffer *> mSamples; // Sample data 971c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 981c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Convenient constructor 991c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Chunk(Track *track, int64_t timeUs, List<MediaBuffer *> samples) 1001c9747a4653aec1395c2bd6896c9b87cb5447837James Dong : mTrack(track), mTimeStampUs(timeUs), mSamples(samples) { 1011c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 1021c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1031c9747a4653aec1395c2bd6896c9b87cb5447837James Dong }; 1041c9747a4653aec1395c2bd6896c9b87cb5447837James Dong struct ChunkInfo { 1051c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Track *mTrack; // Owner 1061c9747a4653aec1395c2bd6896c9b87cb5447837James Dong List<Chunk> mChunks; // Remaining chunks to be written 1071c9747a4653aec1395c2bd6896c9b87cb5447837James Dong }; 1081c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1091c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool mIsFirstChunk; 1101c9747a4653aec1395c2bd6896c9b87cb5447837James Dong volatile bool mDone; // Writer thread is done? 1111c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_t mThread; // Thread id for the writer 1121c9747a4653aec1395c2bd6896c9b87cb5447837James Dong List<ChunkInfo> mChunkInfos; // Chunk infos 1131c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Condition mChunkReadyCondition; // Signal that chunks are available 1141c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1151c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Writer thread handling 1161c9747a4653aec1395c2bd6896c9b87cb5447837James Dong status_t startWriterThread(); 1171c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void stopWriterThread(); 1181c9747a4653aec1395c2bd6896c9b87cb5447837James Dong static void *ThreadWrapper(void *me); 1191c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void threadFunc(); 1201c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1211c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Buffer a single chunk to be written out later. 1221c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void bufferChunk(const Chunk& chunk); 1231c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1241c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Write all buffered chunks from all tracks 1251c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void writeChunks(); 1261c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1271c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Write a chunk if there is one 1281c9747a4653aec1395c2bd6896c9b87cb5447837James Dong status_t writeOneChunk(); 1291c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1301c9747a4653aec1395c2bd6896c9b87cb5447837James Dong // Write the first chunk from the given ChunkInfo. 1311c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void writeFirstChunk(ChunkInfo* info); 1321c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 133e259531ce59ab1f31de5a23124b22536f6a5a767James Dong // Adjust other track media clock (presumably wall clock) 134e259531ce59ab1f31de5a23124b22536f6a5a767James Dong // based on audio track media clock with the drift time. 135e259531ce59ab1f31de5a23124b22536f6a5a767James Dong int64_t mDriftTimeUs; 136d707fcb3e29707ca4a5935c294ef0b38eb5aba5fJames Dong void setDriftTimeUs(int64_t driftTimeUs); 137e259531ce59ab1f31de5a23124b22536f6a5a767James Dong int64_t getDriftTimeUs(); 138e259531ce59ab1f31de5a23124b22536f6a5a767James Dong 139b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong // Return whether the nal length is 4 bytes or 2 bytes 140b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong // Only makes sense for H.264/AVC 141b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong bool useNalLengthFour(); 142b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong 14313aec890216948b0c364f8f92792129d0335f506James Dong void lock(); 14413aec890216948b0c364f8f92792129d0335f506James Dong void unlock(); 14513aec890216948b0c364f8f92792129d0335f506James Dong 14613aec890216948b0c364f8f92792129d0335f506James Dong // Acquire lock before calling these methods 14713aec890216948b0c364f8f92792129d0335f506James Dong off_t addSample_l(MediaBuffer *buffer); 14813aec890216948b0c364f8f92792129d0335f506James Dong off_t addLengthPrefixedSample_l(MediaBuffer *buffer); 14920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1507837c17063a4c50bc856ba59418516fdab731de7James Dong inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream); 151d599cd4573b5a2d5914c5040e0565ef866749b77James Dong bool exceedsFileSizeLimit(); 1521f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong bool use32BitFileOffset() const; 153d599cd4573b5a2d5914c5040e0565ef866749b77James Dong bool exceedsFileDurationLimit(); 154faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong void trackProgressStatus(const Track* track, int64_t timeUs, status_t err = OK); 1557837c17063a4c50bc856ba59418516fdab731de7James Dong 15620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MPEG4Writer(const MPEG4Writer &); 15720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MPEG4Writer &operator=(const MPEG4Writer &); 15820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}; 15920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 16020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} // namespace android 16120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 16220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#endif // MPEG4_WRITER_H_ 163