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 <atomic> 21 #include <chrono> 22 #include <list> 23 #include <memory> 24 #include <set> 25 26 #include <C2Component.h> 27 #include <codec2/hidl/client.h> 28 29 #include <android/native_window.h> 30 #include <media/hardware/MetadataBufferType.h> 31 #include <media/stagefright/foundation/Mutexed.h> 32 #include <media/stagefright/CodecBase.h> 33 #include <media/stagefright/FrameRenderTracker.h> 34 #include <media/stagefright/MediaDefs.h> 35 #include <media/stagefright/SkipCutBuffer.h> 36 #include <utils/NativeHandle.h> 37 #include <hardware/gralloc.h> 38 #include <nativebase/nativebase.h> 39 40 namespace android { 41 42 class CCodecBufferChannel; 43 class InputSurfaceWrapper; 44 struct CCodecConfig; 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> ¶ms) 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 static PersistentSurface *CreateInputSurface(); 73 74 static status_t CanFetchLinearBlock( 75 const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible); 76 77 static std::shared_ptr<C2LinearBlock> FetchLinearBlock( 78 size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names); 79 80 static status_t CanFetchGraphicBlock( 81 const std::vector<std::string> &names, bool *isCompatible); 82 83 static std::shared_ptr<C2GraphicBlock> FetchGraphicBlock( 84 int32_t width, 85 int32_t height, 86 int32_t format, 87 uint64_t usage, 88 const std::vector<std::string> &names); 89 90 protected: 91 virtual ~CCodec(); 92 93 virtual void onMessageReceived(const sp<AMessage> &msg) override; 94 95 private: 96 typedef std::chrono::steady_clock::time_point TimePoint; 97 98 status_t tryAndReportOnError(std::function<status_t()> job); 99 100 void initiateStop(); 101 void initiateRelease(bool sendCallback = true); 102 103 void allocate(const sp<MediaCodecInfo> &codecInfo); 104 void configure(const sp<AMessage> &msg); 105 void start(); 106 void stop(); 107 void flush(); 108 void release(bool sendCallback); 109 110 /** 111 * Creates an input surface for the current device configuration compatible with CCodec. 112 * This could be backed by the C2 HAL or the OMX HAL. 113 */ 114 static sp<PersistentSurface> CreateCompatibleInputSurface(); 115 116 /// Creates an input surface to the OMX HAL 117 static sp<PersistentSurface> CreateOmxInputSurface(); 118 119 /// handle a create input surface call 120 void createInputSurface(); 121 void setInputSurface(const sp<PersistentSurface> &surface); 122 status_t setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface); 123 124 void setDeadline( 125 const TimePoint &now, 126 const std::chrono::milliseconds &timeout, 127 const char *name); 128 129 enum { 130 kWhatAllocate, 131 kWhatConfigure, 132 kWhatStart, 133 kWhatFlush, 134 kWhatStop, 135 kWhatRelease, 136 kWhatCreateInputSurface, 137 kWhatSetInputSurface, 138 kWhatSetParameters, 139 140 kWhatWorkDone, 141 kWhatWatch, 142 }; 143 144 enum { 145 RELEASED, 146 ALLOCATED, 147 FLUSHED, 148 RUNNING, 149 150 ALLOCATING, // RELEASED -> ALLOCATED 151 STARTING, // ALLOCATED -> RUNNING 152 STOPPING, // RUNNING -> ALLOCATED 153 FLUSHING, // RUNNING -> FLUSHED 154 RESUMING, // FLUSHED -> RUNNING 155 RELEASING, // {ANY EXCEPT RELEASED} -> RELEASED 156 }; 157 158 struct State { StateState159 inline State() : mState(RELEASED) {} getState160 inline int get() const { return mState; } setState161 inline void set(int newState) { mState = newState; } 162 163 std::shared_ptr<Codec2Client::Component> comp; 164 private: 165 int mState; 166 }; 167 168 struct NamedTimePoint { NamedTimePointNamedTimePoint169 NamedTimePoint() : mTimePoint(TimePoint::max()), mName("") {} 170 setNamedTimePoint171 inline void set( 172 const TimePoint &timePoint, 173 const char *name) { 174 mTimePoint = timePoint; 175 mName = name; 176 } 177 getNamedTimePoint178 inline TimePoint get() const { return mTimePoint; } getNameNamedTimePoint179 inline const char *getName() const { return mName; } 180 private: 181 TimePoint mTimePoint; 182 const char *mName; 183 }; 184 185 Mutexed<State> mState; 186 std::shared_ptr<CCodecBufferChannel> mChannel; 187 188 std::shared_ptr<Codec2Client> mClient; 189 std::shared_ptr<Codec2Client::Listener> mClientListener; 190 struct ClientListener; 191 192 Mutexed<NamedTimePoint> mDeadline; 193 194 Mutexed<std::unique_ptr<CCodecConfig>> mConfig; 195 Mutexed<std::list<std::unique_ptr<C2Work>>> mWorkDoneQueue; 196 std::atomic_flag mSentConfigAfterResume; 197 198 friend class CCodecCallbackImpl; 199 200 DISALLOW_EVIL_CONSTRUCTORS(CCodec); 201 }; 202 203 } // namespace android 204 205 #endif // C_CODEC_H_ 206