James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2009, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MidiMetadataRetriever" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "MidiMetadataRetriever.h" |
| 23 | #include <media/mediametadataretriever.h> |
| 24 | |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 25 | #include <media/IMediaHTTPService.h> |
| 26 | |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | static status_t ERROR_NOT_OPEN = -1; |
| 30 | static status_t ERROR_OPEN_FAILED = -2; |
| 31 | static status_t ERROR_EAS_FAILURE = -3; |
| 32 | static status_t ERROR_ALLOCATE_FAILED = -4; |
| 33 | |
| 34 | void MidiMetadataRetriever::clearMetadataValues() |
| 35 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 36 | ALOGV("clearMetadataValues"); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 37 | mMetadataValues[0][0] = '\0'; |
| 38 | } |
| 39 | |
Andreas Huber | af8791e | 2011-03-21 10:25:44 -0700 | [diff] [blame] | 40 | status_t MidiMetadataRetriever::setDataSource( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 41 | const sp<IMediaHTTPService> &httpService, |
| 42 | const char *url, |
| 43 | const KeyedVector<String8, String8> *headers) |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 44 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 45 | ALOGV("setDataSource: %s", url? url: "NULL pointer"); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 46 | Mutex::Autolock lock(mLock); |
| 47 | clearMetadataValues(); |
| 48 | if (mMidiPlayer == 0) { |
| 49 | mMidiPlayer = new MidiFile(); |
| 50 | } |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 51 | return mMidiPlayer->setDataSource(httpService, url, headers); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | status_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length) |
| 55 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 56 | ALOGV("setDataSource: fd(%d), offset(%lld), and length(%lld)", fd, offset, length); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 57 | Mutex::Autolock lock(mLock); |
| 58 | clearMetadataValues(); |
| 59 | if (mMidiPlayer == 0) { |
| 60 | mMidiPlayer = new MidiFile(); |
| 61 | } |
| 62 | return mMidiPlayer->setDataSource(fd, offset, length);; |
| 63 | } |
| 64 | |
| 65 | const char* MidiMetadataRetriever::extractMetadata(int keyCode) |
| 66 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 67 | ALOGV("extractMetdata: key(%d)", keyCode); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 68 | Mutex::Autolock lock(mLock); |
| 69 | if (mMidiPlayer == 0 || mMidiPlayer->initCheck() != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 70 | ALOGE("Midi player is not initialized yet"); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 71 | return NULL; |
| 72 | } |
| 73 | switch (keyCode) { |
| 74 | case METADATA_KEY_DURATION: |
| 75 | { |
| 76 | if (mMetadataValues[0][0] == '\0') { |
| 77 | int duration = -1; |
| 78 | if (mMidiPlayer->getDuration(&duration) != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 79 | ALOGE("failed to get duration"); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | snprintf(mMetadataValues[0], MAX_METADATA_STRING_LENGTH, "%d", duration); |
| 83 | } |
| 84 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 85 | ALOGV("duration: %s ms", mMetadataValues[0]); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 86 | return mMetadataValues[0]; |
| 87 | } |
| 88 | default: |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 89 | ALOGE("Unsupported key code (%d)", keyCode); |
James Dong | 148c1a2 | 2009-09-06 14:29:45 -0700 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | }; |
| 96 | |