stagefright.cpp revision e0f0b08aa692d15f3bfa19e10abfc84803c099b4
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/MediaDebug.h> 29#include <media/stagefright/MediaPlayerImpl.h> 30#include <media/stagefright/MediaExtractor.h> 31#include <media/stagefright/MediaSource.h> 32#include <media/stagefright/MetaData.h> 33#include <media/stagefright/MmapSource.h> 34#include <media/stagefright/OMXClient.h> 35#include <media/stagefright/OMXCodec.h> 36 37#include "JPEGSource.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(LONG_MAX); 100 } 101 } 102 103 printf("$"); 104 fflush(stdout); 105 106 options.setSeekTo(0); 107 } 108 109 decoder->stop(); 110 printf("\n"); 111 112 int64_t delay = getNowUs() - startTime; 113 printf("avg. %.2f fps\n", n * 1E6 / delay); 114 115 printf("decoded a total of %d frame(s).\n", n); 116} 117 118static void usage(const char *me) { 119 fprintf(stderr, "usage: %s\n", me); 120 fprintf(stderr, " -h(elp)\n"); 121 fprintf(stderr, " -a(udio)\n"); 122 fprintf(stderr, " -n repetitions\n"); 123 fprintf(stderr, " -l(ist) components\n"); 124 fprintf(stderr, " -m max-number-of-frames-to-decode in each pass\n"); 125 fprintf(stderr, " -b bug to reproduce\n"); 126} 127 128int main(int argc, char **argv) { 129 android::ProcessState::self()->startThreadPool(); 130 131 bool audioOnly = false; 132 bool listComponents = false; 133 gNumRepetitions = 1; 134 gMaxNumFrames = 0; 135 gReproduceBug = -1; 136 137 int res; 138 while ((res = getopt(argc, argv, "han:lm:b:")) >= 0) { 139 switch (res) { 140 case 'a': 141 { 142 audioOnly = true; 143 break; 144 } 145 146 case 'l': 147 { 148 listComponents = true; 149 break; 150 } 151 152 case 'm': 153 case 'n': 154 case 'b': 155 { 156 char *end; 157 long x = strtol(optarg, &end, 10); 158 159 if (*end != '\0' || end == optarg || x <= 0) { 160 x = 1; 161 } 162 163 if (res == 'n') { 164 gNumRepetitions = x; 165 } else if (res == 'm') { 166 gMaxNumFrames = x; 167 } else { 168 CHECK_EQ(res, 'b'); 169 gReproduceBug = x; 170 } 171 break; 172 } 173 174 case '?': 175 case 'h': 176 default: 177 { 178 usage(argv[0]); 179 exit(1); 180 break; 181 } 182 } 183 } 184 185 argc -= optind; 186 argv += optind; 187 188 if (listComponents) { 189 sp<IServiceManager> sm = defaultServiceManager(); 190 sp<IBinder> binder = sm->getService(String16("media.player")); 191 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 192 193 CHECK(service.get() != NULL); 194 195 sp<IOMX> omx = service->createOMX(); 196 CHECK(omx.get() != NULL); 197 198 List<String8> list; 199 omx->list_nodes(&list); 200 201 for (List<String8>::iterator it = list.begin(); 202 it != list.end(); ++it) { 203 printf("%s\n", (*it).string()); 204 } 205 } 206 207 DataSource::RegisterDefaultSniffers(); 208 209 OMXClient client; 210 status_t err = client.connect(); 211 212 for (int k = 0; k < argc; ++k) { 213 const char *filename = argv[k]; 214 215 sp<DataSource> dataSource; 216 if (!strncasecmp("http://", filename, 7)) { 217 dataSource = new HTTPDataSource(filename); 218 dataSource = new CachingDataSource(dataSource, 64 * 1024, 10); 219 } else { 220 dataSource = new MmapSource(filename); 221 } 222 223 bool isJPEG = false; 224 225 size_t len = strlen(filename); 226 if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) { 227 isJPEG = true; 228 } 229 230 sp<MediaSource> mediaSource; 231 232 if (isJPEG) { 233 mediaSource = new JPEGSource(dataSource); 234 } else { 235 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 236 237 size_t numTracks = extractor->countTracks(); 238 239 sp<MetaData> meta; 240 size_t i; 241 for (i = 0; i < numTracks; ++i) { 242 meta = extractor->getTrackMetaData(i); 243 244 const char *mime; 245 meta->findCString(kKeyMIMEType, &mime); 246 247 if (audioOnly && !strncasecmp(mime, "audio/", 6)) { 248 break; 249 } 250 251 if (!audioOnly && !strncasecmp(mime, "video/", 6)) { 252 break; 253 } 254 } 255 256 mediaSource = extractor->getTrack(i); 257 } 258 259 playSource(&client, mediaSource); 260 } 261 262 client.disconnect(); 263 264 return 0; 265} 266