Converter.h revision c8e07e483c116ecaca1c9c6991588607f1187b75
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    bool needToManuallyPrependSPSPPS() const;
48
49    void feedAccessUnit(const sp<ABuffer> &accessUnit);
50    void signalEOS();
51
52    void requestIDRFrame();
53
54    enum {
55        kWhatAccessUnit,
56        kWhatEOS,
57        kWhatError,
58    };
59
60    enum {
61        kWhatDoMoreWork,
62        kWhatRequestIDRFrame,
63        kWhatShutdown,
64        kWhatMediaPullerNotify,
65        kWhatEncoderActivity,
66    };
67
68    void shutdownAsync();
69
70protected:
71    virtual ~Converter();
72    virtual void onMessageReceived(const sp<AMessage> &msg);
73
74private:
75    status_t mInitCheck;
76    sp<AMessage> mNotify;
77    sp<ALooper> mCodecLooper;
78    sp<AMessage> mInputFormat;
79    bool mIsVideo;
80    bool mIsPCMAudio;
81    sp<AMessage> mOutputFormat;
82    bool mNeedToManuallyPrependSPSPPS;
83
84    sp<MediaCodec> mEncoder;
85    sp<AMessage> mEncoderActivityNotify;
86
87    Vector<sp<ABuffer> > mEncoderInputBuffers;
88    Vector<sp<ABuffer> > mEncoderOutputBuffers;
89
90    List<size_t> mAvailEncoderInputIndices;
91
92    List<sp<ABuffer> > mInputBufferQueue;
93
94    bool mDoMoreWorkPending;
95
96#if ENABLE_SILENCE_DETECTION
97    int64_t mFirstSilentFrameUs;
98    bool mInSilentMode;
99#endif
100
101    sp<ABuffer> mPartialAudioAU;
102
103    status_t initEncoder();
104    void releaseEncoder();
105
106    status_t feedEncoderInputBuffers();
107
108    void scheduleDoMoreWork();
109    status_t doMoreWork();
110
111    void notifyError(status_t err);
112
113    // Packetizes raw PCM audio data available in mInputBufferQueue
114    // into a format suitable for transport stream inclusion and
115    // notifies the observer.
116    status_t feedRawAudioInputBuffers();
117
118    static bool IsSilence(const sp<ABuffer> &accessUnit);
119
120    DISALLOW_EVIL_CONSTRUCTORS(Converter);
121};
122
123}  // namespace android
124
125#endif  // CONVERTER_H_
126