MidiMetadataRetriever.cpp revision 3856b090cd04ba5dd4a59a12430ed724d5995909
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
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    ALOGV("clearMetadataValues");
35    mMetadataValues[0][0] = '\0';
36}
37
38status_t MidiMetadataRetriever::setDataSource(
39        const char *url, const KeyedVector<String8, String8> *headers)
40{
41    ALOGV("setDataSource: %s", url? url: "NULL pointer");
42    Mutex::Autolock lock(mLock);
43    clearMetadataValues();
44    if (mMidiPlayer == 0) {
45        mMidiPlayer = new MidiFile();
46    }
47    return mMidiPlayer->setDataSource(url, headers);
48}
49
50status_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
51{
52    ALOGV("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    ALOGV("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            ALOGV("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