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