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