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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaExtractor" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | 57515f3 | 2009-10-23 09:55:10 -0700 | [diff] [blame] | 21 | #include "include/AMRExtractor.h" |
| 22 | #include "include/MP3Extractor.h" |
| 23 | #include "include/MPEG4Extractor.h" |
Andreas Huber | 6bce6d8 | 2009-11-03 16:00:58 -0800 | [diff] [blame^] | 24 | #include "include/WAVExtractor.h" |
Andreas Huber | 57515f3 | 2009-10-23 09:55:10 -0700 | [diff] [blame] | 25 | |
Andreas Huber | 9a12baf | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 26 | #include <media/stagefright/CachingDataSource.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 27 | #include <media/stagefright/DataSource.h> |
Andreas Huber | 9a12baf | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 28 | #include <media/stagefright/HTTPDataSource.h> |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 29 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 30 | #include <media/stagefright/MediaExtractor.h> |
Andreas Huber | 9a12baf | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 31 | #include <media/stagefright/MmapSource.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 32 | #include <utils/String8.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // static |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 37 | sp<MediaExtractor> MediaExtractor::Create( |
| 38 | const sp<DataSource> &source, const char *mime) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 39 | String8 tmp; |
| 40 | if (mime == NULL) { |
| 41 | float confidence; |
| 42 | if (!source->sniff(&tmp, &confidence)) { |
| 43 | LOGE("FAILED to autodetect media content."); |
| 44 | |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | mime = tmp.string(); |
Andreas Huber | ad28543 | 2009-10-22 09:44:00 -0700 | [diff] [blame] | 49 | LOGV("Autodetected media content as '%s' with confidence %.2f", |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 50 | mime, confidence); |
| 51 | } |
| 52 | |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 53 | if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4) |
| 54 | || !strcasecmp(mime, "audio/mp4")) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 55 | return new MPEG4Extractor(source); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 56 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 57 | return new MP3Extractor(source); |
Andreas Huber | e6c4096 | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 58 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB) |
| 59 | || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) { |
Andreas Huber | 5a65a6e | 2009-09-08 16:07:15 -0700 | [diff] [blame] | 60 | return new AMRExtractor(source); |
Andreas Huber | 6bce6d8 | 2009-11-03 16:00:58 -0800 | [diff] [blame^] | 61 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) { |
| 62 | return new WAVExtractor(source); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Andreas Huber | 9a12baf | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 68 | // static |
| 69 | sp<MediaExtractor> MediaExtractor::CreateFromURI( |
| 70 | const char *uri, const char *mime) { |
| 71 | sp<DataSource> source; |
| 72 | if (!strncasecmp("file://", uri, 7)) { |
| 73 | source = new MmapSource(uri + 7); |
| 74 | } else if (!strncasecmp("http://", uri, 7)) { |
| 75 | source = new HTTPDataSource(uri); |
| 76 | source = new CachingDataSource(source, 64 * 1024, 10); |
| 77 | } else { |
| 78 | // Assume it's a filename. |
| 79 | source = new MmapSource(uri); |
| 80 | } |
| 81 | |
| 82 | if (source == NULL || source->initCheck() != OK) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | return Create(source, mime); |
| 87 | } |
| 88 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 89 | } // namespace android |