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 "WifiDisplaySource.h"
22
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct MediaCodec;
29
30#define ENABLE_SILENCE_DETECTION        0
31
32// Utility class that receives media access units and converts them into
33// media access unit of a different format.
34// Right now this'll convert raw video into H.264 and raw audio into AAC.
35struct Converter : public AHandler {
36    Converter(
37            const sp<AMessage> &notify,
38            const sp<ALooper> &codecLooper,
39            const sp<AMessage> &format,
40            bool usePCMAudio);
41
42    status_t initCheck() const;
43
44    size_t getInputBufferCount() const;
45
46    sp<AMessage> getOutputFormat() const;
47
48    void feedAccessUnit(const sp<ABuffer> &accessUnit);
49    void signalEOS();
50
51    void requestIDRFrame();
52
53    enum {
54        kWhatAccessUnit,
55        kWhatEOS,
56        kWhatError,
57    };
58
59    enum {
60        kWhatDoMoreWork,
61        kWhatRequestIDRFrame,
62        kWhatShutdown,
63        kWhatMediaPullerNotify,
64        kWhatEncoderActivity,
65    };
66
67    void shutdownAsync();
68
69protected:
70    virtual ~Converter();
71    virtual void onMessageReceived(const sp<AMessage> &msg);
72
73private:
74    status_t mInitCheck;
75    sp<AMessage> mNotify;
76    sp<ALooper> mCodecLooper;
77    sp<AMessage> mInputFormat;
78    bool mIsVideo;
79    bool mIsPCMAudio;
80    sp<AMessage> mOutputFormat;
81
82    sp<MediaCodec> mEncoder;
83    sp<AMessage> mEncoderActivityNotify;
84
85    Vector<sp<ABuffer> > mEncoderInputBuffers;
86    Vector<sp<ABuffer> > mEncoderOutputBuffers;
87
88    List<size_t> mAvailEncoderInputIndices;
89
90    List<sp<ABuffer> > mInputBufferQueue;
91
92    bool mDoMoreWorkPending;
93
94#if ENABLE_SILENCE_DETECTION
95    int64_t mFirstSilentFrameUs;
96    bool mInSilentMode;
97#endif
98
99    sp<ABuffer> mPartialAudioAU;
100
101    status_t initEncoder();
102
103    status_t feedEncoderInputBuffers();
104
105    void scheduleDoMoreWork();
106    status_t doMoreWork();
107
108    void notifyError(status_t err);
109
110    // Packetizes raw PCM audio data available in mInputBufferQueue
111    // into a format suitable for transport stream inclusion and
112    // notifies the observer.
113    status_t feedRawAudioInputBuffers();
114
115    static bool IsSilence(const sp<ABuffer> &accessUnit);
116
117    DISALLOW_EVIL_CONSTRUCTORS(Converter);
118};
119
120}  // namespace android
121
122#endif  // CONVERTER_H_
123