Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 1 | /* |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 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 | */ |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "StagefrightMetadataRetriever" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | e4a8380 | 2010-01-08 10:57:34 -0800 | [diff] [blame] | 21 | #include "include/StagefrightMetadataRetriever.h" |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 22 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 23 | #include <media/stagefright/ColorConverter.h> |
| 24 | #include <media/stagefright/DataSource.h> |
Andreas Huber | 27366fc | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 25 | #include <media/stagefright/FileSource.h> |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 26 | #include <media/stagefright/MediaDebug.h> |
| 27 | #include <media/stagefright/MediaExtractor.h> |
| 28 | #include <media/stagefright/MetaData.h> |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 29 | #include <media/stagefright/OMXCodec.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 33 | StagefrightMetadataRetriever::StagefrightMetadataRetriever() |
| 34 | : mParsedMetaData(false), |
| 35 | mAlbumArt(NULL) { |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 36 | LOGV("StagefrightMetadataRetriever()"); |
| 37 | |
| 38 | DataSource::RegisterDefaultSniffers(); |
| 39 | CHECK_EQ(mClient.connect(), OK); |
| 40 | } |
| 41 | |
| 42 | StagefrightMetadataRetriever::~StagefrightMetadataRetriever() { |
| 43 | LOGV("~StagefrightMetadataRetriever()"); |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 44 | |
| 45 | delete mAlbumArt; |
| 46 | mAlbumArt = NULL; |
| 47 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 48 | mClient.disconnect(); |
| 49 | } |
| 50 | |
| 51 | status_t StagefrightMetadataRetriever::setDataSource(const char *uri) { |
| 52 | LOGV("setDataSource(%s)", uri); |
| 53 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 54 | mParsedMetaData = false; |
| 55 | mMetaData.clear(); |
| 56 | delete mAlbumArt; |
| 57 | mAlbumArt = NULL; |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 58 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 59 | mSource = DataSource::CreateFromURI(uri); |
| 60 | |
| 61 | if (mSource == NULL) { |
| 62 | return UNKNOWN_ERROR; |
| 63 | } |
| 64 | |
| 65 | mExtractor = MediaExtractor::Create(mSource); |
| 66 | |
| 67 | if (mExtractor == NULL) { |
| 68 | mSource.clear(); |
| 69 | |
| 70 | return UNKNOWN_ERROR; |
| 71 | } |
| 72 | |
| 73 | return OK; |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 76 | // Warning caller retains ownership of the filedescriptor! Dup it if necessary. |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 77 | status_t StagefrightMetadataRetriever::setDataSource( |
| 78 | int fd, int64_t offset, int64_t length) { |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 79 | fd = dup(fd); |
| 80 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 81 | LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length); |
| 82 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 83 | mParsedMetaData = false; |
| 84 | mMetaData.clear(); |
| 85 | delete mAlbumArt; |
| 86 | mAlbumArt = NULL; |
| 87 | |
| 88 | mSource = new FileSource(fd, offset, length); |
| 89 | |
| 90 | status_t err; |
| 91 | if ((err = mSource->initCheck()) != OK) { |
| 92 | mSource.clear(); |
| 93 | |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | mExtractor = MediaExtractor::Create(mSource); |
| 98 | |
| 99 | if (mExtractor == NULL) { |
| 100 | mSource.clear(); |
| 101 | |
| 102 | return UNKNOWN_ERROR; |
| 103 | } |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 104 | |
| 105 | return OK; |
| 106 | } |
| 107 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 108 | static VideoFrame *extractVideoFrameWithCodecFlags( |
| 109 | OMXClient *client, |
| 110 | const sp<MetaData> &trackMeta, |
| 111 | const sp<MediaSource> &source, uint32_t flags) { |
| 112 | sp<MediaSource> decoder = |
| 113 | OMXCodec::Create( |
| 114 | client->interface(), source->getFormat(), false, source, |
Andreas Huber | 5a40e39 | 2010-10-18 09:57:42 -0700 | [diff] [blame] | 115 | NULL, flags | OMXCodec::kClientNeedsFramebuffer); |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 116 | |
| 117 | if (decoder.get() == NULL) { |
| 118 | LOGV("unable to instantiate video decoder."); |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Andreas Huber | 1919e5a | 2010-05-20 10:37:06 -0700 | [diff] [blame] | 123 | status_t err = decoder->start(); |
| 124 | if (err != OK) { |
| 125 | LOGW("OMXCodec::start returned error %d (0x%08x)\n", err, err); |
| 126 | return NULL; |
| 127 | } |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 128 | |
| 129 | // Read one output buffer, ignore format change notifications |
| 130 | // and spurious empty buffers. |
| 131 | |
| 132 | MediaSource::ReadOptions options; |
| 133 | int64_t thumbNailTime; |
| 134 | if (trackMeta->findInt64(kKeyThumbnailTime, &thumbNailTime)) { |
| 135 | options.setSeekTo(thumbNailTime); |
| 136 | } else { |
| 137 | thumbNailTime = -1; |
| 138 | } |
| 139 | |
| 140 | MediaBuffer *buffer = NULL; |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 141 | do { |
| 142 | if (buffer != NULL) { |
| 143 | buffer->release(); |
| 144 | buffer = NULL; |
| 145 | } |
| 146 | err = decoder->read(&buffer, &options); |
| 147 | options.clearSeekTo(); |
| 148 | } while (err == INFO_FORMAT_CHANGED |
| 149 | || (buffer != NULL && buffer->range_length() == 0)); |
| 150 | |
| 151 | if (err != OK) { |
| 152 | CHECK_EQ(buffer, NULL); |
| 153 | |
| 154 | LOGV("decoding frame failed."); |
| 155 | decoder->stop(); |
| 156 | |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | LOGV("successfully decoded video frame."); |
| 161 | |
Andreas Huber | 1e19416 | 2010-10-06 16:43:57 -0700 | [diff] [blame] | 162 | int32_t unreadable; |
| 163 | if (buffer->meta_data()->findInt32(kKeyIsUnreadable, &unreadable) |
| 164 | && unreadable != 0) { |
| 165 | LOGV("video frame is unreadable, decoder does not give us access " |
| 166 | "to the video data."); |
| 167 | |
| 168 | buffer->release(); |
| 169 | buffer = NULL; |
| 170 | |
| 171 | decoder->stop(); |
| 172 | |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 176 | int64_t timeUs; |
| 177 | CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 178 | if (thumbNailTime >= 0) { |
| 179 | if (timeUs != thumbNailTime) { |
| 180 | const char *mime; |
| 181 | CHECK(trackMeta->findCString(kKeyMIMEType, &mime)); |
| 182 | |
| 183 | LOGV("thumbNailTime = %lld us, timeUs = %lld us, mime = %s", |
| 184 | thumbNailTime, timeUs, mime); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | sp<MetaData> meta = decoder->getFormat(); |
| 189 | |
| 190 | int32_t width, height; |
| 191 | CHECK(meta->findInt32(kKeyWidth, &width)); |
| 192 | CHECK(meta->findInt32(kKeyHeight, &height)); |
| 193 | |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame^] | 194 | int32_t crop_left, crop_top, crop_right, crop_bottom; |
| 195 | if (!meta->findRect( |
| 196 | kKeyCropRect, |
| 197 | &crop_left, &crop_top, &crop_right, &crop_bottom)) { |
| 198 | crop_left = crop_top = 0; |
| 199 | crop_right = width - 1; |
| 200 | crop_bottom = height - 1; |
| 201 | } |
| 202 | |
James Dong | 53ebc72 | 2010-11-08 16:04:27 -0800 | [diff] [blame] | 203 | int32_t rotationAngle; |
| 204 | if (!trackMeta->findInt32(kKeyRotation, &rotationAngle)) { |
| 205 | rotationAngle = 0; // By default, no rotation |
| 206 | } |
| 207 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 208 | VideoFrame *frame = new VideoFrame; |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame^] | 209 | frame->mWidth = crop_right - crop_left + 1; |
| 210 | frame->mHeight = crop_bottom - crop_top + 1; |
| 211 | frame->mDisplayWidth = frame->mWidth; |
| 212 | frame->mDisplayHeight = frame->mHeight; |
| 213 | frame->mSize = frame->mWidth * frame->mHeight * 2; |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 214 | frame->mData = new uint8_t[frame->mSize]; |
James Dong | 53ebc72 | 2010-11-08 16:04:27 -0800 | [diff] [blame] | 215 | frame->mRotationAngle = rotationAngle; |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 216 | |
| 217 | int32_t srcFormat; |
| 218 | CHECK(meta->findInt32(kKeyColorFormat, &srcFormat)); |
| 219 | |
| 220 | ColorConverter converter( |
| 221 | (OMX_COLOR_FORMATTYPE)srcFormat, OMX_COLOR_Format16bitRGB565); |
| 222 | CHECK(converter.isValid()); |
| 223 | |
| 224 | converter.convert( |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 225 | (const uint8_t *)buffer->data() + buffer->range_offset(), |
Andreas Huber | 1bb0ffd | 2010-11-22 13:06:35 -0800 | [diff] [blame^] | 226 | width, height, |
| 227 | crop_left, crop_top, crop_right, crop_bottom, |
| 228 | frame->mData, |
| 229 | frame->mWidth, |
| 230 | frame->mHeight, |
| 231 | 0, 0, frame->mWidth - 1, frame->mHeight - 1); |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 232 | |
| 233 | buffer->release(); |
| 234 | buffer = NULL; |
| 235 | |
| 236 | decoder->stop(); |
| 237 | |
| 238 | return frame; |
| 239 | } |
| 240 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 241 | VideoFrame *StagefrightMetadataRetriever::captureFrame() { |
| 242 | LOGV("captureFrame"); |
| 243 | |
Andreas Huber | e3452d3 | 2010-03-12 15:08:52 -0800 | [diff] [blame] | 244 | if (0 == (mMode & METADATA_MODE_FRAME_CAPTURE_ONLY)) { |
| 245 | LOGV("captureFrame disabled by mode (0x%08x)", mMode); |
| 246 | |
| 247 | return NULL; |
| 248 | } |
| 249 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 250 | if (mExtractor.get() == NULL) { |
Andreas Huber | ad28543 | 2009-10-22 09:44:00 -0700 | [diff] [blame] | 251 | LOGV("no extractor."); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | size_t n = mExtractor->countTracks(); |
| 256 | size_t i; |
| 257 | for (i = 0; i < n; ++i) { |
| 258 | sp<MetaData> meta = mExtractor->getTrackMetaData(i); |
| 259 | |
| 260 | const char *mime; |
| 261 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 262 | |
| 263 | if (!strncasecmp(mime, "video/", 6)) { |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (i == n) { |
Andreas Huber | ad28543 | 2009-10-22 09:44:00 -0700 | [diff] [blame] | 269 | LOGV("no video track found."); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 270 | return NULL; |
| 271 | } |
| 272 | |
Andreas Huber | e981c33 | 2009-10-22 13:49:30 -0700 | [diff] [blame] | 273 | sp<MetaData> trackMeta = mExtractor->getTrackMetaData( |
| 274 | i, MediaExtractor::kIncludeExtensiveMetaData); |
| 275 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 276 | sp<MediaSource> source = mExtractor->getTrack(i); |
| 277 | |
| 278 | if (source.get() == NULL) { |
Andreas Huber | ad28543 | 2009-10-22 09:44:00 -0700 | [diff] [blame] | 279 | LOGV("unable to instantiate video track."); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
| 282 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 283 | VideoFrame *frame = |
| 284 | extractVideoFrameWithCodecFlags( |
| 285 | &mClient, trackMeta, source, OMXCodec::kPreferSoftwareCodecs); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 286 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 287 | if (frame == NULL) { |
| 288 | LOGV("Software decoder failed to extract thumbnail, " |
| 289 | "trying hardware decoder."); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 290 | |
Andreas Huber | 0ffc90f | 2010-01-14 14:15:33 -0800 | [diff] [blame] | 291 | frame = extractVideoFrameWithCodecFlags(&mClient, trackMeta, source, 0); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 294 | return frame; |
| 295 | } |
| 296 | |
| 297 | MediaAlbumArt *StagefrightMetadataRetriever::extractAlbumArt() { |
| 298 | LOGV("extractAlbumArt (extractor: %s)", mExtractor.get() != NULL ? "YES" : "NO"); |
| 299 | |
Andreas Huber | e3452d3 | 2010-03-12 15:08:52 -0800 | [diff] [blame] | 300 | if (0 == (mMode & METADATA_MODE_METADATA_RETRIEVAL_ONLY)) { |
| 301 | LOGV("extractAlbumArt/metadata retrieval disabled by mode"); |
| 302 | |
| 303 | return NULL; |
| 304 | } |
| 305 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 306 | if (mExtractor == NULL) { |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | if (!mParsedMetaData) { |
| 311 | parseMetaData(); |
| 312 | |
| 313 | mParsedMetaData = true; |
| 314 | } |
| 315 | |
| 316 | if (mAlbumArt) { |
| 317 | return new MediaAlbumArt(*mAlbumArt); |
| 318 | } |
| 319 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | const char *StagefrightMetadataRetriever::extractMetadata(int keyCode) { |
Andreas Huber | e3452d3 | 2010-03-12 15:08:52 -0800 | [diff] [blame] | 324 | if (0 == (mMode & METADATA_MODE_METADATA_RETRIEVAL_ONLY)) { |
| 325 | LOGV("extractAlbumArt/metadata retrieval disabled by mode"); |
| 326 | |
| 327 | return NULL; |
| 328 | } |
| 329 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 330 | if (mExtractor == NULL) { |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | if (!mParsedMetaData) { |
| 335 | parseMetaData(); |
| 336 | |
| 337 | mParsedMetaData = true; |
| 338 | } |
| 339 | |
| 340 | ssize_t index = mMetaData.indexOfKey(keyCode); |
| 341 | |
| 342 | if (index < 0) { |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | return strdup(mMetaData.valueAt(index).string()); |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 349 | void StagefrightMetadataRetriever::parseMetaData() { |
| 350 | sp<MetaData> meta = mExtractor->getMetaData(); |
| 351 | |
| 352 | struct Map { |
| 353 | int from; |
| 354 | int to; |
| 355 | }; |
| 356 | static const Map kMap[] = { |
Andreas Huber | 1cb02bf | 2010-01-13 11:25:10 -0800 | [diff] [blame] | 357 | { kKeyMIMEType, METADATA_KEY_MIMETYPE }, |
Andreas Huber | 3a3656c | 2010-01-13 10:45:49 -0800 | [diff] [blame] | 358 | { kKeyCDTrackNumber, METADATA_KEY_CD_TRACK_NUMBER }, |
Marco Nelissen | 655306f | 2010-02-08 14:50:19 -0800 | [diff] [blame] | 359 | { kKeyDiscNumber, METADATA_KEY_DISC_NUMBER }, |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 360 | { kKeyAlbum, METADATA_KEY_ALBUM }, |
| 361 | { kKeyArtist, METADATA_KEY_ARTIST }, |
Marco Nelissen | c5d5ee3 | 2010-02-11 13:31:44 -0800 | [diff] [blame] | 362 | { kKeyAlbumArtist, METADATA_KEY_ALBUMARTIST }, |
Andreas Huber | 3a3656c | 2010-01-13 10:45:49 -0800 | [diff] [blame] | 363 | { kKeyAuthor, METADATA_KEY_AUTHOR }, |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 364 | { kKeyComposer, METADATA_KEY_COMPOSER }, |
Andreas Huber | 3a3656c | 2010-01-13 10:45:49 -0800 | [diff] [blame] | 365 | { kKeyDate, METADATA_KEY_DATE }, |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 366 | { kKeyGenre, METADATA_KEY_GENRE }, |
| 367 | { kKeyTitle, METADATA_KEY_TITLE }, |
| 368 | { kKeyYear, METADATA_KEY_YEAR }, |
Andreas Huber | c2c9dd3 | 2010-01-19 16:43:53 -0800 | [diff] [blame] | 369 | { kKeyWriter, METADATA_KEY_WRITER }, |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 370 | }; |
| 371 | static const size_t kNumMapEntries = sizeof(kMap) / sizeof(kMap[0]); |
| 372 | |
| 373 | for (size_t i = 0; i < kNumMapEntries; ++i) { |
| 374 | const char *value; |
| 375 | if (meta->findCString(kMap[i].from, &value)) { |
| 376 | mMetaData.add(kMap[i].to, String8(value)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | const void *data; |
| 381 | uint32_t type; |
| 382 | size_t dataSize; |
| 383 | if (meta->findData(kKeyAlbumArt, &type, &data, &dataSize)) { |
| 384 | mAlbumArt = new MediaAlbumArt; |
| 385 | mAlbumArt->mSize = dataSize; |
| 386 | mAlbumArt->mData = new uint8_t[dataSize]; |
| 387 | memcpy(mAlbumArt->mData, data, dataSize); |
| 388 | } |
| 389 | |
Andreas Huber | 3a3656c | 2010-01-13 10:45:49 -0800 | [diff] [blame] | 390 | size_t numTracks = mExtractor->countTracks(); |
| 391 | |
| 392 | char tmp[32]; |
| 393 | sprintf(tmp, "%d", numTracks); |
| 394 | |
| 395 | mMetaData.add(METADATA_KEY_NUM_TRACKS, String8(tmp)); |
| 396 | |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 397 | // The overall duration is the duration of the longest track. |
| 398 | int64_t maxDurationUs = 0; |
Andreas Huber | 3a3656c | 2010-01-13 10:45:49 -0800 | [diff] [blame] | 399 | for (size_t i = 0; i < numTracks; ++i) { |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 400 | sp<MetaData> trackMeta = mExtractor->getTrackMetaData(i); |
| 401 | |
| 402 | int64_t durationUs; |
| 403 | if (trackMeta->findInt64(kKeyDuration, &durationUs)) { |
| 404 | if (durationUs > maxDurationUs) { |
| 405 | maxDurationUs = durationUs; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // The duration value is a string representing the duration in ms. |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 411 | sprintf(tmp, "%lld", (maxDurationUs + 500) / 1000); |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 412 | mMetaData.add(METADATA_KEY_DURATION, String8(tmp)); |
Andreas Huber | 072f524 | 2010-05-20 14:56:53 -0700 | [diff] [blame] | 413 | |
| 414 | if (numTracks == 1) { |
| 415 | const char *fileMIME; |
| 416 | CHECK(meta->findCString(kKeyMIMEType, &fileMIME)); |
| 417 | |
| 418 | if (!strcasecmp(fileMIME, "video/x-matroska")) { |
| 419 | sp<MetaData> trackMeta = mExtractor->getTrackMetaData(0); |
| 420 | const char *trackMIME; |
| 421 | CHECK(trackMeta->findCString(kKeyMIMEType, &trackMIME)); |
| 422 | |
| 423 | if (!strncasecmp("audio/", trackMIME, 6)) { |
| 424 | // The matroska file only contains a single audio track, |
| 425 | // rewrite its mime type. |
| 426 | mMetaData.add( |
| 427 | METADATA_KEY_MIMETYPE, String8("audio/x-matroska")); |
| 428 | } |
| 429 | } |
| 430 | } |
Andreas Huber | aee3c63 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 433 | } // namespace android |