blob: aaf2d18cad7e716b91ccb60bf49654780e6bca0e [file] [log] [blame]
James Dong392ff3b2009-09-06 14:29:45 -07001/*
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
25namespace android {
26
27static status_t ERROR_NOT_OPEN = -1;
28static status_t ERROR_OPEN_FAILED = -2;
29static status_t ERROR_EAS_FAILURE = -3;
30static status_t ERROR_ALLOCATE_FAILED = -4;
31
32void MidiMetadataRetriever::clearMetadataValues()
33{
34 LOGV("clearMetadataValues");
35 mMetadataValues[0][0] = '\0';
36}
37
Andreas Huber5b7ced62011-03-21 10:25:44 -070038status_t MidiMetadataRetriever::setDataSource(
39 const char *url, const KeyedVector<String8, String8> *headers)
James Dong392ff3b2009-09-06 14:29:45 -070040{
41 LOGV("setDataSource: %s", url? url: "NULL pointer");
42 Mutex::Autolock lock(mLock);
43 clearMetadataValues();
44 if (mMidiPlayer == 0) {
45 mMidiPlayer = new MidiFile();
46 }
Andreas Huber5b7ced62011-03-21 10:25:44 -070047 return mMidiPlayer->setDataSource(url, headers);
James Dong392ff3b2009-09-06 14:29:45 -070048}
49
50status_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
61const 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