• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 C_CODEC_H_
18 #define C_CODEC_H_
19 
20 #include <chrono>
21 #include <list>
22 #include <memory>
23 #include <set>
24 
25 #include <C2Component.h>
26 #include <codec2/hidl/client.h>
27 
28 #include <android/native_window.h>
29 #include <media/hardware/MetadataBufferType.h>
30 #include <media/stagefright/foundation/Mutexed.h>
31 #include <media/stagefright/CodecBase.h>
32 #include <media/stagefright/FrameRenderTracker.h>
33 #include <media/stagefright/MediaDefs.h>
34 #include <media/stagefright/SkipCutBuffer.h>
35 #include <utils/NativeHandle.h>
36 #include <hardware/gralloc.h>
37 #include <nativebase/nativebase.h>
38 
39 #include "CCodecConfig.h"
40 
41 namespace android {
42 
43 class CCodecBufferChannel;
44 class InputSurfaceWrapper;
45 struct MediaCodecInfo;
46 
47 class CCodec : public CodecBase {
48 public:
49     CCodec();
50 
51     virtual std::shared_ptr<BufferChannelBase> getBufferChannel() override;
52     virtual void initiateAllocateComponent(const sp<AMessage> &msg) override;
53     virtual void initiateConfigureComponent(const sp<AMessage> &msg) override;
54     virtual void initiateCreateInputSurface() override;
55     virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface) override;
56     virtual void initiateStart() override;
57     virtual void initiateShutdown(bool keepComponentAllocated = false) override;
58 
59     virtual status_t setSurface(const sp<Surface> &surface) override;
60 
61     virtual void signalFlush() override;
62     virtual void signalResume() override;
63 
64     virtual void signalSetParameters(const sp<AMessage> &params) override;
65     virtual void signalEndOfInputStream() override;
66     virtual void signalRequestIDRFrame() override;
67 
68     void initiateReleaseIfStuck();
69     void onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems);
70     void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
71 
72 protected:
73     virtual ~CCodec();
74 
75     virtual void onMessageReceived(const sp<AMessage> &msg) override;
76 
77 private:
78     typedef std::chrono::steady_clock::time_point TimePoint;
79 
80     status_t tryAndReportOnError(std::function<status_t()> job);
81 
82     void initiateStop();
83     void initiateRelease(bool sendCallback = true);
84 
85     void allocate(const sp<MediaCodecInfo> &codecInfo);
86     void configure(const sp<AMessage> &msg);
87     void start();
88     void stop();
89     void flush();
90     void release(bool sendCallback);
91 
92     /**
93      * Creates an input surface for the current device configuration compatible with CCodec.
94      * This could be backed by the C2 HAL or the OMX HAL.
95      */
96     static sp<PersistentSurface> CreateCompatibleInputSurface();
97 
98     /// Creates an input surface to the OMX HAL
99     static sp<PersistentSurface> CreateOmxInputSurface();
100 
101     /// handle a create input surface call
102     void createInputSurface();
103     void setInputSurface(const sp<PersistentSurface> &surface);
104     status_t setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
105 
106     void setDeadline(
107             const TimePoint &now,
108             const std::chrono::milliseconds &timeout,
109             const char *name);
110 
111     enum {
112         kWhatAllocate,
113         kWhatConfigure,
114         kWhatStart,
115         kWhatFlush,
116         kWhatStop,
117         kWhatRelease,
118         kWhatCreateInputSurface,
119         kWhatSetInputSurface,
120         kWhatSetParameters,
121 
122         kWhatWorkDone,
123         kWhatWatch,
124     };
125 
126     enum {
127         RELEASED,
128         ALLOCATED,
129         FLUSHED,
130         RUNNING,
131 
132         ALLOCATING,  // RELEASED -> ALLOCATED
133         STARTING,    // ALLOCATED -> RUNNING
134         STOPPING,    // RUNNING -> ALLOCATED
135         FLUSHING,    // RUNNING -> FLUSHED
136         RESUMING,    // FLUSHED -> RUNNING
137         RELEASING,   // {ANY EXCEPT RELEASED} -> RELEASED
138     };
139 
140     struct State {
StateState141         inline State() : mState(RELEASED) {}
getState142         inline int get() const { return mState; }
setState143         inline void set(int newState) { mState = newState; }
144 
145         std::shared_ptr<Codec2Client::Component> comp;
146     private:
147         int mState;
148     };
149 
150     struct NamedTimePoint {
NamedTimePointNamedTimePoint151         NamedTimePoint() : mTimePoint(TimePoint::max()), mName("") {}
152 
setNamedTimePoint153         inline void set(
154                 const TimePoint &timePoint,
155                 const char *name) {
156             mTimePoint = timePoint;
157             mName = name;
158         }
159 
getNamedTimePoint160         inline TimePoint get() const { return mTimePoint; }
getNameNamedTimePoint161         inline const char *getName() const { return mName; }
162     private:
163         TimePoint mTimePoint;
164         const char *mName;
165     };
166 
167     Mutexed<State> mState;
168     std::shared_ptr<CCodecBufferChannel> mChannel;
169 
170     std::shared_ptr<Codec2Client> mClient;
171     std::shared_ptr<Codec2Client::Listener> mClientListener;
172     struct ClientListener;
173 
174     Mutexed<NamedTimePoint> mDeadline;
175     typedef CCodecConfig Config;
176     Mutexed<Config> mConfig;
177     Mutexed<std::list<std::unique_ptr<C2Work>>> mWorkDoneQueue;
178 
179     friend class CCodecCallbackImpl;
180 
181     DISALLOW_EVIL_CONSTRUCTORS(CCodec);
182 };
183 
184 }  // namespace android
185 
186 #endif  // C_CODEC_H_
187