stagefright.cpp revision 777893a928680f09e306b4b9efc1d5cf4479a9da
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 int n = 0; 65 int64_t startTime = getNowUs(); 66 67 long numIterationsLeft = gNumRepetitions; 68 MediaSource::ReadOptions options; 69 70 while (numIterationsLeft-- > 0) { 71 long numFrames = 0; 72 73 MediaBuffer *buffer; 74 75 for (;;) { 76 status_t err = decoder->read(&buffer, &options); 77 options.clearSeekTo(); 78 79 if (err != OK) { 80 CHECK_EQ(buffer, NULL); 81 break; 82 } 83 84 if ((n++ % 16) == 0) { 85 printf("."); 86 fflush(stdout); 87 } 88 89 buffer->release(); 90 buffer = NULL; 91 92 ++numFrames; 93 if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) { 94 break; 95 } 96 97 if (gReproduceBug == 1 && numFrames == 40) { 98 printf("seeking past the end now."); 99 options.setSeekTo(0x7fffffffL); 100 } else if (gReproduceBug == 2 && numFrames == 40) { 101 printf("seeking to 5 secs."); 102 options.setSeekTo(5000000); 103 } 104 } 105 106 printf("$"); 107 fflush(stdout); 108 109 options.setSeekTo(0); 110 } 111 112 decoder->stop(); 113 printf("\n"); 114 115 int64_t delay = getNowUs() - startTime; 116 printf("avg. %.2f fps\n", n * 1E6 / delay); 117 118 printf("decoded a total of %d frame(s).\n", n); 119} 120 121static void usage(const char *me) { 122 fprintf(stderr, "usage: %s\n", me); 123 fprintf(stderr, " -h(elp)\n"); 124 fprintf(stderr, " -a(udio)\n"); 125 fprintf(stderr, " -n repetitions\n"); 126 fprintf(stderr, " -l(ist) components\n"); 127 fprintf(stderr, " -m max-number-of-frames-to-decode in each pass\n"); 128 fprintf(stderr, " -b bug to reproduce\n"); 129 fprintf(stderr, " -p(rofiles) dump decoder profiles supported\n"); 130} 131 132int main(int argc, char **argv) { 133 android::ProcessState::self()->startThreadPool(); 134 135 bool audioOnly = false; 136 bool listComponents = false; 137 bool dumpProfiles = false; 138 gNumRepetitions = 1; 139 gMaxNumFrames = 0; 140 gReproduceBug = -1; 141 142 int res; 143 while ((res = getopt(argc, argv, "han:lm:b:p")) >= 0) { 144 switch (res) { 145 case 'a': 146 { 147 audioOnly = true; 148 break; 149 } 150 151 case 'l': 152 { 153 listComponents = true; 154 break; 155 } 156 157 case 'm': 158 case 'n': 159 case 'b': 160 { 161 char *end; 162 long x = strtol(optarg, &end, 10); 163 164 if (*end != '\0' || end == optarg || x <= 0) { 165 x = 1; 166 } 167 168 if (res == 'n') { 169 gNumRepetitions = x; 170 } else if (res == 'm') { 171 gMaxNumFrames = x; 172 } else { 173 CHECK_EQ(res, 'b'); 174 gReproduceBug = x; 175 } 176 break; 177 } 178 179 case 'p': 180 { 181 dumpProfiles = true; 182 break; 183 } 184 185 case '?': 186 case 'h': 187 default: 188 { 189 usage(argv[0]); 190 exit(1); 191 break; 192 } 193 } 194 } 195 196 argc -= optind; 197 argv += optind; 198 199 if (dumpProfiles) { 200 sp<IServiceManager> sm = defaultServiceManager(); 201 sp<IBinder> binder = sm->getService(String16("media.player")); 202 sp<IMediaPlayerService> service = 203 interface_cast<IMediaPlayerService>(binder); 204 205 CHECK(service.get() != NULL); 206 207 sp<IOMX> omx = service->createOMX(); 208 CHECK(omx.get() != NULL); 209 210 const char *kMimeTypes[] = { 211 MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4, 212 MEDIA_MIMETYPE_VIDEO_H263 213 }; 214 215 for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]); 216 ++k) { 217 printf("type '%s':\n", kMimeTypes[k]); 218 219 Vector<CodecCapabilities> results; 220 CHECK_EQ(QueryCodecs(omx, kMimeTypes[k], 221 true, // queryDecoders 222 &results), OK); 223 224 for (size_t i = 0; i < results.size(); ++i) { 225 printf(" decoder '%s' supports ", 226 results[i].mComponentName.string()); 227 228 if (results[i].mProfileLevels.size() == 0) { 229 printf("NOTHING.\n"); 230 continue; 231 } 232 233 for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) { 234 const CodecProfileLevel &profileLevel = 235 results[i].mProfileLevels[j]; 236 237 printf("%s%ld/%ld", j > 0 ? ", " : "", 238 profileLevel.mProfile, profileLevel.mLevel); 239 } 240 241 printf("\n"); 242 } 243 } 244 } 245 246 if (listComponents) { 247 sp<IServiceManager> sm = defaultServiceManager(); 248 sp<IBinder> binder = sm->getService(String16("media.player")); 249 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 250 251 CHECK(service.get() != NULL); 252 253 sp<IOMX> omx = service->createOMX(); 254 CHECK(omx.get() != NULL); 255 256 List<String8> list; 257 omx->list_nodes(&list); 258 259 for (List<String8>::iterator it = list.begin(); 260 it != list.end(); ++it) { 261 printf("%s\n", (*it).string()); 262 } 263 } 264 265 DataSource::RegisterDefaultSniffers(); 266 267 OMXClient client; 268 status_t err = client.connect(); 269 270 for (int k = 0; k < argc; ++k) { 271 const char *filename = argv[k]; 272 273 sp<DataSource> dataSource; 274 if (!strncasecmp("http://", filename, 7)) { 275 dataSource = new HTTPDataSource(filename); 276 dataSource = new CachingDataSource(dataSource, 64 * 1024, 10); 277 } else { 278 dataSource = new MmapSource(filename); 279 } 280 281 bool isJPEG = false; 282 283 size_t len = strlen(filename); 284 if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) { 285 isJPEG = true; 286 } 287 288 sp<MediaSource> mediaSource; 289 290 if (isJPEG) { 291 mediaSource = new JPEGSource(dataSource); 292 } else { 293 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 294 295 size_t numTracks = extractor->countTracks(); 296 297 sp<MetaData> meta; 298 size_t i; 299 for (i = 0; i < numTracks; ++i) { 300 meta = extractor->getTrackMetaData(i); 301 302 const char *mime; 303 meta->findCString(kKeyMIMEType, &mime); 304 305 if (audioOnly && !strncasecmp(mime, "audio/", 6)) { 306 break; 307 } 308 309 if (!audioOnly && !strncasecmp(mime, "video/", 6)) { 310 break; 311 } 312 } 313 314 mediaSource = extractor->getTrack(i); 315 } 316 317 playSource(&client, mediaSource); 318 } 319 320 client.disconnect(); 321 322 return 0; 323} 324