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 25 namespace android { 26 27 static status_t ERROR_NOT_OPEN = -1; 28 static status_t ERROR_OPEN_FAILED = -2; 29 static status_t ERROR_EAS_FAILURE = -3; 30 static status_t ERROR_ALLOCATE_FAILED = -4; 31 32 void MidiMetadataRetriever::clearMetadataValues() 33 { 34 LOGV("clearMetadataValues"); 35 mMetadataValues[0][0] = '\0'; 36 } 37 38 status_t MidiMetadataRetriever::setDataSource(const char *url) 39 { 40 LOGV("setDataSource: %s", url? url: "NULL pointer"); 41 Mutex::Autolock lock(mLock); 42 clearMetadataValues(); 43 if (mMidiPlayer == 0) { 44 mMidiPlayer = new MidiFile(); 45 } 46 // TODO: support headers in MetadataRetriever interface! 47 return mMidiPlayer->setDataSource(url, NULL /* headers */); 48 } 49 50 status_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length) 51 { 52 LOGV("setDataSource: fd(%d), offset(%lld), and length(%lld)", fd, offset, length); 53 Mutex::Autolock lock(mLock); 54 clearMetadataValues(); 55 if (mMidiPlayer == 0) { 56 mMidiPlayer = new MidiFile(); 57 } 58 return mMidiPlayer->setDataSource(fd, offset, length);; 59 } 60 61 const char* MidiMetadataRetriever::extractMetadata(int keyCode) 62 { 63 LOGV("extractMetdata: key(%d)", keyCode); 64 Mutex::Autolock lock(mLock); 65 if (mMidiPlayer == 0 || mMidiPlayer->initCheck() != NO_ERROR) { 66 LOGE("Midi player is not initialized yet"); 67 return NULL; 68 } 69 switch (keyCode) { 70 case METADATA_KEY_DURATION: 71 { 72 if (mMetadataValues[0][0] == '\0') { 73 int duration = -1; 74 if (mMidiPlayer->getDuration(&duration) != NO_ERROR) { 75 LOGE("failed to get duration"); 76 return NULL; 77 } 78 snprintf(mMetadataValues[0], MAX_METADATA_STRING_LENGTH, "%d", duration); 79 } 80 81 LOGV("duration: %s ms", mMetadataValues[0]); 82 return mMetadataValues[0]; 83 } 84 default: 85 LOGE("Unsupported key code (%d)", keyCode); 86 return NULL; 87 } 88 return NULL; 89 } 90 91 }; 92 93