blob: e060fd81017cbdc6f4b2f3b8465a959875a439a7 [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 /**
76 * Clean up work item reference.
77 *
78 * \param index input work index
79 */
80 void onInputBufferDone(c2_cntr64_t index);
81
Wonsik Kim673dd192021-01-29 14:58:12 -080082 /**
83 * Returns dataspace information from GraphicBufferSource.
84 */
85 android_dataspace getDataspace();
86
Wonsik Kima1335e12021-04-22 16:28:29 -070087 /**
Songyue Hanad01f6a2023-08-17 05:45:35 +000088 * Returns dataspace information from GraphicBufferSource.
89 */
90 uint32_t getPixelFormat();
91
92 /**
Wonsik Kima1335e12021-04-22 16:28:29 -070093 * Sets priority of the queue thread.
94 */
95 void setPriority(int priority);
96
Pawin Vongmasa36653902018-11-15 00:10:25 -080097private:
98 std::weak_ptr<Codec2Client::Component> mComp;
Sungtak Lee64c9d932024-03-26 03:46:53 +000099
Pawin Vongmasa36653902018-11-15 00:10:25 -0800100 sp<IOMXBufferSource> mBufferSource;
Sungtak Lee64c9d932024-03-26 03:46:53 +0000101 std::shared_ptr<::aidl::android::media::IAidlBufferSource> mAidlBufferSource;
102
Pawin Vongmasa36653902018-11-15 00:10:25 -0800103 std::shared_ptr<C2Allocator> mAllocator;
104 std::atomic_uint64_t mFrameIndex;
105 uint32_t mWidth;
106 uint32_t mHeight;
107 uint64_t mUsage;
Wonsik Kim673dd192021-01-29 14:58:12 -0800108 Mutexed<android_dataspace> mDataspace;
Songyue Hanad01f6a2023-08-17 05:45:35 +0000109 Mutexed<uint32_t> mPixelFormat;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800110
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 Kim4f3314d2019-03-26 17:00:34 -0700120
Sungtak Lee64c9d932024-03-26 03:46:53 +0000121 Mutexed<std::map<uint64_t, uint32_t>> mBufferIdsInUse;
Wonsik Kim831b8d72019-06-11 17:52:56 -0700122
123 class QueueThread;
124 sp<QueueThread> mQueueThread;
Sungtak Lee64c9d932024-03-26 03:46:53 +0000125
126 bool mAidlHal;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800127};
128
129} // namespace android