ACodec.h revision cc54fbaa69c0b69929467449d2c19192f15b5039
1/*
2 * Copyright (C) 2010 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 A_CODEC_H_
18
19#define A_CODEC_H_
20
21#include <stdint.h>
22#include <android/native_window.h>
23#include <media/IOMX.h>
24#include <media/stagefright/foundation/AHierarchicalStateMachine.h>
25
26namespace android {
27
28struct ABuffer;
29struct MemoryDealer;
30
31struct ACodec : public AHierarchicalStateMachine {
32    enum {
33        kWhatFillThisBuffer      = 'fill',
34        kWhatDrainThisBuffer     = 'drai',
35        kWhatEOS                 = 'eos ',
36        kWhatShutdownCompleted   = 'scom',
37        kWhatFlushCompleted      = 'fcom',
38        kWhatOutputFormatChanged = 'outC',
39        kWhatError               = 'erro',
40    };
41
42    ACodec();
43
44    void setNotificationMessage(const sp<AMessage> &msg);
45    void initiateSetup(const sp<AMessage> &msg);
46    void signalFlush();
47    void signalResume();
48    void initiateShutdown();
49
50protected:
51    virtual ~ACodec();
52
53private:
54    struct BaseState;
55    struct UninitializedState;
56    struct LoadedToIdleState;
57    struct IdleToExecutingState;
58    struct ExecutingState;
59    struct OutputPortSettingsChangedState;
60    struct ExecutingToIdleState;
61    struct IdleToLoadedState;
62    struct FlushingState;
63
64    enum {
65        kWhatSetup                   = 'setu',
66        kWhatOMXMessage              = 'omx ',
67        kWhatInputBufferFilled       = 'inpF',
68        kWhatOutputBufferDrained     = 'outD',
69        kWhatShutdown                = 'shut',
70        kWhatFlush                   = 'flus',
71        kWhatResume                  = 'resm',
72        kWhatDrainDeferredMessages   = 'drai',
73    };
74
75    enum {
76        kPortIndexInput  = 0,
77        kPortIndexOutput = 1
78    };
79
80    struct BufferInfo {
81        enum Status {
82            OWNED_BY_US,
83            OWNED_BY_COMPONENT,
84            OWNED_BY_UPSTREAM,
85            OWNED_BY_DOWNSTREAM,
86            OWNED_BY_NATIVE_WINDOW,
87        };
88
89        IOMX::buffer_id mBufferID;
90        Status mStatus;
91
92        sp<ABuffer> mData;
93        sp<GraphicBuffer> mGraphicBuffer;
94    };
95
96    sp<AMessage> mNotify;
97
98    sp<UninitializedState> mUninitializedState;
99    sp<LoadedToIdleState> mLoadedToIdleState;
100    sp<IdleToExecutingState> mIdleToExecutingState;
101    sp<ExecutingState> mExecutingState;
102    sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
103    sp<ExecutingToIdleState> mExecutingToIdleState;
104    sp<IdleToLoadedState> mIdleToLoadedState;
105    sp<FlushingState> mFlushingState;
106
107    AString mComponentName;
108    sp<IOMX> mOMX;
109    IOMX::node_id mNode;
110    sp<MemoryDealer> mDealer[2];
111
112    sp<ANativeWindow> mNativeWindow;
113
114    Vector<BufferInfo> mBuffers[2];
115    bool mPortEOS[2];
116
117    List<sp<AMessage> > mDeferredQueue;
118
119    bool mSentFormat;
120
121    status_t allocateBuffersOnPort(OMX_U32 portIndex);
122    status_t freeBuffersOnPort(OMX_U32 portIndex);
123    status_t freeBuffer(OMX_U32 portIndex, size_t i);
124
125    status_t allocateOutputBuffersFromNativeWindow();
126    status_t cancelBufferToNativeWindow(BufferInfo *info);
127    status_t freeOutputBuffersNotOwnedByComponent();
128    BufferInfo *dequeueBufferFromNativeWindow();
129
130    BufferInfo *findBufferByID(
131            uint32_t portIndex, IOMX::buffer_id bufferID,
132            ssize_t *index = NULL);
133
134    void setComponentRole(bool isEncoder, const char *mime);
135    void configureCodec(const char *mime, const sp<AMessage> &msg);
136
137    status_t setVideoPortFormatType(
138            OMX_U32 portIndex,
139            OMX_VIDEO_CODINGTYPE compressionFormat,
140            OMX_COLOR_FORMATTYPE colorFormat);
141
142    status_t setSupportedOutputFormat();
143
144    status_t setupVideoDecoder(
145            const char *mime, int32_t width, int32_t height);
146
147    status_t setVideoFormatOnPort(
148            OMX_U32 portIndex,
149            int32_t width, int32_t height,
150            OMX_VIDEO_CODINGTYPE compressionFormat);
151
152    status_t setupAACDecoder(int32_t numChannels, int32_t sampleRate);
153    status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
154
155    status_t initNativeWindow();
156
157    // Returns true iff all buffers on the given port have status OWNED_BY_US.
158    bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
159
160    bool allYourBuffersAreBelongToUs();
161
162    void deferMessage(const sp<AMessage> &msg);
163    void processDeferredMessages();
164
165    void sendFormatChange();
166
167    void signalError(OMX_ERRORTYPE error = OMX_ErrorUndefined);
168
169    DISALLOW_EVIL_CONSTRUCTORS(ACodec);
170};
171
172}  // namespace android
173
174#endif  // A_CODEC_H_
175