-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport.go
154 lines (119 loc) · 4.65 KB
/
port.go
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
package mmal
/*
#include <stdio.h>
#include <interface/mmal/mmal.h>
#include "interface/mmal/util/mmal_util.h"
void portCbWrapper(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer);
*/
import "C"
import (
"math"
"sync"
"unsafe"
)
type PortType uint32
const (
PortTypeUnknown PortType = iota // Unknown port type
PortTypeControl // Control port
PortTypeInput // Input port
PortTypeOutput // Output port
PortTypeClock // Clock port
PortTypeInvalid PortType = 0xffffffff
)
type Port struct {
priv uintptr // Private member used by the framework
name *C.char // Port name. Used for debugging purposes (Read Only)
Type PortType // Type of the port (Read Only)
Index uint16 // Index of the port in its type list (Read Only)
IndexAll uint16 // Index of the port in the list of all ports (Read Only)
IsEnabled uint32 // Indicates whether the port is enabled or not (Read Only)
Format *ESFormat // Format of the elementary stream
BufferNumMin uint32 // Minimum number of buffers the port requires (Read Only). This is set by the component.
BufferSizeMin uint32 // Minimum size of buffers the port requires (Read Only). This is set by the component.
/* Minimum alignment requirement for the buffers (Read Only).
A value of zero means no special alignment requirements.
This is set by the component. */
BufferAlignmentMin uint32
/* Number of buffers the port recommends for optimal performance (Read Only).
A value of zero means no special recommendation.
This is set by the component. */
BufferNumRecommended uint32
/* Size of buffers the port recommends for optimal performance (Read Only).
A value of zero means no special recommendation.
This is set by the component. */
BufferSizeRecommended uint32
BufferNum uint32 // Actual number of buffers the port will use. This is set by the client.
BufferSize uint32 // Actual maximum size of the buffers that will be sent to the port. This is set by the client.
Component *Component // Component this port belongs to (Read Only)
UserData uintptr // Field reserved for use by the client
/* Flags describing the capabilities of a port (Read Only).
Bitwise combination of \ref portcapabilities "Port capabilities"
values. */
Capabilities uint32
}
func (p *Port) cptr() *C.MMAL_PORT_T {
return (*C.MMAL_PORT_T)(unsafe.Pointer(p))
}
func (p *Port) Name() string {
return C.GoString(p.name)
}
func (p *Port) FormatCommit() error {
return newError(C.mmal_port_format_commit(p.cptr()))
}
type PortBufferHeaderCallback func(*Port, *BufferHeader)
var portCallbacks sync.Map
//export portCbWrapper
func portCbWrapper(port *C.MMAL_PORT_T, buffer *C.MMAL_BUFFER_HEADER_T) {
p := (*Port)(unsafe.Pointer(port))
if v, ok := portCallbacks.Load(p); ok {
v.(PortBufferHeaderCallback)(p, (*BufferHeader)(unsafe.Pointer(buffer)))
}
}
func (p *Port) Enable(cb PortBufferHeaderCallback) error {
if cb != nil {
portCallbacks.Store(p, cb)
}
return newError(C.mmal_port_enable(p.cptr(), C.MMAL_PORT_BH_CB_T(C.portCbWrapper)))
}
func (p *Port) Disable() error {
err := newError(C.mmal_port_disable(p.cptr()))
portCallbacks.Delete(p)
return err
}
func (p *Port) Flush() error {
return newError(C.mmal_port_flush(p.cptr()))
}
func (p *Port) ParameterSet(param Parameter) error {
return newError(C.mmal_port_parameter_set(p.cptr(), (*C.MMAL_PARAMETER_HEADER_T)(param.header())))
}
func (p *Port) ParameterGet(param Parameter) error {
return newError(C.mmal_port_parameter_get(p.cptr(), (*C.MMAL_PARAMETER_HEADER_T)(param.header())))
}
func (p *Port) SendBuffer(buffer *BufferHeader) error {
return newError(C.mmal_port_send_buffer(p.cptr(), buffer.cptr()))
}
func (p *Port) Connect(to *Port) error {
return newError(C.mmal_port_connect(p.cptr(), to.cptr()))
}
func (p *Port) Disconnect() error {
return newError(C.mmal_port_disconnect(p.cptr()))
}
func (p *Port) PayloadAlloc(size uint32) []byte {
buf := C.mmal_port_payload_alloc(p.cptr(), C.uint32_t(size))
return (*[math.MaxInt32]byte)(unsafe.Pointer(buf))[:int(size):int(size)]
}
func (p *Port) PayloadFree(size uint32, buf []byte) {
C.mmal_port_payload_free(p.cptr(), (*C.uint8_t)(&buf[0]))
}
func (p *Port) EventGet(event uint32) (*BufferHeader, error) {
var bh *BufferHeader
err := newError(C.mmal_port_event_get(p.cptr(), (**C.MMAL_BUFFER_HEADER_T)(unsafe.Pointer(&bh)), C.uint32_t(event)))
return bh, err
}
func (p *Port) PoolCreate(headers, payloadSize uint32) *Pool {
return (*Pool)(unsafe.Pointer(C.mmal_port_pool_create(p.cptr(), C.uint(headers), C.uint32_t(payloadSize))))
}
func (p *Port) PoolDestroy(pool *Pool) {
C.mmal_port_pool_destroy(p.cptr(), pool.cptr())
}
type _MMAL_PORT_T C.MMAL_PORT_T