muxer.cpp revision c6ac859f5a82ea8642bc6351a45508a15f224f32
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 android::sp<android::ALooper> &looper,
57        const char *path,
58        bool useAudio,
59        bool useVideo,
60        const char *outputFileName,
61        bool enableTrim,
62        int trimStartTimeMs,
63        int trimEndTimeMs,
64        int rotationDegrees) {
65    sp<NuMediaExtractor> extractor = new NuMediaExtractor;
66    if (extractor->setDataSource(NULL /* httpService */, path) != OK) {
67        fprintf(stderr, "unable to instantiate extractor. %s\n", path);
68        return 1;
69    }
70
71    if (outputFileName == NULL) {
72        outputFileName = "/sdcard/muxeroutput.mp4";
73    }
74
75    ALOGV("input file %s, output file %s", path, outputFileName);
76    ALOGV("useAudio %d, useVideo %d", useAudio, useVideo);
77
78    int fd = open(outputFileName, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
79
80    if (fd < 0) {
81        ALOGE("couldn't open file");
82        return fd;
83    }
84    sp<MediaMuxer> muxer = new MediaMuxer(fd,
85                                          MediaMuxer::OUTPUT_FORMAT_MPEG_4);
86    close(fd);
87
88    size_t trackCount = extractor->countTracks();
89    // Map the extractor's track index to the muxer's track index.
90    KeyedVector<size_t, ssize_t> trackIndexMap;
91    size_t bufferSize = 1 * 1024 * 1024;  // default buffer size is 1MB.
92
93    bool haveAudio = false;
94    bool haveVideo = false;
95
96    int64_t trimStartTimeUs = trimStartTimeMs * 1000;
97    int64_t trimEndTimeUs = trimEndTimeMs * 1000;
98    bool trimStarted = false;
99    int64_t trimOffsetTimeUs = 0;
100
101    for (size_t i = 0; i < trackCount; ++i) {
102        sp<AMessage> format;
103        status_t err = extractor->getTrackFormat(i, &format);
104        CHECK_EQ(err, (status_t)OK);
105        ALOGV("extractor getTrackFormat: %s", format->debugString().c_str());
106
107        AString mime;
108        CHECK(format->findString("mime", &mime));
109
110        bool isAudio = !strncasecmp(mime.c_str(), "audio/", 6);
111        bool isVideo = !strncasecmp(mime.c_str(), "video/", 6);
112
113        if (useAudio && !haveAudio && isAudio) {
114            haveAudio = true;
115        } else if (useVideo && !haveVideo && isVideo) {
116            haveVideo = true;
117        } else {
118            continue;
119        }
120
121        if (isVideo) {
122            int width , height;
123            CHECK(format->findInt32("width", &width));
124            CHECK(format->findInt32("height", &height));
125            bufferSize = width * height * 4;  // Assuming it is maximally 4BPP
126        }
127
128        int64_t duration;
129        CHECK(format->findInt64("durationUs", &duration));
130
131        // Since we got the duration now, correct the start time.
132        if (enableTrim) {
133            if (trimStartTimeUs > duration) {
134                fprintf(stderr, "Warning: trimStartTimeUs > duration,"
135                                " reset to 0\n");
136                trimStartTimeUs = 0;
137            }
138        }
139
140        ALOGV("selecting track %d", i);
141
142        err = extractor->selectTrack(i);
143        CHECK_EQ(err, (status_t)OK);
144
145        ssize_t newTrackIndex = muxer->addTrack(format);
146        CHECK_GE(newTrackIndex, 0);
147        trackIndexMap.add(i, newTrackIndex);
148    }
149
150    int64_t muxerStartTimeUs = ALooper::GetNowUs();
151
152    bool sawInputEOS = false;
153
154    size_t trackIndex = -1;
155    sp<ABuffer> newBuffer = new ABuffer(bufferSize);
156
157    muxer->setOrientationHint(rotationDegrees);
158    muxer->start();
159
160    while (!sawInputEOS) {
161        status_t err = extractor->getSampleTrackIndex(&trackIndex);
162        if (err != OK) {
163            ALOGV("saw input eos, err %d", err);
164            sawInputEOS = true;
165            break;
166        } else {
167            err = extractor->readSampleData(newBuffer);
168            CHECK_EQ(err, (status_t)OK);
169
170            int64_t timeUs;
171            err = extractor->getSampleTime(&timeUs);
172            CHECK_EQ(err, (status_t)OK);
173
174            sp<MetaData> meta;
175            err = extractor->getSampleMeta(&meta);
176            CHECK_EQ(err, (status_t)OK);
177
178            uint32_t sampleFlags = 0;
179            int32_t val;
180            if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
181                // We only support BUFFER_FLAG_SYNCFRAME in the flag for now.
182                sampleFlags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
183
184                // We turn on trimming at the sync frame.
185                if (enableTrim && timeUs > trimStartTimeUs &&
186                    timeUs <= trimEndTimeUs) {
187                    if (trimStarted == false) {
188                        trimOffsetTimeUs = timeUs;
189                    }
190                    trimStarted = true;
191                }
192            }
193            // Trim can end at any non-sync frame.
194            if (enableTrim && timeUs > trimEndTimeUs) {
195                trimStarted = false;
196            }
197
198            if (!enableTrim || (enableTrim && trimStarted)) {
199                err = muxer->writeSampleData(newBuffer,
200                                             trackIndexMap.valueFor(trackIndex),
201                                             timeUs - trimOffsetTimeUs, sampleFlags);
202            }
203
204            extractor->advance();
205        }
206    }
207
208    muxer->stop();
209    newBuffer.clear();
210    trackIndexMap.clear();
211
212    int64_t elapsedTimeUs = ALooper::GetNowUs() - muxerStartTimeUs;
213    fprintf(stderr, "SUCCESS: muxer generate the video in %" PRId64 " ms\n",
214            elapsedTimeUs / 1000);
215
216    return 0;
217}
218
219int main(int argc, char **argv) {
220    const char *me = argv[0];
221
222    bool useAudio = false;
223    bool useVideo = false;
224    char *outputFileName = NULL;
225    int trimStartTimeMs = -1;
226    int trimEndTimeMs = -1;
227    int rotationDegrees = 0;
228    // When trimStartTimeMs and trimEndTimeMs seems valid, we turn this switch
229    // to true.
230    bool enableTrim = false;
231
232    int res;
233    while ((res = getopt(argc, argv, "h?avo:s:e:r:")) >= 0) {
234        switch (res) {
235            case 'a':
236            {
237                useAudio = true;
238                break;
239            }
240
241            case 'v':
242            {
243                useVideo = true;
244                break;
245            }
246
247            case 'o':
248            {
249                outputFileName = optarg;
250                break;
251            }
252
253            case 's':
254            {
255                trimStartTimeMs = atoi(optarg);
256                break;
257            }
258
259            case 'e':
260            {
261                trimEndTimeMs = atoi(optarg);
262                break;
263            }
264
265            case 'r':
266            {
267                rotationDegrees = atoi(optarg);
268                break;
269            }
270
271            case '?':
272            case 'h':
273            default:
274            {
275                usage(me);
276            }
277        }
278    }
279
280    argc -= optind;
281    argv += optind;
282
283    if (argc != 1) {
284        usage(me);
285    }
286
287    if (trimStartTimeMs < 0 || trimEndTimeMs < 0) {
288        // If no input on either 's' or 'e', or they are obviously wrong input,
289        // then turn off trimming.
290        ALOGV("Trimming is disabled, copying the whole length video.");
291        enableTrim = false;
292    } else if (trimStartTimeMs > trimEndTimeMs) {
293        fprintf(stderr, "ERROR: start time is bigger\n");
294        return 1;
295    } else {
296        enableTrim = true;
297    }
298
299    if (!useAudio && !useVideo) {
300        fprintf(stderr, "ERROR: Missing both -a and -v, no track to mux then.\n");
301        return 1;
302    }
303    ProcessState::self()->startThreadPool();
304
305    // Make sure setDataSource() works.
306    DataSource::RegisterDefaultSniffers();
307
308    sp<ALooper> looper = new ALooper;
309    looper->start();
310
311    int result = muxing(looper, argv[0], useAudio, useVideo, outputFileName,
312                        enableTrim, trimStartTimeMs, trimEndTimeMs, rotationDegrees);
313
314    looper->stop();
315
316    return result;
317}
318