Converter.h revision 03e2ffa64470eec4e886614a4fa4facbae58a862
1/*
2 * Copyright 2012, 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 CONVERTER_H_
18
19#define CONVERTER_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22
23namespace android {
24
25struct ABuffer;
26struct MediaCodec;
27
28// Utility class that receives media access units and converts them into
29// media access unit of a different format.
30// Right now this'll convert raw video into H.264 and raw audio into AAC.
31struct Converter : public AHandler {
32    Converter(
33            const sp<AMessage> &notify,
34            const sp<ALooper> &codecLooper,
35            const sp<AMessage> &format);
36
37    status_t initCheck() const;
38
39    size_t getInputBufferCount() const;
40
41    sp<AMessage> getOutputFormat() const;
42
43    void feedAccessUnit(const sp<ABuffer> &accessUnit);
44    void signalEOS();
45
46    void requestIDRFrame();
47
48    enum {
49        kWhatAccessUnit,
50        kWhatEOS,
51        kWhatError,
52    };
53
54protected:
55    virtual ~Converter();
56    virtual void onMessageReceived(const sp<AMessage> &msg);
57
58private:
59    enum {
60        kWhatFeedAccessUnit,
61        kWhatInputEOS,
62        kWhatDoMoreWork,
63        kWhatRequestIDRFrame,
64    };
65
66    status_t mInitCheck;
67    sp<AMessage> mNotify;
68    sp<ALooper> mCodecLooper;
69    sp<AMessage> mInputFormat;
70    bool mIsVideo;
71    sp<AMessage> mOutputFormat;
72
73    sp<MediaCodec> mEncoder;
74
75    Vector<sp<ABuffer> > mEncoderInputBuffers;
76    Vector<sp<ABuffer> > mEncoderOutputBuffers;
77
78    List<size_t> mAvailEncoderInputIndices;
79
80    List<sp<ABuffer> > mInputBufferQueue;
81
82    bool mDoMoreWorkPending;
83
84    status_t initEncoder();
85
86    status_t feedEncoderInputBuffers();
87
88    void scheduleDoMoreWork();
89    status_t doMoreWork();
90
91    void notifyError(status_t err);
92
93    DISALLOW_EVIL_CONSTRUCTORS(Converter);
94};
95
96}  // namespace android
97
98#endif  // CONVERTER_H_
99
100