Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <unistd.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 22 | |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 23 | #include "SineSource.h" |
| 24 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
| 26 | #include <binder/ProcessState.h> |
| 27 | #include <media/IMediaPlayerService.h> |
Andreas Huber | 4e4173b | 2010-07-22 09:20:13 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/ALooper.h> |
| 29 | #include "include/ARTSPController.h" |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 30 | #include <media/stagefright/AudioPlayer.h> |
Andreas Huber | c066884 | 2010-06-10 11:16:10 -0700 | [diff] [blame] | 31 | #include <media/stagefright/DataSource.h> |
Andreas Huber | 6026a51 | 2009-09-11 07:47:55 -0700 | [diff] [blame] | 32 | #include <media/stagefright/JPEGSource.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 33 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 34 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | c066884 | 2010-06-10 11:16:10 -0700 | [diff] [blame] | 35 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 36 | #include <media/stagefright/MediaExtractor.h> |
| 37 | #include <media/stagefright/MediaSource.h> |
| 38 | #include <media/stagefright/MetaData.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 39 | #include <media/stagefright/OMXClient.h> |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 40 | #include <media/stagefright/OMXCodec.h> |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 41 | #include <media/mediametadataretriever.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 42 | |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 43 | #include <media/stagefright/foundation/hexdump.h> |
| 44 | #include <media/stagefright/MPEG4Writer.h> |
| 45 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 46 | using namespace android; |
| 47 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 48 | static long gNumRepetitions; |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 49 | static long gMaxNumFrames; // 0 means decode all available. |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 50 | static long gReproduceBug; // if not -1. |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 51 | static bool gPreferSoftwareCodec; |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 52 | static bool gPlaybackAudio; |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 53 | static bool gWriteMP4; |
| 54 | static String8 gWriteMP4Filename; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 55 | |
| 56 | static int64_t getNowUs() { |
| 57 | struct timeval tv; |
| 58 | gettimeofday(&tv, NULL); |
| 59 | |
Andreas Huber | d73a81c | 2010-03-16 14:34:17 -0700 | [diff] [blame] | 60 | return (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 63 | static void playSource(OMXClient *client, const sp<MediaSource> &source) { |
| 64 | sp<MetaData> meta = source->getFormat(); |
| 65 | |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 66 | const char *mime; |
| 67 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 68 | |
| 69 | sp<MediaSource> rawSource; |
| 70 | if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) { |
| 71 | rawSource = source; |
| 72 | } else { |
| 73 | rawSource = OMXCodec::Create( |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 74 | client->interface(), meta, false /* createEncoder */, source, |
| 75 | NULL /* matchComponentName */, |
| 76 | gPreferSoftwareCodec ? OMXCodec::kPreferSoftwareCodecs : 0); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 77 | |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 78 | if (rawSource == NULL) { |
| 79 | fprintf(stderr, "Failed to instantiate decoder for '%s'.\n", mime); |
| 80 | return; |
| 81 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Andreas Huber | 1919e5a | 2010-05-20 10:37:06 -0700 | [diff] [blame] | 84 | status_t err = rawSource->start(); |
| 85 | |
| 86 | if (err != OK) { |
| 87 | fprintf(stderr, "rawSource returned error %d (0x%08x)\n", err, err); |
| 88 | return; |
| 89 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 90 | |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 91 | if (gPlaybackAudio) { |
| 92 | AudioPlayer *player = new AudioPlayer(NULL); |
| 93 | player->setSource(rawSource); |
Andreas Huber | 4979601 | 2010-04-21 15:47:15 -0700 | [diff] [blame] | 94 | rawSource.clear(); |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 95 | |
| 96 | player->start(true /* sourceAlreadyStarted */); |
| 97 | |
| 98 | status_t finalStatus; |
| 99 | while (!player->reachedEOS(&finalStatus)) { |
| 100 | usleep(100000ll); |
| 101 | } |
| 102 | |
| 103 | delete player; |
| 104 | player = NULL; |
Andreas Huber | 4979601 | 2010-04-21 15:47:15 -0700 | [diff] [blame] | 105 | |
| 106 | return; |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 107 | } else if (gReproduceBug >= 3 && gReproduceBug <= 5) { |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 108 | int64_t durationUs; |
| 109 | CHECK(meta->findInt64(kKeyDuration, &durationUs)); |
| 110 | |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 111 | status_t err; |
| 112 | MediaBuffer *buffer; |
| 113 | MediaSource::ReadOptions options; |
| 114 | int64_t seekTimeUs = -1; |
| 115 | for (;;) { |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 116 | err = rawSource->read(&buffer, &options); |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 117 | options.clearSeekTo(); |
| 118 | |
| 119 | bool shouldSeek = false; |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 120 | if (err == INFO_FORMAT_CHANGED) { |
| 121 | CHECK_EQ(buffer, NULL); |
| 122 | |
| 123 | printf("format changed.\n"); |
| 124 | continue; |
| 125 | } else if (err != OK) { |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 126 | printf("reached EOF.\n"); |
| 127 | |
| 128 | shouldSeek = true; |
| 129 | } else { |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 130 | int64_t timestampUs; |
| 131 | CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 132 | |
| 133 | bool failed = false; |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 134 | |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 135 | if (seekTimeUs >= 0) { |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 136 | int64_t diff = timestampUs - seekTimeUs; |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 137 | |
Andreas Huber | 11ebbfd | 2009-10-16 15:05:45 -0700 | [diff] [blame] | 138 | if (diff < 0) { |
| 139 | diff = -diff; |
| 140 | } |
| 141 | |
| 142 | if ((gReproduceBug == 4 && diff > 500000) |
| 143 | || (gReproduceBug == 5 && timestampUs < 0)) { |
| 144 | printf("wanted: %.2f secs, got: %.2f secs\n", |
| 145 | seekTimeUs / 1E6, timestampUs / 1E6); |
| 146 | |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 147 | printf("ERROR: "); |
| 148 | failed = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | printf("buffer has timestamp %lld us (%.2f secs)\n", |
Andreas Huber | fa8de75 | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 153 | timestampUs, timestampUs / 1E6); |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 154 | |
| 155 | buffer->release(); |
| 156 | buffer = NULL; |
| 157 | |
| 158 | if (failed) { |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | shouldSeek = ((double)rand() / RAND_MAX) < 0.1; |
Andreas Huber | 11ebbfd | 2009-10-16 15:05:45 -0700 | [diff] [blame] | 163 | |
| 164 | if (gReproduceBug == 3) { |
| 165 | shouldSeek = false; |
| 166 | } |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | seekTimeUs = -1; |
| 170 | |
| 171 | if (shouldSeek) { |
Andreas Huber | 11ebbfd | 2009-10-16 15:05:45 -0700 | [diff] [blame] | 172 | seekTimeUs = (rand() * (float)durationUs) / RAND_MAX; |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 173 | options.setSeekTo(seekTimeUs); |
| 174 | |
| 175 | printf("seeking to %lld us (%.2f secs)\n", |
| 176 | seekTimeUs, seekTimeUs / 1E6); |
| 177 | } |
| 178 | } |
| 179 | |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 180 | rawSource->stop(); |
Andreas Huber | 1a77b68e | 2009-09-17 11:16:52 -0700 | [diff] [blame] | 181 | |
| 182 | return; |
| 183 | } |
| 184 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 185 | int n = 0; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 186 | int64_t startTime = getNowUs(); |
| 187 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 188 | long numIterationsLeft = gNumRepetitions; |
| 189 | MediaSource::ReadOptions options; |
Andreas Huber | 4f5e602 | 2009-08-19 09:29:34 -0700 | [diff] [blame] | 190 | |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 191 | int64_t sumDecodeUs = 0; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 192 | int64_t totalBytes = 0; |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 193 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 194 | while (numIterationsLeft-- > 0) { |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 195 | long numFrames = 0; |
| 196 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 197 | MediaBuffer *buffer; |
| 198 | |
| 199 | for (;;) { |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 200 | int64_t startDecodeUs = getNowUs(); |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 201 | status_t err = rawSource->read(&buffer, &options); |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 202 | int64_t delayDecodeUs = getNowUs() - startDecodeUs; |
| 203 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 204 | options.clearSeekTo(); |
| 205 | |
| 206 | if (err != OK) { |
| 207 | CHECK_EQ(buffer, NULL); |
Andreas Huber | cfd5557 | 2009-10-09 14:11:28 -0700 | [diff] [blame] | 208 | |
| 209 | if (err == INFO_FORMAT_CHANGED) { |
| 210 | printf("format changed.\n"); |
| 211 | continue; |
| 212 | } |
| 213 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | |
Andreas Huber | f0d0424 | 2009-12-10 12:21:57 -0800 | [diff] [blame] | 217 | if (buffer->range_length() > 0 && (n++ % 16) == 0) { |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 218 | printf("."); |
| 219 | fflush(stdout); |
| 220 | } |
| 221 | |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 222 | sumDecodeUs += delayDecodeUs; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 223 | totalBytes += buffer->range_length(); |
Andreas Huber | 6c9bf1c | 2010-02-17 14:57:28 -0800 | [diff] [blame] | 224 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 225 | buffer->release(); |
| 226 | buffer = NULL; |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 227 | |
| 228 | ++numFrames; |
| 229 | if (gMaxNumFrames > 0 && numFrames == gMaxNumFrames) { |
| 230 | break; |
| 231 | } |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 232 | |
| 233 | if (gReproduceBug == 1 && numFrames == 40) { |
| 234 | printf("seeking past the end now."); |
Andreas Huber | a84b0e0 | 2009-08-28 08:40:00 -0700 | [diff] [blame] | 235 | options.setSeekTo(0x7fffffffL); |
Andreas Huber | 54ee784 | 2009-09-03 16:09:33 -0700 | [diff] [blame] | 236 | } else if (gReproduceBug == 2 && numFrames == 40) { |
| 237 | printf("seeking to 5 secs."); |
| 238 | options.setSeekTo(5000000); |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 239 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 242 | printf("$"); |
| 243 | fflush(stdout); |
| 244 | |
| 245 | options.setSeekTo(0); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 246 | } |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 247 | |
Andreas Huber | 6f6bf3d | 2009-11-03 15:59:13 -0800 | [diff] [blame] | 248 | rawSource->stop(); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 249 | printf("\n"); |
| 250 | |
| 251 | int64_t delay = getNowUs() - startTime; |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 252 | if (!strncasecmp("video/", mime, 6)) { |
| 253 | printf("avg. %.2f fps\n", n * 1E6 / delay); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 254 | |
Andreas Huber | a4357ad | 2010-04-02 12:49:54 -0700 | [diff] [blame] | 255 | printf("avg. time to decode one buffer %.2f usecs\n", |
| 256 | (double)sumDecodeUs / n); |
| 257 | |
| 258 | printf("decoded a total of %d frame(s).\n", n); |
| 259 | } else if (!strncasecmp("audio/", mime, 6)) { |
| 260 | // Frame count makes less sense for audio, as the output buffer |
| 261 | // sizes may be different across decoders. |
| 262 | printf("avg. %.2f KB/sec\n", totalBytes / 1024 * 1E6 / delay); |
| 263 | |
| 264 | printf("decoded a total of %lld bytes\n", totalBytes); |
| 265 | } |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 268 | static void writeSourceToMP4(const sp<MediaSource> &source) { |
| 269 | sp<MPEG4Writer> writer = |
| 270 | new MPEG4Writer(gWriteMP4Filename.string()); |
| 271 | |
| 272 | CHECK_EQ(writer->addSource(source), OK); |
| 273 | |
| 274 | sp<MetaData> params = new MetaData; |
| 275 | CHECK_EQ(writer->start(), OK); |
| 276 | |
| 277 | while (!writer->reachedEOS()) { |
| 278 | usleep(100000); |
| 279 | } |
| 280 | writer->stop(); |
| 281 | } |
| 282 | |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 283 | static void performSeekTest(const sp<MediaSource> &source) { |
| 284 | CHECK_EQ(OK, source->start()); |
| 285 | |
| 286 | int64_t durationUs; |
| 287 | CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs)); |
| 288 | |
| 289 | for (int64_t seekTimeUs = 0; seekTimeUs <= durationUs; |
| 290 | seekTimeUs += 60000ll) { |
| 291 | MediaSource::ReadOptions options; |
| 292 | options.setSeekTo( |
| 293 | seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC); |
| 294 | |
| 295 | MediaBuffer *buffer; |
| 296 | status_t err; |
| 297 | for (;;) { |
| 298 | err = source->read(&buffer, &options); |
| 299 | |
| 300 | options.clearSeekTo(); |
| 301 | |
| 302 | if (err == INFO_FORMAT_CHANGED) { |
| 303 | CHECK(buffer == NULL); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (err != OK) { |
| 308 | CHECK(buffer == NULL); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | if (buffer->range_length() > 0) { |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | CHECK(buffer != NULL); |
| 317 | |
| 318 | buffer->release(); |
| 319 | buffer = NULL; |
| 320 | } |
| 321 | |
| 322 | if (err == OK) { |
| 323 | int64_t timeUs; |
| 324 | CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 325 | |
| 326 | printf("%lld\t%lld\t%lld\n", seekTimeUs, timeUs, seekTimeUs - timeUs); |
| 327 | |
| 328 | buffer->release(); |
| 329 | buffer = NULL; |
| 330 | } else { |
| 331 | printf("ERROR\n"); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | CHECK_EQ(OK, source->stop()); |
| 337 | } |
| 338 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 339 | static void usage(const char *me) { |
| 340 | fprintf(stderr, "usage: %s\n", me); |
| 341 | fprintf(stderr, " -h(elp)\n"); |
| 342 | fprintf(stderr, " -a(udio)\n"); |
| 343 | fprintf(stderr, " -n repetitions\n"); |
| 344 | fprintf(stderr, " -l(ist) components\n"); |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 345 | fprintf(stderr, " -m max-number-of-frames-to-decode in each pass\n"); |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 346 | fprintf(stderr, " -b bug to reproduce\n"); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 347 | fprintf(stderr, " -p(rofiles) dump decoder profiles supported\n"); |
Andreas Huber | edfeb1a | 2010-03-10 11:07:12 -0800 | [diff] [blame] | 348 | fprintf(stderr, " -t(humbnail) extract video thumbnail or album art\n"); |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 349 | fprintf(stderr, " -s(oftware) prefer software codec\n"); |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 350 | fprintf(stderr, " -o playback audio\n"); |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 351 | fprintf(stderr, " -w(rite) filename (write to .mp4 file)\n"); |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 352 | fprintf(stderr, " -k seek test\n"); |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 355 | int main(int argc, char **argv) { |
| 356 | android::ProcessState::self()->startThreadPool(); |
| 357 | |
Andreas Huber | 8a43277 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 358 | bool audioOnly = false; |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 359 | bool listComponents = false; |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 360 | bool dumpProfiles = false; |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 361 | bool extractThumbnail = false; |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 362 | bool seekTest = false; |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 363 | gNumRepetitions = 1; |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 364 | gMaxNumFrames = 0; |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 365 | gReproduceBug = -1; |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 366 | gPreferSoftwareCodec = false; |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 367 | gPlaybackAudio = false; |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 368 | gWriteMP4 = false; |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 369 | |
Andreas Huber | 4e4173b | 2010-07-22 09:20:13 -0700 | [diff] [blame] | 370 | sp<ALooper> looper; |
| 371 | sp<ARTSPController> rtspController; |
| 372 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 373 | int res; |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 374 | while ((res = getopt(argc, argv, "han:lm:b:ptsow:k")) >= 0) { |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 375 | switch (res) { |
| 376 | case 'a': |
| 377 | { |
| 378 | audioOnly = true; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | case 'l': |
| 383 | { |
| 384 | listComponents = true; |
| 385 | break; |
| 386 | } |
| 387 | |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 388 | case 'm': |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 389 | case 'n': |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 390 | case 'b': |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 391 | { |
| 392 | char *end; |
| 393 | long x = strtol(optarg, &end, 10); |
| 394 | |
| 395 | if (*end != '\0' || end == optarg || x <= 0) { |
| 396 | x = 1; |
| 397 | } |
| 398 | |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 399 | if (res == 'n') { |
| 400 | gNumRepetitions = x; |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 401 | } else if (res == 'm') { |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 402 | gMaxNumFrames = x; |
Andreas Huber | 0fb6477 | 2009-08-27 12:10:02 -0700 | [diff] [blame] | 403 | } else { |
| 404 | CHECK_EQ(res, 'b'); |
| 405 | gReproduceBug = x; |
Andreas Huber | 557c6ce | 2009-08-26 10:11:50 -0700 | [diff] [blame] | 406 | } |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 410 | case 'w': |
| 411 | { |
| 412 | gWriteMP4 = true; |
| 413 | gWriteMP4Filename.setTo(optarg); |
| 414 | break; |
| 415 | } |
| 416 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 417 | case 'p': |
| 418 | { |
| 419 | dumpProfiles = true; |
| 420 | break; |
| 421 | } |
| 422 | |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 423 | case 't': |
| 424 | { |
| 425 | extractThumbnail = true; |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | case 's': |
| 430 | { |
| 431 | gPreferSoftwareCodec = true; |
| 432 | break; |
| 433 | } |
| 434 | |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 435 | case 'o': |
| 436 | { |
| 437 | gPlaybackAudio = true; |
| 438 | break; |
| 439 | } |
| 440 | |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 441 | case 'k': |
| 442 | { |
| 443 | seekTest = true; |
| 444 | break; |
| 445 | } |
| 446 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 447 | case '?': |
| 448 | case 'h': |
| 449 | default: |
| 450 | { |
| 451 | usage(argv[0]); |
| 452 | exit(1); |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 458 | if (gPlaybackAudio && !audioOnly) { |
| 459 | // This doesn't make any sense if we're decoding the video track. |
| 460 | gPlaybackAudio = false; |
| 461 | } |
| 462 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 463 | argc -= optind; |
| 464 | argv += optind; |
| 465 | |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 466 | if (extractThumbnail) { |
| 467 | sp<IServiceManager> sm = defaultServiceManager(); |
| 468 | sp<IBinder> binder = sm->getService(String16("media.player")); |
| 469 | sp<IMediaPlayerService> service = |
| 470 | interface_cast<IMediaPlayerService>(binder); |
| 471 | |
| 472 | CHECK(service.get() != NULL); |
| 473 | |
| 474 | sp<IMediaMetadataRetriever> retriever = |
| 475 | service->createMetadataRetriever(getpid()); |
| 476 | |
| 477 | CHECK(retriever != NULL); |
| 478 | |
| 479 | for (int k = 0; k < argc; ++k) { |
| 480 | const char *filename = argv[k]; |
| 481 | |
| 482 | CHECK_EQ(retriever->setDataSource(filename), OK); |
Andreas Huber | edfeb1a | 2010-03-10 11:07:12 -0800 | [diff] [blame] | 483 | CHECK_EQ(retriever->setMode( |
| 484 | METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL), |
| 485 | OK); |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 486 | |
| 487 | sp<IMemory> mem = retriever->captureFrame(); |
| 488 | |
Andreas Huber | edfeb1a | 2010-03-10 11:07:12 -0800 | [diff] [blame] | 489 | if (mem != NULL) { |
| 490 | printf("captureFrame(%s) => OK\n", filename); |
| 491 | } else { |
| 492 | mem = retriever->extractAlbumArt(); |
| 493 | |
| 494 | if (mem != NULL) { |
| 495 | printf("extractAlbumArt(%s) => OK\n", filename); |
| 496 | } else { |
| 497 | printf("both captureFrame and extractAlbumArt " |
| 498 | "failed on file '%s'.\n", filename); |
| 499 | } |
| 500 | } |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 506 | if (dumpProfiles) { |
| 507 | sp<IServiceManager> sm = defaultServiceManager(); |
| 508 | sp<IBinder> binder = sm->getService(String16("media.player")); |
| 509 | sp<IMediaPlayerService> service = |
| 510 | interface_cast<IMediaPlayerService>(binder); |
| 511 | |
| 512 | CHECK(service.get() != NULL); |
| 513 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 514 | sp<IOMX> omx = service->getOMX(); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 515 | CHECK(omx.get() != NULL); |
| 516 | |
| 517 | const char *kMimeTypes[] = { |
| 518 | MEDIA_MIMETYPE_VIDEO_AVC, MEDIA_MIMETYPE_VIDEO_MPEG4, |
Andreas Huber | 1a189a8 | 2010-03-24 13:49:20 -0700 | [diff] [blame] | 519 | MEDIA_MIMETYPE_VIDEO_H263, MEDIA_MIMETYPE_AUDIO_AAC, |
| 520 | MEDIA_MIMETYPE_AUDIO_AMR_NB, MEDIA_MIMETYPE_AUDIO_AMR_WB, |
| 521 | MEDIA_MIMETYPE_AUDIO_MPEG |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | for (size_t k = 0; k < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]); |
| 525 | ++k) { |
| 526 | printf("type '%s':\n", kMimeTypes[k]); |
| 527 | |
| 528 | Vector<CodecCapabilities> results; |
| 529 | CHECK_EQ(QueryCodecs(omx, kMimeTypes[k], |
| 530 | true, // queryDecoders |
| 531 | &results), OK); |
| 532 | |
| 533 | for (size_t i = 0; i < results.size(); ++i) { |
| 534 | printf(" decoder '%s' supports ", |
| 535 | results[i].mComponentName.string()); |
| 536 | |
| 537 | if (results[i].mProfileLevels.size() == 0) { |
| 538 | printf("NOTHING.\n"); |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | for (size_t j = 0; j < results[i].mProfileLevels.size(); ++j) { |
| 543 | const CodecProfileLevel &profileLevel = |
| 544 | results[i].mProfileLevels[j]; |
| 545 | |
| 546 | printf("%s%ld/%ld", j > 0 ? ", " : "", |
| 547 | profileLevel.mProfile, profileLevel.mLevel); |
| 548 | } |
| 549 | |
| 550 | printf("\n"); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 555 | if (listComponents) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 556 | sp<IServiceManager> sm = defaultServiceManager(); |
| 557 | sp<IBinder> binder = sm->getService(String16("media.player")); |
| 558 | sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); |
| 559 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 560 | CHECK(service.get() != NULL); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 561 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 562 | sp<IOMX> omx = service->getOMX(); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 563 | CHECK(omx.get() != NULL); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 564 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 565 | List<IOMX::ComponentInfo> list; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 566 | omx->listNodes(&list); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 567 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 568 | for (List<IOMX::ComponentInfo>::iterator it = list.begin(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 569 | it != list.end(); ++it) { |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 570 | printf("%s\n", (*it).mName.string()); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 571 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 574 | DataSource::RegisterDefaultSniffers(); |
| 575 | |
| 576 | OMXClient client; |
| 577 | status_t err = client.connect(); |
| 578 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 579 | for (int k = 0; k < argc; ++k) { |
| 580 | const char *filename = argv[k]; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 581 | |
Andreas Huber | c066884 | 2010-06-10 11:16:10 -0700 | [diff] [blame] | 582 | sp<DataSource> dataSource = DataSource::CreateFromURI(filename); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 583 | |
Andreas Huber | 4e4173b | 2010-07-22 09:20:13 -0700 | [diff] [blame] | 584 | if ((strncasecmp(filename, "sine:", 5) |
| 585 | && strncasecmp(filename, "rtsp://", 7)) && dataSource == NULL) { |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 586 | fprintf(stderr, "Unable to create data source.\n"); |
| 587 | return 1; |
| 588 | } |
| 589 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 590 | bool isJPEG = false; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 591 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 592 | size_t len = strlen(filename); |
| 593 | if (len >= 4 && !strcasecmp(filename + len - 4, ".jpg")) { |
| 594 | isJPEG = true; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 595 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 596 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 597 | sp<MediaSource> mediaSource; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 598 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 599 | if (isJPEG) { |
| 600 | mediaSource = new JPEGSource(dataSource); |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 601 | } else if (!strncasecmp("sine:", filename, 5)) { |
| 602 | char *end; |
| 603 | long sampleRate = strtol(filename + 5, &end, 10); |
| 604 | |
| 605 | if (end == filename + 5) { |
| 606 | sampleRate = 44100; |
| 607 | } |
| 608 | mediaSource = new SineSource(sampleRate, 1); |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 609 | } else { |
Andreas Huber | 4e4173b | 2010-07-22 09:20:13 -0700 | [diff] [blame] | 610 | sp<MediaExtractor> extractor; |
| 611 | |
| 612 | if (!strncasecmp("rtsp://", filename, 7)) { |
| 613 | if (looper == NULL) { |
| 614 | looper = new ALooper; |
| 615 | looper->start(); |
| 616 | } |
| 617 | |
| 618 | rtspController = new ARTSPController(looper); |
| 619 | status_t err = rtspController->connect(filename); |
| 620 | if (err != OK) { |
| 621 | fprintf(stderr, "could not connect to rtsp server.\n"); |
| 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | extractor = rtspController.get(); |
| 626 | } else { |
| 627 | extractor = MediaExtractor::Create(dataSource); |
| 628 | if (extractor == NULL) { |
| 629 | fprintf(stderr, "could not create extractor.\n"); |
| 630 | return -1; |
| 631 | } |
James Dong | e4cfcb8 | 2010-02-04 18:08:06 -0800 | [diff] [blame] | 632 | } |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 633 | |
| 634 | size_t numTracks = extractor->countTracks(); |
| 635 | |
| 636 | sp<MetaData> meta; |
| 637 | size_t i; |
| 638 | for (i = 0; i < numTracks; ++i) { |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 639 | meta = extractor->getTrackMetaData( |
| 640 | i, MediaExtractor::kIncludeExtensiveMetaData); |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 641 | |
| 642 | const char *mime; |
| 643 | meta->findCString(kKeyMIMEType, &mime); |
| 644 | |
| 645 | if (audioOnly && !strncasecmp(mime, "audio/", 6)) { |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | if (!audioOnly && !strncasecmp(mime, "video/", 6)) { |
| 650 | break; |
| 651 | } |
Andreas Huber | 662292a | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 652 | |
| 653 | meta = NULL; |
| 654 | } |
| 655 | |
| 656 | if (meta == NULL) { |
| 657 | fprintf(stderr, |
| 658 | "No suitable %s track found. The '-a' option will " |
| 659 | "target audio tracks only, the default is to target " |
| 660 | "video tracks only.\n", |
| 661 | audioOnly ? "audio" : "video"); |
| 662 | return -1; |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Andreas Huber | e42fdb5 | 2010-01-14 11:31:22 -0800 | [diff] [blame] | 665 | int64_t thumbTimeUs; |
| 666 | if (meta->findInt64(kKeyThumbnailTime, &thumbTimeUs)) { |
| 667 | printf("thumbnailTime: %lld us (%.2f secs)\n", |
| 668 | thumbTimeUs, thumbTimeUs / 1E6); |
| 669 | } |
| 670 | |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 671 | mediaSource = extractor->getTrack(i); |
| 672 | } |
| 673 | |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 674 | if (gWriteMP4) { |
| 675 | writeSourceToMP4(mediaSource); |
Andreas Huber | 5850a48 | 2010-07-20 16:30:35 -0700 | [diff] [blame] | 676 | } else if (seekTest) { |
| 677 | performSeekTest(mediaSource); |
Andreas Huber | 45bac57 | 2010-07-01 08:19:52 -0700 | [diff] [blame] | 678 | } else { |
| 679 | playSource(&client, mediaSource); |
| 680 | } |
Andreas Huber | 4e4173b | 2010-07-22 09:20:13 -0700 | [diff] [blame] | 681 | |
| 682 | if (rtspController != NULL) { |
| 683 | rtspController->disconnect(); |
| 684 | rtspController.clear(); |
| 685 | |
| 686 | sleep(3); |
| 687 | } |
Andreas Huber | 09daada | 2009-08-18 11:27:32 -0700 | [diff] [blame] | 688 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 689 | |
| 690 | client.disconnect(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 691 | |
| 692 | return 0; |
| 693 | } |