-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
231 lines (211 loc) · 6.04 KB
/
main.go
File metadata and controls
231 lines (211 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Codec2 H.264 encoding via HIDL hwbinder.
//
// This example connects to the Codec2 software IComponentStore via HIDL
// hwbinder, creates an H.264 encoder component, feeds a single gray YUV
// frame, and reports the result.
//
// Build:
//
// GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -buildmode=pie -o build/codec2_encode ./examples/codec2_encode/
// patchelf --set-interpreter /system/bin/linker64 --replace-needed libdl.so.2 libdl.so --replace-needed libpthread.so.0 libc.so --replace-needed libc.so.6 libc.so build/codec2_encode
// adb push build/codec2_encode /data/local/tmp/ && adb shell /data/local/tmp/codec2_encode
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/codec2/hidlcodec2"
"github.com/AndroidGoLab/binder/kernelbinder"
"golang.org/x/sys/unix"
)
const (
avcEncoderName = "c2.android.avc.encoder"
width = 320
height = 240
bitrate = 512000
)
func makeGrayYUVFrame(w, h int) []byte {
ySize := w * h
uvSize := (w / 2) * (h / 2) * 2 // NV12: interleaved UV
frame := make([]byte, ySize+uvSize)
for i := range frame {
frame[i] = 128
}
return frame
}
func run(ctx context.Context) error {
// Open hwbinder for HIDL services.
drv, err := kernelbinder.Open(ctx,
binder.WithDevicePath("/dev/hwbinder"),
binder.WithMapSize(256*1024),
)
if err != nil {
return fmt.Errorf("open hwbinder: %w", err)
}
defer func() { _ = drv.Close(ctx) }()
// Connect to the Codec2 IComponentStore service.
store, err := hidlcodec2.GetComponentStore(ctx, drv)
if err != nil {
return fmt.Errorf("get component store: %w", err)
}
fmt.Printf("Connected to HIDL Codec2 store (handle=%d)\n", store.Handle())
// List components.
components, err := store.ListComponents(ctx)
if err != nil {
return fmt.Errorf("list components: %w", err)
}
fmt.Printf("Found %d Codec2 components\n", len(components))
var found bool
for _, comp := range components {
if comp.Name == avcEncoderName {
found = true
fmt.Printf(" -> %s [%s] rank=%d\n", comp.Name, comp.MediaType, comp.Rank)
}
}
if !found {
return fmt.Errorf("encoder %s not found", avcEncoderName)
}
// Register a listener stub for callbacks.
workDoneCh := make(chan []byte, 16)
listener := &hidlcodec2.ComponentListenerStub{
OnWorkDone: func(data []byte) {
cp := make([]byte, len(data))
copy(cp, data)
select {
case workDoneCh <- cp:
default:
}
},
}
listenerCookie := hidlcodec2.RegisterListener(ctx, drv, listener)
defer hidlcodec2.UnregisterListener(ctx, drv, listenerCookie)
// Create the encoder component.
component, err := store.CreateComponent(ctx, avcEncoderName, listenerCookie)
if err != nil {
return fmt.Errorf("create component: %w", err)
}
defer func() { _ = component.Release(ctx) }()
fmt.Printf("Encoder component created (handle=%d)\n", component.Handle())
// Configure via IConfigurable.
iface, err := component.GetInterface(ctx)
if err != nil {
return fmt.Errorf("get interface: %w", err)
}
cfg, err := iface.GetConfigurable(ctx)
if err != nil {
return fmt.Errorf("get configurable: %w", err)
}
configParams := hidlcodec2.ConcatParams(
hidlcodec2.BuildPictureSizeParam(1, width, height),
hidlcodec2.BuildBitrateParam(1, bitrate),
)
cfgStatus, _, err := cfg.Config(ctx, configParams, true)
if err != nil {
return fmt.Errorf("config: %w", err)
}
fmt.Printf("Config: status=%s\n", cfgStatus)
// Start the encoder.
if err := component.Start(ctx); err != nil {
return fmt.Errorf("start: %w", err)
}
defer func() { _ = component.Stop(ctx) }()
fmt.Println("Encoder started")
// Create a gray frame in a memfd.
frameData := makeGrayYUVFrame(width, height)
frameFd, err := unix.MemfdCreate("c2-frame", 0)
if err != nil {
return fmt.Errorf("memfd_create: %w", err)
}
defer unix.Close(frameFd)
if _, err := unix.Write(frameFd, frameData); err != nil {
return fmt.Errorf("write memfd: %w", err)
}
// Build the work bundle with proper C2Handle format.
workBundle := &hidlcodec2.WorkBundle{
Works: []hidlcodec2.Work{
{
Input: hidlcodec2.FrameData{
Flags: 0,
Ordinal: hidlcodec2.WorkOrdinal{
TimestampUs: 0,
FrameIndex: 0,
CustomOrdinal: 0,
},
Buffers: []hidlcodec2.Buffer{
{
Blocks: []hidlcodec2.Block{
{
Index: 0,
Meta: hidlcodec2.BuildRangeInfoParam(0, uint32(len(frameData))),
},
},
},
},
},
Worklets: []hidlcodec2.Worklet{
{ComponentId: 0},
},
WorkletsProcessed: 0,
Result: hidlcodec2.StatusOK,
},
},
BaseBlocks: []hidlcodec2.BaseBlock{
{
Tag: 0, // nativeBlock
NativeBlockFds: []int32{int32(frameFd)},
NativeBlockInts: hidlcodec2.C2HandleLinearInts(uint64(len(frameData))),
},
},
}
if err := component.Queue(ctx, workBundle); err != nil {
return fmt.Errorf("queue: %w", err)
}
fmt.Println("Frame queued")
// Send EOS.
eosBundle := &hidlcodec2.WorkBundle{
Works: []hidlcodec2.Work{
{
Input: hidlcodec2.FrameData{
Flags: hidlcodec2.FrameDataEndOfStream,
Ordinal: hidlcodec2.WorkOrdinal{
TimestampUs: 33333,
FrameIndex: 1,
CustomOrdinal: 0,
},
},
Worklets: []hidlcodec2.Worklet{
{ComponentId: 0},
},
WorkletsProcessed: 0,
Result: hidlcodec2.StatusOK,
},
},
}
if err := component.Queue(ctx, eosBundle); err != nil {
return fmt.Errorf("queue EOS: %w", err)
}
fmt.Println("EOS queued")
// Wait for output callback or timeout.
select {
case data := <-workDoneCh:
fmt.Printf("OnWorkDone received (%d bytes)\n", len(data))
case <-time.After(5 * time.Second):
fmt.Println("Timeout waiting for callback; trying Flush...")
if flushErr := component.Flush(ctx); flushErr != nil {
fmt.Printf("Flush error: %v\n", flushErr)
} else {
fmt.Println("Flush succeeded")
}
}
fmt.Println("Encode pipeline completed successfully")
return nil
}
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}