• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
clearMetadataValues()32 void MidiMetadataRetriever::clearMetadataValues()
33 {
34     LOGV("clearMetadataValues");
35     mMetadataValues[0][0] = '\0';
36 }
37 
setDataSource(const char * url)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     return mMidiPlayer->setDataSource(url);
47 }
48 
setDataSource(int fd,int64_t offset,int64_t length)49 status_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
50 {
51     LOGV("setDataSource: fd(%d), offset(%lld), and length(%lld)", fd, offset, length);
52     Mutex::Autolock lock(mLock);
53     clearMetadataValues();
54     if (mMidiPlayer == 0) {
55         mMidiPlayer = new MidiFile();
56     }
57     return mMidiPlayer->setDataSource(fd, offset, length);;
58 }
59 
extractMetadata(int keyCode)60 const char* MidiMetadataRetriever::extractMetadata(int keyCode)
61 {
62     LOGV("extractMetdata: key(%d)", keyCode);
63     Mutex::Autolock lock(mLock);
64     if (mMidiPlayer == 0 || mMidiPlayer->initCheck() != NO_ERROR) {
65         LOGE("Midi player is not initialized yet");
66         return NULL;
67     }
68     switch (keyCode) {
69     case METADATA_KEY_DURATION:
70         {
71             if (mMetadataValues[0][0] == '\0') {
72                 int duration = -1;
73                 if (mMidiPlayer->getDuration(&duration) != NO_ERROR) {
74                     LOGE("failed to get duration");
75                     return NULL;
76                 }
77                 snprintf(mMetadataValues[0], MAX_METADATA_STRING_LENGTH, "%d", duration);
78             }
79 
80             LOGV("duration: %s ms", mMetadataValues[0]);
81             return mMetadataValues[0];
82         }
83     default:
84         LOGE("Unsupported key code (%d)", keyCode);
85         return NULL;
86     }
87     return NULL;
88 }
89 
90 };
91 
92