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