/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef C_CODEC_H_ #define C_CODEC_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "CCodecConfig.h" namespace android { class CCodecBufferChannel; class InputSurfaceWrapper; struct MediaCodecInfo; class CCodec : public CodecBase { public: CCodec(); virtual std::shared_ptr getBufferChannel() override; virtual void initiateAllocateComponent(const sp &msg) override; virtual void initiateConfigureComponent(const sp &msg) override; virtual void initiateCreateInputSurface() override; virtual void initiateSetInputSurface(const sp &surface) override; virtual void initiateStart() override; virtual void initiateShutdown(bool keepComponentAllocated = false) override; virtual status_t setSurface(const sp &surface) override; virtual void signalFlush() override; virtual void signalResume() override; virtual void signalSetParameters(const sp ¶ms) override; virtual void signalEndOfInputStream() override; virtual void signalRequestIDRFrame() override; void initiateReleaseIfStuck(); void onWorkDone(std::list> &workItems, size_t numDiscardedInputBuffers); void onInputBufferDone(const std::shared_ptr& buffer); protected: virtual ~CCodec(); virtual void onMessageReceived(const sp &msg) override; private: typedef std::chrono::time_point TimePoint; status_t tryAndReportOnError(std::function job); void initiateStop(); void initiateRelease(bool sendCallback = true); void allocate(const sp &codecInfo); void configure(const sp &msg); void start(); void stop(); void flush(); void release(bool sendCallback); void createInputSurface(); void setInputSurface(const sp &surface); status_t setupInputSurface(const std::shared_ptr &surface); void setParameters(const sp ¶ms); void setDeadline( const TimePoint &now, const std::chrono::milliseconds &timeout, const char *name); void onWorkQueued(bool eos); void subQueuedWorkCount(uint32_t count); enum { kWhatAllocate, kWhatConfigure, kWhatStart, kWhatFlush, kWhatStop, kWhatRelease, kWhatCreateInputSurface, kWhatSetInputSurface, kWhatSetParameters, kWhatWorkDone, kWhatWatch, }; enum { RELEASED, ALLOCATED, FLUSHED, RUNNING, ALLOCATING, // RELEASED -> ALLOCATED STARTING, // ALLOCATED -> RUNNING STOPPING, // RUNNING -> ALLOCATED FLUSHING, // RUNNING -> FLUSHED RESUMING, // FLUSHED -> RUNNING RELEASING, // {ANY EXCEPT RELEASED} -> RELEASED }; struct State { inline State() : mState(RELEASED) {} inline int get() const { return mState; } inline void set(int newState) { mState = newState; } std::shared_ptr comp; private: int mState; }; struct NamedTimePoint { NamedTimePoint() : mTimePoint(TimePoint::max()), mName("") {} inline void set( const TimePoint &timePoint, const char *name) { mTimePoint = timePoint; mName = name; } inline TimePoint get() const { return mTimePoint; } inline const char *getName() const { return mName; } private: TimePoint mTimePoint; const char *mName; }; Mutexed mState; std::shared_ptr mChannel; std::shared_ptr mClient; std::shared_ptr mClientListener; struct ClientListener; Mutexed mDeadline; std::atomic_int32_t mQueuedWorkCount; Mutexed mQueueDeadline; Mutexed mEosDeadline; typedef CCodecConfig Config; Mutexed mConfig; Mutexed>> mWorkDoneQueue; Mutexed> mNumDiscardedInputBuffersQueue; friend class CCodecCallbackImpl; DISALLOW_EVIL_CONSTRUCTORS(CCodec); }; } // namespace android #endif // C_CODEC_H_