blob: cc826b498f9b1ad40d920400111c2e653f260673 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
Sungtak Lee64c9d932024-03-26 03:46:53 +00002 * Copyright 2024, The Android Open Source Project
Pawin Vongmasa36653902018-11-15 00:10:25 -08003 *
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
Sungtak Lee64c9d932024-03-26 03:46:53 +000017#pragma once
Pawin Vongmasa36653902018-11-15 00:10:25 -080018
19#include <atomic>
20
21#include <android/IOMXBufferSource.h>
Sungtak Lee64c9d932024-03-26 03:46:53 +000022#include <aidl/android/media/IAidlBufferSource.h>
23#include <aidl/android/media/IAidlNode.h>
Wonsik Kima261e382019-04-10 11:37:50 -070024#include <codec2/hidl/client.h>
25#include <media/stagefright/foundation/Mutexed.h>
Sungtak Lee64c9d932024-03-26 03:46:53 +000026#include <media/stagefright/aidlpersistentsurface/C2NodeDef.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080027
28namespace android {
29
30/**
31 * IOmxNode implementation around codec 2.0 component, only to be used in
Sungtak Lee64c9d932024-03-26 03:46:53 +000032 * IGraphicBufferSource::configure. Only subset of IOmxNode API is implemented.
33 * As a result, one cannot expect this IOmxNode to work in any other usage than
34 * IGraphicBufferSource(if aidl hal is used, IAidlGraphicBufferSource).
Pawin Vongmasa36653902018-11-15 00:10:25 -080035 */
Sungtak Lee64c9d932024-03-26 03:46:53 +000036struct C2NodeImpl {
37 explicit C2NodeImpl(const std::shared_ptr<Codec2Client::Component> &comp, bool aidl);
38 ~C2NodeImpl();
Pawin Vongmasa36653902018-11-15 00:10:25 -080039
Sungtak Lee64c9d932024-03-26 03:46:53 +000040 // IOMXNode and/or IAidlNode
41 status_t freeNode();
42
43 void onFirstInputFrame();
44 void getConsumerUsageBits(uint64_t *usage /* nonnull */);
45 void getInputBufferParams(
46 ::aidl::android::media::IAidlNode::InputBufferParams *params /* nonnull */);
47 void setConsumerUsageBits(uint64_t usage);
48 void setAdjustTimestampGapUs(int32_t gapUs);
49
Pawin Vongmasa36653902018-11-15 00:10:25 -080050 status_t setInputSurface(
Sungtak Lee64c9d932024-03-26 03:46:53 +000051 const sp<IOMXBufferSource> &bufferSource);
52 status_t setAidlInputSurface(
53 const std::shared_ptr<::aidl::android::media::IAidlBufferSource> &aidlBufferSource);
54
55 status_t submitBuffer(
56 uint32_t buffer, const sp<GraphicBuffer> &graphicBuffer,
57 uint32_t flags, int64_t timestamp, int fenceFd);
58 status_t onDataspaceChanged(uint32_t dataSpace, uint32_t pixelFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -080059
Wonsik Kim4f3314d2019-03-26 17:00:34 -070060 /**
61 * Returns underlying IOMXBufferSource object.
62 */
Pawin Vongmasa36653902018-11-15 00:10:25 -080063 sp<IOMXBufferSource> getSource();
Wonsik Kim4f3314d2019-03-26 17:00:34 -070064
65 /**
Sungtak Lee64c9d932024-03-26 03:46:53 +000066 * Returns underlying IAidlBufferSource object.
67 */
68 std::shared_ptr<::aidl::android::media::IAidlBufferSource> getAidlSource();
69
70 /**
Wonsik Kim4f3314d2019-03-26 17:00:34 -070071 * Configure the frame size.
72 */
Pawin Vongmasa36653902018-11-15 00:10:25 -080073 void setFrameSize(uint32_t width, uint32_t height);
74
Wonsik Kim4f3314d2019-03-26 17:00:34 -070075 /**
Wonsik Kim1951d932024-05-23 22:59:00 +000076 * Notify that the input buffer reference is no longer needed by the component.
77 * Clean up if necessary.
Wonsik Kim4f3314d2019-03-26 17:00:34 -070078 *
79 * \param index input work index
80 */
81 void onInputBufferDone(c2_cntr64_t index);
82
Wonsik Kim673dd192021-01-29 14:58:12 -080083 /**
Wonsik Kim1951d932024-05-23 22:59:00 +000084 * Notify input buffer is emptied.
85 */
86 void onInputBufferEmptied();
87
88 /**
Wonsik Kim673dd192021-01-29 14:58:12 -080089 * Returns dataspace information from GraphicBufferSource.
90 */
91 android_dataspace getDataspace();
92
Wonsik Kima1335e12021-04-22 16:28:29 -070093 /**
Songyue Hanad01f6a2023-08-17 05:45:35 +000094 * Returns dataspace information from GraphicBufferSource.
95 */
96 uint32_t getPixelFormat();
97
98 /**
Wonsik Kima1335e12021-04-22 16:28:29 -070099 * Sets priority of the queue thread.
100 */
101 void setPriority(int priority);
102
Pawin Vongmasa36653902018-11-15 00:10:25 -0800103private:
104 std::weak_ptr<Codec2Client::Component> mComp;
Sungtak Lee64c9d932024-03-26 03:46:53 +0000105
Pawin Vongmasa36653902018-11-15 00:10:25 -0800106 sp<IOMXBufferSource> mBufferSource;
Sungtak Lee64c9d932024-03-26 03:46:53 +0000107 std::shared_ptr<::aidl::android::media::IAidlBufferSource> mAidlBufferSource;
108
Pawin Vongmasa36653902018-11-15 00:10:25 -0800109 std::shared_ptr<C2Allocator> mAllocator;
110 std::atomic_uint64_t mFrameIndex;
111 uint32_t mWidth;
112 uint32_t mHeight;
113 uint64_t mUsage;
Wonsik Kim673dd192021-01-29 14:58:12 -0800114 Mutexed<android_dataspace> mDataspace;
Songyue Hanad01f6a2023-08-17 05:45:35 +0000115 Mutexed<uint32_t> mPixelFormat;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800116
117 // WORKAROUND: timestamp adjustment
118
119 // if >0: this is the max timestamp gap, if <0: this is -1 times the fixed timestamp gap
120 // if 0: no timestamp adjustment is made
121 // note that C2OMXNode can be recycled between encoding sessions.
122 int32_t mAdjustTimestampGapUs;
123 bool mFirstInputFrame; // true for first input
124 c2_cntr64_t mPrevInputTimestamp; // input timestamp for previous frame
125 c2_cntr64_t mPrevCodecTimestamp; // adjusted (codec) timestamp for previous frame
Wonsik Kim4f3314d2019-03-26 17:00:34 -0700126
Wonsik Kim1951d932024-05-23 22:59:00 +0000127 // Tracks the status of buffers
128 struct BuffersTracker {
129 BuffersTracker() = default;
130
131 // Keeps track of buffers that are used by the component. Maps timestamp -> ID
132 std::map<uint64_t, uint32_t> mIdsInUse;
133 // Keeps track of the buffer IDs that are available after being released from the component.
134 std::list<uint32_t> mAvailableIds;
135 };
136 Mutexed<BuffersTracker> mBuffersTracker;
Wonsik Kim831b8d72019-06-11 17:52:56 -0700137
138 class QueueThread;
139 sp<QueueThread> mQueueThread;
Sungtak Lee64c9d932024-03-26 03:46:53 +0000140
141 bool mAidlHal;
Wonsik Kim1951d932024-05-23 22:59:00 +0000142
143 bool hasBufferSource();
144 void notifyInputBufferEmptied(int32_t bufferId);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800145};
146
147} // namespace android