blob: f3cf6ef892f23716f24cb40420547ecf55ec75f8 [file] [log] [blame]
James Dong148c1a22009-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
Andreas Huber1b86fe02014-01-29 11:13:26 -080025#include <media/IMediaHTTPService.h>
26
James Dong148c1a22009-09-06 14:29:45 -070027namespace 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{
Steve Block3856b092011-10-20 11:56:00 +010036 ALOGV("clearMetadataValues");
James Dong148c1a22009-09-06 14:29:45 -070037 mMetadataValues[0][0] = '\0';
38}
39
Andreas Huberaf8791e2011-03-21 10:25:44 -070040status_t MidiMetadataRetriever::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -080041 const sp<IMediaHTTPService> &httpService,
42 const char *url,
43 const KeyedVector<String8, String8> *headers)
James Dong148c1a22009-09-06 14:29:45 -070044{
Steve Block3856b092011-10-20 11:56:00 +010045 ALOGV("setDataSource: %s", url? url: "NULL pointer");
James Dong148c1a22009-09-06 14:29:45 -070046 Mutex::Autolock lock(mLock);
47 clearMetadataValues();
48 if (mMidiPlayer == 0) {
49 mMidiPlayer = new MidiFile();
50 }
Andreas Huber1b86fe02014-01-29 11:13:26 -080051 return mMidiPlayer->setDataSource(httpService, url, headers);
James Dong148c1a22009-09-06 14:29:45 -070052}
53
54status_t MidiMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
55{
Steve Block3856b092011-10-20 11:56:00 +010056 ALOGV("setDataSource: fd(%d), offset(%lld), and length(%lld)", fd, offset, length);
James Dong148c1a22009-09-06 14:29:45 -070057 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{
Steve Block3856b092011-10-20 11:56:00 +010067 ALOGV("extractMetdata: key(%d)", keyCode);
James Dong148c1a22009-09-06 14:29:45 -070068 Mutex::Autolock lock(mLock);
69 if (mMidiPlayer == 0 || mMidiPlayer->initCheck() != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +000070 ALOGE("Midi player is not initialized yet");
James Dong148c1a22009-09-06 14:29:45 -070071 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) {
Steve Block29357bc2012-01-06 19:20:56 +000079 ALOGE("failed to get duration");
James Dong148c1a22009-09-06 14:29:45 -070080 return NULL;
81 }
82 snprintf(mMetadataValues[0], MAX_METADATA_STRING_LENGTH, "%d", duration);
83 }
84
Steve Block3856b092011-10-20 11:56:00 +010085 ALOGV("duration: %s ms", mMetadataValues[0]);
James Dong148c1a22009-09-06 14:29:45 -070086 return mMetadataValues[0];
87 }
88 default:
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE("Unsupported key code (%d)", keyCode);
James Dong148c1a22009-09-06 14:29:45 -070090 return NULL;
91 }
92 return NULL;
93}
94
95};
96