muxer.cpp revision a0cf6c4d7d13c73feef5b88760b52b18289b0db2
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "muxer" 19#include <inttypes.h> 20#include <sys/types.h> 21#include <sys/stat.h> 22#include <fcntl.h> 23#include <utils/Log.h> 24 25#include <binder/ProcessState.h> 26#include <media/IMediaHTTPService.h> 27#include <media/stagefright/foundation/ABuffer.h> 28#include <media/stagefright/foundation/ADebug.h> 29#include <media/stagefright/foundation/ALooper.h> 30#include <media/stagefright/foundation/AMessage.h> 31#include <media/stagefright/foundation/AString.h> 32#include <media/stagefright/DataSource.h> 33#include <media/stagefright/MediaCodec.h> 34#include <media/stagefright/MediaDefs.h> 35#include <media/stagefright/MediaMuxer.h> 36#include <media/stagefright/MetaData.h> 37#include <media/stagefright/NuMediaExtractor.h> 38 39static void usage(const char *me) { 40 fprintf(stderr, "usage: %s [-a] [-v] [-s <trim start time>]" 41 " [-e <trim end time>] [-o <output file>]" 42 " <input video file>\n", me); 43 fprintf(stderr, " -h help\n"); 44 fprintf(stderr, " -a use audio\n"); 45 fprintf(stderr, " -v use video\n"); 46 fprintf(stderr, " -s Time in milli-seconds when the trim should start\n"); 47 fprintf(stderr, " -e Time in milli-seconds when the trim should end\n"); 48 fprintf(stderr, " -o output file name. Default is /sdcard/muxeroutput.mp4\n"); 49 50 exit(1); 51} 52 53using namespace android; 54 55static int muxing( 56 const char *path, 57 bool useAudio, 58 bool useVideo, 59 const char *outputFileName, 60 bool enableTrim, 61 int trimStartTimeMs, 62 int trimEndTimeMs, 63 int rotationDegrees) { 64 sp<NuMediaExtractor> extractor = new NuMediaExtractor; 65 if (extractor->setDataSource(NULL /* httpService */, path) != OK) { 66 fprintf(stderr, "unable to instantiate extractor. %s\n", path); 67 return 1; 68 } 69 70 if (outputFileName == NULL) { 71 outputFileName = "/sdcard/muxeroutput.mp4"; 72 } 73 74 ALOGV("input file %s, output file %s", path, outputFileName); 75 ALOGV("useAudio %d, useVideo %d", useAudio, useVideo); 76 77 int fd = open(outputFileName, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); 78 79 if (fd < 0) { 80 ALOGE("couldn't open file"); 81 return fd; 82 } 83 sp<MediaMuxer> muxer = new MediaMuxer(fd, 84 MediaMuxer::OUTPUT_FORMAT_MPEG_4); 85 close(fd); 86 87 size_t trackCount = extractor->countTracks(); 88 // Map the extractor's track index to the muxer's track index. 89 KeyedVector<size_t, ssize_t> trackIndexMap; 90 size_t bufferSize = 1 * 1024 * 1024; // default buffer size is 1MB. 91 92 bool haveAudio = false; 93 bool haveVideo = false; 94 95 int64_t trimStartTimeUs = trimStartTimeMs * 1000; 96 int64_t trimEndTimeUs = trimEndTimeMs * 1000; 97 bool trimStarted = false; 98 int64_t trimOffsetTimeUs = 0; 99 100 for (size_t i = 0; i < trackCount; ++i) { 101 sp<AMessage> format; 102 status_t err = extractor->getTrackFormat(i, &format); 103 CHECK_EQ(err, (status_t)OK); 104 ALOGV("extractor getTrackFormat: %s", format->debugString().c_str()); 105 106 AString mime; 107 CHECK(format->findString("mime", &mime)); 108 109 bool isAudio = !strncasecmp(mime.c_str(), "audio/", 6); 110 bool isVideo = !strncasecmp(mime.c_str(), "video/", 6); 111 112 if (useAudio && !haveAudio && isAudio) { 113 haveAudio = true; 114 } else if (useVideo && !haveVideo && isVideo) { 115 haveVideo = true; 116 } else { 117 continue; 118 } 119 120 if (isVideo) { 121 int width , height; 122 CHECK(format->findInt32("width", &width)); 123 CHECK(format->findInt32("height", &height)); 124 bufferSize = width * height * 4; // Assuming it is maximally 4BPP 125 } 126 127 int64_t duration; 128 CHECK(format->findInt64("durationUs", &duration)); 129 130 // Since we got the duration now, correct the start time. 131 if (enableTrim) { 132 if (trimStartTimeUs > duration) { 133 fprintf(stderr, "Warning: trimStartTimeUs > duration," 134 " reset to 0\n"); 135 trimStartTimeUs = 0; 136 } 137 } 138 139 ALOGV("selecting track %zu", i); 140 141 err = extractor->selectTrack(i); 142 CHECK_EQ(err, (status_t)OK); 143 144 ssize_t newTrackIndex = muxer->addTrack(format); 145 if (newTrackIndex < 0) { 146 fprintf(stderr, "%s track (%d) unsupported by muxer\n", 147 isAudio ? "audio" : "video", 148 i); 149 } else { 150 trackIndexMap.add(i, newTrackIndex); 151 } 152 } 153 154 int64_t muxerStartTimeUs = ALooper::GetNowUs(); 155 156 bool sawInputEOS = false; 157 158 size_t trackIndex = -1; 159 sp<ABuffer> newBuffer = new ABuffer(bufferSize); 160 161 muxer->setOrientationHint(rotationDegrees); 162 muxer->start(); 163 164 while (!sawInputEOS) { 165 status_t err = extractor->getSampleTrackIndex(&trackIndex); 166 if (err != OK) { 167 ALOGV("saw input eos, err %d", err); 168 sawInputEOS = true; 169 break; 170 } else if (trackIndexMap.indexOfKey(trackIndex) < 0) { 171 // ALOGV("skipping input from unsupported track %zu", trackIndex); 172 extractor->advance(); 173 continue; 174 } else { 175 // ALOGV("reading sample from track index %zu\n", trackIndex); 176 err = extractor->readSampleData(newBuffer); 177 CHECK_EQ(err, (status_t)OK); 178 179 int64_t timeUs; 180 err = extractor->getSampleTime(&timeUs); 181 CHECK_EQ(err, (status_t)OK); 182 183 sp<MetaData> meta; 184 err = extractor->getSampleMeta(&meta); 185 CHECK_EQ(err, (status_t)OK); 186 187 uint32_t sampleFlags = 0; 188 int32_t val; 189 if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) { 190 // We only support BUFFER_FLAG_SYNCFRAME in the flag for now. 191 sampleFlags |= MediaCodec::BUFFER_FLAG_SYNCFRAME; 192 193 // We turn on trimming at the sync frame. 194 if (enableTrim && timeUs > trimStartTimeUs && 195 timeUs <= trimEndTimeUs) { 196 if (trimStarted == false) { 197 trimOffsetTimeUs = timeUs; 198 } 199 trimStarted = true; 200 } 201 } 202 // Trim can end at any non-sync frame. 203 if (enableTrim && timeUs > trimEndTimeUs) { 204 trimStarted = false; 205 } 206 207 if (!enableTrim || (enableTrim && trimStarted)) { 208 err = muxer->writeSampleData(newBuffer, 209 trackIndexMap.valueFor(trackIndex), 210 timeUs - trimOffsetTimeUs, sampleFlags); 211 } 212 213 extractor->advance(); 214 } 215 } 216 217 muxer->stop(); 218 newBuffer.clear(); 219 trackIndexMap.clear(); 220 221 int64_t elapsedTimeUs = ALooper::GetNowUs() - muxerStartTimeUs; 222 fprintf(stderr, "SUCCESS: muxer generate the video in %" PRId64 " ms\n", 223 elapsedTimeUs / 1000); 224 225 return 0; 226} 227 228int main(int argc, char **argv) { 229 const char *me = argv[0]; 230 231 bool useAudio = false; 232 bool useVideo = false; 233 char *outputFileName = NULL; 234 int trimStartTimeMs = -1; 235 int trimEndTimeMs = -1; 236 int rotationDegrees = 0; 237 // When trimStartTimeMs and trimEndTimeMs seems valid, we turn this switch 238 // to true. 239 bool enableTrim = false; 240 241 int res; 242 while ((res = getopt(argc, argv, "h?avo:s:e:r:")) >= 0) { 243 switch (res) { 244 case 'a': 245 { 246 useAudio = true; 247 break; 248 } 249 250 case 'v': 251 { 252 useVideo = true; 253 break; 254 } 255 256 case 'o': 257 { 258 outputFileName = optarg; 259 break; 260 } 261 262 case 's': 263 { 264 trimStartTimeMs = atoi(optarg); 265 break; 266 } 267 268 case 'e': 269 { 270 trimEndTimeMs = atoi(optarg); 271 break; 272 } 273 274 case 'r': 275 { 276 rotationDegrees = atoi(optarg); 277 break; 278 } 279 280 case '?': 281 case 'h': 282 default: 283 { 284 usage(me); 285 } 286 } 287 } 288 289 argc -= optind; 290 argv += optind; 291 292 if (argc != 1) { 293 usage(me); 294 } 295 296 if (trimStartTimeMs < 0 || trimEndTimeMs < 0) { 297 // If no input on either 's' or 'e', or they are obviously wrong input, 298 // then turn off trimming. 299 ALOGV("Trimming is disabled, copying the whole length video."); 300 enableTrim = false; 301 } else if (trimStartTimeMs > trimEndTimeMs) { 302 fprintf(stderr, "ERROR: start time is bigger\n"); 303 return 1; 304 } else { 305 enableTrim = true; 306 } 307 308 if (!useAudio && !useVideo) { 309 fprintf(stderr, "ERROR: Missing both -a and -v, no track to mux then.\n"); 310 return 1; 311 } 312 ProcessState::self()->startThreadPool(); 313 314 // Make sure setDataSource() works. 315 DataSource::RegisterDefaultSniffers(); 316 317 sp<ALooper> looper = new ALooper; 318 looper->start(); 319 320 int result = muxing(argv[0], useAudio, useVideo, outputFileName, 321 enableTrim, trimStartTimeMs, trimEndTimeMs, rotationDegrees); 322 323 looper->stop(); 324 325 return result; 326} 327