blob: bb8bd194f47c5ef42517ef6909fdc5fe9f02672d [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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
41namespace android {
42
43class CCodecBufferChannel;
44class InputSurfaceWrapper;
45struct MediaCodecInfo;
46
47class CCodec : public CodecBase {
48public:
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();
Wonsik Kimab34ed62019-01-31 15:28:46 -080069 void onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems);
70 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -080071
72protected:
73 virtual ~CCodec();
74
75 virtual void onMessageReceived(const sp<AMessage> &msg) override;
76
77private:
Wonsik Kimab34ed62019-01-31 15:28:46 -080078 typedef std::chrono::steady_clock::time_point TimePoint;
Pawin Vongmasa36653902018-11-15 00:10:25 -080079
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 void createInputSurface();
93 void setInputSurface(const sp<PersistentSurface> &surface);
94 status_t setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
95 void setParameters(const sp<AMessage> &params);
96
97 void setDeadline(
98 const TimePoint &now,
99 const std::chrono::milliseconds &timeout,
100 const char *name);
101
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102 enum {
103 kWhatAllocate,
104 kWhatConfigure,
105 kWhatStart,
106 kWhatFlush,
107 kWhatStop,
108 kWhatRelease,
109 kWhatCreateInputSurface,
110 kWhatSetInputSurface,
111 kWhatSetParameters,
112
113 kWhatWorkDone,
114 kWhatWatch,
115 };
116
117 enum {
118 RELEASED,
119 ALLOCATED,
120 FLUSHED,
121 RUNNING,
122
123 ALLOCATING, // RELEASED -> ALLOCATED
124 STARTING, // ALLOCATED -> RUNNING
125 STOPPING, // RUNNING -> ALLOCATED
126 FLUSHING, // RUNNING -> FLUSHED
127 RESUMING, // FLUSHED -> RUNNING
128 RELEASING, // {ANY EXCEPT RELEASED} -> RELEASED
129 };
130
131 struct State {
132 inline State() : mState(RELEASED) {}
133 inline int get() const { return mState; }
134 inline void set(int newState) { mState = newState; }
135
136 std::shared_ptr<Codec2Client::Component> comp;
137 private:
138 int mState;
139 };
140
141 struct NamedTimePoint {
142 NamedTimePoint() : mTimePoint(TimePoint::max()), mName("") {}
143
144 inline void set(
145 const TimePoint &timePoint,
146 const char *name) {
147 mTimePoint = timePoint;
148 mName = name;
149 }
150
151 inline TimePoint get() const { return mTimePoint; }
152 inline const char *getName() const { return mName; }
153 private:
154 TimePoint mTimePoint;
155 const char *mName;
156 };
157
158 Mutexed<State> mState;
159 std::shared_ptr<CCodecBufferChannel> mChannel;
160
161 std::shared_ptr<Codec2Client> mClient;
162 std::shared_ptr<Codec2Client::Listener> mClientListener;
163 struct ClientListener;
164
165 Mutexed<NamedTimePoint> mDeadline;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800166 typedef CCodecConfig Config;
167 Mutexed<Config> mConfig;
168 Mutexed<std::list<std::unique_ptr<C2Work>>> mWorkDoneQueue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800169
170 friend class CCodecCallbackImpl;
171
172 DISALLOW_EVIL_CONSTRUCTORS(CCodec);
173};
174
175} // namespace android
176
177#endif // C_CODEC_H_