MPEG4Writer.cpp revision 316d08c1f115499fb0ccd82d22592bed7e54aad7
120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber/* 220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Copyright (C) 2009 The Android Open Source Project 320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License"); 520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * you may not use this file except in compliance with the License. 620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * You may obtain a copy of the License at 720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * http://www.apache.org/licenses/LICENSE-2.0 920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * 1020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * Unless required by applicable law or agreed to in writing, software 1120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS, 1220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * See the License for the specific language governing permissions and 1420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber * limitations under the License. 1520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber */ 1620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 17050b28a593350047845a45a14cc5026221ac1620James Dong//#define LOG_NDEBUG 0 18050b28a593350047845a45a14cc5026221ac1620James Dong#define LOG_TAG "MPEG4Writer" 19050b28a593350047845a45a14cc5026221ac1620James Dong 2020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <arpa/inet.h> 21a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <fcntl.h> 22a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <inttypes.h> 2320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <pthread.h> 24a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong#include <sys/prctl.h> 25a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <sys/stat.h> 26a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <sys/types.h> 27a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <unistd.h> 28a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn 29a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn#include <utils/Log.h> 3020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3143089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong#include <media/stagefright/foundation/ADebug.h> 32e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang#include <media/stagefright/foundation/AMessage.h> 3320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MPEG4Writer.h> 3420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MediaBuffer.h> 3520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MetaData.h> 3618291bc20e55e8f3fd5feb786771a8ed32c19c59Andreas Huber#include <media/stagefright/MediaDefs.h> 3703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber#include <media/stagefright/MediaErrors.h> 3820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/MediaSource.h> 3920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber#include <media/stagefright/Utils.h> 40d599cd4573b5a2d5914c5040e0565ef866749b77James Dong#include <media/mediarecorder.h> 4107ec01904613a0bac32caaa8444b4690998faed7James Dong#include <cutils/properties.h> 4220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 4319c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber#include "include/ESDS.h" 449aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim#include "include/HevcUtils.h" 45dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn 46dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn#ifndef __predict_false 47dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn#define __predict_false(exp) __builtin_expect((exp) != 0, 0) 48dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn#endif 49dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn 5011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih#define WARN_UNLESS(condition, message, ...) \ 51dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7Mark Salyzyn( (__predict_false(condition)) ? false : ({ \ 5211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih ALOGW("Condition %s failed " message, #condition, ##__VA_ARGS__); \ 5311f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih true; \ 5411f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih})) 5511f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 5620111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubernamespace android { 5720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 5877e8ae9967a078770416619e99ddb5b010def312James Dongstatic const int64_t kMinStreamableFileSizeInBytes = 5 * 1024 * 1024; 591f1f2b1678fd0d038dfc501252dd2b65ecf10caeRachadstatic const int64_t kMax32BitFileSize = 0x00ffffffffLL; // 2^32-1 : max FAT32 601f1f2b1678fd0d038dfc501252dd2b65ecf10caeRachad // filesystem file size 611f1f2b1678fd0d038dfc501252dd2b65ecf10caeRachad // used by most SD cards 623266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatic const uint8_t kNalUnitTypeSeqParamSet = 0x07; 633266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatic const uint8_t kNalUnitTypePicParamSet = 0x08; 6470ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dongstatic const int64_t kInitialDelayTimeUs = 700000LL; 655b6a01e65aa4129a9226667536d1bc1dad5980d8James Dong 667c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhangstatic const char kMetaKey_Version[] = "com.android.version"; 673b0da19c78f6a61ed343a07f2448b94faafe4c02Lajos Molnar#ifdef SHOW_MODEL_BUILD 683b0da19c78f6a61ed343a07f2448b94faafe4c02Lajos Molnarstatic const char kMetaKey_Model[] = "com.android.model"; 697c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhangstatic const char kMetaKey_Build[] = "com.android.build"; 703b0da19c78f6a61ed343a07f2448b94faafe4c02Lajos Molnar#endif 717c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhangstatic const char kMetaKey_CaptureFps[] = "com.android.capture.fps"; 727c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 739aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatic const uint8_t kMandatoryHevcNalUnitTypes[3] = { 749aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypeVps, 759aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypeSps, 769aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypePps, 779aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim}; 789aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatic const uint8_t kHevcNalUnitTypes[5] = { 799aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypeVps, 809aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypeSps, 819aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypePps, 829aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypePrefixSei, 839aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim kHevcNalUnitTypeSuffixSei, 849aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim}; 857c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang/* uncomment to include model and build in meta */ 867c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang//#define SHOW_MODEL_BUILD 1 877c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 8820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass MPEG4Writer::Track { 8920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberpublic: 90b2487f03f12dcafdb801fc0007c8df8412397f44Marco Nelissen Track(MPEG4Writer *owner, const sp<IMediaSource> &source, size_t trackId); 918f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong 9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber ~Track(); 9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9493d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong status_t start(MetaData *params); 9537187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t stop(); 9637187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t pause(); 9725b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber bool reachedEOS(); 9820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 993b240d6bf235f6f6b40ee4c37a2862de286ca1e4Andreas Huber int64_t getDurationUs() const; 100d599cd4573b5a2d5914c5040e0565ef866749b77James Dong int64_t getEstimatedTrackSizeBytes() const; 101b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeTrackHeader(bool use32BitOffset = true); 1021c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void bufferChunk(int64_t timestampUs); 1031c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool isAvc() const { return mIsAvc; } 1049aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim bool isHevc() const { return mIsHevc; } 1051c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool isAudio() const { return mIsAudio; } 1061c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool isMPEG4() const { return mIsMPEG4; } 107c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong void addChunkOffset(off64_t offset); 10870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int32_t getTrackId() const { return mTrackId; } 109dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong status_t dump(int fd, const Vector<String16>& args) const; 1108b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan static const char *getFourCCForMime(const char *mime); 11120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 11220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberprivate: 113000e18370baae60ffd9f25b509501dd8c26deabfJames Dong enum { 114000e18370baae60ffd9f25b509501dd8c26deabfJames Dong kMaxCttsOffsetTimeUs = 1000000LL, // 1 second 1158c460498c028888c533ab442be12b6d4b669b965James Dong kSampleArraySize = 1000, 116000e18370baae60ffd9f25b509501dd8c26deabfJames Dong }; 117000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 118c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // A helper class to handle faster write box with table entries 119c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong template<class TYPE> 120c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong struct ListTableEntries { 121c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries(uint32_t elementCapacity, uint32_t entryCapacity) 122c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong : mElementCapacity(elementCapacity), 123c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mEntryCapacity(entryCapacity), 124c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mTotalNumTableEntries(0), 125c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mNumValuesInCurrEntry(0), 126c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCurrTableEntriesElement(NULL) { 127c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_GT(mElementCapacity, 0); 128c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_GT(mEntryCapacity, 0); 1292177f21c1ae5bf5cb8c1113a1adbe1fd7db34206Wei Jia // Ensure no integer overflow on allocation in add(). 1302177f21c1ae5bf5cb8c1113a1adbe1fd7db34206Wei Jia CHECK_LT(mEntryCapacity, UINT32_MAX / mElementCapacity); 131c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 132c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 133c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Free the allocated memory. 134c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ~ListTableEntries() { 135c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong while (!mTableEntryList.empty()) { 136c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong typename List<TYPE *>::iterator it = mTableEntryList.begin(); 137c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete[] (*it); 138c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mTableEntryList.erase(it); 139c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 140c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 141c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 142c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Replace the value at the given position by the given value. 143c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // There must be an existing value at the given position. 144c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg value must be in network byte order 145c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg pos location the value must be in. 146c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong void set(const TYPE& value, uint32_t pos) { 14725f0d7ba1987de61c75f8c68b19de48e0ad9736cJames Dong CHECK_LT(pos, mTotalNumTableEntries * mEntryCapacity); 148c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 149c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong typename List<TYPE *>::iterator it = mTableEntryList.begin(); 150c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity)); 151c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong while (it != mTableEntryList.end() && iterations > 0) { 152c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ++it; 153c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong --iterations; 154c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 155c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK(it != mTableEntryList.end()); 156c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_EQ(iterations, 0); 157c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 158c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong (*it)[(pos % (mElementCapacity * mEntryCapacity))] = value; 159c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 160c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 161c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Get the value at the given position by the given value. 162c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg value the retrieved value at the position in network byte order. 163c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg pos location the value must be in. 164c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @return true if a value is found. 165c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong bool get(TYPE& value, uint32_t pos) const { 16625f0d7ba1987de61c75f8c68b19de48e0ad9736cJames Dong if (pos >= mTotalNumTableEntries * mEntryCapacity) { 167c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong return false; 168c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 169c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 170c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong typename List<TYPE *>::iterator it = mTableEntryList.begin(); 171c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t iterations = (pos / (mElementCapacity * mEntryCapacity)); 172c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong while (it != mTableEntryList.end() && iterations > 0) { 173c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ++it; 174c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong --iterations; 175c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 176c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK(it != mTableEntryList.end()); 177c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_EQ(iterations, 0); 178c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 179c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong value = (*it)[(pos % (mElementCapacity * mEntryCapacity))]; 180c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong return true; 181c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 182c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 183c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Store a single value. 184c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg value must be in network byte order. 185c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong void add(const TYPE& value) { 186c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_LT(mNumValuesInCurrEntry, mElementCapacity); 187c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t nEntries = mTotalNumTableEntries % mElementCapacity; 188c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t nValues = mNumValuesInCurrEntry % mEntryCapacity; 189c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (nEntries == 0 && nValues == 0) { 190c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCurrTableEntriesElement = new TYPE[mEntryCapacity * mElementCapacity]; 191c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK(mCurrTableEntriesElement != NULL); 192c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mTableEntryList.push_back(mCurrTableEntriesElement); 193c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 194c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 195c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t pos = nEntries * mEntryCapacity + nValues; 196c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCurrTableEntriesElement[pos] = value; 197c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 198c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ++mNumValuesInCurrEntry; 199c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if ((mNumValuesInCurrEntry % mEntryCapacity) == 0) { 200c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ++mTotalNumTableEntries; 201c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mNumValuesInCurrEntry = 0; 202c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 203c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 204c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 205c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Write out the table entries: 206c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // 1. the number of entries goes first 207c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // 2. followed by the values in the table enties in order 208c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // @arg writer the writer to actual write to the storage 209c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong void write(MPEG4Writer *writer) const { 210c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_EQ(mNumValuesInCurrEntry % mEntryCapacity, 0); 211c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t nEntries = mTotalNumTableEntries; 212c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong writer->writeInt32(nEntries); 213c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong for (typename List<TYPE *>::iterator it = mTableEntryList.begin(); 214c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong it != mTableEntryList.end(); ++it) { 215c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK_GT(nEntries, 0); 216c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (nEntries >= mElementCapacity) { 217c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong writer->write(*it, sizeof(TYPE) * mEntryCapacity, mElementCapacity); 218c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong nEntries -= mElementCapacity; 219c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } else { 220c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong writer->write(*it, sizeof(TYPE) * mEntryCapacity, nEntries); 221c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong break; 222c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 223c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 224c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 225c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 226c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong // Return the number of entries in the table. 227c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t count() const { return mTotalNumTableEntries; } 228c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 229c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong private: 230c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t mElementCapacity; // # entries in an element 231c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t mEntryCapacity; // # of values in each entry 232c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t mTotalNumTableEntries; 233c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t mNumValuesInCurrEntry; // up to mEntryCapacity 234c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong TYPE *mCurrTableEntriesElement; 235c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mutable List<TYPE *> mTableEntryList; 236c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 237c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong DISALLOW_EVIL_CONSTRUCTORS(ListTableEntries); 238c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong }; 239c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 240c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 241c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 24220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MPEG4Writer *mOwner; 24320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber sp<MetaData> mMeta; 244b2487f03f12dcafdb801fc0007c8df8412397f44Marco Nelissen sp<IMediaSource> mSource; 24520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber volatile bool mDone; 246a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong volatile bool mPaused; 247a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong volatile bool mResumed; 248eaae38445a340c4857c1c5569475879a728e63b7James Dong volatile bool mStarted; 2491c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool mIsAvc; 2509aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim bool mIsHevc; 2511c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool mIsAudio; 2521c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bool mIsMPEG4; 253bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong int32_t mTrackId; 254c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong int64_t mTrackDurationUs; 25543ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong int64_t mMaxChunkDurationUs; 256e259531ce59ab1f31de5a23124b22536f6a5a767James Dong 257d599cd4573b5a2d5914c5040e0565ef866749b77James Dong int64_t mEstimatedTrackSizeBytes; 2581f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong int64_t mMdatSizeBytes; 2598f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int32_t mTimeScale; 26020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 26120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_t mThread; 26220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 263be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong 26413aec890216948b0c364f8f92792129d0335f506James Dong List<MediaBuffer *> mChunkSamples; 2651f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 266c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong bool mSamplesHaveSameSize; 267c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mStszTableEntries; 268be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong 269c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mStcoTableEntries; 270c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<off64_t> *mCo64TableEntries; 271c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mStscTableEntries; 272c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mStssTableEntries; 273c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mSttsTableEntries; 274c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ListTableEntries<uint32_t> *mCttsTableEntries; 275965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 276000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int64_t mMinCttsOffsetTimeUs; 277000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int64_t mMaxCttsOffsetTimeUs; 278965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 2793266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Sequence parameter set or picture parameter set 2803266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong struct AVCParamSet { 2813266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong AVCParamSet(uint16_t length, const uint8_t *data) 2823266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong : mLength(length), mData(data) {} 2833266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 2843266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint16_t mLength; 2853266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *mData; 2863266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong }; 2873266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong List<AVCParamSet> mSeqParamSets; 2883266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong List<AVCParamSet> mPicParamSets; 2893266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint8_t mProfileIdc; 2903266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint8_t mProfileCompatible; 2913266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint8_t mLevelIdc; 2923266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 29320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void *mCodecSpecificData; 29420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber size_t mCodecSpecificDataSize; 295548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber bool mGotAllCodecSpecificData; 29693d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong bool mTrackingProgressStatus; 29720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 29825b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber bool mReachedEOS; 2993c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong int64_t mStartTimestampUs; 30070ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int64_t mStartTimeRealUs; 30170ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int64_t mFirstSampleTimeRealUs; 30293d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong int64_t mPreviousTrackTimeUs; 30393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong int64_t mTrackEveryTimeDurationUs; 30425b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 305872a481558350634a3fd5cb67939de288af00ecbJames Dong // Update the audio track's drift information. 306872a481558350634a3fd5cb67939de288af00ecbJames Dong void updateDriftTime(const sp<MetaData>& meta); 307872a481558350634a3fd5cb67939de288af00ecbJames Dong 308000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int32_t getStartTimeOffsetScaledTime() const; 309000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 31020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber static void *ThreadWrapper(void *me); 31137187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t threadEntry(); 31220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3133266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *parseParamSet( 3143266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *data, size_t length, int type, size_t *paramSetLen); 3153266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 3169aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t copyCodecSpecificData(const uint8_t *data, size_t size, size_t minLength = 0); 3179aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 318b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong status_t makeAVCCodecSpecificData(const uint8_t *data, size_t size); 319b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong status_t copyAVCCodecSpecificData(const uint8_t *data, size_t size); 320b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong status_t parseAVCCodecSpecificData(const uint8_t *data, size_t size); 321215381ea729086b8359b7f59bdc2bd7cf55a0c45James Dong 3229aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t makeHEVCCodecSpecificData(const uint8_t *data, size_t size); 3239aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t copyHEVCCodecSpecificData(const uint8_t *data, size_t size); 3249aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t parseHEVCCodecSpecificData( 3259aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *data, size_t size, HevcParameterSets ¶mSets); 3269aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 327215381ea729086b8359b7f59bdc2bd7cf55a0c45James Dong // Track authoring progress status 328faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong void trackProgressStatus(int64_t timeUs, status_t err = OK); 32993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong void initTrackingProgressStatus(MetaData *params); 33003b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 33119c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber void getCodecSpecificDataFromInputFormatIfPossible(); 33219c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber 333c059860c73678a202bfa33062723e8f82fb779d9James Dong // Determine the track time scale 334c059860c73678a202bfa33062723e8f82fb779d9James Dong // If it is an audio track, try to use the sampling rate as 335c059860c73678a202bfa33062723e8f82fb779d9James Dong // the time scale; however, if user chooses the overwrite 336c059860c73678a202bfa33062723e8f82fb779d9James Dong // value, the user-supplied time scale will be used. 337c059860c73678a202bfa33062723e8f82fb779d9James Dong void setTimeScale(); 338c059860c73678a202bfa33062723e8f82fb779d9James Dong 339690f546b0ee548dbfe997df36418e5302ec2d786James Dong // Simple validation on the codec specific data 340690f546b0ee548dbfe997df36418e5302ec2d786James Dong status_t checkCodecSpecificData() const; 34113f6284305e4b27395a23db7882d670bdb1bcae1James Dong int32_t mRotation; 342690f546b0ee548dbfe997df36418e5302ec2d786James Dong 3431f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong void updateTrackSizeEstimate(); 3441f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong void addOneStscTableEntry(size_t chunkId, size_t sampleId); 3451f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong void addOneStssTableEntry(size_t sampleId); 34679761ab096f57c3027fad9556c2bc436672d614eJames Dong 34779761ab096f57c3027fad9556c2bc436672d614eJames Dong // Duration is time scale based 34879761ab096f57c3027fad9556c2bc436672d614eJames Dong void addOneSttsTableEntry(size_t sampleCount, int32_t timescaledDur); 349965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong void addOneCttsTableEntry(size_t sampleCount, int32_t timescaledDur); 35045c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 35145c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong bool isTrackMalFormed() const; 35243ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong void sendTrackSummary(bool hasMultipleTracks); 3531f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 354b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Write the boxes 355b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeStcoBox(bool use32BitOffset); 356b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeStscBox(); 357b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeStszBox(); 358b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeStssBox(); 359b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeSttsBox(); 360965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong void writeCttsBox(); 361b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeD263Box(); 362b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writePaspBox(); 363b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeAvccBox(); 3649aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim void writeHvccBox(); 365b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeUrlBox(); 366b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeDrefBox(); 367b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeDinfBox(); 368b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeDamrBox(); 369efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson void writeMdhdBox(uint32_t now); 370b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeSmhdBox(); 371b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeVmhdBox(); 372b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeHdlrBox(); 373efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson void writeTkhdBox(uint32_t now); 374b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeMp4aEsdsBox(); 375b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeMp4vEsdsBox(); 376b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeAudioFourCCBox(); 377b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeVideoFourCCBox(); 378b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong void writeStblBox(bool use32BitOffset); 379b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 38020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Track(const Track &); 38120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Track &operator=(const Track &); 38220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber}; 38320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 38430ab66297501757d745b9ae10da61adcd891f497Andreas HuberMPEG4Writer::MPEG4Writer(int fd) 385674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong : mFd(dup(fd)), 386674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong mInitCheck(mFd < 0? NO_INIT: OK), 387de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui mIsRealTimeRecording(true), 388b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong mUse4ByteNalLength(true), 3891acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong mUse32BitOffset(true), 390a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong mIsFileSizeLimitExplicitlyRequested(false), 391a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused(false), 392a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mStarted(false), 393411ba422e3635d534928ffd81abf54f4f291c739James Dong mWriterThreadStarted(false), 39430ab66297501757d745b9ae10da61adcd891f497Andreas Huber mOffset(0), 39513aec890216948b0c364f8f92792129d0335f506James Dong mMdatOffset(0), 396e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mMoovBoxBuffer(NULL), 397e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mMoovBoxBufferOffset(0), 398e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mWriteMoovBoxToMemory(false), 399e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mFreeBoxOffset(0), 400e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mStreamableFile(false), 4017837c17063a4c50bc856ba59418516fdab731de7James Dong mEstimatedMoovBoxSize(0), 4027c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize(0), 40307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mInterleaveDurationUs(1000000), 404e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mTimeScale(-1), 405e502b443ce9d229d1e9108834887c6e520948816Chong Zhang mStartTimestampUs(-1ll), 40607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mLatitudex10000(0), 40707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mLongitudex10000(0), 40886b7f47aa7482424cf8fd248f1315311919be3b0James Dong mAreGeoTagsAvailable(false), 409ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar mStartTimeOffsetMs(-1), 410ee4e1b1a63758941460ae79a064249d3a5189443Lajos Molnar mMetaKeys(new AMessage()) { 4117c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang addDeviceMeta(); 4122aa74dc097fe860abc84769abf2b6e0962718471Robert Shih 4132aa74dc097fe860abc84769abf2b6e0962718471Robert Shih // Verify mFd is seekable 4142aa74dc097fe860abc84769abf2b6e0962718471Robert Shih off64_t off = lseek64(mFd, 0, SEEK_SET); 4152aa74dc097fe860abc84769abf2b6e0962718471Robert Shih if (off < 0) { 4162aa74dc097fe860abc84769abf2b6e0962718471Robert Shih ALOGE("cannot seek mFd: %s (%d)", strerror(errno), errno); 4172aa74dc097fe860abc84769abf2b6e0962718471Robert Shih release(); 4182aa74dc097fe860abc84769abf2b6e0962718471Robert Shih } 41930ab66297501757d745b9ae10da61adcd891f497Andreas Huber} 42030ab66297501757d745b9ae10da61adcd891f497Andreas Huber 42120111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMPEG4Writer::~MPEG4Writer() { 4228bcc65c753085fe3328592cceda0cf0e8f8b0a45James Dong reset(); 42320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 4241f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong while (!mTracks.empty()) { 4251f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong List<Track *>::iterator it = mTracks.begin(); 42620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber delete *it; 4271f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong (*it) = NULL; 4281f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong mTracks.erase(it); 42920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 43020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mTracks.clear(); 43120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 43220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 433dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dongstatus_t MPEG4Writer::dump( 434dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong int fd, const Vector<String16>& args) { 435dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong const size_t SIZE = 256; 436dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong char buffer[SIZE]; 437dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong String8 result; 438dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong snprintf(buffer, SIZE, " MPEG4Writer %p\n", this); 439dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong result.append(buffer); 440dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong snprintf(buffer, SIZE, " mStarted: %s\n", mStarted? "true": "false"); 441dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong result.append(buffer); 442dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong ::write(fd, result.string(), result.size()); 443dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong for (List<Track *>::iterator it = mTracks.begin(); 444dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong it != mTracks.end(); ++it) { 445dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong (*it)->dump(fd, args); 446dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong } 447dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong return OK; 448dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong} 449dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong 450dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dongstatus_t MPEG4Writer::Track::dump( 45184333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber int fd, const Vector<String16>& /* args */) const { 452dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong const size_t SIZE = 256; 453dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong char buffer[SIZE]; 454dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong String8 result; 455dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong snprintf(buffer, SIZE, " %s track\n", mIsAudio? "Audio": "Video"); 456dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong result.append(buffer); 457dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong snprintf(buffer, SIZE, " reached EOS: %s\n", 458dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong mReachedEOS? "true": "false"); 459dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong result.append(buffer); 460c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong snprintf(buffer, SIZE, " frames encoded : %d\n", mStszTableEntries->count()); 46113210f3346462a86ce9fe3af72a0c200dba84e27James Dong result.append(buffer); 462377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT snprintf(buffer, SIZE, " duration encoded : %" PRId64 " us\n", mTrackDurationUs); 46313210f3346462a86ce9fe3af72a0c200dba84e27James Dong result.append(buffer); 464dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong ::write(fd, result.string(), result.size()); 465dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong return OK; 466dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong} 467dedf414d3fe2e79ee0aad0f1c82ca16ebd886ff6James Dong 4688b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan// static 4698b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavanconst char *MPEG4Writer::Track::getFourCCForMime(const char *mime) { 4708b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (mime == NULL) { 4718b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return NULL; 4728b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } 4738b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (!strncasecmp(mime, "audio/", 6)) { 4748b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime)) { 4758b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "samr"; 4768b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) { 4778b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "sawb"; 4788b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) { 4798b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "mp4a"; 4808b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } 4818b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else if (!strncasecmp(mime, "video/", 6)) { 4828b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 4838b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "mp4v"; 4848b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 4858b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "s263"; 4868b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 4878b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return "avc1"; 4889aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mime)) { 4899aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return "hvc1"; 4908b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } 4918b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } else { 4928b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan ALOGE("Track (%s) other than video or audio is not supported", mime); 4938b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan } 4948b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan return NULL; 4958b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan} 4968b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan 497b2487f03f12dcafdb801fc0007c8df8412397f44Marco Nelissenstatus_t MPEG4Writer::addSource(const sp<IMediaSource> &source) { 498bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong Mutex::Autolock l(mLock); 499bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong if (mStarted) { 50029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Attempt to add source AFTER recording is started"); 501bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong return UNKNOWN_ERROR; 502bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong } 503acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 504acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // At most 2 tracks can be supported. 505acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong if (mTracks.size() >= 2) { 506a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGE("Too many tracks (%zu) to add", mTracks.size()); 507acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong return ERROR_UNSUPPORTED; 508acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong } 509acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 510acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong CHECK(source.get() != NULL); 511acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 512acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong const char *mime; 513acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong source->getFormat()->findCString(kKeyMIMEType, &mime); 514acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong bool isAudio = !strncasecmp(mime, "audio/", 6); 5158b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (Track::getFourCCForMime(mime) == NULL) { 5168b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan ALOGE("Unsupported mime '%s'", mime); 517acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong return ERROR_UNSUPPORTED; 518acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong } 519acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 520acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // At this point, we know the track to be added is either 521acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // video or audio. Thus, we only need to check whether it 522acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // is an audio track or not (if it is not, then it must be 523acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // a video track). 524acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 525acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // No more than one video or one audio track is supported. 526acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong for (List<Track*>::iterator it = mTracks.begin(); 527acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong it != mTracks.end(); ++it) { 528acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong if ((*it)->isAudio() == isAudio) { 529acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong ALOGE("%s track already exists", isAudio? "Audio": "Video"); 530acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong return ERROR_UNSUPPORTED; 531acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong } 532acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong } 533acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 534acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // This is the first track of either audio or video. 535acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong // Go ahead to add the track. 536219f195159f93d627af2b243732e3f9020511a46James Dong Track *track = new Track(this, source, 1 + mTracks.size()); 53720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mTracks.push_back(track); 5382dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber 5392dce41ad26cb3e9e15c9e456a84bcf5309548ca0Andreas Huber return OK; 54020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 54120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 54293d6b102a13afa23bfa80d74c399d93d542e6ad6James Dongstatus_t MPEG4Writer::startTracks(MetaData *params) { 543acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong if (mTracks.empty()) { 544acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong ALOGE("No source added"); 545acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong return INVALID_OPERATION; 546acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong } 547acc47642e0f5d962f6289e6ba687fabf68f8312bJames Dong 548a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong for (List<Track *>::iterator it = mTracks.begin(); 549a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong it != mTracks.end(); ++it) { 55093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong status_t err = (*it)->start(params); 551a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 552a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (err != OK) { 553a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong for (List<Track *>::iterator it2 = mTracks.begin(); 554a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong it2 != it; ++it2) { 555a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong (*it2)->stop(); 556a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 557a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 558a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong return err; 559a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 560a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 561a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong return OK; 562a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong} 563a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 5647c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhangvoid MPEG4Writer::addDeviceMeta() { 5657c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // add device info and estimate space in 'moov' 5667c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang char val[PROPERTY_VALUE_MAX]; 5677c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang size_t n; 5687c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // meta size is estimated by adding up the following: 5697c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // - meta header structures, which occur only once (total 66 bytes) 5707c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // - size for each key, which consists of a fixed header (32 bytes), 5717c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // plus key length and data length. 5727c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += 66; 5737c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang if (property_get("ro.build.version.release", val, NULL) 5747c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang && (n = strlen(val)) > 0) { 5757c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMetaKeys->setString(kMetaKey_Version, val, n + 1); 5767c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += sizeof(kMetaKey_Version) + n + 32; 5777c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang } 5787c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang#ifdef SHOW_MODEL_BUILD 5797c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang if (property_get("ro.product.model", val, NULL) 5807c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang && (n = strlen(val)) > 0) { 5817c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMetaKeys->setString(kMetaKey_Model, val, n + 1); 5827c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += sizeof(kMetaKey_Model) + n + 32; 5837c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang } 5847c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang if (property_get("ro.build.display.id", val, NULL) 5857c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang && (n = strlen(val)) > 0) { 5867c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMetaKeys->setString(kMetaKey_Build, val, n + 1); 5877c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += sizeof(kMetaKey_Build) + n + 32; 5887c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang } 5897c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang#endif 5907c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang} 5917c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 5922dec2b5be2056c6d9428897dc672185872d30d17James Dongint64_t MPEG4Writer::estimateMoovBoxSize(int32_t bitRate) { 5932dec2b5be2056c6d9428897dc672185872d30d17James Dong // This implementation is highly experimental/heurisitic. 5942dec2b5be2056c6d9428897dc672185872d30d17James Dong // 5952dec2b5be2056c6d9428897dc672185872d30d17James Dong // Statistical analysis shows that metadata usually accounts 5962dec2b5be2056c6d9428897dc672185872d30d17James Dong // for a small portion of the total file size, usually < 0.6%. 5972dec2b5be2056c6d9428897dc672185872d30d17James Dong 59878a1a286f736888ae7af8860b2c424af0d978848James Dong // The default MIN_MOOV_BOX_SIZE is set to 0.6% x 1MB / 2, 5992dec2b5be2056c6d9428897dc672185872d30d17James Dong // where 1MB is the common file size limit for MMS application. 60078a1a286f736888ae7af8860b2c424af0d978848James Dong // The default MAX _MOOV_BOX_SIZE value is based on about 3 6012dec2b5be2056c6d9428897dc672185872d30d17James Dong // minute video recording with a bit rate about 3 Mbps, because 6022dec2b5be2056c6d9428897dc672185872d30d17James Dong // statistics also show that most of the video captured are going 6032dec2b5be2056c6d9428897dc672185872d30d17James Dong // to be less than 3 minutes. 6042dec2b5be2056c6d9428897dc672185872d30d17James Dong 6052dec2b5be2056c6d9428897dc672185872d30d17James Dong // If the estimation is wrong, we will pay the price of wasting 6062dec2b5be2056c6d9428897dc672185872d30d17James Dong // some reserved space. This should not happen so often statistically. 6072dec2b5be2056c6d9428897dc672185872d30d17James Dong static const int32_t factor = mUse32BitOffset? 1: 2; 60878a1a286f736888ae7af8860b2c424af0d978848James Dong static const int64_t MIN_MOOV_BOX_SIZE = 3 * 1024; // 3 KB 6092dec2b5be2056c6d9428897dc672185872d30d17James Dong static const int64_t MAX_MOOV_BOX_SIZE = (180 * 3000000 * 6LL / 8000); 6102dec2b5be2056c6d9428897dc672185872d30d17James Dong int64_t size = MIN_MOOV_BOX_SIZE; 6112dec2b5be2056c6d9428897dc672185872d30d17James Dong 61278a1a286f736888ae7af8860b2c424af0d978848James Dong // Max file size limit is set 613a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong if (mMaxFileSizeLimitBytes != 0 && mIsFileSizeLimitExplicitlyRequested) { 61478a1a286f736888ae7af8860b2c424af0d978848James Dong size = mMaxFileSizeLimitBytes * 6 / 1000; 61578a1a286f736888ae7af8860b2c424af0d978848James Dong } 61678a1a286f736888ae7af8860b2c424af0d978848James Dong 61778a1a286f736888ae7af8860b2c424af0d978848James Dong // Max file duration limit is set 61878a1a286f736888ae7af8860b2c424af0d978848James Dong if (mMaxFileDurationLimitUs != 0) { 61978a1a286f736888ae7af8860b2c424af0d978848James Dong if (bitRate > 0) { 62078a1a286f736888ae7af8860b2c424af0d978848James Dong int64_t size2 = 62178a1a286f736888ae7af8860b2c424af0d978848James Dong ((mMaxFileDurationLimitUs * bitRate * 6) / 1000 / 8000000); 62278a1a286f736888ae7af8860b2c424af0d978848James Dong if (mMaxFileSizeLimitBytes != 0 && mIsFileSizeLimitExplicitlyRequested) { 62378a1a286f736888ae7af8860b2c424af0d978848James Dong // When both file size and duration limits are set, 62478a1a286f736888ae7af8860b2c424af0d978848James Dong // we use the smaller limit of the two. 62578a1a286f736888ae7af8860b2c424af0d978848James Dong if (size > size2) { 62678a1a286f736888ae7af8860b2c424af0d978848James Dong size = size2; 62778a1a286f736888ae7af8860b2c424af0d978848James Dong } 62878a1a286f736888ae7af8860b2c424af0d978848James Dong } else { 62978a1a286f736888ae7af8860b2c424af0d978848James Dong // Only max file duration limit is set 63078a1a286f736888ae7af8860b2c424af0d978848James Dong size = size2; 63178a1a286f736888ae7af8860b2c424af0d978848James Dong } 6322dec2b5be2056c6d9428897dc672185872d30d17James Dong } 6332dec2b5be2056c6d9428897dc672185872d30d17James Dong } 63478a1a286f736888ae7af8860b2c424af0d978848James Dong 6352dec2b5be2056c6d9428897dc672185872d30d17James Dong if (size < MIN_MOOV_BOX_SIZE) { 6362dec2b5be2056c6d9428897dc672185872d30d17James Dong size = MIN_MOOV_BOX_SIZE; 6372dec2b5be2056c6d9428897dc672185872d30d17James Dong } 6382dec2b5be2056c6d9428897dc672185872d30d17James Dong 6392dec2b5be2056c6d9428897dc672185872d30d17James Dong // Any long duration recording will be probably end up with 6402dec2b5be2056c6d9428897dc672185872d30d17James Dong // non-streamable mp4 file. 6412dec2b5be2056c6d9428897dc672185872d30d17James Dong if (size > MAX_MOOV_BOX_SIZE) { 6422dec2b5be2056c6d9428897dc672185872d30d17James Dong size = MAX_MOOV_BOX_SIZE; 6432dec2b5be2056c6d9428897dc672185872d30d17James Dong } 6442dec2b5be2056c6d9428897dc672185872d30d17James Dong 6457c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang // Account for the extra stuff (Geo, meta keys, etc.) 6467c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang size += mMoovExtraSize; 6477c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 648a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGI("limits: %" PRId64 "/%" PRId64 " bytes/us, bit rate: %d bps and the" 649a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn " estimated moov size %" PRId64 " bytes", 6502dec2b5be2056c6d9428897dc672185872d30d17James Dong mMaxFileSizeLimitBytes, mMaxFileDurationLimitUs, bitRate, size); 6512dec2b5be2056c6d9428897dc672185872d30d17James Dong return factor * size; 6522dec2b5be2056c6d9428897dc672185872d30d17James Dong} 6532dec2b5be2056c6d9428897dc672185872d30d17James Dong 6542dec2b5be2056c6d9428897dc672185872d30d17James Dongstatus_t MPEG4Writer::start(MetaData *param) { 655674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong if (mInitCheck != OK) { 65625b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return UNKNOWN_ERROR; 65720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 65820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 659a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong /* 660a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong * Check mMaxFileSizeLimitBytes at the beginning 661a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong * since mMaxFileSizeLimitBytes may be implicitly 662a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong * changed later for 32-bit file offset even if 663a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong * user does not ask to set it explicitly. 664a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong */ 665a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong if (mMaxFileSizeLimitBytes != 0) { 666a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong mIsFileSizeLimitExplicitlyRequested = true; 667a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong } 668a007e8229fb2be4866c483f9cd6c4af238a2da5eJames Dong 6692dec2b5be2056c6d9428897dc672185872d30d17James Dong int32_t use64BitOffset; 6702dec2b5be2056c6d9428897dc672185872d30d17James Dong if (param && 6712dec2b5be2056c6d9428897dc672185872d30d17James Dong param->findInt32(kKey64BitFileOffset, &use64BitOffset) && 6722dec2b5be2056c6d9428897dc672185872d30d17James Dong use64BitOffset) { 6732dec2b5be2056c6d9428897dc672185872d30d17James Dong mUse32BitOffset = false; 6742dec2b5be2056c6d9428897dc672185872d30d17James Dong } 6752dec2b5be2056c6d9428897dc672185872d30d17James Dong 6761f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong if (mUse32BitOffset) { 6771f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong // Implicit 32 bit file size limit 6781f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong if (mMaxFileSizeLimitBytes == 0) { 6791f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong mMaxFileSizeLimitBytes = kMax32BitFileSize; 6801f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong } 6811f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 6821f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong // If file size is set to be larger than the 32 bit file 6831f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong // size limit, treat it as an error. 6841f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong if (mMaxFileSizeLimitBytes > kMax32BitFileSize) { 685a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGW("32-bit file size limit (%" PRId64 " bytes) too big. " 686a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn "It is changed to %" PRId64 " bytes", 687d2518e0c0a5bffd30b0dccb04fe7bf5b77c354b1James Dong mMaxFileSizeLimitBytes, kMax32BitFileSize); 688d2518e0c0a5bffd30b0dccb04fe7bf5b77c354b1James Dong mMaxFileSizeLimitBytes = kMax32BitFileSize; 6891f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong } 6901f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong } 6911f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 692b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong int32_t use2ByteNalLength; 693b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong if (param && 694b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong param->findInt32(kKey2ByteNalLength, &use2ByteNalLength) && 695b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong use2ByteNalLength) { 696b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong mUse4ByteNalLength = false; 6972dec2b5be2056c6d9428897dc672185872d30d17James Dong } 6982dec2b5be2056c6d9428897dc672185872d30d17James Dong 699de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui int32_t isRealTimeRecording; 700de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (param && param->findInt32(kKeyRealTimeRecording, &isRealTimeRecording)) { 701de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui mIsRealTimeRecording = isRealTimeRecording; 702de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui } 703de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui 704065d1aff96818df54456053f1574aec8a234d0deJames Dong mStartTimestampUs = -1; 70593d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong 706a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (mStarted) { 707a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (mPaused) { 708a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused = false; 70993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong return startTracks(param); 710a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 711a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong return OK; 712a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 713a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 7148f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong if (!param || 7158f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong !param->findInt32(kKeyTimeScale, &mTimeScale)) { 7168f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong mTimeScale = 1000; 7178f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong } 71843089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GT(mTimeScale, 0); 7193856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("movie time scale: %d", mTimeScale); 7208f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong 72177e8ae9967a078770416619e99ddb5b010def312James Dong /* 72277e8ae9967a078770416619e99ddb5b010def312James Dong * When the requested file size limit is small, the priority 72377e8ae9967a078770416619e99ddb5b010def312James Dong * is to meet the file size limit requirement, rather than 7247b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * to make the file streamable. mStreamableFile does not tell 7257b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * whether the actual recorded file is streamable or not. 72677e8ae9967a078770416619e99ddb5b010def312James Dong */ 72777e8ae9967a078770416619e99ddb5b010def312James Dong mStreamableFile = 72877e8ae9967a078770416619e99ddb5b010def312James Dong (mMaxFileSizeLimitBytes != 0 && 72977e8ae9967a078770416619e99ddb5b010def312James Dong mMaxFileSizeLimitBytes >= kMinStreamableFileSizeInBytes); 73077e8ae9967a078770416619e99ddb5b010def312James Dong 7317b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong /* 7327b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * mWriteMoovBoxToMemory is true if the amount of data in moov box is 7337b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * smaller than the reserved free space at the beginning of a file, AND 7347b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * when the content of moov box is constructed. Note that video/audio 7357b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * frame data is always written to the file but not in the memory. 7367b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 7377b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * Before stop()/reset() is called, mWriteMoovBoxToMemory is always 7387b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * false. When reset() is called at the end of a recording session, 7397b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * Moov box needs to be constructed. 7407b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 7417b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 1) Right before a moov box is constructed, mWriteMoovBoxToMemory 7427b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * to set to mStreamableFile so that if 7437b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * the file is intended to be streamable, it is set to true; 7447b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * otherwise, it is set to false. When the value is set to false, 7457b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * all the content of the moov box is written immediately to 7467b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * the end of the file. When the value is set to true, all the 7477b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * content of the moov box is written to an in-memory cache, 7487b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * mMoovBoxBuffer, util the following condition happens. Note 7497b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * that the size of the in-memory cache is the same as the 7507b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * reserved free space at the beginning of the file. 7517b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 7527b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 2) While the data of the moov box is written to an in-memory 7537b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * cache, the data size is checked against the reserved space. 7547b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * If the data size surpasses the reserved space, subsequent moov 7557b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * data could no longer be hold in the in-memory cache. This also 7567b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * indicates that the reserved space was too small. At this point, 7577b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * _all_ moov data must be written to the end of the file. 7587b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * mWriteMoovBoxToMemory must be set to false to direct the write 7597b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * to the file. 7607b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 7617b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * 3) If the data size in moov box is smaller than the reserved 7627b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * space after moov box is completely constructed, the in-memory 7637b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * cache copy of the moov box is written to the reserved free 7647b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * space. Thus, immediately after the moov is completedly 7657b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong * constructed, mWriteMoovBoxToMemory is always set to false. 7667b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong */ 7677b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong mWriteMoovBoxToMemory = false; 7687837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBuffer = NULL; 7697837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBufferOffset = 0; 7707837c17063a4c50bc856ba59418516fdab731de7James Dong 771b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeFtypBox(param); 77220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7737837c17063a4c50bc856ba59418516fdab731de7James Dong mFreeBoxOffset = mOffset; 77420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 7757837c17063a4c50bc856ba59418516fdab731de7James Dong if (mEstimatedMoovBoxSize == 0) { 7762dec2b5be2056c6d9428897dc672185872d30d17James Dong int32_t bitRate = -1; 7772dec2b5be2056c6d9428897dc672185872d30d17James Dong if (param) { 7782dec2b5be2056c6d9428897dc672185872d30d17James Dong param->findInt32(kKeyBitRate, &bitRate); 7792dec2b5be2056c6d9428897dc672185872d30d17James Dong } 7802dec2b5be2056c6d9428897dc672185872d30d17James Dong mEstimatedMoovBoxSize = estimateMoovBoxSize(bitRate); 7817837c17063a4c50bc856ba59418516fdab731de7James Dong } 78243089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GE(mEstimatedMoovBoxSize, 8); 78377e8ae9967a078770416619e99ddb5b010def312James Dong if (mStreamableFile) { 78477e8ae9967a078770416619e99ddb5b010def312James Dong // Reserve a 'free' box only for streamable file 78577e8ae9967a078770416619e99ddb5b010def312James Dong lseek64(mFd, mFreeBoxOffset, SEEK_SET); 78677e8ae9967a078770416619e99ddb5b010def312James Dong writeInt32(mEstimatedMoovBoxSize); 78777e8ae9967a078770416619e99ddb5b010def312James Dong write("free", 4); 78877e8ae9967a078770416619e99ddb5b010def312James Dong mMdatOffset = mFreeBoxOffset + mEstimatedMoovBoxSize; 78977e8ae9967a078770416619e99ddb5b010def312James Dong } else { 79077e8ae9967a078770416619e99ddb5b010def312James Dong mMdatOffset = mOffset; 79177e8ae9967a078770416619e99ddb5b010def312James Dong } 7927837c17063a4c50bc856ba59418516fdab731de7James Dong 7937837c17063a4c50bc856ba59418516fdab731de7James Dong mOffset = mMdatOffset; 794c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mMdatOffset, SEEK_SET); 7951acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong if (mUse32BitOffset) { 7961acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong write("????mdat", 8); 7971acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong } else { 7981acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong write("\x00\x00\x00\x01mdat????????", 16); 7991acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong } 8001c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8011c9747a4653aec1395c2bd6896c9b87cb5447837James Dong status_t err = startWriterThread(); 8021c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (err != OK) { 8031c9747a4653aec1395c2bd6896c9b87cb5447837James Dong return err; 8041c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 8051c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8061c9747a4653aec1395c2bd6896c9b87cb5447837James Dong err = startTracks(param); 807a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (err != OK) { 808a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong return err; 80920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 8101c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 811a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mStarted = true; 81225b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return OK; 81320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 81420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 8151f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dongbool MPEG4Writer::use32BitFileOffset() const { 8161f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong return mUse32BitOffset; 8171f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 8181f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 81937187916a486504acaf83bea30147eb5fbf46ae5James Dongstatus_t MPEG4Writer::pause() { 820674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong if (mInitCheck != OK) { 82137187916a486504acaf83bea30147eb5fbf46ae5James Dong return OK; 822a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 823a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused = true; 82437187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t err = OK; 825a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong for (List<Track *>::iterator it = mTracks.begin(); 826a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong it != mTracks.end(); ++it) { 82737187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t status = (*it)->pause(); 82837187916a486504acaf83bea30147eb5fbf46ae5James Dong if (status != OK) { 82937187916a486504acaf83bea30147eb5fbf46ae5James Dong err = status; 83037187916a486504acaf83bea30147eb5fbf46ae5James Dong } 831a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 83237187916a486504acaf83bea30147eb5fbf46ae5James Dong return err; 833a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong} 834a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 8351c9747a4653aec1395c2bd6896c9b87cb5447837James Dongvoid MPEG4Writer::stopWriterThread() { 836b8a805261bf0282e992d3608035e47d05a898710Steve Block ALOGD("Stopping writer thread"); 837411ba422e3635d534928ffd81abf54f4f291c739James Dong if (!mWriterThreadStarted) { 838411ba422e3635d534928ffd81abf54f4f291c739James Dong return; 839411ba422e3635d534928ffd81abf54f4f291c739James Dong } 8401c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8411c9747a4653aec1395c2bd6896c9b87cb5447837James Dong { 8421c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Mutex::Autolock autolock(mLock); 8431c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8441c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mDone = true; 8451c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mChunkReadyCondition.signal(); 8461c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 8471c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 8481c9747a4653aec1395c2bd6896c9b87cb5447837James Dong void *dummy; 8491c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_join(mThread, &dummy); 850411ba422e3635d534928ffd81abf54f4f291c739James Dong mWriterThreadStarted = false; 851b8a805261bf0282e992d3608035e47d05a898710Steve Block ALOGD("Writer thread stopped"); 8521c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 8531c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 85413f6284305e4b27395a23db7882d670bdb1bcae1James Dong/* 85513f6284305e4b27395a23db7882d670bdb1bcae1James Dong * MP4 file standard defines a composition matrix: 85613f6284305e4b27395a23db7882d670bdb1bcae1James Dong * | a b u | 85713f6284305e4b27395a23db7882d670bdb1bcae1James Dong * | c d v | 85813f6284305e4b27395a23db7882d670bdb1bcae1James Dong * | x y w | 85913f6284305e4b27395a23db7882d670bdb1bcae1James Dong * 86013f6284305e4b27395a23db7882d670bdb1bcae1James Dong * the element in the matrix is stored in the following 86113f6284305e4b27395a23db7882d670bdb1bcae1James Dong * order: {a, b, u, c, d, v, x, y, w}, 86213f6284305e4b27395a23db7882d670bdb1bcae1James Dong * where a, b, c, d, x, and y is in 16.16 format, while 86313f6284305e4b27395a23db7882d670bdb1bcae1James Dong * u, v and w is in 2.30 format. 86413f6284305e4b27395a23db7882d670bdb1bcae1James Dong */ 86513f6284305e4b27395a23db7882d670bdb1bcae1James Dongvoid MPEG4Writer::writeCompositionMatrix(int degrees) { 8663856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("writeCompositionMatrix"); 86713f6284305e4b27395a23db7882d670bdb1bcae1James Dong uint32_t a = 0x00010000; 86813f6284305e4b27395a23db7882d670bdb1bcae1James Dong uint32_t b = 0; 86913f6284305e4b27395a23db7882d670bdb1bcae1James Dong uint32_t c = 0; 87013f6284305e4b27395a23db7882d670bdb1bcae1James Dong uint32_t d = 0x00010000; 87113f6284305e4b27395a23db7882d670bdb1bcae1James Dong switch (degrees) { 87213f6284305e4b27395a23db7882d670bdb1bcae1James Dong case 0: 87313f6284305e4b27395a23db7882d670bdb1bcae1James Dong break; 87413f6284305e4b27395a23db7882d670bdb1bcae1James Dong case 90: 87513f6284305e4b27395a23db7882d670bdb1bcae1James Dong a = 0; 87613f6284305e4b27395a23db7882d670bdb1bcae1James Dong b = 0x00010000; 87713f6284305e4b27395a23db7882d670bdb1bcae1James Dong c = 0xFFFF0000; 87813f6284305e4b27395a23db7882d670bdb1bcae1James Dong d = 0; 87913f6284305e4b27395a23db7882d670bdb1bcae1James Dong break; 88013f6284305e4b27395a23db7882d670bdb1bcae1James Dong case 180: 88113f6284305e4b27395a23db7882d670bdb1bcae1James Dong a = 0xFFFF0000; 88213f6284305e4b27395a23db7882d670bdb1bcae1James Dong d = 0xFFFF0000; 88313f6284305e4b27395a23db7882d670bdb1bcae1James Dong break; 88413f6284305e4b27395a23db7882d670bdb1bcae1James Dong case 270: 88513f6284305e4b27395a23db7882d670bdb1bcae1James Dong a = 0; 88613f6284305e4b27395a23db7882d670bdb1bcae1James Dong b = 0xFFFF0000; 88713f6284305e4b27395a23db7882d670bdb1bcae1James Dong c = 0x00010000; 88813f6284305e4b27395a23db7882d670bdb1bcae1James Dong d = 0; 88913f6284305e4b27395a23db7882d670bdb1bcae1James Dong break; 89013f6284305e4b27395a23db7882d670bdb1bcae1James Dong default: 89113f6284305e4b27395a23db7882d670bdb1bcae1James Dong CHECK(!"Should never reach this unknown rotation"); 89213f6284305e4b27395a23db7882d670bdb1bcae1James Dong break; 89313f6284305e4b27395a23db7882d670bdb1bcae1James Dong } 89413f6284305e4b27395a23db7882d670bdb1bcae1James Dong 89513f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(a); // a 89613f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(b); // b 89713f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(0); // u 89813f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(c); // c 89913f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(d); // d 90013f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(0); // v 90113f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(0); // x 90213f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(0); // y 90313f6284305e4b27395a23db7882d670bdb1bcae1James Dong writeInt32(0x40000000); // w 90413f6284305e4b27395a23db7882d670bdb1bcae1James Dong} 90513f6284305e4b27395a23db7882d670bdb1bcae1James Dong 906411ba422e3635d534928ffd81abf54f4f291c739James Dongvoid MPEG4Writer::release() { 907411ba422e3635d534928ffd81abf54f4f291c739James Dong close(mFd); 908411ba422e3635d534928ffd81abf54f4f291c739James Dong mFd = -1; 909411ba422e3635d534928ffd81abf54f4f291c739James Dong mInitCheck = NO_INIT; 910411ba422e3635d534928ffd81abf54f4f291c739James Dong mStarted = false; 9114c845fde3d60ad93984879f69b164e35536e080dSuresh Choudhary free(mMoovBoxBuffer); 9124c845fde3d60ad93984879f69b164e35536e080dSuresh Choudhary mMoovBoxBuffer = NULL; 913411ba422e3635d534928ffd81abf54f4f291c739James Dong} 91413f6284305e4b27395a23db7882d670bdb1bcae1James Dong 9158bcc65c753085fe3328592cceda0cf0e8f8b0a45James Dongstatus_t MPEG4Writer::reset() { 916674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong if (mInitCheck != OK) { 91737187916a486504acaf83bea30147eb5fbf46ae5James Dong return OK; 918411ba422e3635d534928ffd81abf54f4f291c739James Dong } else { 919411ba422e3635d534928ffd81abf54f4f291c739James Dong if (!mWriterThreadStarted || 920411ba422e3635d534928ffd81abf54f4f291c739James Dong !mStarted) { 921411ba422e3635d534928ffd81abf54f4f291c739James Dong if (mWriterThreadStarted) { 922411ba422e3635d534928ffd81abf54f4f291c739James Dong stopWriterThread(); 923411ba422e3635d534928ffd81abf54f4f291c739James Dong } 924411ba422e3635d534928ffd81abf54f4f291c739James Dong release(); 925411ba422e3635d534928ffd81abf54f4f291c739James Dong return OK; 926411ba422e3635d534928ffd81abf54f4f291c739James Dong } 92720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 92820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 92937187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t err = OK; 9308f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int64_t maxDurationUs = 0; 93165b3d76025c71d755b0fb3b6ead90255f25417edJames Dong int64_t minDurationUs = 0x7fffffffffffffffLL; 93220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber for (List<Track *>::iterator it = mTracks.begin(); 93320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber it != mTracks.end(); ++it) { 93437187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t status = (*it)->stop(); 93537187916a486504acaf83bea30147eb5fbf46ae5James Dong if (err == OK && status != OK) { 93637187916a486504acaf83bea30147eb5fbf46ae5James Dong err = status; 93737187916a486504acaf83bea30147eb5fbf46ae5James Dong } 93820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9398f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int64_t durationUs = (*it)->getDurationUs(); 9408f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong if (durationUs > maxDurationUs) { 9418f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong maxDurationUs = durationUs; 94220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 94365b3d76025c71d755b0fb3b6ead90255f25417edJames Dong if (durationUs < minDurationUs) { 94465b3d76025c71d755b0fb3b6ead90255f25417edJames Dong minDurationUs = durationUs; 94565b3d76025c71d755b0fb3b6ead90255f25417edJames Dong } 94665b3d76025c71d755b0fb3b6ead90255f25417edJames Dong } 94765b3d76025c71d755b0fb3b6ead90255f25417edJames Dong 94865b3d76025c71d755b0fb3b6ead90255f25417edJames Dong if (mTracks.size() > 1) { 949a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGD("Duration from tracks range is [%" PRId64 ", %" PRId64 "] us", 95065b3d76025c71d755b0fb3b6ead90255f25417edJames Dong minDurationUs, maxDurationUs); 95120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 95220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9531c9747a4653aec1395c2bd6896c9b87cb5447837James Dong stopWriterThread(); 9547837c17063a4c50bc856ba59418516fdab731de7James Dong 95537187916a486504acaf83bea30147eb5fbf46ae5James Dong // Do not write out movie header on error. 95637187916a486504acaf83bea30147eb5fbf46ae5James Dong if (err != OK) { 957411ba422e3635d534928ffd81abf54f4f291c739James Dong release(); 95837187916a486504acaf83bea30147eb5fbf46ae5James Dong return err; 95937187916a486504acaf83bea30147eb5fbf46ae5James Dong } 96037187916a486504acaf83bea30147eb5fbf46ae5James Dong 96120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber // Fix up the size of the 'mdat' chunk. 9621acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong if (mUse32BitOffset) { 963c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mMdatOffset, SEEK_SET); 9641f1f2b1678fd0d038dfc501252dd2b65ecf10caeRachad uint32_t size = htonl(static_cast<uint32_t>(mOffset - mMdatOffset)); 965c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &size, 4); 9661acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong } else { 967c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mMdatOffset + 8, SEEK_SET); 9681f1f2b1678fd0d038dfc501252dd2b65ecf10caeRachad uint64_t size = mOffset - mMdatOffset; 9691acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong size = hton64(size); 970c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &size, 8); 9711acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong } 972c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mOffset, SEEK_SET); 97320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9747b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // Construct moov box now 9757837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBufferOffset = 0; 9767b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong mWriteMoovBoxToMemory = mStreamableFile; 9777b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong if (mWriteMoovBoxToMemory) { 9787b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // There is no need to allocate in-memory cache 9797b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // for moov box if the file is not streamable. 9807b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong 9817b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong mMoovBoxBuffer = (uint8_t *) malloc(mEstimatedMoovBoxSize); 9827b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong CHECK(mMoovBoxBuffer != NULL); 9837b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong } 984b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeMoovBox(maxDurationUs); 98520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 9867b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // mWriteMoovBoxToMemory could be set to false in 9877b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // MPEG4Writer::write() method 9887b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong if (mWriteMoovBoxToMemory) { 9897b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong mWriteMoovBoxToMemory = false; 9907b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // Content of the moov box is saved in the cache, and the in-memory 9917b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // moov box needs to be written to the file in a single shot. 9927b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong 99343089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_LE(mMoovBoxBufferOffset + 8, mEstimatedMoovBoxSize); 9947837c17063a4c50bc856ba59418516fdab731de7James Dong 9957837c17063a4c50bc856ba59418516fdab731de7James Dong // Moov box 996c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mFreeBoxOffset, SEEK_SET); 9977837c17063a4c50bc856ba59418516fdab731de7James Dong mOffset = mFreeBoxOffset; 998674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(mMoovBoxBuffer, 1, mMoovBoxBufferOffset); 9997837c17063a4c50bc856ba59418516fdab731de7James Dong 10007837c17063a4c50bc856ba59418516fdab731de7James Dong // Free box 1001c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mOffset, SEEK_SET); 10027837c17063a4c50bc856ba59418516fdab731de7James Dong writeInt32(mEstimatedMoovBoxSize - mMoovBoxBufferOffset); 10037837c17063a4c50bc856ba59418516fdab731de7James Dong write("free", 4); 10047b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong } else { 10057b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong ALOGI("The mp4 file will not be streamable."); 10067b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong } 10077837c17063a4c50bc856ba59418516fdab731de7James Dong 10087b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // Free in-memory cache for moov box 10097b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong if (mMoovBoxBuffer != NULL) { 10107837c17063a4c50bc856ba59418516fdab731de7James Dong free(mMoovBoxBuffer); 10117837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBuffer = NULL; 10127837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBufferOffset = 0; 10137837c17063a4c50bc856ba59418516fdab731de7James Dong } 10147837c17063a4c50bc856ba59418516fdab731de7James Dong 10150c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber CHECK(mBoxes.empty()); 101620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1017411ba422e3635d534928ffd81abf54f4f291c739James Dong release(); 101837187916a486504acaf83bea30147eb5fbf46ae5James Dong return err; 101920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 102020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1021efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlssonuint32_t MPEG4Writer::getMpeg4Time() { 1022b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong time_t now = time(NULL); 1023efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson // MP4 file uses time counting seconds since midnight, Jan. 1, 1904 1024efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson // while time function returns Unix epoch values which starts 1025efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson // at 1970-01-01. Lets add the number of seconds between them 10262b3b672cf57657640ece98810cb34ae2bfa91e14Marco Nelissen static const uint32_t delta = (66 * 365 + 17) * (24 * 60 * 60); 10272b3b672cf57657640ece98810cb34ae2bfa91e14Marco Nelissen if (now < 0 || uint32_t(now) > UINT32_MAX - delta) { 10282b3b672cf57657640ece98810cb34ae2bfa91e14Marco Nelissen return 0; 10292b3b672cf57657640ece98810cb34ae2bfa91e14Marco Nelissen } 10302b3b672cf57657640ece98810cb34ae2bfa91e14Marco Nelissen uint32_t mpeg4Time = uint32_t(now) + delta; 1031efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson return mpeg4Time; 1032efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson} 1033efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson 1034efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlssonvoid MPEG4Writer::writeMvhdBox(int64_t durationUs) { 1035efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson uint32_t now = getMpeg4Time(); 1036b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong beginBox("mvhd"); 1037b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // version=0, flags=0 1038b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(now); // creation time 1039b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(now); // modification time 1040b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(mTimeScale); // mvhd timescale 1041b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t duration = (durationUs * mTimeScale + 5E5) / 1E6; 1042b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(duration); 1043b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0x10000); // rate: 1.0 1044b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt16(0x100); // volume 1045b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt16(0); // reserved 1046b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // reserved 1047b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // reserved 1048b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeCompositionMatrix(0); // matrix 1049b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1050b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1051b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1052b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1053b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1054b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(0); // predefined 1055b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeInt32(mTracks.size() + 1); // nextTrackID 1056b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong endBox(); // mvhd 1057b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 1058b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 1059b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::writeMoovBox(int64_t durationUs) { 1060b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong beginBox("moov"); 1061b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeMvhdBox(durationUs); 106207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (mAreGeoTagsAvailable) { 106307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeUdtaBox(); 106407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 1065e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeMetaBox(); 1066b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t id = 1; 1067b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong for (List<Track *>::iterator it = mTracks.begin(); 1068b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong it != mTracks.end(); ++it, ++id) { 1069b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong (*it)->writeTrackHeader(mUse32BitOffset); 1070b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 1071b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong endBox(); // moov 1072b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 1073b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 10742cf9c5073ca3342ee52673ad68763fadd2c2be79James Dongvoid MPEG4Writer::writeFtypBox(MetaData *param) { 1075b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong beginBox("ftyp"); 1076b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 1077b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t fileType; 1078b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (param && param->findInt32(kKeyFileType, &fileType) && 1079b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong fileType != OUTPUT_FORMAT_MPEG_4) { 1080b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeFourcc("3gp4"); 10818284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeInt32(0); 10828284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeFourcc("isom"); 10838284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeFourcc("3gp4"); 1084b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else { 10858284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeFourcc("mp42"); 10868284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeInt32(0); 1087b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeFourcc("isom"); 10888284de3be2ac07d8774b15e6565df5aba084db04Robert Shih writeFourcc("mp42"); 1089b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 1090b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 1091b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong endBox(); 1092b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 1093b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 109407ec01904613a0bac32caaa8444b4690998faed7James Dongstatic bool isTestModeEnabled() { 109507ec01904613a0bac32caaa8444b4690998faed7James Dong#if (PROPERTY_VALUE_MAX < 5) 109607ec01904613a0bac32caaa8444b4690998faed7James Dong#error "PROPERTY_VALUE_MAX must be at least 5" 109707ec01904613a0bac32caaa8444b4690998faed7James Dong#endif 109807ec01904613a0bac32caaa8444b4690998faed7James Dong 109907ec01904613a0bac32caaa8444b4690998faed7James Dong // Test mode is enabled only if rw.media.record.test system 110007ec01904613a0bac32caaa8444b4690998faed7James Dong // property is enabled. 110107ec01904613a0bac32caaa8444b4690998faed7James Dong char value[PROPERTY_VALUE_MAX]; 110207ec01904613a0bac32caaa8444b4690998faed7James Dong if (property_get("rw.media.record.test", value, NULL) && 110307ec01904613a0bac32caaa8444b4690998faed7James Dong (!strcasecmp(value, "true") || !strcasecmp(value, "1"))) { 110407ec01904613a0bac32caaa8444b4690998faed7James Dong return true; 110507ec01904613a0bac32caaa8444b4690998faed7James Dong } 110607ec01904613a0bac32caaa8444b4690998faed7James Dong return false; 110707ec01904613a0bac32caaa8444b4690998faed7James Dong} 110807ec01904613a0bac32caaa8444b4690998faed7James Dong 110970ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dongvoid MPEG4Writer::sendSessionSummary() { 111007ec01904613a0bac32caaa8444b4690998faed7James Dong // Send session summary only if test mode is enabled 111107ec01904613a0bac32caaa8444b4690998faed7James Dong if (!isTestModeEnabled()) { 111207ec01904613a0bac32caaa8444b4690998faed7James Dong return; 111307ec01904613a0bac32caaa8444b4690998faed7James Dong } 111407ec01904613a0bac32caaa8444b4690998faed7James Dong 111570ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong for (List<ChunkInfo>::iterator it = mChunkInfos.begin(); 111670ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong it != mChunkInfos.end(); ++it) { 111770ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int trackNum = it->mTrack->getTrackId() << 28; 111870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 111970ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong trackNum | MEDIA_RECORDER_TRACK_INTER_CHUNK_TIME_MS, 112070ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong it->mMaxInterChunkDurUs); 112170ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong } 112270ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong} 112370ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 112413aec890216948b0c364f8f92792129d0335f506James Dongstatus_t MPEG4Writer::setInterleaveDuration(uint32_t durationUs) { 112513aec890216948b0c364f8f92792129d0335f506James Dong mInterleaveDurationUs = durationUs; 112613aec890216948b0c364f8f92792129d0335f506James Dong return OK; 112713aec890216948b0c364f8f92792129d0335f506James Dong} 112813aec890216948b0c364f8f92792129d0335f506James Dong 112913aec890216948b0c364f8f92792129d0335f506James Dongvoid MPEG4Writer::lock() { 113013aec890216948b0c364f8f92792129d0335f506James Dong mLock.lock(); 113113aec890216948b0c364f8f92792129d0335f506James Dong} 113213aec890216948b0c364f8f92792129d0335f506James Dong 113313aec890216948b0c364f8f92792129d0335f506James Dongvoid MPEG4Writer::unlock() { 113413aec890216948b0c364f8f92792129d0335f506James Dong mLock.unlock(); 113513aec890216948b0c364f8f92792129d0335f506James Dong} 113620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1137c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongoff64_t MPEG4Writer::addSample_l(MediaBuffer *buffer) { 1138c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong off64_t old_offset = mOffset; 113920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1140c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, 1141c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong (const uint8_t *)buffer->data() + buffer->range_offset(), 1142c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong buffer->range_length()); 114320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 114420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mOffset += buffer->range_length(); 114520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 114620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber return old_offset; 114720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 114820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 114903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huberstatic void StripStartcode(MediaBuffer *buffer) { 115003b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber if (buffer->range_length() < 4) { 115103b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber return; 115203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 115303b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 115403b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber const uint8_t *ptr = 115503b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber (const uint8_t *)buffer->data() + buffer->range_offset(); 115603b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 115703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber if (!memcmp(ptr, "\x00\x00\x00\x01", 4)) { 115803b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber buffer->set_range( 115903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber buffer->range_offset() + 4, buffer->range_length() - 4); 116003b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 116103b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber} 116203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 1163c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongoff64_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) { 1164c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong off64_t old_offset = mOffset; 116530ab66297501757d745b9ae10da61adcd891f497Andreas Huber 116630ab66297501757d745b9ae10da61adcd891f497Andreas Huber size_t length = buffer->range_length(); 116703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 1168b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong if (mUse4ByteNalLength) { 1169b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong uint8_t x = length >> 24; 1170c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1171b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong x = (length >> 16) & 0xff; 1172c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1173b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong x = (length >> 8) & 0xff; 1174c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1175b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong x = length & 0xff; 1176c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1177c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong 1178c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, 1179c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong (const uint8_t *)buffer->data() + buffer->range_offset(), 1180c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong length); 1181b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong 1182b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong mOffset += length + 4; 1183b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } else { 118443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_LT(length, 65536); 118530ab66297501757d745b9ae10da61adcd891f497Andreas Huber 1186b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong uint8_t x = length >> 8; 1187c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1188b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong x = length & 0xff; 1189c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, &x, 1); 1190c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong ::write(mFd, (const uint8_t *)buffer->data() + buffer->range_offset(), length); 1191b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong mOffset += length + 2; 1192b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } 119330ab66297501757d745b9ae10da61adcd891f497Andreas Huber 119430ab66297501757d745b9ae10da61adcd891f497Andreas Huber return old_offset; 119530ab66297501757d745b9ae10da61adcd891f497Andreas Huber} 119630ab66297501757d745b9ae10da61adcd891f497Andreas Huber 11977837c17063a4c50bc856ba59418516fdab731de7James Dongsize_t MPEG4Writer::write( 1198674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong const void *ptr, size_t size, size_t nmemb) { 11997837c17063a4c50bc856ba59418516fdab731de7James Dong 12007837c17063a4c50bc856ba59418516fdab731de7James Dong const size_t bytes = size * nmemb; 12017837c17063a4c50bc856ba59418516fdab731de7James Dong if (mWriteMoovBoxToMemory) { 12027b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong 1203c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong off64_t moovBoxSize = 8 + mMoovBoxBufferOffset + bytes; 12041acfe8649f8169caf2ff098c2dc2de880d9a3760James Dong if (moovBoxSize > mEstimatedMoovBoxSize) { 12057b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // The reserved moov box at the beginning of the file 12067b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // is not big enough. Moov box should be written to 12077b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // the end of the file from now on, but not to the 12087b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // in-memory cache. 12097b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong 12107b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // We write partial moov box that is in the memory to 12117b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // the file first. 1212c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong for (List<off64_t>::iterator it = mBoxes.begin(); 12137837c17063a4c50bc856ba59418516fdab731de7James Dong it != mBoxes.end(); ++it) { 12147837c17063a4c50bc856ba59418516fdab731de7James Dong (*it) += mOffset; 12157837c17063a4c50bc856ba59418516fdab731de7James Dong } 1216674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong lseek64(mFd, mOffset, SEEK_SET); 1217674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong ::write(mFd, mMoovBoxBuffer, mMoovBoxBufferOffset); 12187b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong ::write(mFd, ptr, bytes); 12197837c17063a4c50bc856ba59418516fdab731de7James Dong mOffset += (bytes + mMoovBoxBufferOffset); 12207b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong 12217b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // All subsequent moov box content will be written 12227b670d4a0a4fa560f536f132e0a3fc7247f6724cJames Dong // to the end of the file. 12237837c17063a4c50bc856ba59418516fdab731de7James Dong mWriteMoovBoxToMemory = false; 12247837c17063a4c50bc856ba59418516fdab731de7James Dong } else { 12257837c17063a4c50bc856ba59418516fdab731de7James Dong memcpy(mMoovBoxBuffer + mMoovBoxBufferOffset, ptr, bytes); 12267837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBufferOffset += bytes; 12277837c17063a4c50bc856ba59418516fdab731de7James Dong } 12287837c17063a4c50bc856ba59418516fdab731de7James Dong } else { 1229674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong ::write(mFd, ptr, size * nmemb); 12307837c17063a4c50bc856ba59418516fdab731de7James Dong mOffset += bytes; 12317837c17063a4c50bc856ba59418516fdab731de7James Dong } 12327837c17063a4c50bc856ba59418516fdab731de7James Dong return bytes; 12337837c17063a4c50bc856ba59418516fdab731de7James Dong} 12347837c17063a4c50bc856ba59418516fdab731de7James Dong 1235e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangvoid MPEG4Writer::beginBox(uint32_t id) { 1236e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang mBoxes.push_back(mWriteMoovBoxToMemory? 1237e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang mMoovBoxBufferOffset: mOffset); 1238e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 1239e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); 1240e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(id); 1241e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 1242e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 124320111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::beginBox(const char *fourcc) { 12440c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber CHECK_EQ(strlen(fourcc), 4); 124520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 12467837c17063a4c50bc856ba59418516fdab731de7James Dong mBoxes.push_back(mWriteMoovBoxToMemory? 12477837c17063a4c50bc856ba59418516fdab731de7James Dong mMoovBoxBufferOffset: mOffset); 124820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 124920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber writeInt32(0); 125020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber writeFourcc(fourcc); 125120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 125220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 125320111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::endBox() { 12540c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber CHECK(!mBoxes.empty()); 125520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1256c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong off64_t offset = *--mBoxes.end(); 125720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mBoxes.erase(--mBoxes.end()); 125820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 12597837c17063a4c50bc856ba59418516fdab731de7James Dong if (mWriteMoovBoxToMemory) { 12607837c17063a4c50bc856ba59418516fdab731de7James Dong int32_t x = htonl(mMoovBoxBufferOffset - offset); 12617837c17063a4c50bc856ba59418516fdab731de7James Dong memcpy(mMoovBoxBuffer + offset, &x, 4); 12627837c17063a4c50bc856ba59418516fdab731de7James Dong } else { 1263c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, offset, SEEK_SET); 12647837c17063a4c50bc856ba59418516fdab731de7James Dong writeInt32(mOffset - offset); 12657837c17063a4c50bc856ba59418516fdab731de7James Dong mOffset -= 4; 1266c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong lseek64(mFd, mOffset, SEEK_SET); 12677837c17063a4c50bc856ba59418516fdab731de7James Dong } 126820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 126920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 127020111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeInt8(int8_t x) { 1271674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(&x, 1, 1); 127220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 127320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 127420111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeInt16(int16_t x) { 127520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber x = htons(x); 1276674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(&x, 1, 2); 127720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 127820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 127920111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeInt32(int32_t x) { 128020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber x = htonl(x); 1281674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(&x, 1, 4); 128220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 128320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 128420111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeInt64(int64_t x) { 128520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber x = hton64(x); 1286674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(&x, 1, 8); 128720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 128820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 128920111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeCString(const char *s) { 129020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber size_t n = strlen(s); 1291674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(s, 1, n + 1); 129220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 129320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 129420111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::writeFourcc(const char *s) { 12950c89199745bc1bf05b997fc7c342017807676b6fAndreas Huber CHECK_EQ(strlen(s), 4); 1296674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(s, 1, 4); 129720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 129820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 129907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 130007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong// Written in +/-DD.DDDD format 130107b1bb529a1ae76c46a71b01338c166f9490629dJames Dongvoid MPEG4Writer::writeLatitude(int degreex10000) { 130207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong bool isNegative = (degreex10000 < 0); 130307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong char sign = isNegative? '-': '+'; 130407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 130507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Handle the whole part 130607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong char str[9]; 130707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong int wholePart = degreex10000 / 10000; 130807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (wholePart == 0) { 130907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(str, 5, "%c%.2d.", sign, wholePart); 131007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } else { 131107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(str, 5, "%+.2d.", wholePart); 131207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 131307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 131407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Handle the fractional part 131507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong int fractionalPart = degreex10000 - (wholePart * 10000); 131607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (fractionalPart < 0) { 131707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong fractionalPart = -fractionalPart; 131807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 131907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(&str[4], 5, "%.4d", fractionalPart); 132007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 132107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Do not write the null terminator 132207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong write(str, 1, 8); 132307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong} 132407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 132507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong// Written in +/- DDD.DDDD format 132607b1bb529a1ae76c46a71b01338c166f9490629dJames Dongvoid MPEG4Writer::writeLongitude(int degreex10000) { 132707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong bool isNegative = (degreex10000 < 0); 132807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong char sign = isNegative? '-': '+'; 132907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 133007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Handle the whole part 133107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong char str[10]; 133207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong int wholePart = degreex10000 / 10000; 133307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (wholePart == 0) { 133407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(str, 6, "%c%.3d.", sign, wholePart); 133507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } else { 133607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(str, 6, "%+.3d.", wholePart); 133707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 133807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 133907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Handle the fractional part 134007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong int fractionalPart = degreex10000 - (wholePart * 10000); 134107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (fractionalPart < 0) { 134207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong fractionalPart = -fractionalPart; 134307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 134407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong snprintf(&str[5], 5, "%.4d", fractionalPart); 134507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 134607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Do not write the null terminator 134707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong write(str, 1, 9); 134807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong} 134907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 135007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong/* 135107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * Geodata is stored according to ISO-6709 standard. 135207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * latitudex10000 is latitude in degrees times 10000, and 135307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * longitudex10000 is longitude in degrees times 10000. 135407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * The range for the latitude is in [-90, +90], and 135507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * The range for the longitude is in [-180, +180] 135607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong */ 135707b1bb529a1ae76c46a71b01338c166f9490629dJames Dongstatus_t MPEG4Writer::setGeoData(int latitudex10000, int longitudex10000) { 135807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong // Is latitude or longitude out of range? 135907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong if (latitudex10000 < -900000 || latitudex10000 > 900000 || 136007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong longitudex10000 < -1800000 || longitudex10000 > 1800000) { 136107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong return BAD_VALUE; 136207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong } 136307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 136407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mLatitudex10000 = latitudex10000; 136507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mLongitudex10000 = longitudex10000; 136607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong mAreGeoTagsAvailable = true; 13677c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += 30; 136807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong return OK; 136907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong} 137007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 1371e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangstatus_t MPEG4Writer::setCaptureRate(float captureFps) { 1372e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang if (captureFps <= 0.0f) { 1373e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang return BAD_VALUE; 1374e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 1375e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 13767c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMetaKeys->setFloat(kMetaKey_CaptureFps, captureFps); 13777c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang mMoovExtraSize += sizeof(kMetaKey_CaptureFps) + 4 + 32; 13787c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 1379e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang return OK; 1380e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 1381e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 138220111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid MPEG4Writer::write(const void *data, size_t size) { 1383674ebd0b4e1143e38392a4e3bb38b4679a4577bcJames Dong write(data, 1, size); 138420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 138520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 138678a1a286f736888ae7af8860b2c424af0d978848James Dongbool MPEG4Writer::isFileStreamable() const { 138778a1a286f736888ae7af8860b2c424af0d978848James Dong return mStreamableFile; 138878a1a286f736888ae7af8860b2c424af0d978848James Dong} 138978a1a286f736888ae7af8860b2c424af0d978848James Dong 1390d599cd4573b5a2d5914c5040e0565ef866749b77James Dongbool MPEG4Writer::exceedsFileSizeLimit() { 1391d599cd4573b5a2d5914c5040e0565ef866749b77James Dong // No limit 1392d599cd4573b5a2d5914c5040e0565ef866749b77James Dong if (mMaxFileSizeLimitBytes == 0) { 1393d599cd4573b5a2d5914c5040e0565ef866749b77James Dong return false; 1394d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 1395d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 1396956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong int64_t nTotalBytesEstimate = static_cast<int64_t>(mEstimatedMoovBoxSize); 1397d599cd4573b5a2d5914c5040e0565ef866749b77James Dong for (List<Track *>::iterator it = mTracks.begin(); 1398d599cd4573b5a2d5914c5040e0565ef866749b77James Dong it != mTracks.end(); ++it) { 1399d599cd4573b5a2d5914c5040e0565ef866749b77James Dong nTotalBytesEstimate += (*it)->getEstimatedTrackSizeBytes(); 1400d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 14011f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 140277e8ae9967a078770416619e99ddb5b010def312James Dong if (!mStreamableFile) { 140377e8ae9967a078770416619e99ddb5b010def312James Dong // Add 1024 bytes as error tolerance 140477e8ae9967a078770416619e99ddb5b010def312James Dong return nTotalBytesEstimate + 1024 >= mMaxFileSizeLimitBytes; 140577e8ae9967a078770416619e99ddb5b010def312James Dong } 1406acd234bba9f048971d66890009eeff9a8db94be3James Dong // Be conservative in the estimate: do not exceed 95% of 1407acd234bba9f048971d66890009eeff9a8db94be3James Dong // the target file limit. For small target file size limit, though, 1408acd234bba9f048971d66890009eeff9a8db94be3James Dong // this will not help. 1409acd234bba9f048971d66890009eeff9a8db94be3James Dong return (nTotalBytesEstimate >= (95 * mMaxFileSizeLimitBytes) / 100); 1410d599cd4573b5a2d5914c5040e0565ef866749b77James Dong} 1411d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 1412d599cd4573b5a2d5914c5040e0565ef866749b77James Dongbool MPEG4Writer::exceedsFileDurationLimit() { 1413d599cd4573b5a2d5914c5040e0565ef866749b77James Dong // No limit 1414d599cd4573b5a2d5914c5040e0565ef866749b77James Dong if (mMaxFileDurationLimitUs == 0) { 1415d599cd4573b5a2d5914c5040e0565ef866749b77James Dong return false; 1416d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 1417d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 1418d599cd4573b5a2d5914c5040e0565ef866749b77James Dong for (List<Track *>::iterator it = mTracks.begin(); 1419d599cd4573b5a2d5914c5040e0565ef866749b77James Dong it != mTracks.end(); ++it) { 1420d599cd4573b5a2d5914c5040e0565ef866749b77James Dong if ((*it)->getDurationUs() >= mMaxFileDurationLimitUs) { 1421d599cd4573b5a2d5914c5040e0565ef866749b77James Dong return true; 1422d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 1423d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 1424d599cd4573b5a2d5914c5040e0565ef866749b77James Dong return false; 1425d599cd4573b5a2d5914c5040e0565ef866749b77James Dong} 1426d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 142725b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huberbool MPEG4Writer::reachedEOS() { 142825b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber bool allDone = true; 142925b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber for (List<Track *>::iterator it = mTracks.begin(); 143025b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber it != mTracks.end(); ++it) { 143125b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber if (!(*it)->reachedEOS()) { 143225b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber allDone = false; 143325b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber break; 143425b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber } 143525b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber } 143625b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 143725b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return allDone; 143825b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber} 143925b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 1440f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dongvoid MPEG4Writer::setStartTimestampUs(int64_t timeUs) { 1441a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGI("setStartTimestampUs: %" PRId64, timeUs); 144243089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GE(timeUs, 0ll); 14433c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong Mutex::Autolock autoLock(mLock); 1444065d1aff96818df54456053f1574aec8a234d0deJames Dong if (mStartTimestampUs < 0 || mStartTimestampUs > timeUs) { 1445f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong mStartTimestampUs = timeUs; 1446a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGI("Earliest track starting time: %" PRId64, mStartTimestampUs); 14473c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong } 14483c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong} 14493c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong 1450f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dongint64_t MPEG4Writer::getStartTimestampUs() { 14513c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong Mutex::Autolock autoLock(mLock); 14523c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong return mStartTimestampUs; 14533c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong} 14543c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong 145558ae9c530247668f8af36e30d228c716c226b3d4James Dongsize_t MPEG4Writer::numTracks() { 145658ae9c530247668f8af36e30d228c716c226b3d4James Dong Mutex::Autolock autolock(mLock); 145758ae9c530247668f8af36e30d228c716c226b3d4James Dong return mTracks.size(); 145858ae9c530247668f8af36e30d228c716c226b3d4James Dong} 145958ae9c530247668f8af36e30d228c716c226b3d4James Dong 146020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber//////////////////////////////////////////////////////////////////////////////// 146120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 146220111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMPEG4Writer::Track::Track( 1463b2487f03f12dcafdb801fc0007c8df8412397f44Marco Nelissen MPEG4Writer *owner, const sp<IMediaSource> &source, size_t trackId) 146420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber : mOwner(owner), 146525b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber mMeta(source->getFormat()), 146620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mSource(source), 146720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mDone(false), 1468a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused(false), 1469a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mResumed(false), 1470eaae38445a340c4857c1c5569475879a728e63b7James Dong mStarted(false), 1471bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong mTrackId(trackId), 1472c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong mTrackDurationUs(0), 1473956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong mEstimatedTrackSizeBytes(0), 1474be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong mSamplesHaveSameSize(true), 1475c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStszTableEntries(new ListTableEntries<uint32_t>(1000, 1)), 1476c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStcoTableEntries(new ListTableEntries<uint32_t>(1000, 1)), 1477c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCo64TableEntries(new ListTableEntries<off64_t>(1000, 1)), 1478c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries(new ListTableEntries<uint32_t>(1000, 3)), 1479c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStssTableEntries(new ListTableEntries<uint32_t>(1000, 1)), 1480c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)), 1481c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries(new ListTableEntries<uint32_t>(1000, 2)), 148220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mCodecSpecificData(NULL), 148325b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber mCodecSpecificDataSize(0), 1484548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber mGotAllCodecSpecificData(false), 148513f6284305e4b27395a23db7882d670bdb1bcae1James Dong mReachedEOS(false), 148613f6284305e4b27395a23db7882d670bdb1bcae1James Dong mRotation(0) { 148719c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber getCodecSpecificDataFromInputFormatIfPossible(); 14888f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong 14891c9747a4653aec1395c2bd6896c9b87cb5447837James Dong const char *mime; 14901c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mMeta->findCString(kKeyMIMEType, &mime); 14911c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsAvc = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC); 14929aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mIsHevc = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_HEVC); 14931c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsAudio = !strncasecmp(mime, "audio/", 6); 14941c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsMPEG4 = !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) || 14951c9747a4653aec1395c2bd6896c9b87cb5447837James Dong !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC); 14961c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1497c059860c73678a202bfa33062723e8f82fb779d9James Dong setTimeScale(); 1498c059860c73678a202bfa33062723e8f82fb779d9James Dong} 1499c059860c73678a202bfa33062723e8f82fb779d9James Dong 15001f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dongvoid MPEG4Writer::Track::updateTrackSizeEstimate() { 15011f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 1502c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t stcoBoxCount = (mOwner->use32BitFileOffset() 1503c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ? mStcoTableEntries->count() 1504c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong : mCo64TableEntries->count()); 1505c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong int64_t stcoBoxSizeBytes = stcoBoxCount * 4; 1506c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong int64_t stszBoxSizeBytes = mSamplesHaveSameSize? 4: (mStszTableEntries->count() * 4); 15071f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 150878a1a286f736888ae7af8860b2c424af0d978848James Dong mEstimatedTrackSizeBytes = mMdatSizeBytes; // media data size 150978a1a286f736888ae7af8860b2c424af0d978848James Dong if (!mOwner->isFileStreamable()) { 151078a1a286f736888ae7af8860b2c424af0d978848James Dong // Reserved free space is not large enough to hold 151178a1a286f736888ae7af8860b2c424af0d978848James Dong // all meta data and thus wasted. 1512c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mEstimatedTrackSizeBytes += mStscTableEntries->count() * 12 + // stsc box size 1513c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStssTableEntries->count() * 4 + // stss box size 1514c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries->count() * 8 + // stts box size 1515c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->count() * 8 + // ctts box size 151678a1a286f736888ae7af8860b2c424af0d978848James Dong stcoBoxSizeBytes + // stco box size 151778a1a286f736888ae7af8860b2c424af0d978848James Dong stszBoxSizeBytes; // stsz box size 151878a1a286f736888ae7af8860b2c424af0d978848James Dong } 15191f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 15201f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 15211f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dongvoid MPEG4Writer::Track::addOneStscTableEntry( 15221f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong size_t chunkId, size_t sampleId) { 15231f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 1524c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries->add(htonl(chunkId)); 1525c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries->add(htonl(sampleId)); 1526c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries->add(htonl(1)); 15271f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 15281f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 15291f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dongvoid MPEG4Writer::Track::addOneStssTableEntry(size_t sampleId) { 1530c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStssTableEntries->add(htonl(sampleId)); 15311f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 15321f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 15331f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dongvoid MPEG4Writer::Track::addOneSttsTableEntry( 153479761ab096f57c3027fad9556c2bc436672d614eJames Dong size_t sampleCount, int32_t duration) { 15351f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 15365a217fba010e801c255503602bda4b86ac5a6ac9James Dong if (duration == 0) { 1537377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGW("0-duration samples found: %zu", sampleCount); 15385a217fba010e801c255503602bda4b86ac5a6ac9James Dong } 1539c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries->add(htonl(sampleCount)); 1540c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries->add(htonl(duration)); 15411f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 15421f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 1543965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dongvoid MPEG4Writer::Track::addOneCttsTableEntry( 1544965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong size_t sampleCount, int32_t duration) { 1545965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 1546965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong if (mIsAudio) { 1547965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong return; 1548965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong } 1549c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->add(htonl(sampleCount)); 1550c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->add(htonl(duration)); 1551965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong} 1552965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 1553c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dongvoid MPEG4Writer::Track::addChunkOffset(off64_t offset) { 1554c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mOwner->use32BitFileOffset()) { 1555c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t value = offset; 1556c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStcoTableEntries->add(htonl(value)); 1557c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } else { 1558c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCo64TableEntries->add(hton64(offset)); 1559c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } 15601f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong} 15611f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 1562c059860c73678a202bfa33062723e8f82fb779d9James Dongvoid MPEG4Writer::Track::setTimeScale() { 15633856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("setTimeScale"); 1564c059860c73678a202bfa33062723e8f82fb779d9James Dong // Default time scale 1565c059860c73678a202bfa33062723e8f82fb779d9James Dong mTimeScale = 90000; 1566c059860c73678a202bfa33062723e8f82fb779d9James Dong 1567c059860c73678a202bfa33062723e8f82fb779d9James Dong if (mIsAudio) { 1568c059860c73678a202bfa33062723e8f82fb779d9James Dong // Use the sampling rate as the default time scale for audio track. 1569c059860c73678a202bfa33062723e8f82fb779d9James Dong int32_t sampleRate; 1570c059860c73678a202bfa33062723e8f82fb779d9James Dong bool success = mMeta->findInt32(kKeySampleRate, &sampleRate); 1571c059860c73678a202bfa33062723e8f82fb779d9James Dong CHECK(success); 1572c059860c73678a202bfa33062723e8f82fb779d9James Dong mTimeScale = sampleRate; 1573c059860c73678a202bfa33062723e8f82fb779d9James Dong } 1574c059860c73678a202bfa33062723e8f82fb779d9James Dong 1575c059860c73678a202bfa33062723e8f82fb779d9James Dong // If someone would like to overwrite the timescale, use user-supplied value. 1576c059860c73678a202bfa33062723e8f82fb779d9James Dong int32_t timeScale; 1577c059860c73678a202bfa33062723e8f82fb779d9James Dong if (mMeta->findInt32(kKeyTimeScale, &timeScale)) { 1578c059860c73678a202bfa33062723e8f82fb779d9James Dong mTimeScale = timeScale; 1579c059860c73678a202bfa33062723e8f82fb779d9James Dong } 1580c059860c73678a202bfa33062723e8f82fb779d9James Dong 158143089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GT(mTimeScale, 0); 158219c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber} 158319c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber 158419c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Hubervoid MPEG4Writer::Track::getCodecSpecificDataFromInputFormatIfPossible() { 158519c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber const char *mime; 158619c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber CHECK(mMeta->findCString(kKeyMIMEType, &mime)); 158719c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber 15889aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim uint32_t type; 15899aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const void *data = NULL; 15909aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size_t size = 0; 159119c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) { 15929aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mMeta->findData(kKeyAVCC, &type, &data, &size); 15939aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_HEVC)) { 15949aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mMeta->findData(kKeyHVCC, &type, &data, &size); 159519c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4) 159619c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) { 159719c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber if (mMeta->findData(kKeyESDS, &type, &data, &size)) { 159819c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber ESDS esds(data, size); 15999aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (esds.getCodecSpecificInfo(&data, &size) != OK) { 16009aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim data = NULL; 16019aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size = 0; 160219c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber } 160319c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber } 160419c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber } 16059aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (data != NULL && copyCodecSpecificData((uint8_t *)data, size) == OK) { 16069aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mGotAllCodecSpecificData = true; 16079aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 160820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 160920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 161020111aa043c5f404472bc63b90bc5aad906b1101Andreas HuberMPEG4Writer::Track::~Track() { 161120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber stop(); 161220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 1613c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mStszTableEntries; 1614c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mStcoTableEntries; 1615c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mCo64TableEntries; 1616c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mStscTableEntries; 1617c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mSttsTableEntries; 1618c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mStssTableEntries; 1619c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong delete mCttsTableEntries; 1620c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 1621c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStszTableEntries = NULL; 1622c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStcoTableEntries = NULL; 1623c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCo64TableEntries = NULL; 1624c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries = NULL; 1625c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries = NULL; 1626c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStssTableEntries = NULL; 1627c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries = NULL; 1628c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 162920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber if (mCodecSpecificData != NULL) { 163020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber free(mCodecSpecificData); 163120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mCodecSpecificData = NULL; 163220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 163320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 163420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 163593d6b102a13afa23bfa80d74c399d93d542e6ad6James Dongvoid MPEG4Writer::Track::initTrackingProgressStatus(MetaData *params) { 16363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("initTrackingProgressStatus"); 163793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mPreviousTrackTimeUs = -1; 163893d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mTrackingProgressStatus = false; 163993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mTrackEveryTimeDurationUs = 0; 164093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong { 164193d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong int64_t timeUs; 164293d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong if (params && params->findInt64(kKeyTrackTimeStatus, &timeUs)) { 1643a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("Receive request to track progress status for every %" PRId64 " us", timeUs); 164493d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mTrackEveryTimeDurationUs = timeUs; 164593d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mTrackingProgressStatus = true; 164693d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong } 164793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong } 164893d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong} 164993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong 16501c9747a4653aec1395c2bd6896c9b87cb5447837James Dong// static 16511c9747a4653aec1395c2bd6896c9b87cb5447837James Dongvoid *MPEG4Writer::ThreadWrapper(void *me) { 16523856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("ThreadWrapper: %p", me); 16531c9747a4653aec1395c2bd6896c9b87cb5447837James Dong MPEG4Writer *writer = static_cast<MPEG4Writer *>(me); 16541c9747a4653aec1395c2bd6896c9b87cb5447837James Dong writer->threadFunc(); 16551c9747a4653aec1395c2bd6896c9b87cb5447837James Dong return NULL; 16561c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 16571c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 16581c9747a4653aec1395c2bd6896c9b87cb5447837James Dongvoid MPEG4Writer::bufferChunk(const Chunk& chunk) { 16593856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("bufferChunk: %p", chunk.mTrack); 16601c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Mutex::Autolock autolock(mLock); 16611c9747a4653aec1395c2bd6896c9b87cb5447837James Dong CHECK_EQ(mDone, false); 16621c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 16631c9747a4653aec1395c2bd6896c9b87cb5447837James Dong for (List<ChunkInfo>::iterator it = mChunkInfos.begin(); 16641c9747a4653aec1395c2bd6896c9b87cb5447837James Dong it != mChunkInfos.end(); ++it) { 16651c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 16661c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (chunk.mTrack == it->mTrack) { // Found owner 16671c9747a4653aec1395c2bd6896c9b87cb5447837James Dong it->mChunks.push_back(chunk); 16681c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mChunkReadyCondition.signal(); 16691c9747a4653aec1395c2bd6896c9b87cb5447837James Dong return; 16701c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 16711c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 16721c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 167343089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK(!"Received a chunk for a unknown track"); 16741c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 16751c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1676fcac8fa9421f442f024018628a6042d7a14fbfb0James Dongvoid MPEG4Writer::writeChunkToFile(Chunk* chunk) { 1677a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("writeChunkToFile: %" PRId64 " from %s track", 16785410afcbb0af5d29d9f710a1c2978c500f9792dcPannag Sanketi chunk->mTimeStampUs, chunk->mTrack->isAudio()? "audio": "video"); 1679fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1680fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong int32_t isFirstSample = true; 1681fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong while (!chunk->mSamples.empty()) { 1682fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong List<MediaBuffer *>::iterator it = chunk->mSamples.begin(); 1683fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 16849aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim off64_t offset = (chunk->mTrack->isAvc() || chunk->mTrack->isHevc()) 1685fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong ? addLengthPrefixedSample_l(*it) 1686fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong : addSample_l(*it); 1687fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1688fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong if (isFirstSample) { 1689fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong chunk->mTrack->addChunkOffset(offset); 1690fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong isFirstSample = false; 16911c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 16921c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 16931c9747a4653aec1395c2bd6896c9b87cb5447837James Dong (*it)->release(); 16941c9747a4653aec1395c2bd6896c9b87cb5447837James Dong (*it) = NULL; 1695fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong chunk->mSamples.erase(it); 16961c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 1697fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong chunk->mSamples.clear(); 16981c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 16991c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1700fcac8fa9421f442f024018628a6042d7a14fbfb0James Dongvoid MPEG4Writer::writeAllChunks() { 17013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("writeAllChunks"); 17021c9747a4653aec1395c2bd6896c9b87cb5447837James Dong size_t outstandingChunks = 0; 170370ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong Chunk chunk; 170470ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong while (findChunkToWrite(&chunk)) { 1705e9f6d0579603372fd2547e6c5ba6e114c6f8cba7James Dong writeChunkToFile(&chunk); 170670ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong ++outstandingChunks; 17071c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 170870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 170970ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong sendSessionSummary(); 171070ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 17111c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mChunkInfos.clear(); 1712377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGD("%zu chunks are written in the last batch", outstandingChunks); 17131c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 17141c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1715fcac8fa9421f442f024018628a6042d7a14fbfb0James Dongbool MPEG4Writer::findChunkToWrite(Chunk *chunk) { 17163856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("findChunkToWrite"); 17171c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17181c9747a4653aec1395c2bd6896c9b87cb5447837James Dong int64_t minTimestampUs = 0x7FFFFFFFFFFFFFFFLL; 17191c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Track *track = NULL; 17201c9747a4653aec1395c2bd6896c9b87cb5447837James Dong for (List<ChunkInfo>::iterator it = mChunkInfos.begin(); 17211c9747a4653aec1395c2bd6896c9b87cb5447837James Dong it != mChunkInfos.end(); ++it) { 17221c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (!it->mChunks.empty()) { 17231c9747a4653aec1395c2bd6896c9b87cb5447837James Dong List<Chunk>::iterator chunkIt = it->mChunks.begin(); 17241c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (chunkIt->mTimeStampUs < minTimestampUs) { 17251c9747a4653aec1395c2bd6896c9b87cb5447837James Dong minTimestampUs = chunkIt->mTimeStampUs; 17261c9747a4653aec1395c2bd6896c9b87cb5447837James Dong track = it->mTrack; 17271c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17281c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17291c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17301c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17311c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (track == NULL) { 17323856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("Nothing to be written after all"); 1733fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong return false; 17341c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17351c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17361c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (mIsFirstChunk) { 17371c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsFirstChunk = false; 17381c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 1739fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 17401c9747a4653aec1395c2bd6896c9b87cb5447837James Dong for (List<ChunkInfo>::iterator it = mChunkInfos.begin(); 17411c9747a4653aec1395c2bd6896c9b87cb5447837James Dong it != mChunkInfos.end(); ++it) { 17421c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (it->mTrack == track) { 1743fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong *chunk = *(it->mChunks.begin()); 1744fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong it->mChunks.erase(it->mChunks.begin()); 1745fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong CHECK_EQ(chunk->mTrack, track); 174670ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 174770ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int64_t interChunkTimeUs = 174870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong chunk->mTimeStampUs - it->mPrevChunkTimestampUs; 174970ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong if (interChunkTimeUs > it->mPrevChunkTimestampUs) { 175070ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong it->mMaxInterChunkDurUs = interChunkTimeUs; 175170ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong } 175270ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 1753fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong return true; 17541c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17551c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 1756fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1757fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong return false; 17581c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 17591c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17601c9747a4653aec1395c2bd6896c9b87cb5447837James Dongvoid MPEG4Writer::threadFunc() { 17613856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("threadFunc"); 17621c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1763a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong prctl(PR_SET_NAME, (unsigned long)"MPEG4Writer", 0, 0, 0); 1764fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1765fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong Mutex::Autolock autoLock(mLock); 17661c9747a4653aec1395c2bd6896c9b87cb5447837James Dong while (!mDone) { 1767fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong Chunk chunk; 1768fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong bool chunkFound = false; 1769fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1770fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong while (!mDone && !(chunkFound = findChunkToWrite(&chunk))) { 17711c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mChunkReadyCondition.wait(mLock); 17721c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 17731c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 1774de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui // In real time recording mode, write without holding the lock in order 1775de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui // to reduce the blocking time for media track threads. 1776de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui // Otherwise, hold the lock until the existing chunks get written to the 1777de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui // file. 1778fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong if (chunkFound) { 1779de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (mIsRealTimeRecording) { 1780de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui mLock.unlock(); 1781de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui } 1782fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong writeChunkToFile(&chunk); 1783de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (mIsRealTimeRecording) { 1784de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui mLock.lock(); 1785de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui } 1786fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong } 17871c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 1788fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong 1789fcac8fa9421f442f024018628a6042d7a14fbfb0James Dong writeAllChunks(); 17901c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 17911c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17921c9747a4653aec1395c2bd6896c9b87cb5447837James Dongstatus_t MPEG4Writer::startWriterThread() { 17933856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("startWriterThread"); 17941c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 17951c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mDone = false; 17961c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsFirstChunk = true; 1797e259531ce59ab1f31de5a23124b22536f6a5a767James Dong mDriftTimeUs = 0; 17981c9747a4653aec1395c2bd6896c9b87cb5447837James Dong for (List<Track *>::iterator it = mTracks.begin(); 17991c9747a4653aec1395c2bd6896c9b87cb5447837James Dong it != mTracks.end(); ++it) { 18001c9747a4653aec1395c2bd6896c9b87cb5447837James Dong ChunkInfo info; 18011c9747a4653aec1395c2bd6896c9b87cb5447837James Dong info.mTrack = *it; 180270ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong info.mPrevChunkTimestampUs = 0; 180370ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong info.mMaxInterChunkDurUs = 0; 18041c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mChunkInfos.push_back(info); 18051c9747a4653aec1395c2bd6896c9b87cb5447837James Dong } 18061c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 18071c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_attr_t attr; 18081c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_attr_init(&attr); 18091c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 18101c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_create(&mThread, &attr, ThreadWrapper, this); 18111c9747a4653aec1395c2bd6896c9b87cb5447837James Dong pthread_attr_destroy(&attr); 1812411ba422e3635d534928ffd81abf54f4f291c739James Dong mWriterThreadStarted = true; 18131c9747a4653aec1395c2bd6896c9b87cb5447837James Dong return OK; 18141c9747a4653aec1395c2bd6896c9b87cb5447837James Dong} 18151c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 18161f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 181793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dongstatus_t MPEG4Writer::Track::start(MetaData *params) { 1818a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (!mDone && mPaused) { 1819a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused = false; 1820a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mResumed = true; 1821a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong return OK; 1822a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 182325b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 182493d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong int64_t startTimeUs; 182519c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber if (params == NULL || !params->findInt64(kKeyTime, &startTimeUs)) { 182619c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber startTimeUs = 0; 182719c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber } 182870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong mStartTimeRealUs = startTimeUs; 182919c9a1e2bb6ead5e1e895aea8be573fe0f8cc7bbAndreas Huber 183013f6284305e4b27395a23db7882d670bdb1bcae1James Dong int32_t rotationDegrees; 183113f6284305e4b27395a23db7882d670bdb1bcae1James Dong if (!mIsAudio && params && params->findInt32(kKeyRotation, &rotationDegrees)) { 183213f6284305e4b27395a23db7882d670bdb1bcae1James Dong mRotation = rotationDegrees; 183313f6284305e4b27395a23db7882d670bdb1bcae1James Dong } 183413f6284305e4b27395a23db7882d670bdb1bcae1James Dong 183593d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong initTrackingProgressStatus(params); 183693d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong 1837f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong sp<MetaData> meta = new MetaData; 1838de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (mOwner->isRealTimeRecording() && mOwner->numTracks() > 1) { 1839a472613aec322e25891abf5c77bf3f7e3c244920James Dong /* 1840a472613aec322e25891abf5c77bf3f7e3c244920James Dong * This extra delay of accepting incoming audio/video signals 1841a472613aec322e25891abf5c77bf3f7e3c244920James Dong * helps to align a/v start time at the beginning of a recording 1842a472613aec322e25891abf5c77bf3f7e3c244920James Dong * session, and it also helps eliminate the "recording" sound for 1843a472613aec322e25891abf5c77bf3f7e3c244920James Dong * camcorder applications. 1844a472613aec322e25891abf5c77bf3f7e3c244920James Dong * 184586b7f47aa7482424cf8fd248f1315311919be3b0James Dong * If client does not set the start time offset, we fall back to 184686b7f47aa7482424cf8fd248f1315311919be3b0James Dong * use the default initial delay value. 1847a472613aec322e25891abf5c77bf3f7e3c244920James Dong */ 184886b7f47aa7482424cf8fd248f1315311919be3b0James Dong int64_t startTimeOffsetUs = mOwner->getStartTimeOffsetMs() * 1000LL; 184986b7f47aa7482424cf8fd248f1315311919be3b0James Dong if (startTimeOffsetUs < 0) { // Start time offset was not set 185086b7f47aa7482424cf8fd248f1315311919be3b0James Dong startTimeOffsetUs = kInitialDelayTimeUs; 185186b7f47aa7482424cf8fd248f1315311919be3b0James Dong } 185286b7f47aa7482424cf8fd248f1315311919be3b0James Dong startTimeUs += startTimeOffsetUs; 1853a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGI("Start time offset: %" PRId64 " us", startTimeOffsetUs); 1854a472613aec322e25891abf5c77bf3f7e3c244920James Dong } 1855a472613aec322e25891abf5c77bf3f7e3c244920James Dong 1856f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong meta->setInt64(kKeyTime, startTimeUs); 1857a472613aec322e25891abf5c77bf3f7e3c244920James Dong 1858f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong status_t err = mSource->start(meta.get()); 185925b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber if (err != OK) { 186025b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber mDone = mReachedEOS = true; 186125b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return err; 186225b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber } 186320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 186420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_attr_t attr; 186520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_attr_init(&attr); 186620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 186720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 186820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mDone = false; 1869eaae38445a340c4857c1c5569475879a728e63b7James Dong mStarted = true; 1870c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong mTrackDurationUs = 0; 187125b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber mReachedEOS = false; 1872956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong mEstimatedTrackSizeBytes = 0; 18731f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong mMdatSizeBytes = 0; 187443ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mMaxChunkDurationUs = 0; 187520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 187625b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber pthread_create(&mThread, &attr, ThreadWrapper, this); 187720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_attr_destroy(&attr); 187825b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 187925b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return OK; 188020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 188120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 188237187916a486504acaf83bea30147eb5fbf46ae5James Dongstatus_t MPEG4Writer::Track::pause() { 1883a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mPaused = true; 188437187916a486504acaf83bea30147eb5fbf46ae5James Dong return OK; 1885a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong} 1886a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 188737187916a486504acaf83bea30147eb5fbf46ae5James Dongstatus_t MPEG4Writer::Track::stop() { 188872cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang ALOGD("%s track stopping", mIsAudio? "Audio": "Video"); 1889eaae38445a340c4857c1c5569475879a728e63b7James Dong if (!mStarted) { 189029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Stop() called but track is not started"); 1891eaae38445a340c4857c1c5569475879a728e63b7James Dong return ERROR_END_OF_STREAM; 1892eaae38445a340c4857c1c5569475879a728e63b7James Dong } 1893eaae38445a340c4857c1c5569475879a728e63b7James Dong 189420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber if (mDone) { 189537187916a486504acaf83bea30147eb5fbf46ae5James Dong return OK; 189620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 189720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber mDone = true; 189820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 189972cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang ALOGD("%s track source stopping", mIsAudio? "Audio": "Video"); 190072cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang mSource->stop(); 190172cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang ALOGD("%s track source stopped", mIsAudio? "Audio": "Video"); 190272cecca17d735db6532c45f0a7e10c47ee6f065aChong Zhang 190320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber void *dummy; 190420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber pthread_join(mThread, &dummy); 1905377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t err = static_cast<status_t>(reinterpret_cast<uintptr_t>(dummy)); 190637187916a486504acaf83bea30147eb5fbf46ae5James Dong 1907b8a805261bf0282e992d3608035e47d05a898710Steve Block ALOGD("%s track stopped", mIsAudio? "Audio": "Video"); 190837187916a486504acaf83bea30147eb5fbf46ae5James Dong return err; 190920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 191020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 191125b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huberbool MPEG4Writer::Track::reachedEOS() { 191225b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber return mReachedEOS; 191325b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber} 191425b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 191520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber// static 191620111aa043c5f404472bc63b90bc5aad906b1101Andreas Hubervoid *MPEG4Writer::Track::ThreadWrapper(void *me) { 191720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber Track *track = static_cast<Track *>(me); 191820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 191937187916a486504acaf83bea30147eb5fbf46ae5James Dong status_t err = track->threadEntry(); 1920377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT return (void *)(uintptr_t)err; 192120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 192220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 19233266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatic void getNalUnitType(uint8_t byte, uint8_t* type) { 19243856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("getNalUnitType: %d", byte); 19253266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19263266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // nal_unit_type: 5-bit unsigned integer 19273266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong *type = (byte & 0x1F); 19283266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong} 19293266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19303266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatic const uint8_t *findNextStartCode( 19313266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *data, size_t length) { 19323266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 1933a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("findNextStartCode: %p %zu", data, length); 19343266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19353266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong size_t bytesLeft = length; 19363266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong while (bytesLeft > 4 && 19373266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong memcmp("\x00\x00\x00\x01", &data[length - bytesLeft], 4)) { 19383266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong --bytesLeft; 19393266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19403266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (bytesLeft <= 4) { 19413266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong bytesLeft = 0; // Last parameter set 19423266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19433266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return &data[length - bytesLeft]; 19443266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong} 19453266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19463266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongconst uint8_t *MPEG4Writer::Track::parseParamSet( 19473266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *data, size_t length, int type, size_t *paramSetLen) { 19483266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19493856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("parseParamSet"); 19503266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong CHECK(type == kNalUnitTypeSeqParamSet || 19513266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong type == kNalUnitTypePicParamSet); 19523266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19533266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *nextStartCode = findNextStartCode(data, length); 19543266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong *paramSetLen = nextStartCode - data; 19553266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (*paramSetLen == 0) { 195629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Param set is malformed, since its length is 0"); 19573266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return NULL; 19583266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19593266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19603266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong AVCParamSet paramSet(*paramSetLen, data); 19613266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (type == kNalUnitTypeSeqParamSet) { 19623266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (*paramSetLen < 4) { 196329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Seq parameter set malformed"); 19643266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return NULL; 19653266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19663266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (mSeqParamSets.empty()) { 19673266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mProfileIdc = data[1]; 19683266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mProfileCompatible = data[2]; 19693266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mLevelIdc = data[3]; 19703266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } else { 19713266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (mProfileIdc != data[1] || 19723266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mProfileCompatible != data[2] || 19733266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mLevelIdc != data[3]) { 197429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Inconsistent profile/level found in seq parameter sets"); 19753266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return NULL; 19763266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19773266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19783266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mSeqParamSets.push_back(paramSet); 19793266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } else { 19803266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mPicParamSets.push_back(paramSet); 19813266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 19823266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return nextStartCode; 19833266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong} 19843266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19853266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatus_t MPEG4Writer::Track::copyAVCCodecSpecificData( 19863266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *data, size_t size) { 19873856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("copyAVCCodecSpecificData"); 19883266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 19893266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 2 bytes for each of the parameter set length field 19903266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // plus the 7 bytes for the header 19919aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return copyCodecSpecificData(data, size, 4 + 7); 19929aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim} 19939aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 19949aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatus_t MPEG4Writer::Track::copyHEVCCodecSpecificData( 19959aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *data, size_t size) { 19969aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGV("copyHEVCCodecSpecificData"); 19979aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 19989aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim // Min length of HEVC CSD is 23. (ISO/IEC 14496-15:2014 Chapter 8.3.3.1.2) 19999aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return copyCodecSpecificData(data, size, 23); 20009aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim} 20019aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 20029aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatus_t MPEG4Writer::Track::copyCodecSpecificData( 20039aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *data, size_t size, size_t minLength) { 20049aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (size < minLength) { 2005377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGE("Codec specific data length too short: %zu", size); 20063266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20073266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20083266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20093266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mCodecSpecificData = malloc(size); 20109aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mCodecSpecificData == NULL) { 20119aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Failed allocating codec specific data"); 20129aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return NO_MEMORY; 20139aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 20149aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mCodecSpecificDataSize = size; 20153266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong memcpy(mCodecSpecificData, data, size); 20163266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return OK; 20173266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong} 20183266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20193266b2c04867f687e1e1b7b86088d6eb83077fd0James Dongstatus_t MPEG4Writer::Track::parseAVCCodecSpecificData( 20203266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *data, size_t size) { 20213266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20223856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("parseAVCCodecSpecificData"); 20233266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Data starts with a start code. 20243266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // SPS and PPS are separated with start codes. 20253266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Also, SPS must come before PPS 20263266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint8_t type = kNalUnitTypeSeqParamSet; 20273266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong bool gotSps = false; 20283266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong bool gotPps = false; 20293266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *tmp = data; 20303266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong const uint8_t *nextStartCode = data; 20313266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong size_t bytesLeft = size; 20323266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong size_t paramSetLen = 0; 20333266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mCodecSpecificDataSize = 0; 20343266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong while (bytesLeft > 4 && !memcmp("\x00\x00\x00\x01", tmp, 4)) { 20353266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong getNalUnitType(*(tmp + 4), &type); 20363266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (type == kNalUnitTypeSeqParamSet) { 20373266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (gotPps) { 203829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("SPS must come before PPS"); 20393266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20403266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20413266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (!gotSps) { 20423266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong gotSps = true; 20433266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20443266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong nextStartCode = parseParamSet(tmp + 4, bytesLeft - 4, type, ¶mSetLen); 20453266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } else if (type == kNalUnitTypePicParamSet) { 20463266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (!gotSps) { 204729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("SPS must come before PPS"); 20483266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20493266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20503266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (!gotPps) { 20513266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong gotPps = true; 20523266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20533266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong nextStartCode = parseParamSet(tmp + 4, bytesLeft - 4, type, ¶mSetLen); 20543266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } else { 205529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Only SPS and PPS Nal units are expected"); 20563266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20573266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20583266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20593266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (nextStartCode == NULL) { 20603266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20613266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20623266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20633266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Move on to find the next parameter set 20643266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong bytesLeft -= nextStartCode - tmp; 20653266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong tmp = nextStartCode; 20663266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mCodecSpecificDataSize += (2 + paramSetLen); 20673266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20683266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20693266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong { 20703266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Check on the number of seq parameter sets 20713266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong size_t nSeqParamSets = mSeqParamSets.size(); 20723266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (nSeqParamSets == 0) { 207329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Cound not find sequence parameter set"); 20743266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20753266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20763266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20773266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (nSeqParamSets > 0x1F) { 2078377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGE("Too many seq parameter sets (%zu) found", nSeqParamSets); 20793266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20803266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20813266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20823266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 20833266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong { 20843266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Check on the number of pic parameter sets 20853266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong size_t nPicParamSets = mPicParamSets.size(); 20863266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (nPicParamSets == 0) { 208729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Cound not find picture parameter set"); 20883266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20893266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20903266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (nPicParamSets > 0xFF) { 2091377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGE("Too many pic parameter sets (%zd) found", nPicParamSets); 20923266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return ERROR_MALFORMED; 20933266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20943266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 20951374eddc4455b26d1dffdca10fc70534b3f08c1dDandawate Saket// FIXME: 20961374eddc4455b26d1dffdca10fc70534b3f08c1dDandawate Saket// Add chromat_format_idc, bit depth values, etc for AVC/h264 high profile and above 20971374eddc4455b26d1dffdca10fc70534b3f08c1dDandawate Saket// and remove #if 0 20981374eddc4455b26d1dffdca10fc70534b3f08c1dDandawate Saket#if 0 20993266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong { 21003266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Check on the profiles 21013266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // These profiles requires additional parameter set extensions 21023266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (mProfileIdc == 100 || mProfileIdc == 110 || 21033266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mProfileIdc == 122 || mProfileIdc == 144) { 210429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Sorry, no support for profile_idc: %d!", mProfileIdc); 21053266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return BAD_VALUE; 21063266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 21073266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 21081374eddc4455b26d1dffdca10fc70534b3f08c1dDandawate Saket#endif 21093266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return OK; 21103266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong} 2111548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber 211203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huberstatus_t MPEG4Writer::Track::makeAVCCodecSpecificData( 211303b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber const uint8_t *data, size_t size) { 2114548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber 211503b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber if (mCodecSpecificData != NULL) { 211629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Already have codec specific data"); 211703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber return ERROR_MALFORMED; 211803b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 211903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21203266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (size < 4) { 2121377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT ALOGE("Codec specific data length too short: %zu", size); 212203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber return ERROR_MALFORMED; 212303b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 212403b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21253266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // Data is in the form of AVCCodecSpecificData 21263266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (memcmp("\x00\x00\x00\x01", data, 4)) { 21273266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong return copyAVCCodecSpecificData(data, size); 212803b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 212903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21303266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong if (parseAVCCodecSpecificData(data, size) != OK) { 213103b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber return ERROR_MALFORMED; 213203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber } 213303b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21343266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // ISO 14496-15: AVC file format 21353266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong mCodecSpecificDataSize += 7; // 7 more bytes in the header 213603b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber mCodecSpecificData = malloc(mCodecSpecificDataSize); 21379aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mCodecSpecificData == NULL) { 21389aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mCodecSpecificDataSize = 0; 21399aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Failed allocating codec specific data"); 21409aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return NO_MEMORY; 21419aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 214203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber uint8_t *header = (uint8_t *)mCodecSpecificData; 21433266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[0] = 1; // version 21443266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[1] = mProfileIdc; // profile indication 21453266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[2] = mProfileCompatible; // profile compatibility 21463266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[3] = mLevelIdc; 214703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21483266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 6-bit '111111' followed by 2-bit to lengthSizeMinuusOne 2149b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong if (mOwner->useNalLengthFour()) { 2150b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong header[4] = 0xfc | 3; // length size == 4 bytes 2151b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } else { 2152b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong header[4] = 0xfc | 1; // length size == 2 bytes 2153b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } 215403b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21553266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 3-bit '111' followed by 5-bit numSequenceParameterSets 21563266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong int nSequenceParamSets = mSeqParamSets.size(); 21573266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[5] = 0xe0 | nSequenceParamSets; 21583266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header += 6; 21593266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong for (List<AVCParamSet>::iterator it = mSeqParamSets.begin(); 21603266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong it != mSeqParamSets.end(); ++it) { 21613266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 16-bit sequence parameter set length 21623266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint16_t seqParamSetLength = it->mLength; 21633266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[0] = seqParamSetLength >> 8; 21643266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[1] = seqParamSetLength & 0xff; 21653266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 21663266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // SPS NAL unit (sequence parameter length bytes) 21673266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong memcpy(&header[2], it->mData, seqParamSetLength); 21683266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header += (2 + seqParamSetLength); 21693266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 21703266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 21713266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 8-bit nPictureParameterSets 21723266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong int nPictureParamSets = mPicParamSets.size(); 21733266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[0] = nPictureParamSets; 21743266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header += 1; 21753266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong for (List<AVCParamSet>::iterator it = mPicParamSets.begin(); 21763266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong it != mPicParamSets.end(); ++it) { 21773266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // 16-bit picture parameter set length 21783266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong uint16_t picParamSetLength = it->mLength; 21793266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[0] = picParamSetLength >> 8; 21803266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header[1] = picParamSetLength & 0xff; 21813266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong 21823266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong // PPS Nal unit (picture parameter set length bytes) 21833266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong memcpy(&header[2], it->mData, picParamSetLength); 21843266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong header += (2 + picParamSetLength); 21853266b2c04867f687e1e1b7b86088d6eb83077fd0James Dong } 218603b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 218703b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber return OK; 218803b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber} 218903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber 21909aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 21919aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatus_t MPEG4Writer::Track::parseHEVCCodecSpecificData( 21929aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *data, size_t size, HevcParameterSets ¶mSets) { 21939aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 21949aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGV("parseHEVCCodecSpecificData"); 21959aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *tmp = data; 21969aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *nextStartCode = data; 21979aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size_t bytesLeft = size; 21989aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim while (bytesLeft > 4 && !memcmp("\x00\x00\x00\x01", tmp, 4)) { 21999aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim nextStartCode = findNextStartCode(tmp + 4, bytesLeft - 4); 22009aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (nextStartCode == NULL) { 22019aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22029aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22039aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t err = paramSets.addNalUnit(tmp + 4, (nextStartCode - tmp) - 4); 22049aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (err != OK) { 22059aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22069aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22079aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22089aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim // Move on to find the next parameter set 22099aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim bytesLeft -= nextStartCode - tmp; 22109aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim tmp = nextStartCode; 22119aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22129aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22139aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size_t csdSize = 23; 22149aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const size_t numNalUnits = paramSets.getNumNalUnits(); 22159aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim for (size_t i = 0; i < ARRAY_SIZE(kMandatoryHevcNalUnitTypes); ++i) { 22169aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim int type = kMandatoryHevcNalUnitTypes[i]; 22179aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size_t numParamSets = paramSets.getNumNalUnitsOfType(type); 22189aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (numParamSets == 0) { 22199aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Cound not find NAL unit of type %d", type); 22209aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22219aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22229aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22239aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim for (size_t i = 0; i < ARRAY_SIZE(kHevcNalUnitTypes); ++i) { 22249aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim int type = kHevcNalUnitTypes[i]; 22259aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim size_t numParamSets = paramSets.getNumNalUnitsOfType(type); 22269aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (numParamSets > 0xffff) { 22279aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Too many seq parameter sets (%zu) found", numParamSets); 22289aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22299aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22309aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim csdSize += 3; 22319aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim for (size_t j = 0; j < numNalUnits; ++j) { 22329aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (paramSets.getType(j) != type) { 22339aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim continue; 22349aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22359aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim csdSize += 2 + paramSets.getSize(j); 22369aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22379aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22389aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mCodecSpecificDataSize = csdSize; 22399aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return OK; 22409aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim} 22419aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22429aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimstatus_t MPEG4Writer::Track::makeHEVCCodecSpecificData( 22439aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim const uint8_t *data, size_t size) { 22449aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22459aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mCodecSpecificData != NULL) { 22469aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Already have codec specific data"); 22479aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22489aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22499aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22509aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (size < 4) { 22519aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Codec specific data length too short: %zu", size); 22529aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22539aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22549aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22559aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim // Data is in the form of HEVCCodecSpecificData 22569aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (memcmp("\x00\x00\x00\x01", data, 4)) { 22579aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return copyHEVCCodecSpecificData(data, size); 22589aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22599aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22609aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim HevcParameterSets paramSets; 22619aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (parseHEVCCodecSpecificData(data, size, paramSets) != OK) { 22629aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return ERROR_MALFORMED; 22639aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22649aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22659aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mCodecSpecificData = malloc(mCodecSpecificDataSize); 22669aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mCodecSpecificData == NULL) { 22679aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mCodecSpecificDataSize = 0; 22689aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ALOGE("Failed allocating codec specific data"); 22699aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return NO_MEMORY; 22709aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22719aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t err = paramSets.makeHvcc((uint8_t *)mCodecSpecificData, 22729aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim &mCodecSpecificDataSize, mOwner->useNalLengthFour() ? 5 : 2); 22739aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (err != OK) { 22749aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return err; 22759aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } 22769aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 22779aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim return OK; 22789aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim} 22799aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 2280872a481558350634a3fd5cb67939de288af00ecbJames Dong/* 2281872a481558350634a3fd5cb67939de288af00ecbJames Dong * Updates the drift time from the audio track so that 2282872a481558350634a3fd5cb67939de288af00ecbJames Dong * the video track can get the updated drift time information 2283872a481558350634a3fd5cb67939de288af00ecbJames Dong * from the file writer. The fluctuation of the drift time of the audio 2284872a481558350634a3fd5cb67939de288af00ecbJames Dong * encoding path is smoothed out with a simple filter by giving a larger 2285872a481558350634a3fd5cb67939de288af00ecbJames Dong * weight to more recently drift time. The filter coefficients, 0.5 and 0.5, 2286872a481558350634a3fd5cb67939de288af00ecbJames Dong * are heuristically determined. 2287872a481558350634a3fd5cb67939de288af00ecbJames Dong */ 2288872a481558350634a3fd5cb67939de288af00ecbJames Dongvoid MPEG4Writer::Track::updateDriftTime(const sp<MetaData>& meta) { 2289872a481558350634a3fd5cb67939de288af00ecbJames Dong int64_t driftTimeUs = 0; 2290872a481558350634a3fd5cb67939de288af00ecbJames Dong if (meta->findInt64(kKeyDriftTime, &driftTimeUs)) { 2291872a481558350634a3fd5cb67939de288af00ecbJames Dong int64_t prevDriftTimeUs = mOwner->getDriftTimeUs(); 2292872a481558350634a3fd5cb67939de288af00ecbJames Dong int64_t timeUs = (driftTimeUs + prevDriftTimeUs) >> 1; 2293872a481558350634a3fd5cb67939de288af00ecbJames Dong mOwner->setDriftTimeUs(timeUs); 2294872a481558350634a3fd5cb67939de288af00ecbJames Dong } 2295872a481558350634a3fd5cb67939de288af00ecbJames Dong} 2296872a481558350634a3fd5cb67939de288af00ecbJames Dong 229737187916a486504acaf83bea30147eb5fbf46ae5James Dongstatus_t MPEG4Writer::Track::threadEntry() { 229830ab66297501757d745b9ae10da61adcd891f497Andreas Huber int32_t count = 0; 229913aec890216948b0c364f8f92792129d0335f506James Dong const int64_t interleaveDurationUs = mOwner->interleaveDuration(); 230043ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong const bool hasMultipleTracks = (mOwner->numTracks() > 1); 230113aec890216948b0c364f8f92792129d0335f506James Dong int64_t chunkTimestampUs = 0; 230213aec890216948b0c364f8f92792129d0335f506James Dong int32_t nChunks = 0; 230313aec890216948b0c364f8f92792129d0335f506James Dong int32_t nZeroLengthFrames = 0; 2304965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t lastTimestampUs = 0; // Previous sample time stamp 2305965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t lastDurationUs = 0; // Between the previous two samples 2306965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t currDurationTicks = 0; // Timescale based ticks 2307965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t lastDurationTicks = 0; // Timescale based ticks 2308965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int32_t sampleCount = 1; // Sample count in the current stts table entry 2309000e18370baae60ffd9f25b509501dd8c26deabfJames Dong uint32_t previousSampleSize = 0; // Size of the previous sample 2310a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong int64_t previousPausedDurationUs = 0; 2311965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t timestampUs = 0; 2312000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int64_t cttsOffsetTimeUs = 0; 2313000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int64_t currCttsOffsetTimeTicks = 0; // Timescale based ticks 2314000e18370baae60ffd9f25b509501dd8c26deabfJames Dong int64_t lastCttsOffsetTimeTicks = -1; // Timescale based ticks 231543089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong int32_t cttsSampleCount = 0; // Sample count in the current ctts table entry 2316c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t lastSamplesPerChunk = 0; 2317e259531ce59ab1f31de5a23124b22536f6a5a767James Dong 2318a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong if (mIsAudio) { 2319a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong prctl(PR_SET_NAME, (unsigned long)"AudioTrackEncoding", 0, 0, 0); 2320a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong } else { 2321a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong prctl(PR_SET_NAME, (unsigned long)"VideoTrackEncoding", 0, 0, 0); 2322a6f61fc8e3d06373c17f0a38ff600e2b71c414faJames Dong } 2323de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui 2324de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (mOwner->isRealTimeRecording()) { 2325de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); 2326de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui } 2327985f838934510983d8a887461e98dca60a6e858fJames Dong 2328d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong sp<MetaData> meta_data; 232920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 233093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong status_t err = OK; 233120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber MediaBuffer *buffer; 233211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih const char *trackName = mIsAudio ? "Audio" : "Video"; 233393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong while (!mDone && (err = mSource->read(&buffer)) == OK) { 233420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber if (buffer->range_length() == 0) { 233520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber buffer->release(); 233620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber buffer = NULL; 233713aec890216948b0c364f8f92792129d0335f506James Dong ++nZeroLengthFrames; 233820111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber continue; 233920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 234020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2341a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong // If the codec specific data has not been received yet, delay pause. 2342a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong // After the codec specific data is received, discard what we received 2343a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong // when the track is to be paused. 2344a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (mPaused && !mResumed) { 2345a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong buffer->release(); 2346a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong buffer = NULL; 2347a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong continue; 2348a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 2349a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 235030ab66297501757d745b9ae10da61adcd891f497Andreas Huber ++count; 235130ab66297501757d745b9ae10da61adcd891f497Andreas Huber 235203b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber int32_t isCodecConfig; 235303b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecConfig) 235403b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber && isCodecConfig) { 2355548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber CHECK(!mGotAllCodecSpecificData); 2356548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber 23571c9747a4653aec1395c2bd6896c9b87cb5447837James Dong if (mIsAvc) { 235803b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber status_t err = makeAVCCodecSpecificData( 235903b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber (const uint8_t *)buffer->data() 236003b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber + buffer->range_offset(), 236103b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber buffer->range_length()); 236243089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_EQ((status_t)OK, err); 23639aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } else if (mIsHevc) { 23649aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim status_t err = makeHEVCCodecSpecificData( 236503b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber (const uint8_t *)buffer->data() 236603b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber + buffer->range_offset(), 23679aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim buffer->range_length()); 23689aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim CHECK_EQ((status_t)OK, err); 23699aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } else if (mIsMPEG4) { 23709aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim copyCodecSpecificData((const uint8_t *)buffer->data() + buffer->range_offset(), 23719aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim buffer->range_length()); 237230ab66297501757d745b9ae10da61adcd891f497Andreas Huber } 237330ab66297501757d745b9ae10da61adcd891f497Andreas Huber 237430ab66297501757d745b9ae10da61adcd891f497Andreas Huber buffer->release(); 237530ab66297501757d745b9ae10da61adcd891f497Andreas Huber buffer = NULL; 237630ab66297501757d745b9ae10da61adcd891f497Andreas Huber 2377548e31844937b37518fbb62ff69e9ff1f794183bAndreas Huber mGotAllCodecSpecificData = true; 237830ab66297501757d745b9ae10da61adcd891f497Andreas Huber continue; 2379a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 2380a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 2381d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong // Make a deep copy of the MediaBuffer and Metadata and release 2382d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong // the original as soon as we can 2383d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong MediaBuffer *copy = new MediaBuffer(buffer->range_length()); 2384d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong memcpy(copy->data(), (uint8_t *)buffer->data() + buffer->range_offset(), 2385d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong buffer->range_length()); 2386d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong copy->set_range(0, buffer->range_length()); 2387d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong meta_data = new MetaData(*buffer->meta_data().get()); 2388d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong buffer->release(); 2389d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong buffer = NULL; 2390d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong 23919aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mIsAvc || mIsHevc) StripStartcode(copy); 2392e136c3bb38e88315bf8797a464ebf2c788296b22James Dong 2393b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong size_t sampleSize = copy->range_length(); 23949aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim if (mIsAvc || mIsHevc) { 2395b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong if (mOwner->useNalLengthFour()) { 2396b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong sampleSize += 4; 2397b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } else { 2398b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong sampleSize += 2; 2399b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } 2400b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong } 2401050b28a593350047845a45a14cc5026221ac1620James Dong 2402d599cd4573b5a2d5914c5040e0565ef866749b77James Dong // Max file size or duration handling 24031f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong mMdatSizeBytes += sampleSize; 24041f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong updateTrackSizeEstimate(); 24051f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong 2406d599cd4573b5a2d5914c5040e0565ef866749b77James Dong if (mOwner->exceedsFileSizeLimit()) { 2407316d08c1f115499fb0ccd82d22592bed7e54aad7Hangyu Kuang ALOGW("Recorded file size exceeds limit %" PRId64 "bytes", 2408316d08c1f115499fb0ccd82d22592bed7e54aad7Hangyu Kuang mOwner->mMaxFileSizeLimitBytes); 2409d599cd4573b5a2d5914c5040e0565ef866749b77James Dong mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); 2410d599cd4573b5a2d5914c5040e0565ef866749b77James Dong break; 2411d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 2412d599cd4573b5a2d5914c5040e0565ef866749b77James Dong if (mOwner->exceedsFileDurationLimit()) { 2413316d08c1f115499fb0ccd82d22592bed7e54aad7Hangyu Kuang ALOGW("Recorded file duration exceeds limit %" PRId64 "microseconds", 2414316d08c1f115499fb0ccd82d22592bed7e54aad7Hangyu Kuang mOwner->mMaxFileDurationLimitUs); 2415d599cd4573b5a2d5914c5040e0565ef866749b77James Dong mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); 2416d599cd4573b5a2d5914c5040e0565ef866749b77James Dong break; 2417d599cd4573b5a2d5914c5040e0565ef866749b77James Dong } 2418d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 2419050b28a593350047845a45a14cc5026221ac1620James Dong 2420d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong int32_t isSync = false; 2421d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong meta_data->findInt32(kKeyIsSyncFrame, &isSync); 2422d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong CHECK(meta_data->findInt64(kKeyTime, ×tampUs)); 2423d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong 2424d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong//////////////////////////////////////////////////////////////////////////////// 2425c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 0) { 242670ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong mFirstSampleTimeRealUs = systemTime() / 1000; 2427f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong mStartTimestampUs = timestampUs; 2428f60cafe0e6aad8f9ce54660fa88b651ae4e749e6James Dong mOwner->setStartTimestampUs(mStartTimestampUs); 24298428af5381e835cc783b7ecb0d71cb60961c99c2James Dong previousPausedDurationUs = mStartTimestampUs; 24303c0131f02b6f008321608044c53bccce2ac5f6ddJames Dong } 243148c948b1137e7bbdb161b51908657ab72ac5e2daAndreas Huber 2432a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong if (mResumed) { 24338428af5381e835cc783b7ecb0d71cb60961c99c2James Dong int64_t durExcludingEarlierPausesUs = timestampUs - previousPausedDurationUs; 243411f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(durExcludingEarlierPausesUs >= 0ll, "for %s track", trackName)) { 243511f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 243611f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 243711f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 243811f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 24398428af5381e835cc783b7ecb0d71cb60961c99c2James Dong int64_t pausedDurationUs = durExcludingEarlierPausesUs - mTrackDurationUs; 244011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(pausedDurationUs >= lastDurationUs, "for %s track", trackName)) { 244111f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 244211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 244311f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 244411f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 24458428af5381e835cc783b7ecb0d71cb60961c99c2James Dong previousPausedDurationUs += pausedDurationUs - lastDurationUs; 2446a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong mResumed = false; 2447a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong } 2448a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong 2449a7d1a2dd776bf356c228785a94ba8e0ff6a2ec7fJames Dong timestampUs -= previousPausedDurationUs; 245011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(timestampUs >= 0ll, "for %s track", trackName)) { 245111f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 245211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 245311f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 245411f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 2455000e18370baae60ffd9f25b509501dd8c26deabfJames Dong if (!mIsAudio) { 2456965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong /* 2457965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong * Composition time: timestampUs 2458965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong * Decoding time: decodingTimeUs 2459000e18370baae60ffd9f25b509501dd8c26deabfJames Dong * Composition time offset = composition time - decoding time 2460965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong */ 2461965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong int64_t decodingTimeUs; 2462965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong CHECK(meta_data->findInt64(kKeyDecodingTime, &decodingTimeUs)); 2463965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong decodingTimeUs -= previousPausedDurationUs; 2464000e18370baae60ffd9f25b509501dd8c26deabfJames Dong cttsOffsetTimeUs = 2465000e18370baae60ffd9f25b509501dd8c26deabfJames Dong timestampUs + kMaxCttsOffsetTimeUs - decodingTimeUs; 246611f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(cttsOffsetTimeUs >= 0ll, "for %s track", trackName)) { 246711f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 246811f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 246911f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 247011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 2471965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong timestampUs = decodingTimeUs; 2472a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("decoding time: %" PRId64 " and ctts offset time: %" PRId64, 2473000e18370baae60ffd9f25b509501dd8c26deabfJames Dong timestampUs, cttsOffsetTimeUs); 2474000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 2475000e18370baae60ffd9f25b509501dd8c26deabfJames Dong // Update ctts box table if necessary 2476000e18370baae60ffd9f25b509501dd8c26deabfJames Dong currCttsOffsetTimeTicks = 2477000e18370baae60ffd9f25b509501dd8c26deabfJames Dong (cttsOffsetTimeUs * mTimeScale + 500000LL) / 1000000LL; 247811f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(currCttsOffsetTimeTicks <= 0x0FFFFFFFFLL, "for %s track", trackName)) { 247911f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 248011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 248111f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 248211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 2483c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 0) { 248443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong // Force the first ctts table entry to have one single entry 248543089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong // so that we can do adjustment for the initial track start 248643089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong // time offset easily in writeCttsBox(). 248743089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks; 248843089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong addOneCttsTableEntry(1, currCttsOffsetTimeTicks); 248943089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong cttsSampleCount = 0; // No sample in ctts box is pending 249043089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } else { 249143089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong if (currCttsOffsetTimeTicks != lastCttsOffsetTimeTicks) { 249243089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks); 249343089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong lastCttsOffsetTimeTicks = currCttsOffsetTimeTicks; 249443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong cttsSampleCount = 1; // One sample in ctts box is pending 249543089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } else { 249643089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong ++cttsSampleCount; 249743089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } 249843089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } 2499000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 2500000e18370baae60ffd9f25b509501dd8c26deabfJames Dong // Update ctts time offset range 2501c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 0) { 2502000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mMinCttsOffsetTimeUs = currCttsOffsetTimeTicks; 2503000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mMaxCttsOffsetTimeUs = currCttsOffsetTimeTicks; 2504000e18370baae60ffd9f25b509501dd8c26deabfJames Dong } else { 2505000e18370baae60ffd9f25b509501dd8c26deabfJames Dong if (currCttsOffsetTimeTicks > mMaxCttsOffsetTimeUs) { 2506000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mMaxCttsOffsetTimeUs = currCttsOffsetTimeTicks; 2507000e18370baae60ffd9f25b509501dd8c26deabfJames Dong } else if (currCttsOffsetTimeTicks < mMinCttsOffsetTimeUs) { 2508000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mMinCttsOffsetTimeUs = currCttsOffsetTimeTicks; 2509000e18370baae60ffd9f25b509501dd8c26deabfJames Dong } 2510000e18370baae60ffd9f25b509501dd8c26deabfJames Dong } 2511000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 2512965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong } 2513872a481558350634a3fd5cb67939de288af00ecbJames Dong 2514de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui if (mOwner->isRealTimeRecording()) { 2515872a481558350634a3fd5cb67939de288af00ecbJames Dong if (mIsAudio) { 2516872a481558350634a3fd5cb67939de288af00ecbJames Dong updateDriftTime(meta_data); 2517e259531ce59ab1f31de5a23124b22536f6a5a767James Dong } 2518e259531ce59ab1f31de5a23124b22536f6a5a767James Dong } 2519872a481558350634a3fd5cb67939de288af00ecbJames Dong 252011f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih if (WARN_UNLESS(timestampUs >= 0ll, "for %s track", trackName)) { 252111f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 252211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih return ERROR_MALFORMED; 252311f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih } 252411f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih 2525a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("%s media time stamp: %" PRId64 " and previous paused duration %" PRId64, 252611f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih trackName, timestampUs, previousPausedDurationUs); 2527c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong if (timestampUs > mTrackDurationUs) { 2528c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong mTrackDurationUs = timestampUs; 25293b240d6bf235f6f6b40ee4c37a2862de286ca1e4Andreas Huber } 25303b240d6bf235f6f6b40ee4c37a2862de286ca1e4Andreas Huber 25315a217fba010e801c255503602bda4b86ac5a6ac9James Dong // We need to use the time scale based ticks, rather than the 25325a217fba010e801c255503602bda4b86ac5a6ac9James Dong // timestamp itself to determine whether we have to use a new 25335a217fba010e801c255503602bda4b86ac5a6ac9James Dong // stts entry, since we may have rounding errors. 25345a217fba010e801c255503602bda4b86ac5a6ac9James Dong // The calculation is intended to reduce the accumulated 25355a217fba010e801c255503602bda4b86ac5a6ac9James Dong // rounding errors. 25365a217fba010e801c255503602bda4b86ac5a6ac9James Dong currDurationTicks = 25375a217fba010e801c255503602bda4b86ac5a6ac9James Dong ((timestampUs * mTimeScale + 500000LL) / 1000000LL - 25385a217fba010e801c255503602bda4b86ac5a6ac9James Dong (lastTimestampUs * mTimeScale + 500000LL) / 1000000LL); 2539c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (currDurationTicks < 0ll) { 2540a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGE("timestampUs %" PRId64 " < lastTimestampUs %" PRId64 " for %s track", 254111f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih timestampUs, lastTimestampUs, trackName); 254211f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih copy->release(); 25430332fadec6e91c37fe39ab92b2c02922370bc853Hangyu Kuang mSource->stop(); 2544c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong return UNKNOWN_ERROR; 25458c460498c028888c533ab442be12b6d4b669b965James Dong } 25468c460498c028888c533ab442be12b6d4b669b965James Dong 254785fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // if the duration is different for this sample, see if it is close enough to the previous 254885fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // duration that we can fudge it and use the same value, to avoid filling the stts table 254985fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // with lots of near-identical entries. 255085fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // "close enough" here means that the current duration needs to be adjusted by less 255185fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // than 0.1 milliseconds 255285fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen if (lastDurationTicks && (currDurationTicks != lastDurationTicks)) { 255385fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen int64_t deltaUs = ((lastDurationTicks - currDurationTicks) * 1000000LL 255485fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen + (mTimeScale / 2)) / mTimeScale; 255585fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen if (deltaUs > -100 && deltaUs < 100) { 255685fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // use previous ticks, and adjust timestamp as if it was actually that number 255785fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen // of ticks 255885fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen currDurationTicks = lastDurationTicks; 255985fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen timestampUs += deltaUs; 256085fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen } 256185fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen } 256285fcbd6aec4eab3368e9266be0a1151a081f204dMarco Nelissen 2563c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStszTableEntries->add(htonl(sampleSize)); 2564c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() > 2) { 2565c059860c73678a202bfa33062723e8f82fb779d9James Dong 2566a472613aec322e25891abf5c77bf3f7e3c244920James Dong // Force the first sample to have its own stts entry so that 2567a472613aec322e25891abf5c77bf3f7e3c244920James Dong // we can adjust its value later to maintain the A/V sync. 2568c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 3 || currDurationTicks != lastDurationTicks) { 256979761ab096f57c3027fad9556c2bc436672d614eJames Dong addOneSttsTableEntry(sampleCount, lastDurationTicks); 2570be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong sampleCount = 1; 2571be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } else { 2572be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong ++sampleCount; 2573be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } 2574965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 2575be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } 2576be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong if (mSamplesHaveSameSize) { 2577c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() >= 2 && previousSampleSize != sampleSize) { 2578be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong mSamplesHaveSameSize = false; 2579be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } 25808644c14618d30d9e57a69df40ed939986ebf02c4James Dong previousSampleSize = sampleSize; 2581be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } 2582a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("%s timestampUs/lastTimestampUs: %" PRId64 "/%" PRId64, 258311f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih trackName, timestampUs, lastTimestampUs); 25848644c14618d30d9e57a69df40ed939986ebf02c4James Dong lastDurationUs = timestampUs - lastTimestampUs; 2585c059860c73678a202bfa33062723e8f82fb779d9James Dong lastDurationTicks = currDurationTicks; 25868644c14618d30d9e57a69df40ed939986ebf02c4James Dong lastTimestampUs = timestampUs; 258720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2588d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong if (isSync != 0) { 2589c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong addOneStssTableEntry(mStszTableEntries->count()); 2590d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong } 2591d07139e2e817a9b3ae9c87ba4e1e8d65d3e549daJames Dong 259293d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong if (mTrackingProgressStatus) { 259393d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong if (mPreviousTrackTimeUs <= 0) { 259493d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mPreviousTrackTimeUs = mStartTimestampUs; 259593d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong } 2596faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong trackProgressStatus(timestampUs); 259793d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong } 259843ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong if (!hasMultipleTracks) { 25999aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim off64_t offset = (mIsAvc || mIsHevc) ? mOwner->addLengthPrefixedSample_l(copy) 260058ae9c530247668f8af36e30d228c716c226b3d4James Dong : mOwner->addSample_l(copy); 2601c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 2602c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t count = (mOwner->use32BitFileOffset() 2603c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong ? mStcoTableEntries->count() 2604c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong : mCo64TableEntries->count()); 2605c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 2606c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (count == 0) { 26071f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong addChunkOffset(offset); 260858ae9c530247668f8af36e30d228c716c226b3d4James Dong } 260958ae9c530247668f8af36e30d228c716c226b3d4James Dong copy->release(); 261058ae9c530247668f8af36e30d228c716c226b3d4James Dong copy = NULL; 261158ae9c530247668f8af36e30d228c716c226b3d4James Dong continue; 261258ae9c530247668f8af36e30d228c716c226b3d4James Dong } 261313aec890216948b0c364f8f92792129d0335f506James Dong 261413aec890216948b0c364f8f92792129d0335f506James Dong mChunkSamples.push_back(copy); 261513aec890216948b0c364f8f92792129d0335f506James Dong if (interleaveDurationUs == 0) { 26161f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong addOneStscTableEntry(++nChunks, 1); 26171c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bufferChunk(timestampUs); 261813aec890216948b0c364f8f92792129d0335f506James Dong } else { 261913aec890216948b0c364f8f92792129d0335f506James Dong if (chunkTimestampUs == 0) { 262013aec890216948b0c364f8f92792129d0335f506James Dong chunkTimestampUs = timestampUs; 262113aec890216948b0c364f8f92792129d0335f506James Dong } else { 262243ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong int64_t chunkDurationUs = timestampUs - chunkTimestampUs; 262343ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong if (chunkDurationUs > interleaveDurationUs) { 262443ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong if (chunkDurationUs > mMaxChunkDurationUs) { 262543ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mMaxChunkDurationUs = chunkDurationUs; 262643ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong } 262713aec890216948b0c364f8f92792129d0335f506James Dong ++nChunks; 262813aec890216948b0c364f8f92792129d0335f506James Dong if (nChunks == 1 || // First chunk 2629c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong lastSamplesPerChunk != mChunkSamples.size()) { 2630c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong lastSamplesPerChunk = mChunkSamples.size(); 2631c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong addOneStscTableEntry(nChunks, lastSamplesPerChunk); 263213aec890216948b0c364f8f92792129d0335f506James Dong } 26331c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bufferChunk(timestampUs); 263413aec890216948b0c364f8f92792129d0335f506James Dong chunkTimestampUs = timestampUs; 263513aec890216948b0c364f8f92792129d0335f506James Dong } 263613aec890216948b0c364f8f92792129d0335f506James Dong } 263713aec890216948b0c364f8f92792129d0335f506James Dong } 263813aec890216948b0c364f8f92792129d0335f506James Dong 263920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber } 264025b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 264145c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong if (isTrackMalFormed()) { 2642690f546b0ee548dbfe997df36418e5302ec2d786James Dong err = ERROR_MALFORMED; 2643f0ce2fb0c7bf3a414279e5aba61105f3d9025c0eJames Dong } 264445c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 2645bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong mOwner->trackProgressStatus(mTrackId, -1, err); 2646be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong 264713aec890216948b0c364f8f92792129d0335f506James Dong // Last chunk 264843ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong if (!hasMultipleTracks) { 2649c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong addOneStscTableEntry(1, mStszTableEntries->count()); 265058ae9c530247668f8af36e30d228c716c226b3d4James Dong } else if (!mChunkSamples.empty()) { 26511f90c4b3fda9dbd2824d67fd679f850572f114dcJames Dong addOneStscTableEntry(++nChunks, mChunkSamples.size()); 26521c9747a4653aec1395c2bd6896c9b87cb5447837James Dong bufferChunk(timestampUs); 265313aec890216948b0c364f8f92792129d0335f506James Dong } 265413aec890216948b0c364f8f92792129d0335f506James Dong 2655be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong // We don't really know how long the last frame lasts, since 2656be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong // there is no frame time after it, just repeat the previous 2657be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong // frame's duration. 2658c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 1) { 26598f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong lastDurationUs = 0; // A single sample's duration 266079761ab096f57c3027fad9556c2bc436672d614eJames Dong lastDurationTicks = 0; 2661be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } else { 2662be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong ++sampleCount; // Count for the last sample 2663be83c9e8c71ce16c0d0e9ed9df525510a49a541bJames Dong } 2664a472613aec322e25891abf5c77bf3f7e3c244920James Dong 2665c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() <= 2) { 266679761ab096f57c3027fad9556c2bc436672d614eJames Dong addOneSttsTableEntry(1, lastDurationTicks); 2667a472613aec322e25891abf5c77bf3f7e3c244920James Dong if (sampleCount - 1 > 0) { 266879761ab096f57c3027fad9556c2bc436672d614eJames Dong addOneSttsTableEntry(sampleCount - 1, lastDurationTicks); 2669a472613aec322e25891abf5c77bf3f7e3c244920James Dong } 2670a472613aec322e25891abf5c77bf3f7e3c244920James Dong } else { 267179761ab096f57c3027fad9556c2bc436672d614eJames Dong addOneSttsTableEntry(sampleCount, lastDurationTicks); 2672a472613aec322e25891abf5c77bf3f7e3c244920James Dong } 2673a472613aec322e25891abf5c77bf3f7e3c244920James Dong 267443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong // The last ctts box may not have been written yet, and this 267543089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong // is to make sure that we write out the last ctts box. 267643089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong if (currCttsOffsetTimeTicks == lastCttsOffsetTimeTicks) { 267743089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong if (cttsSampleCount > 0) { 267843089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong addOneCttsTableEntry(cttsSampleCount, lastCttsOffsetTimeTicks); 267943089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } 268043089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong } 268143089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong 2682c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong mTrackDurationUs += lastDurationUs; 268325b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber mReachedEOS = true; 268443ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 268543ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong sendTrackSummary(hasMultipleTracks); 268643ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 2687df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block ALOGI("Received total/0-length (%d/%d) buffers and encoded %d frames. - %s", 268811f15ddbca475b5f6a3d7970b22234e04c595b37Robert Shih count, nZeroLengthFrames, mStszTableEntries->count(), trackName); 2689872a481558350634a3fd5cb67939de288af00ecbJames Dong if (mIsAudio) { 2690a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGI("Audio track drift time: %" PRId64 " us", mOwner->getDriftTimeUs()); 2691872a481558350634a3fd5cb67939de288af00ecbJames Dong } 2692365a963142093a1cd8efdcea76b5f65096a5b115James Dong 269337187916a486504acaf83bea30147eb5fbf46ae5James Dong if (err == ERROR_END_OF_STREAM) { 269437187916a486504acaf83bea30147eb5fbf46ae5James Dong return OK; 269537187916a486504acaf83bea30147eb5fbf46ae5James Dong } 269637187916a486504acaf83bea30147eb5fbf46ae5James Dong return err; 2697365a963142093a1cd8efdcea76b5f65096a5b115James Dong} 2698365a963142093a1cd8efdcea76b5f65096a5b115James Dong 269945c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dongbool MPEG4Writer::Track::isTrackMalFormed() const { 2700c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mStszTableEntries->count() == 0) { // no samples written 270129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("The number of recorded samples is 0"); 270245c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong return true; 270345c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong } 270445c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 2705c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (!mIsAudio && mStssTableEntries->count() == 0) { // no sync frames for video 270629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("There are no sync frames for video track"); 270745c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong return true; 270845c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong } 270945c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 271045c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong if (OK != checkCodecSpecificData()) { // no codec specific data 271145c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong return true; 271245c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong } 271345c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 271445c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong return false; 271545c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong} 271645c254c0c535e1d62f23d14cab7385d536c1e2bfJames Dong 271743ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dongvoid MPEG4Writer::Track::sendTrackSummary(bool hasMultipleTracks) { 271807ec01904613a0bac32caaa8444b4690998faed7James Dong 271907ec01904613a0bac32caaa8444b4690998faed7James Dong // Send track summary only if test mode is enabled. 272007ec01904613a0bac32caaa8444b4690998faed7James Dong if (!isTestModeEnabled()) { 272107ec01904613a0bac32caaa8444b4690998faed7James Dong return; 272207ec01904613a0bac32caaa8444b4690998faed7James Dong } 272307ec01904613a0bac32caaa8444b4690998faed7James Dong 272443ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong int trackNum = (mTrackId << 28); 272543ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 272643ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 272743ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_TYPE, 272843ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mIsAudio? 0: 1); 272943ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 273043ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 273143ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_DURATION_MS, 273243ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mTrackDurationUs / 1000); 273343ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 273443ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 273543ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_ENCODED_FRAMES, 2736c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStszTableEntries->count()); 273743ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 273886b7f47aa7482424cf8fd248f1315311919be3b0James Dong { 273986b7f47aa7482424cf8fd248f1315311919be3b0James Dong // The system delay time excluding the requested initial delay that 274086b7f47aa7482424cf8fd248f1315311919be3b0James Dong // is used to eliminate the recording sound. 274186b7f47aa7482424cf8fd248f1315311919be3b0James Dong int64_t startTimeOffsetUs = mOwner->getStartTimeOffsetMs() * 1000LL; 274286b7f47aa7482424cf8fd248f1315311919be3b0James Dong if (startTimeOffsetUs < 0) { // Start time offset was not set 274386b7f47aa7482424cf8fd248f1315311919be3b0James Dong startTimeOffsetUs = kInitialDelayTimeUs; 274486b7f47aa7482424cf8fd248f1315311919be3b0James Dong } 274586b7f47aa7482424cf8fd248f1315311919be3b0James Dong int64_t initialDelayUs = 274686b7f47aa7482424cf8fd248f1315311919be3b0James Dong mFirstSampleTimeRealUs - mStartTimeRealUs - startTimeOffsetUs; 274786b7f47aa7482424cf8fd248f1315311919be3b0James Dong 274886b7f47aa7482424cf8fd248f1315311919be3b0James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 274970ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_INITIAL_DELAY_MS, 275070ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong (initialDelayUs) / 1000); 275186b7f47aa7482424cf8fd248f1315311919be3b0James Dong } 275270ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 275307ec01904613a0bac32caaa8444b4690998faed7James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 275407ec01904613a0bac32caaa8444b4690998faed7James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_DATA_KBYTES, 275507ec01904613a0bac32caaa8444b4690998faed7James Dong mMdatSizeBytes / 1024); 275607ec01904613a0bac32caaa8444b4690998faed7James Dong 275743ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong if (hasMultipleTracks) { 275843ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 275943ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_MAX_CHUNK_DUR_MS, 276043ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong mMaxChunkDurationUs / 1000); 276170ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong 276270ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int64_t moovStartTimeUs = mOwner->getStartTimestampUs(); 276370ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong if (mStartTimestampUs != moovStartTimeUs) { 276470ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong int64_t startTimeOffsetUs = mStartTimestampUs - moovStartTimeUs; 276570ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong mOwner->notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 276670ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_START_OFFSET_MS, 276770ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong startTimeOffsetUs / 1000); 276870ccfd44c4bfe95ddecc2df6c3695efc48229d72James Dong } 276943ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong } 277043ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong} 277143ec1dfc5dc3934680a52a026c5519ddc51bdbd3James Dong 2772faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dongvoid MPEG4Writer::Track::trackProgressStatus(int64_t timeUs, status_t err) { 2773a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("trackProgressStatus: %" PRId64 " us", timeUs); 2774c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong 2775215381ea729086b8359b7f59bdc2bd7cf55a0c45James Dong if (mTrackEveryTimeDurationUs > 0 && 2776215381ea729086b8359b7f59bdc2bd7cf55a0c45James Dong timeUs - mPreviousTrackTimeUs >= mTrackEveryTimeDurationUs) { 2777a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("Fire time tracking progress status at %" PRId64 " us", timeUs); 2778bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong mOwner->trackProgressStatus(mTrackId, timeUs - mPreviousTrackTimeUs, err); 277993d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong mPreviousTrackTimeUs = timeUs; 278093d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong } 278193d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong} 278293d6b102a13afa23bfa80d74c399d93d542e6ad6James Dong 2783faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dongvoid MPEG4Writer::trackProgressStatus( 2784bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong size_t trackId, int64_t timeUs, status_t err) { 2785faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong Mutex::Autolock lock(mLock); 2786bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong int32_t trackNum = (trackId << 28); 2787faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong 2788faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong // Error notification 2789faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong // Do not consider ERROR_END_OF_STREAM an error 2790faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong if (err != OK && err != ERROR_END_OF_STREAM) { 2791bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong notify(MEDIA_RECORDER_TRACK_EVENT_ERROR, 2792bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong trackNum | MEDIA_RECORDER_TRACK_ERROR_GENERAL, 2793faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong err); 2794faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong return; 2795faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong } 2796faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong 2797faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong if (timeUs == -1) { 2798faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong // Send completion notification 2799bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 2800bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_COMPLETION_STATUS, 2801faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong err); 2802faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong } else { 2803faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong // Send progress status 2804bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong notify(MEDIA_RECORDER_TRACK_EVENT_INFO, 2805bc07bcc65e91c7aea9713631ae67760dcf1b0286James Dong trackNum | MEDIA_RECORDER_TRACK_INFO_PROGRESS_IN_TIME, 2806faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong timeUs / 1000); 2807faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong } 2808faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong} 2809faed5cd124a54b8db3429f4c26b3220bbec4c8bbJames Dong 2810d707fcb3e29707ca4a5935c294ef0b38eb5aba5fJames Dongvoid MPEG4Writer::setDriftTimeUs(int64_t driftTimeUs) { 2811a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("setDriftTimeUs: %" PRId64 " us", driftTimeUs); 2812e259531ce59ab1f31de5a23124b22536f6a5a767James Dong Mutex::Autolock autolock(mLock); 2813d707fcb3e29707ca4a5935c294ef0b38eb5aba5fJames Dong mDriftTimeUs = driftTimeUs; 2814e259531ce59ab1f31de5a23124b22536f6a5a767James Dong} 2815e259531ce59ab1f31de5a23124b22536f6a5a767James Dong 2816e259531ce59ab1f31de5a23124b22536f6a5a767James Dongint64_t MPEG4Writer::getDriftTimeUs() { 2817a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("getDriftTimeUs: %" PRId64 " us", mDriftTimeUs); 2818e259531ce59ab1f31de5a23124b22536f6a5a767James Dong Mutex::Autolock autolock(mLock); 2819e259531ce59ab1f31de5a23124b22536f6a5a767James Dong return mDriftTimeUs; 2820e259531ce59ab1f31de5a23124b22536f6a5a767James Dong} 2821e259531ce59ab1f31de5a23124b22536f6a5a767James Dong 2822de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghuibool MPEG4Writer::isRealTimeRecording() const { 2823de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui return mIsRealTimeRecording; 2824de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui} 2825de05c8eab188e98798f2b9c3dfac53dbc18ef584ztenghui 2826b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dongbool MPEG4Writer::useNalLengthFour() { 2827b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong return mUse4ByteNalLength; 2828b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong} 2829b4d5320bda29cd9694461c9b61d0211f801ff0afJames Dong 28301c9747a4653aec1395c2bd6896c9b87cb5447837James Dongvoid MPEG4Writer::Track::bufferChunk(int64_t timestampUs) { 28313856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("bufferChunk"); 28321c9747a4653aec1395c2bd6896c9b87cb5447837James Dong 28331c9747a4653aec1395c2bd6896c9b87cb5447837James Dong Chunk chunk(this, timestampUs, mChunkSamples); 28341c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mOwner->bufferChunk(chunk); 283513aec890216948b0c364f8f92792129d0335f506James Dong mChunkSamples.clear(); 283620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 283720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 28383b240d6bf235f6f6b40ee4c37a2862de286ca1e4Andreas Huberint64_t MPEG4Writer::Track::getDurationUs() const { 2839c5f0c714dc4225cd2ec305d5ddd297964a3dd3dcJames Dong return mTrackDurationUs; 284020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 284120111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 2842d599cd4573b5a2d5914c5040e0565ef866749b77James Dongint64_t MPEG4Writer::Track::getEstimatedTrackSizeBytes() const { 2843d599cd4573b5a2d5914c5040e0565ef866749b77James Dong return mEstimatedTrackSizeBytes; 2844d599cd4573b5a2d5914c5040e0565ef866749b77James Dong} 2845d599cd4573b5a2d5914c5040e0565ef866749b77James Dong 2846690f546b0ee548dbfe997df36418e5302ec2d786James Dongstatus_t MPEG4Writer::Track::checkCodecSpecificData() const { 2847690f546b0ee548dbfe997df36418e5302ec2d786James Dong const char *mime; 2848690f546b0ee548dbfe997df36418e5302ec2d786James Dong CHECK(mMeta->findCString(kKeyMIMEType, &mime)); 2849690f546b0ee548dbfe997df36418e5302ec2d786James Dong if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime) || 2850690f546b0ee548dbfe997df36418e5302ec2d786James Dong !strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime) || 28519aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime) || 28529aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim !strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mime)) { 2853690f546b0ee548dbfe997df36418e5302ec2d786James Dong if (!mCodecSpecificData || 2854690f546b0ee548dbfe997df36418e5302ec2d786James Dong mCodecSpecificDataSize <= 0) { 285529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Missing codec specific data"); 2856690f546b0ee548dbfe997df36418e5302ec2d786James Dong return ERROR_MALFORMED; 2857690f546b0ee548dbfe997df36418e5302ec2d786James Dong } 2858690f546b0ee548dbfe997df36418e5302ec2d786James Dong } else { 2859690f546b0ee548dbfe997df36418e5302ec2d786James Dong if (mCodecSpecificData || 2860690f546b0ee548dbfe997df36418e5302ec2d786James Dong mCodecSpecificDataSize > 0) { 286129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Unexepected codec specific data found"); 2862690f546b0ee548dbfe997df36418e5302ec2d786James Dong return ERROR_MALFORMED; 2863690f546b0ee548dbfe997df36418e5302ec2d786James Dong } 2864690f546b0ee548dbfe997df36418e5302ec2d786James Dong } 2865690f546b0ee548dbfe997df36418e5302ec2d786James Dong return OK; 2866690f546b0ee548dbfe997df36418e5302ec2d786James Dong} 2867690f546b0ee548dbfe997df36418e5302ec2d786James Dong 2868b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeTrackHeader(bool use32BitOffset) { 286920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 28703856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("%s track time scale: %d", 28711c9747a4653aec1395c2bd6896c9b87cb5447837James Dong mIsAudio? "Audio": "Video", mTimeScale); 28728f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong 2873efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlsson uint32_t now = getMpeg4Time(); 2874b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("trak"); 2875b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeTkhdBox(now); 2876b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("mdia"); 2877b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeMdhdBox(now); 2878b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeHdlrBox(); 2879b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("minf"); 2880b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (mIsAudio) { 2881b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeSmhdBox(); 2882b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else { 2883b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeVmhdBox(); 2884b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2885b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeDinfBox(); 2886b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeStblBox(use32BitOffset); 2887b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // minf 2888b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // mdia 2889b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // trak 2890b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 2891b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2892b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeStblBox(bool use32BitOffset) { 2893b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("stbl"); 2894b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("stsd"); 2895b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 2896b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(1); // entry count 2897b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (mIsAudio) { 2898b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeAudioFourCCBox(); 2899b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else { 2900b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeVideoFourCCBox(); 2901b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2902b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stsd 2903b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeSttsBox(); 2904965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong writeCttsBox(); 2905b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (!mIsAudio) { 2906b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeStssBox(); 2907b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2908b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeStszBox(); 2909b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeStscBox(); 2910b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeStcoBox(use32BitOffset); 2911b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stbl 2912b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 2913b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2914b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeVideoFourCCBox() { 2915b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong const char *mime; 2916b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong bool success = mMeta->findCString(kKeyMIMEType, &mime); 2917b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(success); 29188b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan const char *fourcc = getFourCCForMime(mime); 29198b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (fourcc == NULL) { 292029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Unknown mime type '%s'.", mime); 2921b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(!"should not be here, unknown mime type."); 2922b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2923b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 29248b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan mOwner->beginBox(fourcc); // video format 2925b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 2926b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 2927b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(1); // data ref index 2928b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // predefined 2929b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 2930b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // predefined 2931b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // predefined 2932b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // predefined 2933b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2934b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t width, height; 2935b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong success = mMeta->findInt32(kKeyWidth, &width); 2936b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong success = success && mMeta->findInt32(kKeyHeight, &height); 2937b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(success); 2938b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2939b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(width); 2940b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(height); 2941b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0x480000); // horiz resolution 2942b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0x480000); // vert resolution 2943b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 2944b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(1); // frame count 2945c30a88a273b47bef6728ae1dddea11641090939aMartin Storsjo mOwner->writeInt8(0); // compressor string length 2946c30a88a273b47bef6728ae1dddea11641090939aMartin Storsjo mOwner->write(" ", 31); 2947b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x18); // depth 2948b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(-1); // predefined 2949b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 295043089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_LT(23 + mCodecSpecificDataSize, 128); 2951b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2952b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_MPEG4, mime)) { 2953b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeMp4vEsdsBox(); 2954b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_H263, mime)) { 2955b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeD263Box(); 2956b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime)) { 2957b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeAvccBox(); 29589aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim } else if (!strcasecmp(MEDIA_MIMETYPE_VIDEO_HEVC, mime)) { 29599aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim writeHvccBox(); 2960b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2961b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2962b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writePaspBox(); 2963b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // mp4v, s263 or avc1 2964b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 2965b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2966b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeAudioFourCCBox() { 2967b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong const char *mime; 2968b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong bool success = mMeta->findCString(kKeyMIMEType, &mime); 2969b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(success); 29708b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan const char *fourcc = getFourCCForMime(mime); 29718b07404cfe564885045a63bb592d6b6dc838b408Praveen Chavan if (fourcc == NULL) { 297229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Unknown mime type '%s'.", mime); 2973b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(!"should not be here, unknown mime type."); 2974b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2975b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2976b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox(fourcc); // audio format 2977b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 2978b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 2979b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x1); // data ref index 2980b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 2981b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 2982b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t nChannels; 2983b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK_EQ(true, mMeta->findInt32(kKeyChannelCount, &nChannels)); 2984b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(nChannels); // channel count 2985b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(16); // sample size 2986b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // predefined 2987b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 2988b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 2989b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t samplerate; 2990b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong success = mMeta->findInt32(kKeySampleRate, &samplerate); 2991b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(success); 2992b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(samplerate << 16); 2993b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AAC, mime)) { 2994b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeMp4aEsdsBox(); 2995b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_NB, mime) || 2996b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong !strcasecmp(MEDIA_MIMETYPE_AUDIO_AMR_WB, mime)) { 2997b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeDamrBox(); 2998b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 2999b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3000b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3001b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3002b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeMp4aEsdsBox() { 3003b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("esds"); 3004b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(mCodecSpecificData); 300543089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GT(mCodecSpecificDataSize, 0); 3006b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3007b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Make sure all sizes encode to a single byte. 300843089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_LT(mCodecSpecificDataSize + 23, 128); 3009b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3010b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3011b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x03); // ES_DescrTag 3012b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(23 + mCodecSpecificDataSize); 3013b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x0000);// ES_ID 3014b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x00); 3015b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3016b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x04); // DecoderConfigDescrTag 3017b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(15 + mCodecSpecificDataSize); 3018b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x40); // objectTypeIndication ISO/IEC 14492-2 3019b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x15); // streamType AudioStream 3020b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3021b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x03); // XXX 3022b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x00); // buffer size 24-bit 3023ebada74ec20c6bd46e81d2f4c160ac176c10784dDaniel Bonnevier int32_t bitRate; 3024ebada74ec20c6bd46e81d2f4c160ac176c10784dDaniel Bonnevier bool success = mMeta->findInt32(kKeyBitRate, &bitRate); 3025ebada74ec20c6bd46e81d2f4c160ac176c10784dDaniel Bonnevier mOwner->writeInt32(success ? bitRate : 96000); // max bit rate 3026ebada74ec20c6bd46e81d2f4c160ac176c10784dDaniel Bonnevier mOwner->writeInt32(success ? bitRate : 96000); // avg bit rate 3027b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3028b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x05); // DecoderSpecificInfoTag 3029b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(mCodecSpecificDataSize); 3030b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 3031b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3032b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong static const uint8_t kData2[] = { 3033b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x06, // SLConfigDescriptorTag 3034b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x01, 3035b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x02 3036b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong }; 3037b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(kData2, sizeof(kData2)); 3038b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3039b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // esds 3040b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3041b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3042b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeMp4vEsdsBox() { 3043b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(mCodecSpecificData); 304443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GT(mCodecSpecificDataSize, 0); 3045b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("esds"); 3046b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3047b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3048b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3049b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x03); // ES_DescrTag 3050b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(23 + mCodecSpecificDataSize); 3051b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x0000); // ES_ID 3052b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x1f); 3053b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3054b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x04); // DecoderConfigDescrTag 3055b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(15 + mCodecSpecificDataSize); 3056b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x20); // objectTypeIndication ISO/IEC 14492-2 3057b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x11); // streamType VisualStream 3058b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3059b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong static const uint8_t kData[] = { 3060b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x01, 0x77, 0x00, 3061b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x00, 0x03, 0xe8, 0x00, 3062b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x00, 0x03, 0xe8, 0x00 3063b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong }; 3064b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(kData, sizeof(kData)); 3065b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3066b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0x05); // DecoderSpecificInfoTag 3067b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3068b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(mCodecSpecificDataSize); 3069b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 3070b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3071b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong static const uint8_t kData2[] = { 3072b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x06, // SLConfigDescriptorTag 3073b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x01, 3074b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 0x02 3075b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong }; 3076b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(kData2, sizeof(kData2)); 3077b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3078b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // esds 3079b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3080b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3081efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlssonvoid MPEG4Writer::Track::writeTkhdBox(uint32_t now) { 3082b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("tkhd"); 3083b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Flags = 7 to indicate that the track is enabled, and 3084b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // part of the presentation 3085b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0x07); // version=0, flags=7 3086b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(now); // creation time 3087b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(now); // modification time 3088219f195159f93d627af2b243732e3f9020511a46James Dong mOwner->writeInt32(mTrackId); // track id starts with 1 3089b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 30908f5f2fcee5c12d08df71d17017410c50951fc2e3James Dong int64_t trakDurationUs = getDurationUs(); 3091b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t mvhdTimeScale = mOwner->getTimeScale(); 3092b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t tkhdDuration = 3093b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong (trakDurationUs * mvhdTimeScale + 5E5) / 1E6; 3094b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(tkhdDuration); // in mvhd timescale 3095b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 3096b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 3097b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // layer 3098b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // alternate group 3099b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(mIsAudio ? 0x100 : 0); // volume 3100b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 3101b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3102b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeCompositionMatrix(mRotation); // matrix 310320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3104b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (mIsAudio) { 3105b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); 3106b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); 3107b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } else { 3108b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t width, height; 3109b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong bool success = mMeta->findInt32(kKeyWidth, &width); 3110b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong success = success && mMeta->findInt32(kKeyHeight, &height); 3111b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(success); 3112b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3113b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(width << 16); // 32-bit fixed-point value 3114b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(height << 16); // 32-bit fixed-point value 3115b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 3116b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // tkhd 3117b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3118b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3119b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeVmhdBox() { 3120b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("vmhd"); 3121b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0x01); // version=0, flags=1 3122b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // graphics mode 3123b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // opcolor 3124b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); 3125b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); 3126b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3127b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3128b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3129b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeSmhdBox() { 3130b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("smhd"); 3131b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3132b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // balance 3133b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // reserved 3134b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3135b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3136b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3137b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeHdlrBox() { 3138b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("hdlr"); 3139b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3140b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // component type: should be mhlr 3141b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeFourcc(mIsAudio ? "soun" : "vide"); // component subtype 3142b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 3143b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 3144b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // reserved 3145b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Removing "r" for the name string just makes the string 4 byte aligned 3146b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeCString(mIsAudio ? "SoundHandle": "VideoHandle"); // name 3147b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3148b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3149b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3150efcdf187baab9ec29e12adcf85e63fe3e6d5aa4dJohannes Carlssonvoid MPEG4Writer::Track::writeMdhdBox(uint32_t now) { 3151b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int64_t trakDurationUs = getDurationUs(); 3152b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("mdhd"); 3153b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3154b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(now); // creation time 3155b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(now); // modification time 3156b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(mTimeScale); // media timescale 3157b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int32_t mdhdDuration = (trakDurationUs * mTimeScale + 5E5) / 1E6; 3158b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(mdhdDuration); // use media timescale 3159b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Language follows the three letter standard ISO-639-2/T 3160b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // 'e', 'n', 'g' for "English", for instance. 3161b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Each character is packed as the difference between its ASCII value and 0x60. 3162b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // For "English", these are 00101, 01110, 00111. 3163b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // XXX: Where is the padding bit located: 0x15C7? 3164b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // language code 3165b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0); // predefined 3166b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3167b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3168b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3169b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeDamrBox() { 3170b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // 3gpp2 Spec AMRSampleEntry fields 3171b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("damr"); 3172b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeCString(" "); // vendor: 4 bytes 3173b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0); // decoder version 3174b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt16(0x83FF); // mode set: all enabled 3175b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0); // mode change period 3176b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(1); // frames per sample 3177b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); 3178b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3179b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3180b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeUrlBox() { 3181b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // The table index here refers to the sample description index 3182b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // in the sample table entries. 3183b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("url "); 3184b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(1); // version=0, flags=1 (self-contained) 3185b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // url 3186b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3187b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3188b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeDrefBox() { 3189b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("dref"); 3190b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3191b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(1); // entry count (either url or urn) 3192b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeUrlBox(); 3193b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // dref 3194b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3195b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3196b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeDinfBox() { 3197b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("dinf"); 3198b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong writeDrefBox(); 3199b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // dinf 3200b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3201b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3202b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeAvccBox() { 3203b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong CHECK(mCodecSpecificData); 320443089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GE(mCodecSpecificDataSize, 5); 3205b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3206b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // Patch avcc's lengthSize field to match the number 3207b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong // of bytes we use to indicate the size of a nal unit. 3208b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong uint8_t *ptr = (uint8_t *)mCodecSpecificData; 3209b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong ptr[4] = (ptr[4] & 0xfc) | (mOwner->useNalLengthFour() ? 3 : 1); 3210b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("avcC"); 3211b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 3212b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // avcC 3213b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3214b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 32159aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 32169aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kimvoid MPEG4Writer::Track::writeHvccBox() { 32179aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim CHECK(mCodecSpecificData); 32189aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim CHECK_GE(mCodecSpecificDataSize, 5); 32199aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 32209aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim // Patch avcc's lengthSize field to match the number 32219aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim // of bytes we use to indicate the size of a nal unit. 32229aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim uint8_t *ptr = (uint8_t *)mCodecSpecificData; 32239aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim ptr[21] = (ptr[21] & 0xfc) | (mOwner->useNalLengthFour() ? 3 : 1); 32249aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mOwner->beginBox("hvcC"); 32259aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mOwner->write(mCodecSpecificData, mCodecSpecificDataSize); 32269aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim mOwner->endBox(); // hvcC 32279aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim} 32289aa87d4ef502c9700a31fe46dc6e1d6f99cf4e5eWonsik Kim 3229b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeD263Box() { 3230b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("d263"); 3231b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // vendor 3232b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0); // decoder version 3233b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(10); // level: 10 3234b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt8(0); // profile: 0 3235b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // d263 3236b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3237b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3238b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong// This is useful if the pixel is not square 3239b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writePaspBox() { 3240b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("pasp"); 3241b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(1 << 16); // hspacing 3242b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(1 << 16); // vspacing 3243b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // pasp 3244b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 3245b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong 3246000e18370baae60ffd9f25b509501dd8c26deabfJames Dongint32_t MPEG4Writer::Track::getStartTimeOffsetScaledTime() const { 3247a472613aec322e25891abf5c77bf3f7e3c244920James Dong int64_t trackStartTimeOffsetUs = 0; 3248b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong int64_t moovStartTimeUs = mOwner->getStartTimestampUs(); 3249b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong if (mStartTimestampUs != moovStartTimeUs) { 325043089daaf82bd2b8e5f9a29b80af5abaae4657b3James Dong CHECK_GT(mStartTimestampUs, moovStartTimeUs); 3251b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong trackStartTimeOffsetUs = mStartTimestampUs - moovStartTimeUs; 3252b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 3253000e18370baae60ffd9f25b509501dd8c26deabfJames Dong return (trackStartTimeOffsetUs * mTimeScale + 500000LL) / 1000000LL; 3254000e18370baae60ffd9f25b509501dd8c26deabfJames Dong} 3255000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 3256000e18370baae60ffd9f25b509501dd8c26deabfJames Dongvoid MPEG4Writer::Track::writeSttsBox() { 3257000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mOwner->beginBox("stts"); 3258000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mOwner->writeInt32(0); // version=0, flags=0 3259c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t duration; 3260c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK(mSttsTableEntries->get(duration, 1)); 3261c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong duration = htonl(duration); // Back to host byte order 3262c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries->set(htonl(duration + getStartTimeOffsetScaledTime()), 1); 3263c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mSttsTableEntries->write(mOwner); 3264b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stts 3265b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 326620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3267965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dongvoid MPEG4Writer::Track::writeCttsBox() { 3268965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong if (mIsAudio) { // ctts is not for audio 3269965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong return; 3270965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong } 3271965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 3272000e18370baae60ffd9f25b509501dd8c26deabfJames Dong // There is no B frame at all 3273000e18370baae60ffd9f25b509501dd8c26deabfJames Dong if (mMinCttsOffsetTimeUs == mMaxCttsOffsetTimeUs) { 3274000e18370baae60ffd9f25b509501dd8c26deabfJames Dong return; 3275000e18370baae60ffd9f25b509501dd8c26deabfJames Dong } 3276000e18370baae60ffd9f25b509501dd8c26deabfJames Dong 3277965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong // Do not write ctts box when there is no need to have it. 3278c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (mCttsTableEntries->count() == 0) { 3279965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong return; 3280965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong } 3281965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 3282a5750e0dad9e90f2195ce36f2c4457fa04b2b83eMark Salyzyn ALOGV("ctts box has %d entries with range [%" PRId64 ", %" PRId64 "]", 3283c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->count(), mMinCttsOffsetTimeUs, mMaxCttsOffsetTimeUs); 3284965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 3285965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong mOwner->beginBox("ctts"); 3286000e18370baae60ffd9f25b509501dd8c26deabfJames Dong mOwner->writeInt32(0); // version=0, flags=0 3287c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong uint32_t duration; 3288c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong CHECK(mCttsTableEntries->get(duration, 1)); 3289c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong duration = htonl(duration); // Back host byte order 3290c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->set(htonl(duration + getStartTimeOffsetScaledTime() - mMinCttsOffsetTimeUs), 1); 3291c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCttsTableEntries->write(mOwner); 3292965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong mOwner->endBox(); // ctts 3293965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong} 3294965e4239ca1cf6c824c1f8ce23116f9ba8cf6ebdJames Dong 3295b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeStssBox() { 3296b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("stss"); 3297b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3298c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStssTableEntries->write(mOwner); 3299b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stss 3300b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 330125b130939339d57789a86fac837a2a8cedbcb7d8Andreas Huber 3302b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeStszBox() { 3303b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("stsz"); 3304b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3305c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mOwner->writeInt32(0); 3306c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStszTableEntries->write(mOwner); 3307b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stsz 3308b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 330920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3310b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeStscBox() { 3311b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox("stsc"); 3312b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3313c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStscTableEntries->write(mOwner); 3314b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stsc 3315b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong} 331620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 3317b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dongvoid MPEG4Writer::Track::writeStcoBox(bool use32BitOffset) { 3318b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->beginBox(use32BitOffset? "stco": "co64"); 3319b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->writeInt32(0); // version=0, flags=0 3320c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong if (use32BitOffset) { 3321c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mStcoTableEntries->write(mOwner); 3322c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong } else { 3323c620cbd7f8dc75f6819e2b862d3c09970560bc63James Dong mCo64TableEntries->write(mOwner); 3324b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong } 3325b21c564ce47041f9dd3ab65e36fed57c4937a42dJames Dong mOwner->endBox(); // stco or co64 332620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} 332720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber 332807b1bb529a1ae76c46a71b01338c166f9490629dJames Dongvoid MPEG4Writer::writeUdtaBox() { 332907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong beginBox("udta"); 333007b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeGeoDataBox(); 333107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong endBox(); 333207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong} 333307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 3334e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangvoid MPEG4Writer::writeHdlr() { 3335e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox("hdlr"); 3336e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Version, Flags 3337e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Predefined 3338e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeFourcc("mdta"); 3339e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Reserved[0] 3340e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Reserved[1] 3341e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Reserved[2] 3342e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt8(0); // Name (empty) 3343e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); 3344e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 3345e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3346e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangvoid MPEG4Writer::writeKeys() { 3347e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang size_t count = mMetaKeys->countEntries(); 3348e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3349e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox("keys"); 3350e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); // Version, Flags 3351e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(count); // Entry_count 3352e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang for (size_t i = 0; i < count; i++) { 3353e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang AMessage::Type type; 3354e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang const char *key = mMetaKeys->getEntryNameAt(i, &type); 3355e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang size_t n = strlen(key); 3356e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(n + 8); 3357e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeFourcc("mdta"); 3358e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang write(key, n); // write without the \0 3359e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3360e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); 3361e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 3362e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3363e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangvoid MPEG4Writer::writeIlst() { 3364e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang size_t count = mMetaKeys->countEntries(); 3365e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3366e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox("ilst"); 3367e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang for (size_t i = 0; i < count; i++) { 3368e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox(i + 1); // key id (1-based) 3369e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox("data"); 3370e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang AMessage::Type type; 3371e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang const char *key = mMetaKeys->getEntryNameAt(i, &type); 3372e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang switch (type) { 33737c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang case AMessage::kTypeString: 33747c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang { 33757c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang AString val; 33767c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang CHECK(mMetaKeys->findString(key, &val)); 33777c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(1); // type = UTF8 33787c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(0); // default country/language 33797c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang write(val.c_str(), strlen(val.c_str())); // write without \0 33807c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang break; 33817c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang } 33827c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang 3383e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang case AMessage::kTypeFloat: 3384e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang { 3385e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang float val; 3386e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang CHECK(mMetaKeys->findFloat(key, &val)); 33877c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(23); // type = float32 33887c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(0); // default country/language 3389e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(*reinterpret_cast<int32_t *>(&val)); 3390e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang break; 3391e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3392e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3393e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang case AMessage::kTypeInt32: 3394e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang { 3395e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang int32_t val; 3396e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang CHECK(mMetaKeys->findInt32(key, &val)); 33977c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(67); // type = signed int32 33987c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(0); // default country/language 3399e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(val); 3400e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang break; 3401e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3402e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3403e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang default: 3404e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang { 3405e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang ALOGW("Unsupported key type, writing 0 instead"); 34067c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(77); // type = unsigned int32 34077c4820d23b68f748b8dfff3d4bb5b13e9d4811a8Chong Zhang writeInt32(0); // default country/language 3408e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeInt32(0); 3409e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang break; 3410e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3411e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3412e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); // data 3413e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); // key id 3414e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3415e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); // ilst 3416e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 3417e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3418e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhangvoid MPEG4Writer::writeMetaBox() { 3419e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang size_t count = mMetaKeys->countEntries(); 3420e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang if (count == 0) { 3421e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang return; 3422e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang } 3423e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 3424e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang beginBox("meta"); 3425e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeHdlr(); 3426e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeKeys(); 3427e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang writeIlst(); 3428e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang endBox(); 3429e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang} 3430e76dba7af9589d9ed7b116eec3a74168a8352925Chong Zhang 343107b1bb529a1ae76c46a71b01338c166f9490629dJames Dong/* 343207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * Geodata is stored according to ISO-6709 standard. 343307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong */ 343407b1bb529a1ae76c46a71b01338c166f9490629dJames Dongvoid MPEG4Writer::writeGeoDataBox() { 343507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong beginBox("\xA9xyz"); 343607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong /* 343707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * For historical reasons, any user data start 343807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * with "\0xA9", must be followed by its assoicated 343907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong * language code. 3440432ec3768cc4a3b1b01afedc456689d75c89ee2bJames Dong * 0x0012: text string length 3441432ec3768cc4a3b1b01afedc456689d75c89ee2bJames Dong * 0x15c7: lang (locale) code: en 344207b1bb529a1ae76c46a71b01338c166f9490629dJames Dong */ 344307b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeInt32(0x001215c7); 344407b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeLatitude(mLatitudex10000); 344507b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeLongitude(mLongitudex10000); 344607b1bb529a1ae76c46a71b01338c166f9490629dJames Dong writeInt8(0x2F); 344707b1bb529a1ae76c46a71b01338c166f9490629dJames Dong endBox(); 344807b1bb529a1ae76c46a71b01338c166f9490629dJames Dong} 344907b1bb529a1ae76c46a71b01338c166f9490629dJames Dong 345020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber} // namespace android 3451