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