MPEG4Writer.cpp revision a0c0a9ab441fc3cbb302c7a2e783f6d4c3b63bed
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//#define LOG_NDEBUG 0
18#define LOG_TAG "MPEG4Writer"
19#include <inttypes.h>
20#include <utils/Log.h>
21
22#include <arpa/inet.h>
23
24#include <pthread.h>
25#include <sys/prctl.h>
26
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/MPEG4Writer.h>
29#include <media/stagefright/MediaBuffer.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/MediaDefs.h>
32#include <media/stagefright/MediaErrors.h>
33#include <media/stagefright/MediaSource.h>
34#include <media/stagefright/Utils.h>
35#include <media/mediarecorder.h>
36#include <cutils/properties.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <unistd.h>
41
42#include "include/ESDS.h"
43
44namespace android {
45
46static const int64_t kMinStreamableFileSizeInBytes = 5 * 1024 * 1024;
47static const int64_t kMax32BitFileSize = 0x00ffffffffLL; // 2^32-1 : max FAT32
48                                                         // filesystem file size
49                                                         // used by most SD cards
50static const uint8_t kNalUnitTypeSeqParamSet = 0x07;
51static const uint8_t kNalUnitTypePicParamSet = 0x08;
52static const int64_t kInitialDelayTimeUs     = 700000LL;
53
54class MPEG4Writer::Track {
55public:
56    Track(MPEG4Writer *owner, const sp<MediaSource> &source, size_t trackId);
57
58    ~Track();
59
60    status_t start(MetaData *params);
61    status_t stop();
62    status_t pause();
63    bool reachedEOS();
64
65    int64_t getDurationUs() const;
66    int64_t getEstimatedTrackSizeBytes() const;
67    void writeTrackHeader(bool use32BitOffset = true);
68    void bufferChunk(int64_t timestampUs);
69    bool isAvc() const { return mIsAvc; }
70    bool isAudio() const { return mIsAudio; }
71    bool isMPEG4() const { return mIsMPEG4; }
72    void addChunkOffset(off64_t offset);
73    int32_t getTrackId() const { return mTrackId; }
74    status_t dump(int fd, const Vector<String16>& args) const;
75
76private:
77    enum {
78        kMaxCttsOffsetTimeUs = 1000000LL,  // 1 second
79        kSampleArraySize = 1000,
80    };
81
82    // A helper class to handle faster write box with table entries
83    template<class TYPE>
84    struct ListTableEntries {
85        ListTableEntries(uint32_t elementCapacity, uint32_t entryCapacity)
86            : mElementCapacity(elementCapacity),
87            mEntryCapacity(entryCapacity),
88            mTotalNumTableEntries(0),
89            mNumValuesInCurrEntry(0),
90            mCurrTableEntriesElement(NULL) {
91            CHECK_GT(mElementCapacity, 0);
92            CHECK_GT(mEntryCapacity, 0);
93        }
94
95        // Free the allocated memory.
96        ~ListTableEntries() {
97            while (!mTableEntryList.empty()) {
98                typename List<TYPE *>::iterator it = mTableEntryList.begin();
99                delete[] (*it);
100                mTableEntryList.erase(it);
101            }
102        }
103
104        // Replace the value at the given position by the given value.
105        // There must be an existing value at the given position.
106        // @arg value must be in network byte order
107        // @arg pos location the value must be in.
108        void set(const TYPE& value, uint32_t pos) {
109            CHECK_LT(pos, mTotalNumTableEntries * mEntryCapacity);
110
111            typename List<TYPE *>::iterator it = mTableEntryList.begin();
112            uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity));
113            while (it != mTableEntryList.end() && iterations > 0) {
114                ++it;
115                --iterations;
116            }
117            CHECK(it != mTableEntryList.end());
118            CHECK_EQ(iterations, 0);
119
120            (*it)[(pos % (mElementCapacity * mEntryCapacity))] = value;
121        }
122
123        // Get the value at the given position by the given value.
124        // @arg value the retrieved value at the position in network byte order.
125        // @arg pos location the value must be in.
126        // @return true if a value is found.
127        bool get(TYPE& value, uint32_t pos) const {
128            if (pos >= mTotalNumTableEntries * mEntryCapacity) {
129                return false;
130            }
131
132            typename List<TYPE *>::iterator it = mTableEntryList.begin();
133            uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity));
134            while (it != mTableEntryList.end() && iterations > 0) {
135                ++it;
136                --iterations;
137            }
138            CHECK(it != mTableEntryList.end());
139            CHECK_EQ(iterations, 0);
140
141            value = (*it)[(pos % (mElementCapacity * mEntryCapacity))];
142            return true;
143        }
144
145        // Store a single value.
146        // @arg value must be in network byte order.
147        void add(const TYPE& value) {
148            CHECK_LT(mNumValuesInCurrEntry, mElementCapacity);
149            uint32_t nEntries = mTotalNumTableEntries % mElementCapacity;
150            uint32_t nValues  = mNumValuesInCurrEntry % mEntryCapacity;
151            if (nEntries == 0 && nValues == 0) {
152                mCurrTableEntriesElement = new TYPE[mEntryCapacity * mElementCapacity];
153                CHECK(mCurrTableEntriesElement != NULL);
154                mTableEntryList.push_back(mCurrTableEntriesElement);
155            }
156
157            uint32_t pos = nEntries * mEntryCapacity + nValues;
158            mCurrTableEntriesElement[pos] = value;
159
160            ++mNumValuesInCurrEntry;
161            if ((mNumValuesInCurrEntry % mEntryCapacity) == 0) {
162                ++mTotalNumTableEntries;
163                mNumValuesInCurrEntry = 0;
164            }
165        }
166
167        // Write out the table entries:
168        // 1. the number of entries goes first
169        // 2. followed by the values in the table enties in order
170        // @arg writer the writer to actual write to the storage
171        void write(MPEG4Writer *writer) const {
172            CHECK_EQ(mNumValuesInCurrEntry % mEntryCapacity, 0);
173            uint32_t nEntries = mTotalNumTableEntries;
174            writer->writeInt32(nEntries);
175            for (typename List<TYPE *>::iterator it = mTableEntryList.begin();
176                it != mTableEntryList.end(); ++it) {
177                CHECK_GT(nEntries, 0);
178                if (nEntries >= mElementCapacity) {
179                    writer->write(*it, sizeof(TYPE) * mEntryCapacity, mElementCapacity);
180                    nEntries -= mElementCapacity;
181                } else {
182                    writer->write(*it, sizeof(TYPE) * mEntryCapacity, nEntries);
183                    break;
184                }
185            }
186        }
187
188        // Return the number of entries in the table.
189        uint32_t count() const { return mTotalNumTableEntries; }
190
191    private:
192        uint32_t         mElementCapacity;  // # entries in an element
193        uint32_t         mEntryCapacity;    // # of values in each entry
194        uint32_t         mTotalNumTableEntries;
195        uint32_t         mNumValuesInCurrEntry;  // up to mEntryCapacity
196        TYPE             *mCurrTableEntriesElement;
197        mutable List<TYPE *>     mTableEntryList;
198
199        DISALLOW_EVIL_CONSTRUCTORS(ListTableEntries);
200    };
201
202
203
204    MPEG4Writer *mOwner;
205    sp<MetaData> mMeta;
206    sp<MediaSource> mSource;
207    volatile bool mDone;
208    volatile bool mPaused;
209    volatile bool mResumed;
210    volatile bool mStarted;
211    bool mIsAvc;
212    bool mIsAudio;
213    bool mIsMPEG4;
214    int32_t mTrackId;
215    int64_t mTrackDurationUs;
216    int64_t mMaxChunkDurationUs;
217
218    int64_t mEstimatedTrackSizeBytes;
219    int64_t mMdatSizeBytes;
220    int32_t mTimeScale;
221
222    pthread_t mThread;
223
224
225    List<MediaBuffer *> mChunkSamples;
226
227    bool                mSamplesHaveSameSize;
228    ListTableEntries<uint32_t> *mStszTableEntries;
229
230    ListTableEntries<uint32_t> *mStcoTableEntries;
231    ListTableEntries<off64_t> *mCo64TableEntries;
232    ListTableEntries<uint32_t> *mStscTableEntries;
233    ListTableEntries<uint32_t> *mStssTableEntries;
234    ListTableEntries<uint32_t> *mSttsTableEntries;
235    ListTableEntries<uint32_t> *mCttsTableEntries;
236
237    int64_t mMinCttsOffsetTimeUs;
238    int64_t mMaxCttsOffsetTimeUs;
239
240    // Sequence parameter set or picture parameter set
241    struct AVCParamSet {
242        AVCParamSet(uint16_t length, const uint8_t *data)
243            : mLength(length), mData(data) {}
244
245        uint16_t mLength;
246        const uint8_t *mData;
247    };
248    List<AVCParamSet> mSeqParamSets;
249    List<AVCParamSet> mPicParamSets;
250    uint8_t mProfileIdc;
251    uint8_t mProfileCompatible;
252    uint8_t mLevelIdc;
253
254    void *mCodecSpecificData;
255    size_t mCodecSpecificDataSize;
256    bool mGotAllCodecSpecificData;
257    bool mTrackingProgressStatus;
258
259    bool mReachedEOS;
260    int64_t mStartTimestampUs;
261    int64_t mStartTimeRealUs;
262    int64_t mFirstSampleTimeRealUs;
263    int64_t mPreviousTrackTimeUs;
264    int64_t mTrackEveryTimeDurationUs;
265
266    // Update the audio track's drift information.
267    void updateDriftTime(const sp<MetaData>& meta);
268
269    int32_t getStartTimeOffsetScaledTime() const;
270
271    static void *ThreadWrapper(void *me);
272    status_t threadEntry();
273
274    const uint8_t *parseParamSet(
275        const uint8_t *data, size_t length, int type, size_t *paramSetLen);
276
277    status_t makeAVCCodecSpecificData(const uint8_t *data, size_t size);
278    status_t copyAVCCodecSpecificData(const uint8_t *data, size_t size);
279    status_t parseAVCCodecSpecificData(const uint8_t *data, size_t size);
280
281    // Track authoring progress status
282    void trackProgressStatus(int64_t timeUs, status_t err = OK);
283    void initTrackingProgressStatus(MetaData *params);
284
285    void getCodecSpecificDataFromInputFormatIfPossible();
286
287    // Determine the track time scale
288    // If it is an audio track, try to use the sampling rate as
289    // the time scale; however, if user chooses the overwrite
290    // value, the user-supplied time scale will be used.
291    void setTimeScale();
292
293    // Simple validation on the codec specific data
294    status_t checkCodecSpecificData() const;
295    int32_t mRotation;
296
297    void updateTrackSizeEstimate();
298    void addOneStscTableEntry(size_t chunkId, size_t sampleId);
299    void addOneStssTableEntry(size_t sampleId);
300
301    // Duration is time scale based
302    void addOneSttsTableEntry(size_t sampleCount, int32_t timescaledDur);
303    void addOneCttsTableEntry(size_t sampleCount, int32_t timescaledDur);
304
305    bool isTrackMalFormed() const;
306    void sendTrackSummary(bool hasMultipleTracks);
307
308    // Write the boxes
309    void writeStcoBox(bool use32BitOffset);
310    void writeStscBox();
311    void writeStszBox();
312    void writeStssBox();
313    void writeSttsBox();
314    void writeCttsBox();
315    void writeD263Box();
316    void writePaspBox();
317    void writeAvccBox();
318    void writeUrlBox();
319    void writeDrefBox();
320    void writeDinfBox();
321    void writeDamrBox();
322    void writeMdhdBox(uint32_t now);
323    void writeSmhdBox();
324    void writeVmhdBox();
325    void writeHdlrBox();
326    void writeTkhdBox(uint32_t now);
327    void writeMp4aEsdsBox();
328    void writeMp4vEsdsBox();
329    void writeAudioFourCCBox();
330    void writeVideoFourCCBox();
331    void writeStblBox(bool use32BitOffset);
332
333    Track(const Track &);
334    Track &operator=(const Track &);
335};
336
337MPEG4Writer::MPEG4Writer(const char *filename)
338    : mFd(-1),
339      mInitCheck(NO_INIT),
340      mIsRealTimeRecording(true),
341      mUse4ByteNalLength(true),
342      mUse32BitOffset(true),
343      mIsFileSizeLimitExplicitlyRequested(false),
344      mPaused(false),
345      mStarted(false),
346      mWriterThreadStarted(false),
347      mOffset(0),
348      mMdatOffset(0),
349      mEstimatedMoovBoxSize(0),
350      mInterleaveDurationUs(1000000),
351      mLatitudex10000(0),
352      mLongitudex10000(0),
353      mAreGeoTagsAvailable(false),
354      mStartTimeOffsetMs(-1) {
355
356    mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
357    if (mFd >= 0) {
358        mInitCheck = OK;
359    }
360}
361
362MPEG4Writer::MPEG4Writer(int fd)
363    : mFd(dup(fd)),
364      mInitCheck(mFd < 0? NO_INIT: OK),
365      mIsRealTimeRecording(true),
366      mUse4ByteNalLength(true),
367      mUse32BitOffset(true),
368      mIsFileSizeLimitExplicitlyRequested(false),
369      mPaused(false),
370      mStarted(false),
371      mWriterThreadStarted(false),
372      mOffset(0),
373      mMdatOffset(0),
374      mEstimatedMoovBoxSize(0),
375      mInterleaveDurationUs(1000000),
376      mLatitudex10000(0),
377      mLongitudex10000(0),
378      mAreGeoTagsAvailable(false),
379      mStartTimeOffsetMs(-1) {
380}
381
382MPEG4Writer::~MPEG4Writer() {
383    reset();
384
385    while (!mTracks.empty()) {
386        List<Track *>::iterator it = mTracks.begin();
387        delete *it;
388        (*it) = NULL;
389        mTracks.erase(it);
390    }
391    mTracks.clear();
392}
393
394status_t MPEG4Writer::dump(
395        int fd, const Vector<String16>& args) {
396    const size_t SIZE = 256;
397    char buffer[SIZE];
398    String8 result;
399    snprintf(buffer, SIZE, "   MPEG4Writer %p\n", this);
400    result.append(buffer);
401    snprintf(buffer, SIZE, "     mStarted: %s\n", mStarted? "true": "false");
402    result.append(buffer);
403    ::write(fd, result.string(), result.size());
404    for (List<Track *>::iterator it = mTracks.begin();
405         it != mTracks.end(); ++it) {
406        (*it)->dump(fd, args);
407    }
408    return OK;
409}
410
411status_t MPEG4Writer::Track::dump(
412        int fd, const Vector<String16>& /* args */) const {
413    const size_t SIZE = 256;
414    char buffer[SIZE];
415    String8 result;
416    snprintf(buffer, SIZE, "     %s track\n", mIsAudio? "Audio": "Video");
417    result.append(buffer);
418    snprintf(buffer, SIZE, "       reached EOS: %s\n",
419            mReachedEOS? "true": "false");
420    result.append(buffer);
421    snprintf(buffer, SIZE, "       frames encoded : %d\n", mStszTableEntries->count());
422    result.append(buffer);
423    snprintf(buffer, SIZE, "       duration encoded : %" PRId64 " us\n", mTrackDurationUs);
424    result.append(buffer);
425    ::write(fd, result.string(), result.size());
426    return OK;
427}
428
429status_t MPEG4Writer::addSource(const sp<MediaSource> &source) {
430    Mutex::Autolock l(mLock);
431    if (mStarted) {
432        ALOGE("Attempt to add source AFTER recording is started");
433        return UNKNOWN_ERROR;
434    }
435
436    // At most 2 tracks can be supported.
437    if (mTracks.size() >= 2) {
438        ALOGE("Too many tracks (%d) to add", mTracks.size());
439        return ERROR_UNSUPPORTED;
440    }
441
442    CHECK(source.get() != NULL);
443
444    // A track of type other than video or audio is not supported.
445    const char *mime;
446    source->getFormat()->findCString(kKeyMIMEType, &mime);
447    bool isAudio = !strncasecmp(mime, "audio/", 6);
448    bool isVideo = !strncasecmp(mime, "video/", 6);
449    if (!isAudio && !isVideo) {
450        ALOGE("Track (%s) other than video or audio is not supported",
451            mime);
452        return ERROR_UNSUPPORTED;
453    }
454
455    // At this point, we know the track to be added is either
456    // video or audio. Thus, we only need to check whether it
457    // is an audio track or not (if it is not, then it must be
458    // a video track).
459
460    // No more than one video or one audio track is supported.
461    for (List<Track*>::iterator it = mTracks.begin();
462         it != mTracks.end(); ++it) {
463        if ((*it)->isAudio() == isAudio) {
464            ALOGE("%s track already exists", isAudio? "Audio": "Video");
465            return ERROR_UNSUPPORTED;
466        }
467    }
468
469    // This is the first track of either audio or video.
470    // Go ahead to add the track.
471    Track *track = new Track(this, source, 1 + mTracks.size());
472    mTracks.push_back(track);
473
474    return OK;
475}
476
477status_t MPEG4Writer::startTracks(MetaData *params) {
478    if (mTracks.empty()) {
479        ALOGE("No source added");
480        return INVALID_OPERATION;
481    }
482
483    for (List<Track *>::iterator it = mTracks.begin();
484         it != mTracks.end(); ++it) {
485        status_t err = (*it)->start(params);
486
487        if (err != OK) {
488            for (List<Track *>::iterator it2 = mTracks.begin();
489                 it2 != it; ++it2) {
490                (*it2)->stop();
491            }
492
493            return err;
494        }
495    }
496    return OK;
497}
498
499int64_t MPEG4Writer::estimateMoovBoxSize(int32_t bitRate) {
500    // This implementation is highly experimental/heurisitic.
501    //
502    // Statistical analysis shows that metadata usually accounts
503    // for a small portion of the total file size, usually < 0.6%.
504
505    // The default MIN_MOOV_BOX_SIZE is set to 0.6% x 1MB / 2,
506    // where 1MB is the common file size limit for MMS application.
507    // The default MAX _MOOV_BOX_SIZE value is based on about 3
508    // minute video recording with a bit rate about 3 Mbps, because
509    // statistics also show that most of the video captured are going
510    // to be less than 3 minutes.
511
512    // If the estimation is wrong, we will pay the price of wasting
513    // some reserved space. This should not happen so often statistically.
514    static const int32_t factor = mUse32BitOffset? 1: 2;
515    static const int64_t MIN_MOOV_BOX_SIZE = 3 * 1024;  // 3 KB
516    static const int64_t MAX_MOOV_BOX_SIZE = (180 * 3000000 * 6LL / 8000);
517    int64_t size = MIN_MOOV_BOX_SIZE;
518
519    // Max file size limit is set
520    if (mMaxFileSizeLimitBytes != 0 && mIsFileSizeLimitExplicitlyRequested) {
521        size = mMaxFileSizeLimitBytes * 6 / 1000;
522    }
523
524    // Max file duration limit is set
525    if (mMaxFileDurationLimitUs != 0) {
526        if (bitRate > 0) {
527            int64_t size2 =
528                ((mMaxFileDurationLimitUs * bitRate * 6) / 1000 / 8000000);
529            if (mMaxFileSizeLimitBytes != 0 && mIsFileSizeLimitExplicitlyRequested) {
530                // When both file size and duration limits are set,
531                // we use the smaller limit of the two.
532                if (size > size2) {
533                    size = size2;
534                }
535            } else {
536                // Only max file duration limit is set
537                size = size2;
538            }
539        }
540    }
541
542    if (size < MIN_MOOV_BOX_SIZE) {
543        size = MIN_MOOV_BOX_SIZE;
544    }
545
546    // Any long duration recording will be probably end up with
547    // non-streamable mp4 file.
548    if (size > MAX_MOOV_BOX_SIZE) {
549        size = MAX_MOOV_BOX_SIZE;
550    }
551
552    ALOGI("limits: %lld/%lld bytes/us, bit rate: %d bps and the estimated"
553         " moov size %lld bytes",
554         mMaxFileSizeLimitBytes, mMaxFileDurationLimitUs, bitRate, size);
555    return factor * size;
556}
557
558status_t MPEG4Writer::start(MetaData *param) {
559    if (mInitCheck != OK) {
560        return UNKNOWN_ERROR;
561    }
562
563    /*
564     * Check mMaxFileSizeLimitBytes at the beginning
565     * since mMaxFileSizeLimitBytes may be implicitly
566     * changed later for 32-bit file offset even if
567     * user does not ask to set it explicitly.
568     */
569    if (mMaxFileSizeLimitBytes != 0) {
570        mIsFileSizeLimitExplicitlyRequested = true;
571    }
572
573    int32_t use64BitOffset;
574    if (param &&
575        param->findInt32(kKey64BitFileOffset, &use64BitOffset) &&
576        use64BitOffset) {
577        mUse32BitOffset = false;
578    }
579
580    if (mUse32BitOffset) {
581        // Implicit 32 bit file size limit
582        if (mMaxFileSizeLimitBytes == 0) {
583            mMaxFileSizeLimitBytes = kMax32BitFileSize;
584        }
585
586        // If file size is set to be larger than the 32 bit file
587        // size limit, treat it as an error.
588        if (mMaxFileSizeLimitBytes > kMax32BitFileSize) {
589            ALOGW("32-bit file size limit (%lld bytes) too big. "
590                 "It is changed to %lld bytes",
591                mMaxFileSizeLimitBytes, kMax32BitFileSize);
592            mMaxFileSizeLimitBytes = kMax32BitFileSize;
593        }
594    }
595
596    int32_t use2ByteNalLength;
597    if (param &&
598        param->findInt32(kKey2ByteNalLength, &use2ByteNalLength) &&
599        use2ByteNalLength) {
600        mUse4ByteNalLength = false;
601    }
602
603    int32_t isRealTimeRecording;
604    if (param && param->findInt32(kKeyRealTimeRecording, &isRealTimeRecording)) {
605        mIsRealTimeRecording = isRealTimeRecording;
606    }
607
608    mStartTimestampUs = -1;
609
610    if (mStarted) {
611        if (mPaused) {
612            mPaused = false;
613            return startTracks(param);
614        }
615        return OK;
616    }
617
618    if (!param ||
619        !param->findInt32(kKeyTimeScale, &mTimeScale)) {
620        mTimeScale = 1000;
621    }
622    CHECK_GT(mTimeScale, 0);
623    ALOGV("movie time scale: %d", mTimeScale);
624
625    /*
626     * When the requested file size limit is small, the priority
627     * is to meet the file size limit requirement, rather than
628     * to make the file streamable. mStreamableFile does not tell
629     * whether the actual recorded file is streamable or not.
630     */
631    mStreamableFile =
632        (mMaxFileSizeLimitBytes != 0 &&
633         mMaxFileSizeLimitBytes >= kMinStreamableFileSizeInBytes);
634
635    /*
636     * mWriteMoovBoxToMemory is true if the amount of data in moov box is
637     * smaller than the reserved free space at the beginning of a file, AND
638     * when the content of moov box is constructed. Note that video/audio
639     * frame data is always written to the file but not in the memory.
640     *
641     * Before stop()/reset() is called, mWriteMoovBoxToMemory is always
642     * false. When reset() is called at the end of a recording session,
643     * Moov box needs to be constructed.
644     *
645     * 1) Right before a moov box is constructed, mWriteMoovBoxToMemory
646     * to set to mStreamableFile so that if
647     * the file is intended to be streamable, it is set to true;
648     * otherwise, it is set to false. When the value is set to false,
649     * all the content of the moov box is written immediately to
650     * the end of the file. When the value is set to true, all the
651     * content of the moov box is written to an in-memory cache,
652     * mMoovBoxBuffer, util the following condition happens. Note
653     * that the size of the in-memory cache is the same as the
654     * reserved free space at the beginning of the file.
655     *
656     * 2) While the data of the moov box is written to an in-memory
657     * cache, the data size is checked against the reserved space.
658     * If the data size surpasses the reserved space, subsequent moov
659     * data could no longer be hold in the in-memory cache. This also
660     * indicates that the reserved space was too small. At this point,
661     * _all_ moov data must be written to the end of the file.
662     * mWriteMoovBoxToMemory must be set to false to direct the write
663     * to the file.
664     *
665     * 3) If the data size in moov box is smaller than the reserved
666     * space after moov box is completely constructed, the in-memory
667     * cache copy of the moov box is written to the reserved free
668     * space. Thus, immediately after the moov is completedly
669     * constructed, mWriteMoovBoxToMemory is always set to false.
670     */
671    mWriteMoovBoxToMemory = false;
672    mMoovBoxBuffer = NULL;
673    mMoovBoxBufferOffset = 0;
674
675    writeFtypBox(param);
676
677    mFreeBoxOffset = mOffset;
678
679    if (mEstimatedMoovBoxSize == 0) {
680        int32_t bitRate = -1;
681        if (param) {
682            param->findInt32(kKeyBitRate, &bitRate);
683        }
684        mEstimatedMoovBoxSize = estimateMoovBoxSize(bitRate);
685    }
686    CHECK_GE(mEstimatedMoovBoxSize, 8);
687    if (mStreamableFile) {
688        // Reserve a 'free' box only for streamable file
689        lseek64(mFd, mFreeBoxOffset, SEEK_SET);
690        writeInt32(mEstimatedMoovBoxSize);
691        write("free", 4);
692        mMdatOffset = mFreeBoxOffset + mEstimatedMoovBoxSize;
693    } else {
694        mMdatOffset = mOffset;
695    }
696
697    mOffset = mMdatOffset;
698    lseek64(mFd, mMdatOffset, SEEK_SET);
699    if (mUse32BitOffset) {
700        write("????mdat", 8);
701    } else {
702        write("\x00\x00\x00\x01mdat????????", 16);
703    }
704
705    status_t err = startWriterThread();
706    if (err != OK) {
707        return err;
708    }
709
710    err = startTracks(param);
711    if (err != OK) {
712        return err;
713    }
714
715    mStarted = true;
716    return OK;
717}
718
719bool MPEG4Writer::use32BitFileOffset() const {
720    return mUse32BitOffset;
721}
722
723status_t MPEG4Writer::pause() {
724    if (mInitCheck != OK) {
725        return OK;
726    }
727    mPaused = true;
728    status_t err = OK;
729    for (List<Track *>::iterator it = mTracks.begin();
730         it != mTracks.end(); ++it) {
731        status_t status = (*it)->pause();
732        if (status != OK) {
733            err = status;
734        }
735    }
736    return err;
737}
738
739void MPEG4Writer::stopWriterThread() {
740    ALOGD("Stopping writer thread");
741    if (!mWriterThreadStarted) {
742        return;
743    }
744
745    {
746        Mutex::Autolock autolock(mLock);
747
748        mDone = true;
749        mChunkReadyCondition.signal();
750    }
751
752    void *dummy;
753    pthread_join(mThread, &dummy);
754    mWriterThreadStarted = false;
755    ALOGD("Writer thread stopped");
756}
757
758/*
759 * MP4 file standard defines a composition matrix:
760 * | a  b  u |
761 * | c  d  v |
762 * | x  y  w |
763 *
764 * the element in the matrix is stored in the following
765 * order: {a, b, u, c, d, v, x, y, w},
766 * where a, b, c, d, x, and y is in 16.16 format, while
767 * u, v and w is in 2.30 format.
768 */
769void MPEG4Writer::writeCompositionMatrix(int degrees) {
770    ALOGV("writeCompositionMatrix");
771    uint32_t a = 0x00010000;
772    uint32_t b = 0;
773    uint32_t c = 0;
774    uint32_t d = 0x00010000;
775    switch (degrees) {
776        case 0:
777            break;
778        case 90:
779            a = 0;
780            b = 0x00010000;
781            c = 0xFFFF0000;
782            d = 0;
783            break;
784        case 180:
785            a = 0xFFFF0000;
786            d = 0xFFFF0000;
787            break;
788        case 270:
789            a = 0;
790            b = 0xFFFF0000;
791            c = 0x00010000;
792            d = 0;
793            break;
794        default:
795            CHECK(!"Should never reach this unknown rotation");
796            break;
797    }
798
799    writeInt32(a);           // a
800    writeInt32(b);           // b
801    writeInt32(0);           // u
802    writeInt32(c);           // c
803    writeInt32(d);           // d
804    writeInt32(0);           // v
805    writeInt32(0);           // x
806    writeInt32(0);           // y
807    writeInt32(0x40000000);  // w
808}
809
810void MPEG4Writer::release() {
811    close(mFd);
812    mFd = -1;
813    mInitCheck = NO_INIT;
814    mStarted = false;
815}
816
817status_t MPEG4Writer::reset() {
818    if (mInitCheck != OK) {
819        return OK;
820    } else {
821        if (!mWriterThreadStarted ||
822            !mStarted) {
823            if (mWriterThreadStarted) {
824                stopWriterThread();
825            }
826            release();
827            return OK;
828        }
829    }
830
831    status_t err = OK;
832    int64_t maxDurationUs = 0;
833    int64_t minDurationUs = 0x7fffffffffffffffLL;
834    for (List<Track *>::iterator it = mTracks.begin();
835         it != mTracks.end(); ++it) {
836        status_t status = (*it)->stop();
837        if (err == OK && status != OK) {
838            err = status;
839        }
840
841        int64_t durationUs = (*it)->getDurationUs();
842        if (durationUs > maxDurationUs) {
843            maxDurationUs = durationUs;
844        }
845        if (durationUs < minDurationUs) {
846            minDurationUs = durationUs;
847        }
848    }
849
850    if (mTracks.size() > 1) {
851        ALOGD("Duration from tracks range is [%lld, %lld] us",
852            minDurationUs, maxDurationUs);
853    }
854
855    stopWriterThread();
856
857    // Do not write out movie header on error.
858    if (err != OK) {
859        release();
860        return err;
861    }
862
863    // Fix up the size of the 'mdat' chunk.
864    if (mUse32BitOffset) {
865        lseek64(mFd, mMdatOffset, SEEK_SET);
866        uint32_t size = htonl(static_cast<uint32_t>(mOffset - mMdatOffset));
867        ::write(mFd, &size, 4);
868    } else {
869        lseek64(mFd, mMdatOffset + 8, SEEK_SET);
870        uint64_t size = mOffset - mMdatOffset;
871        size = hton64(size);
872        ::write(mFd, &size, 8);
873    }
874    lseek64(mFd, mOffset, SEEK_SET);
875
876    // Construct moov box now
877    mMoovBoxBufferOffset = 0;
878    mWriteMoovBoxToMemory = mStreamableFile;
879    if (mWriteMoovBoxToMemory) {
880        // There is no need to allocate in-memory cache
881        // for moov box if the file is not streamable.
882
883        mMoovBoxBuffer = (uint8_t *) malloc(mEstimatedMoovBoxSize);
884        CHECK(mMoovBoxBuffer != NULL);
885    }
886    writeMoovBox(maxDurationUs);
887
888    // mWriteMoovBoxToMemory could be set to false in
889    // MPEG4Writer::write() method
890    if (mWriteMoovBoxToMemory) {
891        mWriteMoovBoxToMemory = false;
892        // Content of the moov box is saved in the cache, and the in-memory
893        // moov box needs to be written to the file in a single shot.
894
895        CHECK_LE(mMoovBoxBufferOffset + 8, mEstimatedMoovBoxSize);
896
897        // Moov box
898        lseek64(mFd, mFreeBoxOffset, SEEK_SET);
899        mOffset = mFreeBoxOffset;
900        write(mMoovBoxBuffer, 1, mMoovBoxBufferOffset);
901
902        // Free box
903        lseek64(mFd, mOffset, SEEK_SET);
904        writeInt32(mEstimatedMoovBoxSize - mMoovBoxBufferOffset);
905        write("free", 4);
906    } else {
907        ALOGI("The mp4 file will not be streamable.");
908    }
909
910    // Free in-memory cache for moov box
911    if (mMoovBoxBuffer != NULL) {
912        free(mMoovBoxBuffer);
913        mMoovBoxBuffer = NULL;
914        mMoovBoxBufferOffset = 0;
915    }
916
917    CHECK(mBoxes.empty());
918
919    release();
920    return err;
921}
922
923uint32_t MPEG4Writer::getMpeg4Time() {
924    time_t now = time(NULL);
925    // MP4 file uses time counting seconds since midnight, Jan. 1, 1904
926    // while time function returns Unix epoch values which starts
927    // at 1970-01-01. Lets add the number of seconds between them
928    uint32_t mpeg4Time = now + (66 * 365 + 17) * (24 * 60 * 60);
929    return mpeg4Time;
930}
931
932void MPEG4Writer::writeMvhdBox(int64_t durationUs) {
933    uint32_t now = getMpeg4Time();
934    beginBox("mvhd");
935    writeInt32(0);             // version=0, flags=0
936    writeInt32(now);           // creation time
937    writeInt32(now);           // modification time
938    writeInt32(mTimeScale);    // mvhd timescale
939    int32_t duration = (durationUs * mTimeScale + 5E5) / 1E6;
940    writeInt32(duration);
941    writeInt32(0x10000);       // rate: 1.0
942    writeInt16(0x100);         // volume
943    writeInt16(0);             // reserved
944    writeInt32(0);             // reserved
945    writeInt32(0);             // reserved
946    writeCompositionMatrix(0); // matrix
947    writeInt32(0);             // predefined
948    writeInt32(0);             // predefined
949    writeInt32(0);             // predefined
950    writeInt32(0);             // predefined
951    writeInt32(0);             // predefined
952    writeInt32(0);             // predefined
953    writeInt32(mTracks.size() + 1);  // nextTrackID
954    endBox();  // mvhd
955}
956
957void MPEG4Writer::writeMoovBox(int64_t durationUs) {
958    beginBox("moov");
959    writeMvhdBox(durationUs);
960    if (mAreGeoTagsAvailable) {
961        writeUdtaBox();
962    }
963    int32_t id = 1;
964    for (List<Track *>::iterator it = mTracks.begin();
965        it != mTracks.end(); ++it, ++id) {
966        (*it)->writeTrackHeader(mUse32BitOffset);
967    }
968    endBox();  // moov
969}
970
971void MPEG4Writer::writeFtypBox(MetaData *param) {
972    beginBox("ftyp");
973
974    int32_t fileType;
975    if (param && param->findInt32(kKeyFileType, &fileType) &&
976        fileType != OUTPUT_FORMAT_MPEG_4) {
977        writeFourcc("3gp4");
978        writeInt32(0);
979        writeFourcc("isom");
980        writeFourcc("3gp4");
981    } else {
982        writeFourcc("mp42");
983        writeInt32(0);
984        writeFourcc("isom");
985        writeFourcc("mp42");
986    }
987
988    endBox();
989}
990
991static bool isTestModeEnabled() {
992#if (PROPERTY_VALUE_MAX < 5)
993#error "PROPERTY_VALUE_MAX must be at least 5"
994#endif
995
996    // Test mode is enabled only if rw.media.record.test system
997    // property is enabled.
998    char value[PROPERTY_VALUE_MAX];
999    if (property_get("rw.media.record.test", value, NULL) &&
1000        (!strcasecmp(value, "true") || !strcasecmp(value, "1"))) {
1001        return true;
1002    }
1003    return false;
1004}
1005
1006void MPEG4Writer::sendSessionSummary() {
1007    // Send session summary only if test mode is enabled
1008    if (!isTestModeEnabled()) {
1009        return;
1010    }
1011
1012    for (List<ChunkInfo>::iterator it = mChunkInfos.begin();
1013         it != mChunkInfos.end(); ++it) {
1014        int trackNum = it->mTrack->getTrackId() << 28;
1015        notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
1016                trackNum | MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS,
1017                it->mMaxInterChunkDurUs);
1018    }
1019}
1020
1021status_t MPEG4Writer::setInterleaveDuration(uint32_t durationUs) {
1022    mInterleaveDurationUs = durationUs;
1023    return OK;
1024}
1025
1026void MPEG4Writer::lock() {
1027    mLock.lock();
1028}
1029
1030void MPEG4Writer::unlock() {
1031    mLock.unlock();
1032}
1033
1034off64_t MPEG4Writer::addSample_l(MediaBuffer *buffer) {
1035    off64_t old_offset = mOffset;
1036
1037    ::write(mFd,
1038          (const uint8_t *)buffer->data() + buffer->range_offset(),
1039          buffer->range_length());
1040
1041    mOffset += buffer->range_length();
1042
1043    return old_offset;
1044}
1045
1046static void StripStartcode(MediaBuffer *buffer) {
1047    if (buffer->range_length() < 4) {
1048        return;
1049    }
1050
1051    const uint8_t *ptr =
1052        (const uint8_t *)buffer->data() + buffer->range_offset();
1053
1054    if (!memcmp(ptr, "\x00\x00\x00\x01", 4)) {
1055        buffer->set_range(
1056                buffer->range_offset() + 4, buffer->range_length() - 4);
1057    }
1058}
1059
1060off64_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) {
1061    off64_t old_offset = mOffset;
1062
1063    size_t length = buffer->range_length();
1064
1065    if (mUse4ByteNalLength) {
1066        uint8_t x = length >> 24;
1067        ::write(mFd, &x, 1);
1068        x = (length >> 16) & 0xff;
1069        ::write(mFd, &x, 1);
1070        x = (length >> 8) & 0xff;
1071        ::write(mFd, &x, 1);
1072        x = length & 0xff;
1073        ::write(mFd, &x, 1);
1074
1075        ::write(mFd,
1076              (const uint8_t *)buffer->data() + buffer->range_offset(),
1077              length);
1078
1079        mOffset += length + 4;
1080    } else {
1081        CHECK_LT(length, 65536);
1082
1083        uint8_t x = length >> 8;
1084        ::write(mFd, &x, 1);
1085        x = length & 0xff;
1086        ::write(mFd, &x, 1);
1087        ::write(mFd, (const uint8_t *)buffer->data() + buffer->range_offset(), length);
1088        mOffset += length + 2;
1089    }
1090
1091    return old_offset;
1092}
1093
1094size_t MPEG4Writer::write(
1095        const void *ptr, size_t size, size_t nmemb) {
1096
1097    const size_t bytes = size * nmemb;
1098    if (mWriteMoovBoxToMemory) {
1099
1100        off64_t moovBoxSize = 8 + mMoovBoxBufferOffset + bytes;
1101        if (moovBoxSize > mEstimatedMoovBoxSize) {
1102            // The reserved moov box at the beginning of the file
1103            // is not big enough. Moov box should be written to
1104            // the end of the file from now on, but not to the
1105            // in-memory cache.
1106
1107            // We write partial moov box that is in the memory to
1108            // the file first.
1109            for (List<off64_t>::iterator it = mBoxes.begin();
1110                 it != mBoxes.end(); ++it) {
1111                (*it) += mOffset;
1112            }
1113            lseek64(mFd, mOffset, SEEK_SET);
1114            ::write(mFd, mMoovBoxBuffer, mMoovBoxBufferOffset);
1115            ::write(mFd, ptr, bytes);
1116            mOffset += (bytes + mMoovBoxBufferOffset);
1117
1118            // All subsequent moov box content will be written
1119            // to the end of the file.
1120            mWriteMoovBoxToMemory = false;
1121        } else {
1122            memcpy(mMoovBoxBuffer + mMoovBoxBufferOffset, ptr, bytes);
1123            mMoovBoxBufferOffset += bytes;
1124        }
1125    } else {
1126        ::write(mFd, ptr, size * nmemb);
1127        mOffset += bytes;
1128    }
1129    return bytes;
1130}
1131
1132void MPEG4Writer::beginBox(const char *fourcc) {
1133    CHECK_EQ(strlen(fourcc), 4);
1134
1135    mBoxes.push_back(mWriteMoovBoxToMemory?
1136            mMoovBoxBufferOffset: mOffset);
1137
1138    writeInt32(0);
1139    writeFourcc(fourcc);
1140}
1141
1142void MPEG4Writer::endBox() {
1143    CHECK(!mBoxes.empty());
1144
1145    off64_t offset = *--mBoxes.end();
1146    mBoxes.erase(--mBoxes.end());
1147
1148    if (mWriteMoovBoxToMemory) {
1149       int32_t x = htonl(mMoovBoxBufferOffset - offset);
1150       memcpy(mMoovBoxBuffer + offset, &x, 4);
1151    } else {
1152        lseek64(mFd, offset, SEEK_SET);
1153        writeInt32(mOffset - offset);
1154        mOffset -= 4;
1155        lseek64(mFd, mOffset, SEEK_SET);
1156    }
1157}
1158
1159void MPEG4Writer::writeInt8(int8_t x) {
1160    write(&x, 1, 1);
1161}
1162
1163void MPEG4Writer::writeInt16(int16_t x) {
1164    x = htons(x);
1165    write(&x, 1, 2);
1166}
1167
1168void MPEG4Writer::writeInt32(int32_t x) {
1169    x = htonl(x);
1170    write(&x, 1, 4);
1171}
1172
1173void MPEG4Writer::writeInt64(int64_t x) {
1174    x = hton64(x);
1175    write(&x, 1, 8);
1176}
1177
1178void MPEG4Writer::writeCString(const char *s) {
1179    size_t n = strlen(s);
1180    write(s, 1, n + 1);
1181}
1182
1183void MPEG4Writer::writeFourcc(const char *s) {
1184    CHECK_EQ(strlen(s), 4);
1185    write(s, 1, 4);
1186}
1187
1188
1189// Written in +/-DD.DDDD format
1190void MPEG4Writer::writeLatitude(int degreex10000) {
1191    bool isNegative = (degreex10000 < 0);
1192    char sign = isNegative? '-': '+';
1193
1194    // Handle the whole part
1195    char str[9];
1196    int wholePart = degreex10000 / 10000;
1197    if (wholePart == 0) {
1198        snprintf(str, 5, "%c%.2d.", sign, wholePart);
1199    } else {
1200        snprintf(str, 5, "%+.2d.", wholePart);
1201    }
1202
1203    // Handle the fractional part
1204    int fractionalPart = degreex10000 - (wholePart * 10000);
1205    if (fractionalPart < 0) {
1206        fractionalPart = -fractionalPart;
1207    }
1208    snprintf(&str[4], 5, "%.4d", fractionalPart);
1209
1210    // Do not write the null terminator
1211    write(str, 1, 8);
1212}
1213
1214// Written in +/- DDD.DDDD format
1215void MPEG4Writer::writeLongitude(int degreex10000) {
1216    bool isNegative = (degreex10000 < 0);
1217    char sign = isNegative? '-': '+';
1218
1219    // Handle the whole part
1220    char str[10];
1221    int wholePart = degreex10000 / 10000;
1222    if (wholePart == 0) {
1223        snprintf(str, 6, "%c%.3d.", sign, wholePart);
1224    } else {
1225        snprintf(str, 6, "%+.3d.", wholePart);
1226    }
1227
1228    // Handle the fractional part
1229    int fractionalPart = degreex10000 - (wholePart * 10000);
1230    if (fractionalPart < 0) {
1231        fractionalPart = -fractionalPart;
1232    }
1233    snprintf(&str[5], 5, "%.4d", fractionalPart);
1234
1235    // Do not write the null terminator
1236    write(str, 1, 9);
1237}
1238
1239/*
1240 * Geodata is stored according to ISO-6709 standard.
1241 * latitudex10000 is latitude in degrees times 10000, and
1242 * longitudex10000 is longitude in degrees times 10000.
1243 * The range for the latitude is in [-90, +90], and
1244 * The range for the longitude is in [-180, +180]
1245 */
1246status_t MPEG4Writer::setGeoData(int latitudex10000, int longitudex10000) {
1247    // Is latitude or longitude out of range?
1248    if (latitudex10000 < -900000 || latitudex10000 > 900000 ||
1249        longitudex10000 < -1800000 || longitudex10000 > 1800000) {
1250        return BAD_VALUE;
1251    }
1252
1253    mLatitudex10000 = latitudex10000;
1254    mLongitudex10000 = longitudex10000;
1255    mAreGeoTagsAvailable = true;
1256    return OK;
1257}
1258
1259void MPEG4Writer::write(const void *data, size_t size) {
1260    write(data, 1, size);
1261}
1262
1263bool MPEG4Writer::isFileStreamable() const {
1264    return mStreamableFile;
1265}
1266
1267bool MPEG4Writer::exceedsFileSizeLimit() {
1268    // No limit
1269    if (mMaxFileSizeLimitBytes == 0) {
1270        return false;
1271    }
1272
1273    int64_t nTotalBytesEstimate = static_cast<int64_t>(mEstimatedMoovBoxSize);
1274    for (List<Track *>::iterator it = mTracks.begin();
1275         it != mTracks.end(); ++it) {
1276        nTotalBytesEstimate += (*it)->getEstimatedTrackSizeBytes();
1277    }
1278
1279    if (!mStreamableFile) {
1280        // Add 1024 bytes as error tolerance
1281        return nTotalBytesEstimate + 1024 >= mMaxFileSizeLimitBytes;
1282    }
1283    // Be conservative in the estimate: do not exceed 95% of
1284    // the target file limit. For small target file size limit, though,
1285    // this will not help.
1286    return (nTotalBytesEstimate >= (95 * mMaxFileSizeLimitBytes) / 100);
1287}
1288
1289bool MPEG4Writer::exceedsFileDurationLimit() {
1290    // No limit
1291    if (mMaxFileDurationLimitUs == 0) {
1292        return false;
1293    }
1294
1295    for (List<Track *>::iterator it = mTracks.begin();
1296         it != mTracks.end(); ++it) {
1297        if ((*it)->getDurationUs() >= mMaxFileDurationLimitUs) {
1298            return true;
1299        }
1300    }
1301    return false;
1302}
1303
1304bool MPEG4Writer::reachedEOS() {
1305    bool allDone = true;
1306    for (List<Track *>::iterator it = mTracks.begin();
1307         it != mTracks.end(); ++it) {
1308        if (!(*it)->reachedEOS()) {
1309            allDone = false;
1310            break;
1311        }
1312    }
1313
1314    return allDone;
1315}
1316
1317void MPEG4Writer::setStartTimestampUs(int64_t timeUs) {
1318    ALOGI("setStartTimestampUs: %lld", timeUs);
1319    CHECK_GE(timeUs, 0ll);
1320    Mutex::Autolock autoLock(mLock);
1321    if (mStartTimestampUs < 0 || mStartTimestampUs > timeUs) {
1322        mStartTimestampUs = timeUs;
1323        ALOGI("Earliest track starting time: %lld", mStartTimestampUs);
1324    }
1325}
1326
1327int64_t MPEG4Writer::getStartTimestampUs() {
1328    Mutex::Autolock autoLock(mLock);
1329    return mStartTimestampUs;
1330}
1331
1332size_t MPEG4Writer::numTracks() {
1333    Mutex::Autolock autolock(mLock);
1334    return mTracks.size();
1335}
1336
1337////////////////////////////////////////////////////////////////////////////////
1338
1339MPEG4Writer::Track::Track(
1340        MPEG4Writer *owner, const sp<MediaSource> &source, size_t trackId)
1341    : mOwner(owner),
1342      mMeta(source->getFormat()),
1343      mSource(source),
1344      mDone(false),
1345      mPaused(false),
1346      mResumed(false),
1347      mStarted(false),
1348      mTrackId(trackId),
1349      mTrackDurationUs(0),
1350      mEstimatedTrackSizeBytes(0),
1351      mSamplesHaveSameSize(true),
1352      mStszTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
1353      mStcoTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
1354      mCo64TableEntries(new ListTableEntries<off64_t>(1000, 1)),
1355      mStscTableEntries(new ListTableEntries<uint32_t>(1000, 3)),
1356      mStssTableEntries(new ListTableEntries<uint32_t>(1000, 1)),
1357      mSttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)),
1358      mCttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)),
1359      mCodecSpecificData(NULL),
1360      mCodecSpecificDataSize(0),
1361      mGotAllCodecSpecificData(false),
1362      mReachedEOS(false),
1363      mRotation(0) {
1364    getCodecSpecificDataFromInputFormatIfPossible();
1365
1366    const char *mime;
1367    mMeta->findCString(kKeyMIMEType, &mime);
1368    mIsAvc = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC);
1369    mIsAudio = !strncasecmp(mime, "audio/", 6);
1370    mIsMPEG4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) ||
1371               !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC);
1372
1373    setTimeScale();
1374}
1375
1376void MPEG4Writer::Track::updateTrackSizeEstimate() {
1377
1378    uint32_t stcoBoxCount = (mOwner->use32BitFileOffset()
1379                            ? mStcoTableEntries->count()
1380                            : mCo64TableEntries->count());
1381    int64_t stcoBoxSizeBytes = stcoBoxCount * 4;
1382    int64_t stszBoxSizeBytes = mSamplesHaveSameSize? 4: (mStszTableEntries->count() * 4);
1383
1384    mEstimatedTrackSizeBytes = mMdatSizeBytes;  // media data size
1385    if (!mOwner->isFileStreamable()) {
1386        // Reserved free space is not large enough to hold
1387        // all meta data and thus wasted.
1388        mEstimatedTrackSizeBytes += mStscTableEntries->count() * 12 +  // stsc box size
1389                                    mStssTableEntries->count() * 4 +   // stss box size
1390                                    mSttsTableEntries->count() * 8 +   // stts box size
1391                                    mCttsTableEntries->count() * 8 +   // ctts box size
1392                                    stcoBoxSizeBytes +           // stco box size
1393                                    stszBoxSizeBytes;            // stsz box size
1394    }
1395}
1396
1397void MPEG4Writer::Track::addOneStscTableEntry(
1398        size_t chunkId, size_t sampleId) {
1399
1400        mStscTableEntries->add(htonl(chunkId));
1401        mStscTableEntries->add(htonl(sampleId));
1402        mStscTableEntries->add(htonl(1));
1403}
1404
1405void MPEG4Writer::Track::addOneStssTableEntry(size_t sampleId) {
1406    mStssTableEntries->add(htonl(sampleId));
1407}
1408
1409void MPEG4Writer::Track::addOneSttsTableEntry(
1410        size_t sampleCount, int32_t duration) {
1411
1412    if (duration == 0) {
1413        ALOGW("0-duration samples found: %zu", sampleCount);
1414    }
1415    mSttsTableEntries->add(htonl(sampleCount));
1416    mSttsTableEntries->add(htonl(duration));
1417}
1418
1419void MPEG4Writer::Track::addOneCttsTableEntry(
1420        size_t sampleCount, int32_t duration) {
1421
1422    if (mIsAudio) {
1423        return;
1424    }
1425    mCttsTableEntries->add(htonl(sampleCount));
1426    mCttsTableEntries->add(htonl(duration));
1427}
1428
1429void MPEG4Writer::Track::addChunkOffset(off64_t offset) {
1430    if (mOwner->use32BitFileOffset()) {
1431        uint32_t value = offset;
1432        mStcoTableEntries->add(htonl(value));
1433    } else {
1434        mCo64TableEntries->add(hton64(offset));
1435    }
1436}
1437
1438void MPEG4Writer::Track::setTimeScale() {
1439    ALOGV("setTimeScale");
1440    // Default time scale
1441    mTimeScale = 90000;
1442
1443    if (mIsAudio) {
1444        // Use the sampling rate as the default time scale for audio track.
1445        int32_t sampleRate;
1446        bool success = mMeta->findInt32(kKeySampleRate, &sampleRate);
1447        CHECK(success);
1448        mTimeScale = sampleRate;
1449    }
1450
1451    // If someone would like to overwrite the timescale, use user-supplied value.
1452    int32_t timeScale;
1453    if (mMeta->findInt32(kKeyTimeScale, &timeScale)) {
1454        mTimeScale = timeScale;
1455    }
1456
1457    CHECK_GT(mTimeScale, 0);
1458}
1459
1460void MPEG4Writer::Track::getCodecSpecificDataFromInputFormatIfPossible() {
1461    const char *mime;
1462    CHECK(mMeta->findCString(kKeyMIMEType, &mime));
1463
1464    if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
1465        uint32_t type;
1466        const void *data;
1467        size_t size;
1468        if (mMeta->findData(kKeyAVCC, &type, &data, &size)) {
1469            mCodecSpecificData = malloc(size);
1470            mCodecSpecificDataSize = size;
1471            memcpy(mCodecSpecificData, data, size);
1472            mGotAllCodecSpecificData = true;
1473        }
1474    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)
1475            || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
1476        uint32_t type;
1477        const void *data;
1478        size_t size;
1479        if (mMeta->findData(kKeyESDS, &type, &data, &size)) {
1480            ESDS esds(data, size);
1481            if (esds.getCodecSpecificInfo(&data, &size) == OK) {
1482                mCodecSpecificData = malloc(size);
1483                mCodecSpecificDataSize = size;
1484                memcpy(mCodecSpecificData, data, size);
1485                mGotAllCodecSpecificData = true;
1486            }
1487        }
1488    }
1489}
1490
1491MPEG4Writer::Track::~Track() {
1492    stop();
1493
1494    delete mStszTableEntries;
1495    delete mStcoTableEntries;
1496    delete mCo64TableEntries;
1497    delete mStscTableEntries;
1498    delete mSttsTableEntries;
1499    delete mStssTableEntries;
1500    delete mCttsTableEntries;
1501
1502    mStszTableEntries = NULL;
1503    mStcoTableEntries = NULL;
1504    mCo64TableEntries = NULL;
1505    mStscTableEntries = NULL;
1506    mSttsTableEntries = NULL;
1507    mStssTableEntries = NULL;
1508    mCttsTableEntries = NULL;
1509
1510    if (mCodecSpecificData != NULL) {
1511        free(mCodecSpecificData);
1512        mCodecSpecificData = NULL;
1513    }
1514}
1515
1516void MPEG4Writer::Track::initTrackingProgressStatus(MetaData *params) {
1517    ALOGV("initTrackingProgressStatus");
1518    mPreviousTrackTimeUs = -1;
1519    mTrackingProgressStatus = false;
1520    mTrackEveryTimeDurationUs = 0;
1521    {
1522        int64_t timeUs;
1523        if (params && params->findInt64(kKeyTrackTimeStatus, &timeUs)) {
1524            ALOGV("Receive request to track progress status for every %lld us", timeUs);
1525            mTrackEveryTimeDurationUs = timeUs;
1526            mTrackingProgressStatus = true;
1527        }
1528    }
1529}
1530
1531// static
1532void *MPEG4Writer::ThreadWrapper(void *me) {
1533    ALOGV("ThreadWrapper: %p", me);
1534    MPEG4Writer *writer = static_cast<MPEG4Writer *>(me);
1535    writer->threadFunc();
1536    return NULL;
1537}
1538
1539void MPEG4Writer::bufferChunk(const Chunk& chunk) {
1540    ALOGV("bufferChunk: %p", chunk.mTrack);
1541    Mutex::Autolock autolock(mLock);
1542    CHECK_EQ(mDone, false);
1543
1544    for (List<ChunkInfo>::iterator it = mChunkInfos.begin();
1545         it != mChunkInfos.end(); ++it) {
1546
1547        if (chunk.mTrack == it->mTrack) {  // Found owner
1548            it->mChunks.push_back(chunk);
1549            mChunkReadyCondition.signal();
1550            return;
1551        }
1552    }
1553
1554    CHECK(!"Received a chunk for a unknown track");
1555}
1556
1557void MPEG4Writer::writeChunkToFile(Chunk* chunk) {
1558    ALOGV("writeChunkToFile: %lld from %s track",
1559        chunk->mTimeStampUs, chunk->mTrack->isAudio()? "audio": "video");
1560
1561    int32_t isFirstSample = true;
1562    while (!chunk->mSamples.empty()) {
1563        List<MediaBuffer *>::iterator it = chunk->mSamples.begin();
1564
1565        off64_t offset = chunk->mTrack->isAvc()
1566                                ? addLengthPrefixedSample_l(*it)
1567                                : addSample_l(*it);
1568
1569        if (isFirstSample) {
1570            chunk->mTrack->addChunkOffset(offset);
1571            isFirstSample = false;
1572        }
1573
1574        (*it)->release();
1575        (*it) = NULL;
1576        chunk->mSamples.erase(it);
1577    }
1578    chunk->mSamples.clear();
1579}
1580
1581void MPEG4Writer::writeAllChunks() {
1582    ALOGV("writeAllChunks");
1583    size_t outstandingChunks = 0;
1584    Chunk chunk;
1585    while (findChunkToWrite(&chunk)) {
1586        writeChunkToFile(&chunk);
1587        ++outstandingChunks;
1588    }
1589
1590    sendSessionSummary();
1591
1592    mChunkInfos.clear();
1593    ALOGD("%zu chunks are written in the last batch", outstandingChunks);
1594}
1595
1596bool MPEG4Writer::findChunkToWrite(Chunk *chunk) {
1597    ALOGV("findChunkToWrite");
1598
1599    int64_t minTimestampUs = 0x7FFFFFFFFFFFFFFFLL;
1600    Track *track = NULL;
1601    for (List<ChunkInfo>::iterator it = mChunkInfos.begin();
1602         it != mChunkInfos.end(); ++it) {
1603        if (!it->mChunks.empty()) {
1604            List<Chunk>::iterator chunkIt = it->mChunks.begin();
1605            if (chunkIt->mTimeStampUs < minTimestampUs) {
1606                minTimestampUs = chunkIt->mTimeStampUs;
1607                track = it->mTrack;
1608            }
1609        }
1610    }
1611
1612    if (track == NULL) {
1613        ALOGV("Nothing to be written after all");
1614        return false;
1615    }
1616
1617    if (mIsFirstChunk) {
1618        mIsFirstChunk = false;
1619    }
1620
1621    for (List<ChunkInfo>::iterator it = mChunkInfos.begin();
1622         it != mChunkInfos.end(); ++it) {
1623        if (it->mTrack == track) {
1624            *chunk = *(it->mChunks.begin());
1625            it->mChunks.erase(it->mChunks.begin());
1626            CHECK_EQ(chunk->mTrack, track);
1627
1628            int64_t interChunkTimeUs =
1629                chunk->mTimeStampUs - it->mPrevChunkTimestampUs;
1630            if (interChunkTimeUs > it->mPrevChunkTimestampUs) {
1631                it->mMaxInterChunkDurUs = interChunkTimeUs;
1632            }
1633
1634            return true;
1635        }
1636    }
1637
1638    return false;
1639}
1640
1641void MPEG4Writer::threadFunc() {
1642    ALOGV("threadFunc");
1643
1644    prctl(PR_SET_NAME, (unsigned long)"MPEG4Writer", 0, 0, 0);
1645
1646    Mutex::Autolock autoLock(mLock);
1647    while (!mDone) {
1648        Chunk chunk;
1649        bool chunkFound = false;
1650
1651        while (!mDone && !(chunkFound = findChunkToWrite(&chunk))) {
1652            mChunkReadyCondition.wait(mLock);
1653        }
1654
1655        // In real time recording mode, write without holding the lock in order
1656        // to reduce the blocking time for media track threads.
1657        // Otherwise, hold the lock until the existing chunks get written to the
1658        // file.
1659        if (chunkFound) {
1660            if (mIsRealTimeRecording) {
1661                mLock.unlock();
1662            }
1663            writeChunkToFile(&chunk);
1664            if (mIsRealTimeRecording) {
1665                mLock.lock();
1666            }
1667        }
1668    }
1669
1670    writeAllChunks();
1671}
1672
1673status_t MPEG4Writer::startWriterThread() {
1674    ALOGV("startWriterThread");
1675
1676    mDone = false;
1677    mIsFirstChunk = true;
1678    mDriftTimeUs = 0;
1679    for (List<Track *>::iterator it = mTracks.begin();
1680         it != mTracks.end(); ++it) {
1681        ChunkInfo info;
1682        info.mTrack = *it;
1683        info.mPrevChunkTimestampUs = 0;
1684        info.mMaxInterChunkDurUs = 0;
1685        mChunkInfos.push_back(info);
1686    }
1687
1688    pthread_attr_t attr;
1689    pthread_attr_init(&attr);
1690    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1691    pthread_create(&mThread, &attr, ThreadWrapper, this);
1692    pthread_attr_destroy(&attr);
1693    mWriterThreadStarted = true;
1694    return OK;
1695}
1696
1697
1698status_t MPEG4Writer::Track::start(MetaData *params) {
1699    if (!mDone && mPaused) {
1700        mPaused = false;
1701        mResumed = true;
1702        return OK;
1703    }
1704
1705    int64_t startTimeUs;
1706    if (params == NULL || !params->findInt64(kKeyTime, &startTimeUs)) {
1707        startTimeUs = 0;
1708    }
1709    mStartTimeRealUs = startTimeUs;
1710
1711    int32_t rotationDegrees;
1712    if (!mIsAudio && params && params->findInt32(kKeyRotation, &rotationDegrees)) {
1713        mRotation = rotationDegrees;
1714    }
1715
1716    initTrackingProgressStatus(params);
1717
1718    sp<MetaData> meta = new MetaData;
1719    if (mOwner->isRealTimeRecording() && mOwner->numTracks() > 1) {
1720        /*
1721         * This extra delay of accepting incoming audio/video signals
1722         * helps to align a/v start time at the beginning of a recording
1723         * session, and it also helps eliminate the "recording" sound for
1724         * camcorder applications.
1725         *
1726         * If client does not set the start time offset, we fall back to
1727         * use the default initial delay value.
1728         */
1729        int64_t startTimeOffsetUs = mOwner->getStartTimeOffsetMs() * 1000LL;
1730        if (startTimeOffsetUs < 0) {  // Start time offset was not set
1731            startTimeOffsetUs = kInitialDelayTimeUs;
1732        }
1733        startTimeUs += startTimeOffsetUs;
1734        ALOGI("Start time offset: %lld us", startTimeOffsetUs);
1735    }
1736
1737    meta->setInt64(kKeyTime, startTimeUs);
1738
1739    status_t err = mSource->start(meta.get());
1740    if (err != OK) {
1741        mDone = mReachedEOS = true;
1742        return err;
1743    }
1744
1745    pthread_attr_t attr;
1746    pthread_attr_init(&attr);
1747    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1748
1749    mDone = false;
1750    mStarted = true;
1751    mTrackDurationUs = 0;
1752    mReachedEOS = false;
1753    mEstimatedTrackSizeBytes = 0;
1754    mMdatSizeBytes = 0;
1755    mMaxChunkDurationUs = 0;
1756
1757    pthread_create(&mThread, &attr, ThreadWrapper, this);
1758    pthread_attr_destroy(&attr);
1759
1760    return OK;
1761}
1762
1763status_t MPEG4Writer::Track::pause() {
1764    mPaused = true;
1765    return OK;
1766}
1767
1768status_t MPEG4Writer::Track::stop() {
1769    ALOGD("%s track stopping", mIsAudio? "Audio": "Video");
1770    if (!mStarted) {
1771        ALOGE("Stop() called but track is not started");
1772        return ERROR_END_OF_STREAM;
1773    }
1774
1775    if (mDone) {
1776        return OK;
1777    }
1778    mDone = true;
1779
1780    ALOGD("%s track source stopping", mIsAudio? "Audio": "Video");
1781    mSource->stop();
1782    ALOGD("%s track source stopped", mIsAudio? "Audio": "Video");
1783
1784    void *dummy;
1785    pthread_join(mThread, &dummy);
1786    status_t err = static_cast<status_t>(reinterpret_cast<uintptr_t>(dummy));
1787
1788    ALOGD("%s track stopped", mIsAudio? "Audio": "Video");
1789    return err;
1790}
1791
1792bool MPEG4Writer::Track::reachedEOS() {
1793    return mReachedEOS;
1794}
1795
1796// static
1797void *MPEG4Writer::Track::ThreadWrapper(void *me) {
1798    Track *track = static_cast<Track *>(me);
1799
1800    status_t err = track->threadEntry();
1801    return (void *)(uintptr_t)err;
1802}
1803
1804static void getNalUnitType(uint8_t byte, uint8_t* type) {
1805    ALOGV("getNalUnitType: %d", byte);
1806
1807    // nal_unit_type: 5-bit unsigned integer
1808    *type = (byte & 0x1F);
1809}
1810
1811static const uint8_t *findNextStartCode(
1812        const uint8_t *data, size_t length) {
1813
1814    ALOGV("findNextStartCode: %p %d", data, length);
1815
1816    size_t bytesLeft = length;
1817    while (bytesLeft > 4  &&
1818            memcmp("\x00\x00\x00\x01", &data[length - bytesLeft], 4)) {
1819        --bytesLeft;
1820    }
1821    if (bytesLeft <= 4) {
1822        bytesLeft = 0; // Last parameter set
1823    }
1824    return &data[length - bytesLeft];
1825}
1826
1827const uint8_t *MPEG4Writer::Track::parseParamSet(
1828        const uint8_t *data, size_t length, int type, size_t *paramSetLen) {
1829
1830    ALOGV("parseParamSet");
1831    CHECK(type == kNalUnitTypeSeqParamSet ||
1832          type == kNalUnitTypePicParamSet);
1833
1834    const uint8_t *nextStartCode = findNextStartCode(data, length);
1835    *paramSetLen = nextStartCode - data;
1836    if (*paramSetLen == 0) {
1837        ALOGE("Param set is malformed, since its length is 0");
1838        return NULL;
1839    }
1840
1841    AVCParamSet paramSet(*paramSetLen, data);
1842    if (type == kNalUnitTypeSeqParamSet) {
1843        if (*paramSetLen < 4) {
1844            ALOGE("Seq parameter set malformed");
1845            return NULL;
1846        }
1847        if (mSeqParamSets.empty()) {
1848            mProfileIdc = data[1];
1849            mProfileCompatible = data[2];
1850            mLevelIdc = data[3];
1851        } else {
1852            if (mProfileIdc != data[1] ||
1853                mProfileCompatible != data[2] ||
1854                mLevelIdc != data[3]) {
1855                ALOGE("Inconsistent profile/level found in seq parameter sets");
1856                return NULL;
1857            }
1858        }
1859        mSeqParamSets.push_back(paramSet);
1860    } else {
1861        mPicParamSets.push_back(paramSet);
1862    }
1863    return nextStartCode;
1864}
1865
1866status_t MPEG4Writer::Track::copyAVCCodecSpecificData(
1867        const uint8_t *data, size_t size) {
1868    ALOGV("copyAVCCodecSpecificData");
1869
1870    // 2 bytes for each of the parameter set length field
1871    // plus the 7 bytes for the header
1872    if (size < 4 + 7) {
1873        ALOGE("Codec specific data length too short: %zu", size);
1874        return ERROR_MALFORMED;
1875    }
1876
1877    mCodecSpecificDataSize = size;
1878    mCodecSpecificData = malloc(size);
1879    memcpy(mCodecSpecificData, data, size);
1880    return OK;
1881}
1882
1883status_t MPEG4Writer::Track::parseAVCCodecSpecificData(
1884        const uint8_t *data, size_t size) {
1885
1886    ALOGV("parseAVCCodecSpecificData");
1887    // Data starts with a start code.
1888    // SPS and PPS are separated with start codes.
1889    // Also, SPS must come before PPS
1890    uint8_t type = kNalUnitTypeSeqParamSet;
1891    bool gotSps = false;
1892    bool gotPps = false;
1893    const uint8_t *tmp = data;
1894    const uint8_t *nextStartCode = data;
1895    size_t bytesLeft = size;
1896    size_t paramSetLen = 0;
1897    mCodecSpecificDataSize = 0;
1898    while (bytesLeft > 4 && !memcmp("\x00\x00\x00\x01", tmp, 4)) {
1899        getNalUnitType(*(tmp + 4), &type);
1900        if (type == kNalUnitTypeSeqParamSet) {
1901            if (gotPps) {
1902                ALOGE("SPS must come before PPS");
1903                return ERROR_MALFORMED;
1904            }
1905            if (!gotSps) {
1906                gotSps = true;
1907            }
1908            nextStartCode = parseParamSet(tmp + 4, bytesLeft - 4, type, &paramSetLen);
1909        } else if (type == kNalUnitTypePicParamSet) {
1910            if (!gotSps) {
1911                ALOGE("SPS must come before PPS");
1912                return ERROR_MALFORMED;
1913            }
1914            if (!gotPps) {
1915                gotPps = true;
1916            }
1917            nextStartCode = parseParamSet(tmp + 4, bytesLeft - 4, type, &paramSetLen);
1918        } else {
1919            ALOGE("Only SPS and PPS Nal units are expected");
1920            return ERROR_MALFORMED;
1921        }
1922
1923        if (nextStartCode == NULL) {
1924            return ERROR_MALFORMED;
1925        }
1926
1927        // Move on to find the next parameter set
1928        bytesLeft -= nextStartCode - tmp;
1929        tmp = nextStartCode;
1930        mCodecSpecificDataSize += (2 + paramSetLen);
1931    }
1932
1933    {
1934        // Check on the number of seq parameter sets
1935        size_t nSeqParamSets = mSeqParamSets.size();
1936        if (nSeqParamSets == 0) {
1937            ALOGE("Cound not find sequence parameter set");
1938            return ERROR_MALFORMED;
1939        }
1940
1941        if (nSeqParamSets > 0x1F) {
1942            ALOGE("Too many seq parameter sets (%zu) found", nSeqParamSets);
1943            return ERROR_MALFORMED;
1944        }
1945    }
1946
1947    {
1948        // Check on the number of pic parameter sets
1949        size_t nPicParamSets = mPicParamSets.size();
1950        if (nPicParamSets == 0) {
1951            ALOGE("Cound not find picture parameter set");
1952            return ERROR_MALFORMED;
1953        }
1954        if (nPicParamSets > 0xFF) {
1955            ALOGE("Too many pic parameter sets (%zd) found", nPicParamSets);
1956            return ERROR_MALFORMED;
1957        }
1958    }
1959// FIXME:
1960// Add chromat_format_idc, bit depth values, etc for AVC/h264 high profile and above
1961// and remove #if 0
1962#if 0
1963    {
1964        // Check on the profiles
1965        // These profiles requires additional parameter set extensions
1966        if (mProfileIdc == 100 || mProfileIdc == 110 ||
1967            mProfileIdc == 122 || mProfileIdc == 144) {
1968            ALOGE("Sorry, no support for profile_idc: %d!", mProfileIdc);
1969            return BAD_VALUE;
1970        }
1971    }
1972#endif
1973    return OK;
1974}
1975
1976status_t MPEG4Writer::Track::makeAVCCodecSpecificData(
1977        const uint8_t *data, size_t size) {
1978
1979    if (mCodecSpecificData != NULL) {
1980        ALOGE("Already have codec specific data");
1981        return ERROR_MALFORMED;
1982    }
1983
1984    if (size < 4) {
1985        ALOGE("Codec specific data length too short: %zu", size);
1986        return ERROR_MALFORMED;
1987    }
1988
1989    // Data is in the form of AVCCodecSpecificData
1990    if (memcmp("\x00\x00\x00\x01", data, 4)) {
1991        return copyAVCCodecSpecificData(data, size);
1992    }
1993
1994    if (parseAVCCodecSpecificData(data, size) != OK) {
1995        return ERROR_MALFORMED;
1996    }
1997
1998    // ISO 14496-15: AVC file format
1999    mCodecSpecificDataSize += 7;  // 7 more bytes in the header
2000    mCodecSpecificData = malloc(mCodecSpecificDataSize);
2001    uint8_t *header = (uint8_t *)mCodecSpecificData;
2002    header[0] = 1;                     // version
2003    header[1] = mProfileIdc;           // profile indication
2004    header[2] = mProfileCompatible;    // profile compatibility
2005    header[3] = mLevelIdc;
2006
2007    // 6-bit '111111' followed by 2-bit to lengthSizeMinuusOne
2008    if (mOwner->useNalLengthFour()) {
2009        header[4] = 0xfc | 3;  // length size == 4 bytes
2010    } else {
2011        header[4] = 0xfc | 1;  // length size == 2 bytes
2012    }
2013
2014    // 3-bit '111' followed by 5-bit numSequenceParameterSets
2015    int nSequenceParamSets = mSeqParamSets.size();
2016    header[5] = 0xe0 | nSequenceParamSets;
2017    header += 6;
2018    for (List<AVCParamSet>::iterator it = mSeqParamSets.begin();
2019         it != mSeqParamSets.end(); ++it) {
2020        // 16-bit sequence parameter set length
2021        uint16_t seqParamSetLength = it->mLength;
2022        header[0] = seqParamSetLength >> 8;
2023        header[1] = seqParamSetLength & 0xff;
2024
2025        // SPS NAL unit (sequence parameter length bytes)
2026        memcpy(&header[2], it->mData, seqParamSetLength);
2027        header += (2 + seqParamSetLength);
2028    }
2029
2030    // 8-bit nPictureParameterSets
2031    int nPictureParamSets = mPicParamSets.size();
2032    header[0] = nPictureParamSets;
2033    header += 1;
2034    for (List<AVCParamSet>::iterator it = mPicParamSets.begin();
2035         it != mPicParamSets.end(); ++it) {
2036        // 16-bit picture parameter set length
2037        uint16_t picParamSetLength = it->mLength;
2038        header[0] = picParamSetLength >> 8;
2039        header[1] = picParamSetLength & 0xff;
2040
2041        // PPS Nal unit (picture parameter set length bytes)
2042        memcpy(&header[2], it->mData, picParamSetLength);
2043        header += (2 + picParamSetLength);
2044    }
2045
2046    return OK;
2047}
2048
2049/*
2050 * Updates the drift time from the audio track so that
2051 * the video track can get the updated drift time information
2052 * from the file writer. The fluctuation of the drift time of the audio
2053 * encoding path is smoothed out with a simple filter by giving a larger
2054 * weight to more recently drift time. The filter coefficients, 0.5 and 0.5,
2055 * are heuristically determined.
2056 */
2057void MPEG4Writer::Track::updateDriftTime(const sp<MetaData>& meta) {
2058    int64_t driftTimeUs = 0;
2059    if (meta->findInt64(kKeyDriftTime, &driftTimeUs)) {
2060        int64_t prevDriftTimeUs = mOwner->getDriftTimeUs();
2061        int64_t timeUs = (driftTimeUs + prevDriftTimeUs) >> 1;
2062        mOwner->setDriftTimeUs(timeUs);
2063    }
2064}
2065
2066status_t MPEG4Writer::Track::threadEntry() {
2067    int32_t count = 0;
2068    const int64_t interleaveDurationUs = mOwner->interleaveDuration();
2069    const bool hasMultipleTracks = (mOwner->numTracks() > 1);
2070    int64_t chunkTimestampUs = 0;
2071    int32_t nChunks = 0;
2072    int32_t nZeroLengthFrames = 0;
2073    int64_t lastTimestampUs = 0;      // Previous sample time stamp
2074    int64_t lastDurationUs = 0;       // Between the previous two samples
2075    int64_t currDurationTicks = 0;    // Timescale based ticks
2076    int64_t lastDurationTicks = 0;    // Timescale based ticks
2077    int32_t sampleCount = 1;          // Sample count in the current stts table entry
2078    uint32_t previousSampleSize = 0;  // Size of the previous sample
2079    int64_t previousPausedDurationUs = 0;
2080    int64_t timestampUs = 0;
2081    int64_t cttsOffsetTimeUs = 0;
2082    int64_t currCttsOffsetTimeTicks = 0;   // Timescale based ticks
2083    int64_t lastCttsOffsetTimeTicks = -1;  // Timescale based ticks
2084    int32_t cttsSampleCount = 0;           // Sample count in the current ctts table entry
2085    uint32_t lastSamplesPerChunk = 0;
2086
2087    if (mIsAudio) {
2088        prctl(PR_SET_NAME, (unsigned long)"AudioTrackEncoding", 0, 0, 0);
2089    } else {
2090        prctl(PR_SET_NAME, (unsigned long)"VideoTrackEncoding", 0, 0, 0);
2091    }
2092
2093    if (mOwner->isRealTimeRecording()) {
2094        androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
2095    }
2096
2097    sp<MetaData> meta_data;
2098
2099    status_t err = OK;
2100    MediaBuffer *buffer;
2101    while (!mDone && (err = mSource->read(&buffer)) == OK) {
2102        if (buffer->range_length() == 0) {
2103            buffer->release();
2104            buffer = NULL;
2105            ++nZeroLengthFrames;
2106            continue;
2107        }
2108
2109        // If the codec specific data has not been received yet, delay pause.
2110        // After the codec specific data is received, discard what we received
2111        // when the track is to be paused.
2112        if (mPaused && !mResumed) {
2113            buffer->release();
2114            buffer = NULL;
2115            continue;
2116        }
2117
2118        ++count;
2119
2120        int32_t isCodecConfig;
2121        if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecConfig)
2122                && isCodecConfig) {
2123            CHECK(!mGotAllCodecSpecificData);
2124
2125            if (mIsAvc) {
2126                status_t err = makeAVCCodecSpecificData(
2127                        (const uint8_t *)buffer->data()
2128                            + buffer->range_offset(),
2129                        buffer->range_length());
2130                CHECK_EQ((status_t)OK, err);
2131            } else if (mIsMPEG4) {
2132                mCodecSpecificDataSize = buffer->range_length();
2133                mCodecSpecificData = malloc(mCodecSpecificDataSize);
2134                memcpy(mCodecSpecificData,
2135                        (const uint8_t *)buffer->data()
2136                            + buffer->range_offset(),
2137                       buffer->range_length());
2138            }
2139
2140            buffer->release();
2141            buffer = NULL;
2142
2143            mGotAllCodecSpecificData = true;
2144            continue;
2145        }
2146
2147        // Make a deep copy of the MediaBuffer and Metadata and release
2148        // the original as soon as we can
2149        MediaBuffer *copy = new MediaBuffer(buffer->range_length());
2150        memcpy(copy->data(), (uint8_t *)buffer->data() + buffer->range_offset(),
2151                buffer->range_length());
2152        copy->set_range(0, buffer->range_length());
2153        meta_data = new MetaData(*buffer->meta_data().get());
2154        buffer->release();
2155        buffer = NULL;
2156
2157        if (mIsAvc) StripStartcode(copy);
2158
2159        size_t sampleSize = copy->range_length();
2160        if (mIsAvc) {
2161            if (mOwner->useNalLengthFour()) {
2162                sampleSize += 4;
2163            } else {
2164                sampleSize += 2;
2165            }
2166        }
2167
2168        // Max file size or duration handling
2169        mMdatSizeBytes += sampleSize;
2170        updateTrackSizeEstimate();
2171
2172        if (mOwner->exceedsFileSizeLimit()) {
2173            mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
2174            break;
2175        }
2176        if (mOwner->exceedsFileDurationLimit()) {
2177            mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
2178            break;
2179        }
2180
2181
2182        int32_t isSync = false;
2183        meta_data->findInt32(kKeyIsSyncFrame, &isSync);
2184        CHECK(meta_data->findInt64(kKeyTime, &timestampUs));
2185
2186////////////////////////////////////////////////////////////////////////////////
2187        if (mStszTableEntries->count() == 0) {
2188            mFirstSampleTimeRealUs = systemTime() / 1000;
2189            mStartTimestampUs = timestampUs;
2190            mOwner->setStartTimestampUs(mStartTimestampUs);
2191            previousPausedDurationUs = mStartTimestampUs;
2192        }
2193
2194        if (mResumed) {
2195            int64_t durExcludingEarlierPausesUs = timestampUs - previousPausedDurationUs;
2196            CHECK_GE(durExcludingEarlierPausesUs, 0ll);
2197            int64_t pausedDurationUs = durExcludingEarlierPausesUs - mTrackDurationUs;
2198            CHECK_GE(pausedDurationUs, lastDurationUs);
2199            previousPausedDurationUs += pausedDurationUs - lastDurationUs;
2200            mResumed = false;
2201        }
2202
2203        timestampUs -= previousPausedDurationUs;
2204        CHECK_GE(timestampUs, 0ll);
2205        if (!mIsAudio) {
2206            /*
2207             * Composition time: timestampUs
2208             * Decoding time: decodingTimeUs
2209             * Composition time offset = composition time - decoding time
2210             */
2211            int64_t decodingTimeUs;
2212            CHECK(meta_data->findInt64(kKeyDecodingTime, &decodingTimeUs));
2213            decodingTimeUs -= previousPausedDurationUs;
2214            cttsOffsetTimeUs =
2215                    timestampUs + kMaxCttsOffsetTimeUs - decodingTimeUs;
2216            CHECK_GE(cttsOffsetTimeUs, 0ll);
2217            timestampUs = decodingTimeUs;
2218            ALOGV("decoding time: %lld and ctts offset time: %lld",
2219                timestampUs, cttsOffsetTimeUs);
2220
2221            // Update ctts box table if necessary
2222            currCttsOffsetTimeTicks =
2223                    (cttsOffsetTimeUs * mTimeScale + 500000LL) / 1000000LL;
2224            CHECK_LE(currCttsOffsetTimeTicks, 0x0FFFFFFFFLL);
2225            if (mStszTableEntries->count() == 0) {
2226                // Force the first ctts table entry to have one single entry
2227                // so that we can do adjustment for the initial track start
2228                // time offset easily in writeCttsBox().
2229                lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks;
2230                addOneCttsTableEntry(1, currCttsOffsetTimeTicks);
2231                cttsSampleCount = 0;      // No sample in ctts box is pending
2232            } else {
2233                if (currCttsOffsetTimeTicks != lastCttsOffsetTimeTicks) {
2234                    addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
2235                    lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks;
2236                    cttsSampleCount = 1;  // One sample in ctts box is pending
2237                } else {
2238                    ++cttsSampleCount;
2239                }
2240            }
2241
2242            // Update ctts time offset range
2243            if (mStszTableEntries->count() == 0) {
2244                mMinCttsOffsetTimeUs = currCttsOffsetTimeTicks;
2245                mMaxCttsOffsetTimeUs = currCttsOffsetTimeTicks;
2246            } else {
2247                if (currCttsOffsetTimeTicks > mMaxCttsOffsetTimeUs) {
2248                    mMaxCttsOffsetTimeUs = currCttsOffsetTimeTicks;
2249                } else if (currCttsOffsetTimeTicks < mMinCttsOffsetTimeUs) {
2250                    mMinCttsOffsetTimeUs = currCttsOffsetTimeTicks;
2251                }
2252            }
2253
2254        }
2255
2256        if (mOwner->isRealTimeRecording()) {
2257            if (mIsAudio) {
2258                updateDriftTime(meta_data);
2259            }
2260        }
2261
2262        CHECK_GE(timestampUs, 0ll);
2263        ALOGV("%s media time stamp: %lld and previous paused duration %lld",
2264                mIsAudio? "Audio": "Video", timestampUs, previousPausedDurationUs);
2265        if (timestampUs > mTrackDurationUs) {
2266            mTrackDurationUs = timestampUs;
2267        }
2268
2269        // We need to use the time scale based ticks, rather than the
2270        // timestamp itself to determine whether we have to use a new
2271        // stts entry, since we may have rounding errors.
2272        // The calculation is intended to reduce the accumulated
2273        // rounding errors.
2274        currDurationTicks =
2275            ((timestampUs * mTimeScale + 500000LL) / 1000000LL -
2276                (lastTimestampUs * mTimeScale + 500000LL) / 1000000LL);
2277        if (currDurationTicks < 0ll) {
2278            ALOGE("timestampUs %lld < lastTimestampUs %lld for %s track",
2279                timestampUs, lastTimestampUs, mIsAudio? "Audio": "Video");
2280            return UNKNOWN_ERROR;
2281        }
2282
2283        // if the duration is different for this sample, see if it is close enough to the previous
2284        // duration that we can fudge it and use the same value, to avoid filling the stts table
2285        // with lots of near-identical entries.
2286        // "close enough" here means that the current duration needs to be adjusted by less
2287        // than 0.1 milliseconds
2288        if (lastDurationTicks && (currDurationTicks != lastDurationTicks)) {
2289            int64_t deltaUs = ((lastDurationTicks - currDurationTicks) * 1000000LL
2290                    + (mTimeScale / 2)) / mTimeScale;
2291            if (deltaUs > -100 && deltaUs < 100) {
2292                // use previous ticks, and adjust timestamp as if it was actually that number
2293                // of ticks
2294                currDurationTicks = lastDurationTicks;
2295                timestampUs += deltaUs;
2296            }
2297        }
2298
2299        mStszTableEntries->add(htonl(sampleSize));
2300        if (mStszTableEntries->count() > 2) {
2301
2302            // Force the first sample to have its own stts entry so that
2303            // we can adjust its value later to maintain the A/V sync.
2304            if (mStszTableEntries->count() == 3 || currDurationTicks != lastDurationTicks) {
2305                addOneSttsTableEntry(sampleCount, lastDurationTicks);
2306                sampleCount = 1;
2307            } else {
2308                ++sampleCount;
2309            }
2310
2311        }
2312        if (mSamplesHaveSameSize) {
2313            if (mStszTableEntries->count() >= 2 && previousSampleSize != sampleSize) {
2314                mSamplesHaveSameSize = false;
2315            }
2316            previousSampleSize = sampleSize;
2317        }
2318        ALOGV("%s timestampUs/lastTimestampUs: %lld/%lld",
2319                mIsAudio? "Audio": "Video", timestampUs, lastTimestampUs);
2320        lastDurationUs = timestampUs - lastTimestampUs;
2321        lastDurationTicks = currDurationTicks;
2322        lastTimestampUs = timestampUs;
2323
2324        if (isSync != 0) {
2325            addOneStssTableEntry(mStszTableEntries->count());
2326        }
2327
2328        if (mTrackingProgressStatus) {
2329            if (mPreviousTrackTimeUs <= 0) {
2330                mPreviousTrackTimeUs = mStartTimestampUs;
2331            }
2332            trackProgressStatus(timestampUs);
2333        }
2334        if (!hasMultipleTracks) {
2335            off64_t offset = mIsAvc? mOwner->addLengthPrefixedSample_l(copy)
2336                                 : mOwner->addSample_l(copy);
2337
2338            uint32_t count = (mOwner->use32BitFileOffset()
2339                        ? mStcoTableEntries->count()
2340                        : mCo64TableEntries->count());
2341
2342            if (count == 0) {
2343                addChunkOffset(offset);
2344            }
2345            copy->release();
2346            copy = NULL;
2347            continue;
2348        }
2349
2350        mChunkSamples.push_back(copy);
2351        if (interleaveDurationUs == 0) {
2352            addOneStscTableEntry(++nChunks, 1);
2353            bufferChunk(timestampUs);
2354        } else {
2355            if (chunkTimestampUs == 0) {
2356                chunkTimestampUs = timestampUs;
2357            } else {
2358                int64_t chunkDurationUs = timestampUs - chunkTimestampUs;
2359                if (chunkDurationUs > interleaveDurationUs) {
2360                    if (chunkDurationUs > mMaxChunkDurationUs) {
2361                        mMaxChunkDurationUs = chunkDurationUs;
2362                    }
2363                    ++nChunks;
2364                    if (nChunks == 1 ||  // First chunk
2365                        lastSamplesPerChunk != mChunkSamples.size()) {
2366                        lastSamplesPerChunk = mChunkSamples.size();
2367                        addOneStscTableEntry(nChunks, lastSamplesPerChunk);
2368                    }
2369                    bufferChunk(timestampUs);
2370                    chunkTimestampUs = timestampUs;
2371                }
2372            }
2373        }
2374
2375    }
2376
2377    if (isTrackMalFormed()) {
2378        err = ERROR_MALFORMED;
2379    }
2380
2381    mOwner->trackProgressStatus(mTrackId, -1, err);
2382
2383    // Last chunk
2384    if (!hasMultipleTracks) {
2385        addOneStscTableEntry(1, mStszTableEntries->count());
2386    } else if (!mChunkSamples.empty()) {
2387        addOneStscTableEntry(++nChunks, mChunkSamples.size());
2388        bufferChunk(timestampUs);
2389    }
2390
2391    // We don't really know how long the last frame lasts, since
2392    // there is no frame time after it, just repeat the previous
2393    // frame's duration.
2394    if (mStszTableEntries->count() == 1) {
2395        lastDurationUs = 0;  // A single sample's duration
2396        lastDurationTicks = 0;
2397    } else {
2398        ++sampleCount;  // Count for the last sample
2399    }
2400
2401    if (mStszTableEntries->count() <= 2) {
2402        addOneSttsTableEntry(1, lastDurationTicks);
2403        if (sampleCount - 1 > 0) {
2404            addOneSttsTableEntry(sampleCount - 1, lastDurationTicks);
2405        }
2406    } else {
2407        addOneSttsTableEntry(sampleCount, lastDurationTicks);
2408    }
2409
2410    // The last ctts box may not have been written yet, and this
2411    // is to make sure that we write out the last ctts box.
2412    if (currCttsOffsetTimeTicks == lastCttsOffsetTimeTicks) {
2413        if (cttsSampleCount > 0) {
2414            addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks);
2415        }
2416    }
2417
2418    mTrackDurationUs += lastDurationUs;
2419    mReachedEOS = true;
2420
2421    sendTrackSummary(hasMultipleTracks);
2422
2423    ALOGI("Received total/0-length (%d/%d) buffers and encoded %d frames. - %s",
2424            count, nZeroLengthFrames, mStszTableEntries->count(), mIsAudio? "audio": "video");
2425    if (mIsAudio) {
2426        ALOGI("Audio track drift time: %lld us", mOwner->getDriftTimeUs());
2427    }
2428
2429    if (err == ERROR_END_OF_STREAM) {
2430        return OK;
2431    }
2432    return err;
2433}
2434
2435bool MPEG4Writer::Track::isTrackMalFormed() const {
2436    if (mStszTableEntries->count() == 0) {                      // no samples written
2437        ALOGE("The number of recorded samples is 0");
2438        return true;
2439    }
2440
2441    if (!mIsAudio && mStssTableEntries->count() == 0) {  // no sync frames for video
2442        ALOGE("There are no sync frames for video track");
2443        return true;
2444    }
2445
2446    if (OK != checkCodecSpecificData()) {         // no codec specific data
2447        return true;
2448    }
2449
2450    return false;
2451}
2452
2453void MPEG4Writer::Track::sendTrackSummary(bool hasMultipleTracks) {
2454
2455    // Send track summary only if test mode is enabled.
2456    if (!isTestModeEnabled()) {
2457        return;
2458    }
2459
2460    int trackNum = (mTrackId << 28);
2461
2462    mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2463                    trackNum | MEDIA_RECORDER_TRACK_INFO_TYPE,
2464                    mIsAudio? 0: 1);
2465
2466    mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2467                    trackNum | MEDIA_RECORDER_TRACK_INFO_DURATION_MS,
2468                    mTrackDurationUs / 1000);
2469
2470    mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2471                    trackNum | MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES,
2472                    mStszTableEntries->count());
2473
2474    {
2475        // The system delay time excluding the requested initial delay that
2476        // is used to eliminate the recording sound.
2477        int64_t startTimeOffsetUs = mOwner->getStartTimeOffsetMs() * 1000LL;
2478        if (startTimeOffsetUs < 0) {  // Start time offset was not set
2479            startTimeOffsetUs = kInitialDelayTimeUs;
2480        }
2481        int64_t initialDelayUs =
2482            mFirstSampleTimeRealUs - mStartTimeRealUs - startTimeOffsetUs;
2483
2484        mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2485                    trackNum | MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS,
2486                    (initialDelayUs) / 1000);
2487    }
2488
2489    mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2490                    trackNum | MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES,
2491                    mMdatSizeBytes / 1024);
2492
2493    if (hasMultipleTracks) {
2494        mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2495                    trackNum | MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS,
2496                    mMaxChunkDurationUs / 1000);
2497
2498        int64_t moovStartTimeUs = mOwner->getStartTimestampUs();
2499        if (mStartTimestampUs != moovStartTimeUs) {
2500            int64_t startTimeOffsetUs = mStartTimestampUs - moovStartTimeUs;
2501            mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2502                    trackNum | MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS,
2503                    startTimeOffsetUs / 1000);
2504        }
2505    }
2506}
2507
2508void MPEG4Writer::Track::trackProgressStatus(int64_t timeUs, status_t err) {
2509    ALOGV("trackProgressStatus: %lld us", timeUs);
2510
2511    if (mTrackEveryTimeDurationUs > 0 &&
2512        timeUs - mPreviousTrackTimeUs >= mTrackEveryTimeDurationUs) {
2513        ALOGV("Fire time tracking progress status at %lld us", timeUs);
2514        mOwner->trackProgressStatus(mTrackId, timeUs - mPreviousTrackTimeUs, err);
2515        mPreviousTrackTimeUs = timeUs;
2516    }
2517}
2518
2519void MPEG4Writer::trackProgressStatus(
2520        size_t trackId, int64_t timeUs, status_t err) {
2521    Mutex::Autolock lock(mLock);
2522    int32_t trackNum = (trackId << 28);
2523
2524    // Error notification
2525    // Do not consider ERROR_END_OF_STREAM an error
2526    if (err != OK && err != ERROR_END_OF_STREAM) {
2527        notify(MEDIA_RECORDER_TRACK_EVENT_ERROR,
2528               trackNum | MEDIA_RECORDER_TRACK_ERROR_GENERAL,
2529               err);
2530        return;
2531    }
2532
2533    if (timeUs == -1) {
2534        // Send completion notification
2535        notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2536               trackNum | MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS,
2537               err);
2538    } else {
2539        // Send progress status
2540        notify(MEDIA_RECORDER_TRACK_EVENT_INFO,
2541               trackNum | MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME,
2542               timeUs / 1000);
2543    }
2544}
2545
2546void MPEG4Writer::setDriftTimeUs(int64_t driftTimeUs) {
2547    ALOGV("setDriftTimeUs: %lld us", driftTimeUs);
2548    Mutex::Autolock autolock(mLock);
2549    mDriftTimeUs = driftTimeUs;
2550}
2551
2552int64_t MPEG4Writer::getDriftTimeUs() {
2553    ALOGV("getDriftTimeUs: %lld us", mDriftTimeUs);
2554    Mutex::Autolock autolock(mLock);
2555    return mDriftTimeUs;
2556}
2557
2558bool MPEG4Writer::isRealTimeRecording() const {
2559    return mIsRealTimeRecording;
2560}
2561
2562bool MPEG4Writer::useNalLengthFour() {
2563    return mUse4ByteNalLength;
2564}
2565
2566void MPEG4Writer::Track::bufferChunk(int64_t timestampUs) {
2567    ALOGV("bufferChunk");
2568
2569    Chunk chunk(this, timestampUs, mChunkSamples);
2570    mOwner->bufferChunk(chunk);
2571    mChunkSamples.clear();
2572}
2573
2574int64_t MPEG4Writer::Track::getDurationUs() const {
2575    return mTrackDurationUs;
2576}
2577
2578int64_t MPEG4Writer::Track::getEstimatedTrackSizeBytes() const {
2579    return mEstimatedTrackSizeBytes;
2580}
2581
2582status_t MPEG4Writer::Track::checkCodecSpecificData() const {
2583    const char *mime;
2584    CHECK(mMeta->findCString(kKeyMIMEType, &mime));
2585    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime) ||
2586        !strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime) ||
2587        !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
2588        if (!mCodecSpecificData ||
2589            mCodecSpecificDataSize <= 0) {
2590            ALOGE("Missing codec specific data");
2591            return ERROR_MALFORMED;
2592        }
2593    } else {
2594        if (mCodecSpecificData ||
2595            mCodecSpecificDataSize > 0) {
2596            ALOGE("Unexepected codec specific data found");
2597            return ERROR_MALFORMED;
2598        }
2599    }
2600    return OK;
2601}
2602
2603void MPEG4Writer::Track::writeTrackHeader(bool use32BitOffset) {
2604
2605    ALOGV("%s track time scale: %d",
2606        mIsAudio? "Audio": "Video", mTimeScale);
2607
2608    uint32_t now = getMpeg4Time();
2609    mOwner->beginBox("trak");
2610        writeTkhdBox(now);
2611        mOwner->beginBox("mdia");
2612            writeMdhdBox(now);
2613            writeHdlrBox();
2614            mOwner->beginBox("minf");
2615                if (mIsAudio) {
2616                    writeSmhdBox();
2617                } else {
2618                    writeVmhdBox();
2619                }
2620                writeDinfBox();
2621                writeStblBox(use32BitOffset);
2622            mOwner->endBox();  // minf
2623        mOwner->endBox();  // mdia
2624    mOwner->endBox();  // trak
2625}
2626
2627void MPEG4Writer::Track::writeStblBox(bool use32BitOffset) {
2628    mOwner->beginBox("stbl");
2629    mOwner->beginBox("stsd");
2630    mOwner->writeInt32(0);               // version=0, flags=0
2631    mOwner->writeInt32(1);               // entry count
2632    if (mIsAudio) {
2633        writeAudioFourCCBox();
2634    } else {
2635        writeVideoFourCCBox();
2636    }
2637    mOwner->endBox();  // stsd
2638    writeSttsBox();
2639    writeCttsBox();
2640    if (!mIsAudio) {
2641        writeStssBox();
2642    }
2643    writeStszBox();
2644    writeStscBox();
2645    writeStcoBox(use32BitOffset);
2646    mOwner->endBox();  // stbl
2647}
2648
2649void MPEG4Writer::Track::writeVideoFourCCBox() {
2650    const char *mime;
2651    bool success = mMeta->findCString(kKeyMIMEType, &mime);
2652    CHECK(success);
2653    if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
2654        mOwner->beginBox("mp4v");
2655    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
2656        mOwner->beginBox("s263");
2657    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
2658        mOwner->beginBox("avc1");
2659    } else {
2660        ALOGE("Unknown mime type '%s'.", mime);
2661        CHECK(!"should not be here, unknown mime type.");
2662    }
2663
2664    mOwner->writeInt32(0);           // reserved
2665    mOwner->writeInt16(0);           // reserved
2666    mOwner->writeInt16(1);           // data ref index
2667    mOwner->writeInt16(0);           // predefined
2668    mOwner->writeInt16(0);           // reserved
2669    mOwner->writeInt32(0);           // predefined
2670    mOwner->writeInt32(0);           // predefined
2671    mOwner->writeInt32(0);           // predefined
2672
2673    int32_t width, height;
2674    success = mMeta->findInt32(kKeyWidth, &width);
2675    success = success && mMeta->findInt32(kKeyHeight, &height);
2676    CHECK(success);
2677
2678    mOwner->writeInt16(width);
2679    mOwner->writeInt16(height);
2680    mOwner->writeInt32(0x480000);    // horiz resolution
2681    mOwner->writeInt32(0x480000);    // vert resolution
2682    mOwner->writeInt32(0);           // reserved
2683    mOwner->writeInt16(1);           // frame count
2684    mOwner->writeInt8(0);            // compressor string length
2685    mOwner->write("                               ", 31);
2686    mOwner->writeInt16(0x18);        // depth
2687    mOwner->writeInt16(-1);          // predefined
2688
2689    CHECK_LT(23 + mCodecSpecificDataSize, 128);
2690
2691    if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) {
2692        writeMp4vEsdsBox();
2693    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) {
2694        writeD263Box();
2695    } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) {
2696        writeAvccBox();
2697    }
2698
2699    writePaspBox();
2700    mOwner->endBox();  // mp4v, s263 or avc1
2701}
2702
2703void MPEG4Writer::Track::writeAudioFourCCBox() {
2704    const char *mime;
2705    bool success = mMeta->findCString(kKeyMIMEType, &mime);
2706    CHECK(success);
2707    const char *fourcc = NULL;
2708    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) {
2709        fourcc = "samr";
2710    } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
2711        fourcc = "sawb";
2712    } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
2713        fourcc = "mp4a";
2714    } else {
2715        ALOGE("Unknown mime type '%s'.", mime);
2716        CHECK(!"should not be here, unknown mime type.");
2717    }
2718
2719    mOwner->beginBox(fourcc);        // audio format
2720    mOwner->writeInt32(0);           // reserved
2721    mOwner->writeInt16(0);           // reserved
2722    mOwner->writeInt16(0x1);         // data ref index
2723    mOwner->writeInt32(0);           // reserved
2724    mOwner->writeInt32(0);           // reserved
2725    int32_t nChannels;
2726    CHECK_EQ(true, mMeta->findInt32(kKeyChannelCount, &nChannels));
2727    mOwner->writeInt16(nChannels);   // channel count
2728    mOwner->writeInt16(16);          // sample size
2729    mOwner->writeInt16(0);           // predefined
2730    mOwner->writeInt16(0);           // reserved
2731
2732    int32_t samplerate;
2733    success = mMeta->findInt32(kKeySampleRate, &samplerate);
2734    CHECK(success);
2735    mOwner->writeInt32(samplerate << 16);
2736    if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) {
2737        writeMp4aEsdsBox();
2738    } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime) ||
2739               !strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) {
2740        writeDamrBox();
2741    }
2742    mOwner->endBox();
2743}
2744
2745void MPEG4Writer::Track::writeMp4aEsdsBox() {
2746    mOwner->beginBox("esds");
2747    CHECK(mCodecSpecificData);
2748    CHECK_GT(mCodecSpecificDataSize, 0);
2749
2750    // Make sure all sizes encode to a single byte.
2751    CHECK_LT(mCodecSpecificDataSize + 23, 128);
2752
2753    mOwner->writeInt32(0);     // version=0, flags=0
2754    mOwner->writeInt8(0x03);   // ES_DescrTag
2755    mOwner->writeInt8(23 + mCodecSpecificDataSize);
2756    mOwner->writeInt16(0x0000);// ES_ID
2757    mOwner->writeInt8(0x00);
2758
2759    mOwner->writeInt8(0x04);   // DecoderConfigDescrTag
2760    mOwner->writeInt8(15 + mCodecSpecificDataSize);
2761    mOwner->writeInt8(0x40);   // objectTypeIndication ISO/IEC 14492-2
2762    mOwner->writeInt8(0x15);   // streamType AudioStream
2763
2764    mOwner->writeInt16(0x03);  // XXX
2765    mOwner->writeInt8(0x00);   // buffer size 24-bit
2766    mOwner->writeInt32(96000); // max bit rate
2767    mOwner->writeInt32(96000); // avg bit rate
2768
2769    mOwner->writeInt8(0x05);   // DecoderSpecificInfoTag
2770    mOwner->writeInt8(mCodecSpecificDataSize);
2771    mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
2772
2773    static const uint8_t kData2[] = {
2774        0x06,  // SLConfigDescriptorTag
2775        0x01,
2776        0x02
2777    };
2778    mOwner->write(kData2, sizeof(kData2));
2779
2780    mOwner->endBox();  // esds
2781}
2782
2783void MPEG4Writer::Track::writeMp4vEsdsBox() {
2784    CHECK(mCodecSpecificData);
2785    CHECK_GT(mCodecSpecificDataSize, 0);
2786    mOwner->beginBox("esds");
2787
2788    mOwner->writeInt32(0);    // version=0, flags=0
2789
2790    mOwner->writeInt8(0x03);  // ES_DescrTag
2791    mOwner->writeInt8(23 + mCodecSpecificDataSize);
2792    mOwner->writeInt16(0x0000);  // ES_ID
2793    mOwner->writeInt8(0x1f);
2794
2795    mOwner->writeInt8(0x04);  // DecoderConfigDescrTag
2796    mOwner->writeInt8(15 + mCodecSpecificDataSize);
2797    mOwner->writeInt8(0x20);  // objectTypeIndication ISO/IEC 14492-2
2798    mOwner->writeInt8(0x11);  // streamType VisualStream
2799
2800    static const uint8_t kData[] = {
2801        0x01, 0x77, 0x00,
2802        0x00, 0x03, 0xe8, 0x00,
2803        0x00, 0x03, 0xe8, 0x00
2804    };
2805    mOwner->write(kData, sizeof(kData));
2806
2807    mOwner->writeInt8(0x05);  // DecoderSpecificInfoTag
2808
2809    mOwner->writeInt8(mCodecSpecificDataSize);
2810    mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
2811
2812    static const uint8_t kData2[] = {
2813        0x06,  // SLConfigDescriptorTag
2814        0x01,
2815        0x02
2816    };
2817    mOwner->write(kData2, sizeof(kData2));
2818
2819    mOwner->endBox();  // esds
2820}
2821
2822void MPEG4Writer::Track::writeTkhdBox(uint32_t now) {
2823    mOwner->beginBox("tkhd");
2824    // Flags = 7 to indicate that the track is enabled, and
2825    // part of the presentation
2826    mOwner->writeInt32(0x07);          // version=0, flags=7
2827    mOwner->writeInt32(now);           // creation time
2828    mOwner->writeInt32(now);           // modification time
2829    mOwner->writeInt32(mTrackId);      // track id starts with 1
2830    mOwner->writeInt32(0);             // reserved
2831    int64_t trakDurationUs = getDurationUs();
2832    int32_t mvhdTimeScale = mOwner->getTimeScale();
2833    int32_t tkhdDuration =
2834        (trakDurationUs * mvhdTimeScale + 5E5) / 1E6;
2835    mOwner->writeInt32(tkhdDuration);  // in mvhd timescale
2836    mOwner->writeInt32(0);             // reserved
2837    mOwner->writeInt32(0);             // reserved
2838    mOwner->writeInt16(0);             // layer
2839    mOwner->writeInt16(0);             // alternate group
2840    mOwner->writeInt16(mIsAudio ? 0x100 : 0);  // volume
2841    mOwner->writeInt16(0);             // reserved
2842
2843    mOwner->writeCompositionMatrix(mRotation);       // matrix
2844
2845    if (mIsAudio) {
2846        mOwner->writeInt32(0);
2847        mOwner->writeInt32(0);
2848    } else {
2849        int32_t width, height;
2850        bool success = mMeta->findInt32(kKeyWidth, &width);
2851        success = success && mMeta->findInt32(kKeyHeight, &height);
2852        CHECK(success);
2853
2854        mOwner->writeInt32(width << 16);   // 32-bit fixed-point value
2855        mOwner->writeInt32(height << 16);  // 32-bit fixed-point value
2856    }
2857    mOwner->endBox();  // tkhd
2858}
2859
2860void MPEG4Writer::Track::writeVmhdBox() {
2861    mOwner->beginBox("vmhd");
2862    mOwner->writeInt32(0x01);        // version=0, flags=1
2863    mOwner->writeInt16(0);           // graphics mode
2864    mOwner->writeInt16(0);           // opcolor
2865    mOwner->writeInt16(0);
2866    mOwner->writeInt16(0);
2867    mOwner->endBox();
2868}
2869
2870void MPEG4Writer::Track::writeSmhdBox() {
2871    mOwner->beginBox("smhd");
2872    mOwner->writeInt32(0);           // version=0, flags=0
2873    mOwner->writeInt16(0);           // balance
2874    mOwner->writeInt16(0);           // reserved
2875    mOwner->endBox();
2876}
2877
2878void MPEG4Writer::Track::writeHdlrBox() {
2879    mOwner->beginBox("hdlr");
2880    mOwner->writeInt32(0);             // version=0, flags=0
2881    mOwner->writeInt32(0);             // component type: should be mhlr
2882    mOwner->writeFourcc(mIsAudio ? "soun" : "vide");  // component subtype
2883    mOwner->writeInt32(0);             // reserved
2884    mOwner->writeInt32(0);             // reserved
2885    mOwner->writeInt32(0);             // reserved
2886    // Removing "r" for the name string just makes the string 4 byte aligned
2887    mOwner->writeCString(mIsAudio ? "SoundHandle": "VideoHandle");  // name
2888    mOwner->endBox();
2889}
2890
2891void MPEG4Writer::Track::writeMdhdBox(uint32_t now) {
2892    int64_t trakDurationUs = getDurationUs();
2893    mOwner->beginBox("mdhd");
2894    mOwner->writeInt32(0);             // version=0, flags=0
2895    mOwner->writeInt32(now);           // creation time
2896    mOwner->writeInt32(now);           // modification time
2897    mOwner->writeInt32(mTimeScale);    // media timescale
2898    int32_t mdhdDuration = (trakDurationUs * mTimeScale + 5E5) / 1E6;
2899    mOwner->writeInt32(mdhdDuration);  // use media timescale
2900    // Language follows the three letter standard ISO-639-2/T
2901    // 'e', 'n', 'g' for "English", for instance.
2902    // Each character is packed as the difference between its ASCII value and 0x60.
2903    // For "English", these are 00101, 01110, 00111.
2904    // XXX: Where is the padding bit located: 0x15C7?
2905    mOwner->writeInt16(0);             // language code
2906    mOwner->writeInt16(0);             // predefined
2907    mOwner->endBox();
2908}
2909
2910void MPEG4Writer::Track::writeDamrBox() {
2911    // 3gpp2 Spec AMRSampleEntry fields
2912    mOwner->beginBox("damr");
2913    mOwner->writeCString("   ");  // vendor: 4 bytes
2914    mOwner->writeInt8(0);         // decoder version
2915    mOwner->writeInt16(0x83FF);   // mode set: all enabled
2916    mOwner->writeInt8(0);         // mode change period
2917    mOwner->writeInt8(1);         // frames per sample
2918    mOwner->endBox();
2919}
2920
2921void MPEG4Writer::Track::writeUrlBox() {
2922    // The table index here refers to the sample description index
2923    // in the sample table entries.
2924    mOwner->beginBox("url ");
2925    mOwner->writeInt32(1);  // version=0, flags=1 (self-contained)
2926    mOwner->endBox();  // url
2927}
2928
2929void MPEG4Writer::Track::writeDrefBox() {
2930    mOwner->beginBox("dref");
2931    mOwner->writeInt32(0);  // version=0, flags=0
2932    mOwner->writeInt32(1);  // entry count (either url or urn)
2933    writeUrlBox();
2934    mOwner->endBox();  // dref
2935}
2936
2937void MPEG4Writer::Track::writeDinfBox() {
2938    mOwner->beginBox("dinf");
2939    writeDrefBox();
2940    mOwner->endBox();  // dinf
2941}
2942
2943void MPEG4Writer::Track::writeAvccBox() {
2944    CHECK(mCodecSpecificData);
2945    CHECK_GE(mCodecSpecificDataSize, 5);
2946
2947    // Patch avcc's lengthSize field to match the number
2948    // of bytes we use to indicate the size of a nal unit.
2949    uint8_t *ptr = (uint8_t *)mCodecSpecificData;
2950    ptr[4] = (ptr[4] & 0xfc) | (mOwner->useNalLengthFour() ? 3 : 1);
2951    mOwner->beginBox("avcC");
2952    mOwner->write(mCodecSpecificData, mCodecSpecificDataSize);
2953    mOwner->endBox();  // avcC
2954}
2955
2956void MPEG4Writer::Track::writeD263Box() {
2957    mOwner->beginBox("d263");
2958    mOwner->writeInt32(0);  // vendor
2959    mOwner->writeInt8(0);   // decoder version
2960    mOwner->writeInt8(10);  // level: 10
2961    mOwner->writeInt8(0);   // profile: 0
2962    mOwner->endBox();  // d263
2963}
2964
2965// This is useful if the pixel is not square
2966void MPEG4Writer::Track::writePaspBox() {
2967    mOwner->beginBox("pasp");
2968    mOwner->writeInt32(1 << 16);  // hspacing
2969    mOwner->writeInt32(1 << 16);  // vspacing
2970    mOwner->endBox();  // pasp
2971}
2972
2973int32_t MPEG4Writer::Track::getStartTimeOffsetScaledTime() const {
2974    int64_t trackStartTimeOffsetUs = 0;
2975    int64_t moovStartTimeUs = mOwner->getStartTimestampUs();
2976    if (mStartTimestampUs != moovStartTimeUs) {
2977        CHECK_GT(mStartTimestampUs, moovStartTimeUs);
2978        trackStartTimeOffsetUs = mStartTimestampUs - moovStartTimeUs;
2979    }
2980    return (trackStartTimeOffsetUs *  mTimeScale + 500000LL) / 1000000LL;
2981}
2982
2983void MPEG4Writer::Track::writeSttsBox() {
2984    mOwner->beginBox("stts");
2985    mOwner->writeInt32(0);  // version=0, flags=0
2986    uint32_t duration;
2987    CHECK(mSttsTableEntries->get(duration, 1));
2988    duration = htonl(duration);  // Back to host byte order
2989    mSttsTableEntries->set(htonl(duration + getStartTimeOffsetScaledTime()), 1);
2990    mSttsTableEntries->write(mOwner);
2991    mOwner->endBox();  // stts
2992}
2993
2994void MPEG4Writer::Track::writeCttsBox() {
2995    if (mIsAudio) {  // ctts is not for audio
2996        return;
2997    }
2998
2999    // There is no B frame at all
3000    if (mMinCttsOffsetTimeUs == mMaxCttsOffsetTimeUs) {
3001        return;
3002    }
3003
3004    // Do not write ctts box when there is no need to have it.
3005    if (mCttsTableEntries->count() == 0) {
3006        return;
3007    }
3008
3009    ALOGV("ctts box has %d entries with range [%lld, %lld]",
3010            mCttsTableEntries->count(), mMinCttsOffsetTimeUs, mMaxCttsOffsetTimeUs);
3011
3012    mOwner->beginBox("ctts");
3013    mOwner->writeInt32(0);  // version=0, flags=0
3014    uint32_t duration;
3015    CHECK(mCttsTableEntries->get(duration, 1));
3016    duration = htonl(duration);  // Back host byte order
3017    mCttsTableEntries->set(htonl(duration + getStartTimeOffsetScaledTime() - mMinCttsOffsetTimeUs), 1);
3018    mCttsTableEntries->write(mOwner);
3019    mOwner->endBox();  // ctts
3020}
3021
3022void MPEG4Writer::Track::writeStssBox() {
3023    mOwner->beginBox("stss");
3024    mOwner->writeInt32(0);  // version=0, flags=0
3025    mStssTableEntries->write(mOwner);
3026    mOwner->endBox();  // stss
3027}
3028
3029void MPEG4Writer::Track::writeStszBox() {
3030    mOwner->beginBox("stsz");
3031    mOwner->writeInt32(0);  // version=0, flags=0
3032    mOwner->writeInt32(0);
3033    mStszTableEntries->write(mOwner);
3034    mOwner->endBox();  // stsz
3035}
3036
3037void MPEG4Writer::Track::writeStscBox() {
3038    mOwner->beginBox("stsc");
3039    mOwner->writeInt32(0);  // version=0, flags=0
3040    mStscTableEntries->write(mOwner);
3041    mOwner->endBox();  // stsc
3042}
3043
3044void MPEG4Writer::Track::writeStcoBox(bool use32BitOffset) {
3045    mOwner->beginBox(use32BitOffset? "stco": "co64");
3046    mOwner->writeInt32(0);  // version=0, flags=0
3047    if (use32BitOffset) {
3048        mStcoTableEntries->write(mOwner);
3049    } else {
3050        mCo64TableEntries->write(mOwner);
3051    }
3052    mOwner->endBox();  // stco or co64
3053}
3054
3055void MPEG4Writer::writeUdtaBox() {
3056    beginBox("udta");
3057    writeGeoDataBox();
3058    endBox();
3059}
3060
3061/*
3062 * Geodata is stored according to ISO-6709 standard.
3063 */
3064void MPEG4Writer::writeGeoDataBox() {
3065    beginBox("\xA9xyz");
3066    /*
3067     * For historical reasons, any user data start
3068     * with "\0xA9", must be followed by its assoicated
3069     * language code.
3070     * 0x0012: text string length
3071     * 0x15c7: lang (locale) code: en
3072     */
3073    writeInt32(0x001215c7);
3074    writeLatitude(mLatitudex10000);
3075    writeLongitude(mLongitudex10000);
3076    writeInt8(0x2F);
3077    endBox();
3078}
3079
3080}  // namespace android
3081