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