stagefright.cpp revision dbc03445db2bbf83b64f0c0a5dc62e61408864d7
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/MediaDebug.h> 27#include <media/stagefright/MediaPlayerImpl.h> 28#include <media/stagefright/MediaExtractor.h> 29#include <media/stagefright/MediaSource.h> 30#include <media/stagefright/MetaData.h> 31#include <media/stagefright/MmapSource.h> 32#include <media/stagefright/OMXClient.h> 33#include <media/stagefright/OMXCodec.h> 34#include <media/stagefright/OMXDecoder.h> 35 36#include "JPEGSource.h" 37 38using namespace android; 39 40static long gNumRepetitions; 41 42static int64_t getNowUs() { 43 struct timeval tv; 44 gettimeofday(&tv, NULL); 45 46 return (int64_t)tv.tv_usec + tv.tv_sec * 1000000; 47} 48 49#define USE_OMX_CODEC 1 50 51static void playSource(OMXClient *client, const sp<MediaSource> &source) { 52 sp<MetaData> meta = source->getFormat(); 53 54#if !USE_OMX_CODEC 55 sp<OMXDecoder> decoder = OMXDecoder::Create( 56 client, meta, false /* createEncoder */, source); 57#else 58 sp<OMXCodec> decoder = OMXCodec::Create( 59 client->interface(), meta, false /* createEncoder */, source); 60#endif 61 62 if (decoder == NULL) { 63 return; 64 } 65 66 decoder->start(); 67 68 int n = 0; 69 int64_t startTime = getNowUs(); 70 71 long numIterationsLeft = gNumRepetitions; 72 MediaSource::ReadOptions options; 73 74 while (numIterationsLeft-- > 0) { 75 MediaBuffer *buffer; 76 77 for (;;) { 78 status_t err = decoder->read(&buffer, &options); 79 options.clearSeekTo(); 80 81 if (err != OK) { 82 CHECK_EQ(buffer, NULL); 83 break; 84 } 85 86 if ((n++ % 16) == 0) { 87 printf("."); 88 fflush(stdout); 89 } 90 91 buffer->release(); 92 buffer = NULL; 93 } 94 95 printf("$"); 96 fflush(stdout); 97 98 options.setSeekTo(0); 99 } 100 101 decoder->stop(); 102 printf("\n"); 103 104 int64_t delay = getNowUs() - startTime; 105 printf("avg. %.2f fps\n", n * 1E6 / delay); 106 107 printf("decoded a total of %d frame(s).\n", n); 108} 109 110static void usage(const char *me) { 111 fprintf(stderr, "usage: %s\n", me); 112 fprintf(stderr, " -h(elp)\n"); 113 fprintf(stderr, " -a(udio)\n"); 114 fprintf(stderr, " -n repetitions\n"); 115 fprintf(stderr, " -l(ist) components\n"); 116} 117 118int main(int argc, char **argv) { 119 android::ProcessState::self()->startThreadPool(); 120 121 bool audioOnly = false; 122 bool listComponents = false; 123 gNumRepetitions = 1; 124 125 int res; 126 while ((res = getopt(argc, argv, "han:l")) >= 0) { 127 switch (res) { 128 case 'a': 129 { 130 audioOnly = true; 131 break; 132 } 133 134 case 'l': 135 { 136 listComponents = true; 137 break; 138 } 139 140 case 'n': 141 { 142 char *end; 143 long x = strtol(optarg, &end, 10); 144 145 if (*end != '\0' || end == optarg || x <= 0) { 146 x = 1; 147 } 148 149 gNumRepetitions = x; 150 break; 151 } 152 153 case '?': 154 case 'h': 155 default: 156 { 157 usage(argv[0]); 158 exit(1); 159 break; 160 } 161 } 162 } 163 164 argc -= optind; 165 argv += optind; 166 167 if (listComponents) { 168 sp<IServiceManager> sm = defaultServiceManager(); 169 sp<IBinder> binder = sm->getService(String16("media.player")); 170 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 171 172 CHECK(service.get() != NULL); 173 174 sp<IOMX> omx = service->createOMX(); 175 CHECK(omx.get() != NULL); 176 177 List<String8> list; 178 omx->list_nodes(&list); 179 180 for (List<String8>::iterator it = list.begin(); 181 it != list.end(); ++it) { 182 printf("%s\n", (*it).string()); 183 } 184 } 185 186 DataSource::RegisterDefaultSniffers(); 187 188 OMXClient client; 189 status_t err = client.connect(); 190 191 for (int k = 0; k < argc; ++k) { 192 const char *filename = argv[k]; 193 194 sp<MmapSource> dataSource = new MmapSource(filename); 195 196 bool isJPEG = false; 197 198 size_t len = strlen(filename); 199 if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) { 200 isJPEG = true; 201 } 202 203 sp<MediaSource> mediaSource; 204 205 if (isJPEG) { 206 mediaSource = new JPEGSource(dataSource); 207 } else { 208 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 209 210 size_t numTracks = extractor->countTracks(); 211 212 sp<MetaData> meta; 213 size_t i; 214 for (i = 0; i < numTracks; ++i) { 215 meta = extractor->getTrackMetaData(i); 216 217 const char *mime; 218 meta->findCString(kKeyMIMEType, &mime); 219 220 if (audioOnly && !strncasecmp(mime, "audio/", 6)) { 221 break; 222 } 223 224 if (!audioOnly && !strncasecmp(mime, "video/", 6)) { 225 break; 226 } 227 } 228 229 mediaSource = extractor->getTrack(i); 230 } 231 232 playSource(&client, mediaSource); 233 } 234 235 client.disconnect(); 236 237 return 0; 238} 239