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