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