stagefright.cpp revision 318ad9c1d9d6515026dfc2c021359d27decaa7a1
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <sys/time.h> 18 19#include <stdlib.h> 20#include <string.h> 21#include <unistd.h> 22 23#include <binder/IServiceManager.h> 24#include <binder/ProcessState.h> 25#include <media/IMediaPlayerService.h> 26#include <media/stagefright/CachingDataSource.h> 27#include <media/stagefright/HTTPDataSource.h> 28#include <media/stagefright/JPEGSource.h> 29#include <media/stagefright/MediaDebug.h> 30#include <media/stagefright/MediaDefs.h> 31#include <media/stagefright/MediaPlayerImpl.h> 32#include <media/stagefright/MediaExtractor.h> 33#include <media/stagefright/MediaSource.h> 34#include <media/stagefright/MetaData.h> 35#include <media/stagefright/MmapSource.h> 36#include <media/stagefright/OMXClient.h> 37#include <media/stagefright/OMXCodec.h> 38 39using namespace android; 40 41static long gNumRepetitions; 42static long gMaxNumFrames; // 0 means decode all available. 43static long gReproduceBug; // if not -1. 44 45static int64_t getNowUs() { 46 struct timeval tv; 47 gettimeofday(&tv, NULL); 48 49 return (int64_t)tv.tv_usec + tv.tv_sec * 1000000; 50} 51 52static void playSource(OMXClient *client, const sp<MediaSource> &source) { 53 sp<MetaData> meta = source->getFormat(); 54 55 sp<OMXCodec> decoder = OMXCodec::Create( 56 client->interface(), meta, false /* createEncoder */, source); 57 58 if (decoder == NULL) { 59 return; 60 } 61 62 decoder->start(); 63 64 if (gReproduceBug == 3) { 65 status_t err; 66 MediaBuffer *buffer; 67 MediaSource::ReadOptions options; 68 int64_t seekTimeUs = -1; 69 for (;;) { 70 err = decoder->read(&buffer, &options); 71 options.clearSeekTo(); 72 73 bool shouldSeek = false; 74 if (err == INFO_FORMAT_CHANGED) { 75 CHECK_EQ(buffer, NULL); 76 77 printf("format changed.\n"); 78 continue; 79 } else if (err != OK) { 80 printf("reached EOF.\n"); 81 82 shouldSeek = true; 83 } else { 84 int64_t timestampUs; 85 CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); 86 87 bool failed = false; 88 if (seekTimeUs >= 0) { 89 int64_t diff = timestampUs - seekTimeUs; 90 91 if (diff > 500000) { 92 printf("ERROR: "); 93 failed = true; 94 } 95 } 96 97 printf("buffer has timestamp %lld us (%.2f secs)\n", 98 timestampUs, timestampUs / 1E6); 99 100 buffer->release(); 101 buffer = NULL; 102 103 if (failed) { 104 break; 105 } 106 107 shouldSeek = ((double)rand() / RAND_MAX) < 0.1; 108 shouldSeek = false; 109 } 110 111 seekTimeUs = -1; 112 113 if (shouldSeek) { 114 seekTimeUs = (rand() * 30E6) / RAND_MAX; 115 options.setSeekTo(seekTimeUs); 116 117 printf("seeking to %lld us (%.2f secs)\n", 118 seekTimeUs, seekTimeUs / 1E6); 119 } 120 } 121 122 decoder->stop(); 123 124 return; 125 } 126 127 int n = 0; 128 int64_t startTime = getNowUs(); 129 130 long numIterationsLeft = gNumRepetitions; 131 MediaSource::ReadOptions options; 132 133 while (numIterationsLeft-- > 0) { 134 long numFrames = 0; 135 136 MediaBuffer *buffer; 137 138 for (;;) { 139 status_t err = decoder->read(&buffer, &options); 140 options.clearSeekTo(); 141 142 if (err != OK) { 143 CHECK_EQ(buffer, NULL); 144 145 if (err == INFO_FORMAT_CHANGED) { 146 printf("format changed.\n"); 147 continue; 148 } 149 150 break; 151 } 152 153 if ((n++ % 16) == 0) { 154 printf("."); 155 fflush(stdout); 156 } 157 158 buffer->release(); 159 buffer = NULL; 160 161 ++numFrames; 162 if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) { 163 break; 164 } 165 166 if (gReproduceBug == 1 && numFrames == 40) { 167 printf("seeking past the end now."); 168 options.setSeekTo(0x7fffffffL); 169 } else if (gReproduceBug == 2 && numFrames == 40) { 170 printf("seeking to 5 secs."); 171 options.setSeekTo(5000000); 172 } 173 } 174 175 printf("$"); 176 fflush(stdout); 177 178 options.setSeekTo(0); 179 } 180 181 decoder->stop(); 182 printf("\n"); 183 184 int64_t delay = getNowUs() - startTime; 185 printf("avg. %.2f fps\n", n * 1E6 / delay); 186 187 printf("decoded a total of %d frame(s).\n", n); 188} 189 190static void usage(const char *me) { 191 fprintf(stderr, "usage: %s\n", me); 192 fprintf(stderr, " -h(elp)\n"); 193 fprintf(stderr, " -a(udio)\n"); 194 fprintf(stderr, " -n repetitions\n"); 195 fprintf(stderr, " -l(ist) components\n"); 196 fprintf(stderr, " -m max-number-of-frames-to-decode in each pass\n"); 197 fprintf(stderr, " -b bug to reproduce\n"); 198 fprintf(stderr, " -p(rofiles) dump decoder profiles supported\n"); 199} 200 201int main(int argc, char **argv) { 202 android::ProcessState::self()->startThreadPool(); 203 204 bool audioOnly = false; 205 bool listComponents = false; 206 bool dumpProfiles = false; 207 gNumRepetitions = 1; 208 gMaxNumFrames = 0; 209 gReproduceBug = -1; 210 211 int res; 212 while ((res = getopt(argc, argv, "han:lm:b:p")) >= 0) { 213 switch (res) { 214 case 'a': 215 { 216 audioOnly = true; 217 break; 218 } 219 220 case 'l': 221 { 222 listComponents = true; 223 break; 224 } 225 226 case 'm': 227 case 'n': 228 case 'b': 229 { 230 char *end; 231 long x = strtol(optarg, &end, 10); 232 233 if (*end != '\0' || end == optarg || x <= 0) { 234 x = 1; 235 } 236 237 if (res == 'n') { 238 gNumRepetitions = x; 239 } else if (res == 'm') { 240 gMaxNumFrames = x; 241 } else { 242 CHECK_EQ(res, 'b'); 243 gReproduceBug = x; 244 } 245 break; 246 } 247 248 case 'p': 249 { 250 dumpProfiles = true; 251 break; 252 } 253 254 case '?': 255 case 'h': 256 default: 257 { 258 usage(argv[0]); 259 exit(1); 260 break; 261 } 262 } 263 } 264 265 argc -= optind; 266 argv += optind; 267 268 if (dumpProfiles) { 269 sp<IServiceManager> sm = defaultServiceManager(); 270 sp<IBinder> binder = sm->getService(String16("media.player")); 271 sp<IMediaPlayerService> service = 272 interface_cast<IMediaPlayerService>(binder); 273 274 CHECK(service.get() != NULL); 275 276 sp<IOMX> omx = service->getOMX(); 277 CHECK(omx.get() != NULL); 278 279 const char *kMimeTypes[] = { 280 MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4, 281 MEDIA_MIMETYPE_VIDEO_H263 282 }; 283 284 for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]); 285 ++k) { 286 printf("type '%s':\n", kMimeTypes[k]); 287 288 Vector<CodecCapabilities> results; 289 CHECK_EQ(QueryCodecs(omx, kMimeTypes[k], 290 true, // queryDecoders 291 &results), OK); 292 293 for (size_t i = 0; i < results.size(); ++i) { 294 printf(" decoder '%s' supports ", 295 results[i].mComponentName.string()); 296 297 if (results[i].mProfileLevels.size() == 0) { 298 printf("NOTHING.\n"); 299 continue; 300 } 301 302 for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) { 303 const CodecProfileLevel &profileLevel = 304 results[i].mProfileLevels[j]; 305 306 printf("%s%ld/%ld", j > 0 ? ", " : "", 307 profileLevel.mProfile, profileLevel.mLevel); 308 } 309 310 printf("\n"); 311 } 312 } 313 } 314 315 if (listComponents) { 316 sp<IServiceManager> sm = defaultServiceManager(); 317 sp<IBinder> binder = sm->getService(String16("media.player")); 318 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 319 320 CHECK(service.get() != NULL); 321 322 sp<IOMX> omx = service->getOMX(); 323 CHECK(omx.get() != NULL); 324 325 List<String8> list; 326 omx->listNodes(&list); 327 328 for (List<String8>::iterator it = list.begin(); 329 it != list.end(); ++it) { 330 printf("%s\n", (*it).string()); 331 } 332 } 333 334 DataSource::RegisterDefaultSniffers(); 335 336 OMXClient client; 337 status_t err = client.connect(); 338 339 for (int k = 0; k < argc; ++k) { 340 const char *filename = argv[k]; 341 342 sp<DataSource> dataSource; 343 if (!strncasecmp("http://", filename, 7)) { 344 dataSource = new HTTPDataSource(filename); 345 dataSource = new CachingDataSource(dataSource, 64 * 1024, 10); 346 } else { 347 dataSource = new MmapSource(filename); 348 } 349 350 bool isJPEG = false; 351 352 size_t len = strlen(filename); 353 if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) { 354 isJPEG = true; 355 } 356 357 sp<MediaSource> mediaSource; 358 359 if (isJPEG) { 360 mediaSource = new JPEGSource(dataSource); 361 } else { 362 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 363 364 size_t numTracks = extractor->countTracks(); 365 366 sp<MetaData> meta; 367 size_t i; 368 for (i = 0; i < numTracks; ++i) { 369 meta = extractor->getTrackMetaData(i); 370 371 const char *mime; 372 meta->findCString(kKeyMIMEType, &mime); 373 374 if (audioOnly && !strncasecmp(mime, "audio/", 6)) { 375 break; 376 } 377 378 if (!audioOnly && !strncasecmp(mime, "video/", 6)) { 379 break; 380 } 381 } 382 383 mediaSource = extractor->getTrack(i); 384 } 385 386 playSource(&client, mediaSource); 387 } 388 389 client.disconnect(); 390 391 return 0; 392} 393