1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef MIDI_EXTRACTOR_H_
18#define MIDI_EXTRACTOR_H_
19
20#include <media/stagefright/DataSource.h>
21#include <media/stagefright/MediaExtractor.h>
22#include <media/stagefright/MediaBuffer.h>
23#include <media/stagefright/MediaBufferGroup.h>
24#include <media/MidiIoWrapper.h>
25#include <utils/String8.h>
26#include <libsonivox/eas.h>
27
28namespace android {
29
30class MidiEngine : public RefBase {
31public:
32    MidiEngine(const sp<DataSource> &dataSource,
33            const sp<MetaData> &fileMetadata,
34            const sp<MetaData> &trackMetadata);
35    ~MidiEngine();
36
37    status_t initCheck();
38
39    status_t allocateBuffers();
40    status_t releaseBuffers();
41    status_t seekTo(int64_t positionUs);
42    MediaBuffer* readBuffer();
43private:
44    sp<MidiIoWrapper> mIoWrapper;
45    MediaBufferGroup *mGroup;
46    EAS_DATA_HANDLE mEasData;
47    EAS_HANDLE mEasHandle;
48    const S_EAS_LIB_CONFIG* mEasConfig;
49    bool mIsInitialized;
50};
51
52class MidiExtractor : public MediaExtractor {
53
54public:
55    // Extractor assumes ownership of source
56    MidiExtractor(const sp<DataSource> &source);
57
58    virtual size_t countTracks();
59    virtual sp<IMediaSource> getTrack(size_t index);
60    virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
61
62    virtual sp<MetaData> getMetaData();
63    virtual const char * name() { return "MidiExtractor"; }
64
65protected:
66    virtual ~MidiExtractor();
67
68private:
69    sp<DataSource> mDataSource;
70    status_t mInitCheck;
71    sp<MetaData> mFileMetadata;
72
73    // There is only one track
74    sp<MetaData> mTrackMetadata;
75
76    sp<MidiEngine> mEngine;
77
78    EAS_DATA_HANDLE     mEasData;
79    EAS_HANDLE          mEasHandle;
80    EAS_PCM*            mAudioBuffer;
81    EAS_I32             mPlayTime;
82    EAS_I32             mDuration;
83    EAS_STATE           mState;
84    EAS_FILE            mFileLocator;
85
86    MidiExtractor(const MidiExtractor &);
87    MidiExtractor &operator=(const MidiExtractor &);
88
89};
90
91bool SniffMidi(const sp<DataSource> &source, String8 *mimeType,
92        float *confidence, sp<AMessage> *);
93
94}  // namespace android
95
96#endif  // MIDI_EXTRACTOR_H_
97