1148c1a2a96774517407717b61e5bc9cb08be8806James Dong/*
2148c1a2a96774517407717b61e5bc9cb08be8806James Dong**
3148c1a2a96774517407717b61e5bc9cb08be8806James Dong** Copyright 2009, The Android Open Source Project
4148c1a2a96774517407717b61e5bc9cb08be8806James Dong**
5148c1a2a96774517407717b61e5bc9cb08be8806James Dong** Licensed under the Apache License, Version 2.0 (the "License");
6148c1a2a96774517407717b61e5bc9cb08be8806James Dong** you may not use this file except in compliance with the License.
7148c1a2a96774517407717b61e5bc9cb08be8806James Dong** You may obtain a copy of the License at
8148c1a2a96774517407717b61e5bc9cb08be8806James Dong**
9148c1a2a96774517407717b61e5bc9cb08be8806James Dong**     http://www.apache.org/licenses/LICENSE-2.0
10148c1a2a96774517407717b61e5bc9cb08be8806James Dong**
11148c1a2a96774517407717b61e5bc9cb08be8806James Dong** Unless required by applicable law or agreed to in writing, software
12148c1a2a96774517407717b61e5bc9cb08be8806James Dong** distributed under the License is distributed on an "AS IS" BASIS,
13148c1a2a96774517407717b61e5bc9cb08be8806James Dong** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14148c1a2a96774517407717b61e5bc9cb08be8806James Dong** See the License for the specific language governing permissions and
15148c1a2a96774517407717b61e5bc9cb08be8806James Dong** limitations under the License.
16148c1a2a96774517407717b61e5bc9cb08be8806James Dong*/
17148c1a2a96774517407717b61e5bc9cb08be8806James Dong
18148c1a2a96774517407717b61e5bc9cb08be8806James Dong//#define LOG_NDEBUG 0
19148c1a2a96774517407717b61e5bc9cb08be8806James Dong#define LOG_TAG "MidiMetadataRetriever"
20148c1a2a96774517407717b61e5bc9cb08be8806James Dong#include <utils/Log.h>
21148c1a2a96774517407717b61e5bc9cb08be8806James Dong
22148c1a2a96774517407717b61e5bc9cb08be8806James Dong#include "MidiMetadataRetriever.h"
23148c1a2a96774517407717b61e5bc9cb08be8806James Dong#include <media/mediametadataretriever.h>
24148c1a2a96774517407717b61e5bc9cb08be8806James Dong
25148c1a2a96774517407717b61e5bc9cb08be8806James Dongnamespace android {
26148c1a2a96774517407717b61e5bc9cb08be8806James Dong
27148c1a2a96774517407717b61e5bc9cb08be8806James Dongstatic status_t ERROR_NOT_OPEN = -1;
28148c1a2a96774517407717b61e5bc9cb08be8806James Dongstatic status_t ERROR_OPEN_FAILED = -2;
29148c1a2a96774517407717b61e5bc9cb08be8806James Dongstatic status_t ERROR_EAS_FAILURE = -3;
30148c1a2a96774517407717b61e5bc9cb08be8806James Dongstatic status_t ERROR_ALLOCATE_FAILED = -4;
31148c1a2a96774517407717b61e5bc9cb08be8806James Dong
32148c1a2a96774517407717b61e5bc9cb08be8806James Dongvoid MidiMetadataRetriever::clearMetadataValues()
33148c1a2a96774517407717b61e5bc9cb08be8806James Dong{
343856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("clearMetadataValues");
35148c1a2a96774517407717b61e5bc9cb08be8806James Dong    mMetadataValues[0][0] = '\0';
36148c1a2a96774517407717b61e5bc9cb08be8806James Dong}
37148c1a2a96774517407717b61e5bc9cb08be8806James Dong
38af8791e112c8072452bd14ef3c43a47511d19542Andreas Huberstatus_t MidiMetadataRetriever::setDataSource(
39af8791e112c8072452bd14ef3c43a47511d19542Andreas Huber        const char *url, const KeyedVector<String8, String8> *headers)
40148c1a2a96774517407717b61e5bc9cb08be8806James Dong{
413856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setDataSource: %s", url? url: "NULL pointer");
42148c1a2a96774517407717b61e5bc9cb08be8806James Dong    Mutex::Autolock lock(mLock);
43148c1a2a96774517407717b61e5bc9cb08be8806James Dong    clearMetadataValues();
44148c1a2a96774517407717b61e5bc9cb08be8806James Dong    if (mMidiPlayer == 0) {
45148c1a2a96774517407717b61e5bc9cb08be8806James Dong        mMidiPlayer = new MidiFile();
46148c1a2a96774517407717b61e5bc9cb08be8806James Dong    }
47af8791e112c8072452bd14ef3c43a47511d19542Andreas Huber    return mMidiPlayer->setDataSource(url, headers);
48148c1a2a96774517407717b61e5bc9cb08be8806James Dong}
49148c1a2a96774517407717b61e5bc9cb08be8806James Dong
50148c1a2a96774517407717b61e5bc9cb08be8806James Dongstatus_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
51148c1a2a96774517407717b61e5bc9cb08be8806James Dong{
523856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setDataSource: fd(%d), offset(%lld), and length(%lld)", fd, offset, length);
53148c1a2a96774517407717b61e5bc9cb08be8806James Dong    Mutex::Autolock lock(mLock);
54148c1a2a96774517407717b61e5bc9cb08be8806James Dong    clearMetadataValues();
55148c1a2a96774517407717b61e5bc9cb08be8806James Dong    if (mMidiPlayer == 0) {
56148c1a2a96774517407717b61e5bc9cb08be8806James Dong        mMidiPlayer = new MidiFile();
57148c1a2a96774517407717b61e5bc9cb08be8806James Dong    }
58148c1a2a96774517407717b61e5bc9cb08be8806James Dong    return mMidiPlayer->setDataSource(fd, offset, length);;
59148c1a2a96774517407717b61e5bc9cb08be8806James Dong}
60148c1a2a96774517407717b61e5bc9cb08be8806James Dong
61148c1a2a96774517407717b61e5bc9cb08be8806James Dongconst char* MidiMetadataRetriever::extractMetadata(int keyCode)
62148c1a2a96774517407717b61e5bc9cb08be8806James Dong{
633856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("extractMetdata: key(%d)", keyCode);
64148c1a2a96774517407717b61e5bc9cb08be8806James Dong    Mutex::Autolock lock(mLock);
65148c1a2a96774517407717b61e5bc9cb08be8806James Dong    if (mMidiPlayer == 0 || mMidiPlayer->initCheck() != NO_ERROR) {
6629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Midi player is not initialized yet");
67148c1a2a96774517407717b61e5bc9cb08be8806James Dong        return NULL;
68148c1a2a96774517407717b61e5bc9cb08be8806James Dong    }
69148c1a2a96774517407717b61e5bc9cb08be8806James Dong    switch (keyCode) {
70148c1a2a96774517407717b61e5bc9cb08be8806James Dong    case METADATA_KEY_DURATION:
71148c1a2a96774517407717b61e5bc9cb08be8806James Dong        {
72148c1a2a96774517407717b61e5bc9cb08be8806James Dong            if (mMetadataValues[0][0] == '\0') {
73148c1a2a96774517407717b61e5bc9cb08be8806James Dong                int duration = -1;
74148c1a2a96774517407717b61e5bc9cb08be8806James Dong                if (mMidiPlayer->getDuration(&duration) != NO_ERROR) {
7529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("failed to get duration");
76148c1a2a96774517407717b61e5bc9cb08be8806James Dong                    return NULL;
77148c1a2a96774517407717b61e5bc9cb08be8806James Dong                }
78148c1a2a96774517407717b61e5bc9cb08be8806James Dong                snprintf(mMetadataValues[0], MAX_METADATA_STRING_LENGTH, "%d", duration);
79148c1a2a96774517407717b61e5bc9cb08be8806James Dong            }
80148c1a2a96774517407717b61e5bc9cb08be8806James Dong
813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("duration: %s ms", mMetadataValues[0]);
82148c1a2a96774517407717b61e5bc9cb08be8806James Dong            return mMetadataValues[0];
83148c1a2a96774517407717b61e5bc9cb08be8806James Dong        }
84148c1a2a96774517407717b61e5bc9cb08be8806James Dong    default:
8529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Unsupported key code (%d)", keyCode);
86148c1a2a96774517407717b61e5bc9cb08be8806James Dong        return NULL;
87148c1a2a96774517407717b61e5bc9cb08be8806James Dong    }
88148c1a2a96774517407717b61e5bc9cb08be8806James Dong    return NULL;
89148c1a2a96774517407717b61e5bc9cb08be8806James Dong}
90148c1a2a96774517407717b61e5bc9cb08be8806James Dong
91148c1a2a96774517407717b61e5bc9cb08be8806James Dong};
92148c1a2a96774517407717b61e5bc9cb08be8806James Dong
93