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 
28 namespace android {
29 
30 class MidiEngine : public RefBase {
31 public:
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();
43 private:
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 
52 class MidiExtractor : public MediaExtractor {
53 
54 public:
55     // Extractor assumes ownership of source
56     MidiExtractor(const sp<DataSource> &source);
57 
58     virtual size_t countTracks();
59     virtual sp<MediaSource> getTrack(size_t index);
60     virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
61 
62     virtual sp<MetaData> getMetaData();
63 
64 protected:
65     virtual ~MidiExtractor();
66 
67 private:
68     sp<DataSource> mDataSource;
69     status_t mInitCheck;
70     sp<MetaData> mFileMetadata;
71 
72     // There is only one track
73     sp<MetaData> mTrackMetadata;
74 
75     sp<MidiEngine> mEngine;
76 
77     EAS_DATA_HANDLE     mEasData;
78     EAS_HANDLE          mEasHandle;
79     EAS_PCM*            mAudioBuffer;
80     EAS_I32             mPlayTime;
81     EAS_I32             mDuration;
82     EAS_STATE           mState;
83     EAS_FILE            mFileLocator;
84 
85     MidiExtractor(const MidiExtractor &);
86     MidiExtractor &operator=(const MidiExtractor &);
87 
88 };
89 
90 bool SniffMidi(const sp<DataSource> &source, String8 *mimeType,
91         float *confidence, sp<AMessage> *);
92 
93 }  // namespace android
94 
95 #endif  // MIDI_EXTRACTOR_H_
96