Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 2 | * Copyright 2024, The Android Open Source Project |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 17 | #pragma once |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 18 | |
| 19 | #include <atomic> |
| 20 | |
| 21 | #include <android/IOMXBufferSource.h> |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 22 | #include <aidl/android/media/IAidlBufferSource.h> |
| 23 | #include <aidl/android/media/IAidlNode.h> |
Wonsik Kim | a261e38 | 2019-04-10 11:37:50 -0700 | [diff] [blame] | 24 | #include <codec2/hidl/client.h> |
| 25 | #include <media/stagefright/foundation/Mutexed.h> |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 26 | #include <media/stagefright/aidlpersistentsurface/C2NodeDef.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | /** |
| 31 | * IOmxNode implementation around codec 2.0 component, only to be used in |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 32 | * 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 Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 35 | */ |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 36 | struct C2NodeImpl { |
| 37 | explicit C2NodeImpl(const std::shared_ptr<Codec2Client::Component> &comp, bool aidl); |
| 38 | ~C2NodeImpl(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 39 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 40 | // 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 Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 50 | status_t setInputSurface( |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 51 | 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 Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 59 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Returns underlying IOMXBufferSource object. |
| 62 | */ |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 63 | sp<IOMXBufferSource> getSource(); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 66 | * Returns underlying IAidlBufferSource object. |
| 67 | */ |
| 68 | std::shared_ptr<::aidl::android::media::IAidlBufferSource> getAidlSource(); |
| 69 | |
| 70 | /** |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 71 | * Configure the frame size. |
| 72 | */ |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 73 | void setFrameSize(uint32_t width, uint32_t height); |
| 74 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 75 | /** |
Wonsik Kim | 1951d93 | 2024-05-23 22:59:00 +0000 | [diff] [blame] | 76 | * Notify that the input buffer reference is no longer needed by the component. |
| 77 | * Clean up if necessary. |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 78 | * |
| 79 | * \param index input work index |
| 80 | */ |
| 81 | void onInputBufferDone(c2_cntr64_t index); |
| 82 | |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 83 | /** |
Wonsik Kim | 1951d93 | 2024-05-23 22:59:00 +0000 | [diff] [blame] | 84 | * Notify input buffer is emptied. |
| 85 | */ |
| 86 | void onInputBufferEmptied(); |
| 87 | |
| 88 | /** |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 89 | * Returns dataspace information from GraphicBufferSource. |
| 90 | */ |
| 91 | android_dataspace getDataspace(); |
| 92 | |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 93 | /** |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 94 | * Returns dataspace information from GraphicBufferSource. |
| 95 | */ |
| 96 | uint32_t getPixelFormat(); |
| 97 | |
| 98 | /** |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 99 | * Sets priority of the queue thread. |
| 100 | */ |
| 101 | void setPriority(int priority); |
| 102 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 103 | private: |
| 104 | std::weak_ptr<Codec2Client::Component> mComp; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 105 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 106 | sp<IOMXBufferSource> mBufferSource; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 107 | std::shared_ptr<::aidl::android::media::IAidlBufferSource> mAidlBufferSource; |
| 108 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 109 | std::shared_ptr<C2Allocator> mAllocator; |
| 110 | std::atomic_uint64_t mFrameIndex; |
| 111 | uint32_t mWidth; |
| 112 | uint32_t mHeight; |
| 113 | uint64_t mUsage; |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 114 | Mutexed<android_dataspace> mDataspace; |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 115 | Mutexed<uint32_t> mPixelFormat; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 116 | |
| 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 Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 126 | |
Wonsik Kim | 1951d93 | 2024-05-23 22:59:00 +0000 | [diff] [blame] | 127 | // 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 Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 137 | |
| 138 | class QueueThread; |
| 139 | sp<QueueThread> mQueueThread; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame] | 140 | |
| 141 | bool mAidlHal; |
Wonsik Kim | 1951d93 | 2024-05-23 22:59:00 +0000 | [diff] [blame] | 142 | |
| 143 | bool hasBufferSource(); |
| 144 | void notifyInputBufferEmptied(int32_t bufferId); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace android |