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 | /** |
| 76 | * Clean up work item reference. |
| 77 | * |
| 78 | * \param index input work index |
| 79 | */ |
| 80 | void onInputBufferDone(c2_cntr64_t index); |
| 81 | |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Returns dataspace information from GraphicBufferSource. |
| 84 | */ |
| 85 | android_dataspace getDataspace(); |
| 86 | |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 87 | /** |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 88 | * Returns dataspace information from GraphicBufferSource. |
| 89 | */ |
| 90 | uint32_t getPixelFormat(); |
| 91 | |
| 92 | /** |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 93 | * Sets priority of the queue thread. |
| 94 | */ |
| 95 | void setPriority(int priority); |
| 96 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 97 | private: |
| 98 | std::weak_ptr<Codec2Client::Component> mComp; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 99 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 100 | sp<IOMXBufferSource> mBufferSource; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 101 | std::shared_ptr<::aidl::android::media::IAidlBufferSource> mAidlBufferSource; |
| 102 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 103 | std::shared_ptr<C2Allocator> mAllocator; |
| 104 | std::atomic_uint64_t mFrameIndex; |
| 105 | uint32_t mWidth; |
| 106 | uint32_t mHeight; |
| 107 | uint64_t mUsage; |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 108 | Mutexed<android_dataspace> mDataspace; |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 109 | Mutexed<uint32_t> mPixelFormat; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 110 | |
| 111 | // WORKAROUND: timestamp adjustment |
| 112 | |
| 113 | // if >0: this is the max timestamp gap, if <0: this is -1 times the fixed timestamp gap |
| 114 | // if 0: no timestamp adjustment is made |
| 115 | // note that C2OMXNode can be recycled between encoding sessions. |
| 116 | int32_t mAdjustTimestampGapUs; |
| 117 | bool mFirstInputFrame; // true for first input |
| 118 | c2_cntr64_t mPrevInputTimestamp; // input timestamp for previous frame |
| 119 | c2_cntr64_t mPrevCodecTimestamp; // adjusted (codec) timestamp for previous frame |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 120 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 121 | Mutexed<std::map<uint64_t, uint32_t>> mBufferIdsInUse; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 122 | |
| 123 | class QueueThread; |
| 124 | sp<QueueThread> mQueueThread; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 125 | |
| 126 | bool mAidlHal; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace android |