This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
MMALConnectionImpl.cs
294 lines (249 loc) · 11.3 KB
/
MMALConnectionImpl.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// <copyright file="MMALConnectionImpl.cs" company="Techyian">
// Copyright (c) Ian Auty and contributors. All rights reserved.
// Licensed under the MIT License. Please see LICENSE.txt for License info.
// </copyright>
using System;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using MMALSharp.Callbacks;
using MMALSharp.Common.Utility;
using MMALSharp.Components;
using MMALSharp.Native;
using MMALSharp.Ports.Inputs;
using MMALSharp.Ports.Outputs;
using static MMALSharp.MMALNativeExceptionHelper;
namespace MMALSharp
{
/// <summary>
/// Represents a connection between two ports.
/// </summary>
public unsafe class MMALConnectionImpl : MMALObject, IConnection
{
/// <summary>
/// The connection callback handler.
/// </summary>
public IConnectionCallbackHandler CallbackHandler { get; internal set; }
/// <summary>
/// The pool of buffer headers in this connection.
/// </summary>
public IBufferPool ConnectionPool { get; set; }
/// <summary>
/// The downstream component associated with the connection.
/// </summary>
public IDownstreamComponent DownstreamComponent { get; }
/// <summary>
/// The upstream component associated with the connection.
/// </summary>
public IComponent UpstreamComponent { get; }
/// <summary>
/// The input port of this connection.
/// </summary>
public IInputPort InputPort { get; }
/// <summary>
/// The output port of this connection.
/// </summary>
public IOutputPort OutputPort { get; }
#region Connection struct wrapper properties
/// <summary>
/// Name of this connection.
/// </summary>
public string Name => Marshal.PtrToStringAnsi((IntPtr)(*this.Ptr).Name);
/// <summary>
/// Indicates whether this connection is enabled.
/// </summary>
public bool Enabled => (*this.Ptr).IsEnabled == 1;
/// <summary>
/// Flags passed during the create call (Read Only). A bitwise combination of Connection flags values.
/// </summary>
public uint Flags => (*this.Ptr).Flags;
/// <summary>
/// Time in microseconds taken to setup the connection.
/// </summary>
public long TimeSetup => (*this.Ptr).TimeSetup;
/// <summary>
/// Time in microseconds taken to enable the connection.
/// </summary>
public long TimeEnable => (*this.Ptr).TimeEnable;
/// <summary>
/// Time in microseconds taken to disable the connection.
/// </summary>
public long TimeDisable => (*this.Ptr).TimeDisable;
#endregion
private MMALConnection.MMAL_CONNECTION_CALLBACK_T NativeCallback;
/// <summary>
/// Native pointer to the connection that this object represents.
/// </summary>
public MMAL_CONNECTION_T* Ptr { get; }
/// <inheritdoc />
public override bool CheckState()
{
return this.Ptr != null && (IntPtr)this.Ptr != IntPtr.Zero;
}
/// <summary>
/// Creates a new instance of <see cref="MMALConnectionImpl"/>.
/// </summary>
/// <param name="ptr">The native connection pointer.</param>
/// <param name="output">The upstream component's output port.</param>
/// <param name="input">The downstream component's input port.</param>
/// <param name="inputComponent">The upstream component.</param>
/// <param name="outputComponent">The downstream component.</param>
/// <param name="useCallback">
/// Configure the connection to intercept native callbacks. Note: will adversely impact performance. In addition, this will implicitly enable
/// zero copy functionality on both the source and sink ports.
/// </param>
protected MMALConnectionImpl(MMAL_CONNECTION_T* ptr, IOutputPort output, IInputPort input, IDownstreamComponent inputComponent, IComponent outputComponent, bool useCallback)
{
this.Ptr = ptr;
this.OutputPort = output;
this.InputPort = input;
this.DownstreamComponent = inputComponent;
this.UpstreamComponent = outputComponent;
if (useCallback)
{
this.CallbackHandler = new DefaultConnectionCallbackHandler(this);
this.ConfigureConnectionCallback(output, input);
}
this.Enable();
if (useCallback)
{
this.OutputPort.SendAllBuffers(this.ConnectionPool);
}
}
/// <inheritdoc />
public override string ToString()
{
return $"Component connection - Upstream component: {this.UpstreamComponent.Name} on port {this.OutputPort.Name} Downstream component: {this.DownstreamComponent.Name} on port {this.InputPort.Name}";
}
/// <inheritdoc />
public override void Dispose()
{
MMALLog.Logger.LogDebug("Disposing connection.");
this.OutputPort?.CloseConnection();
this.InputPort?.CloseConnection();
this.Destroy();
base.Dispose();
}
/// <summary>
/// Enable a connection. The format of the two ports must have been committed before calling this function, although note that on creation,
/// the connection automatically copies and commits the output port's format to the input port.
/// </summary>
public void Enable()
{
if (!this.Enabled)
{
MMALLog.Logger.LogDebug($"Enabling connection between {this.OutputPort.Name} and {this.InputPort.Name}");
MMALCheck(MMALConnection.mmal_connection_enable(this.Ptr), "Unable to enable connection");
}
}
/// <summary>
/// Disable a connection.
/// </summary>
public void Disable()
{
if (this.Enabled)
{
MMALLog.Logger.LogDebug($"Disabling connection between {this.OutputPort.Name} and {this.InputPort.Name}");
MMALCheck(MMALConnection.mmal_connection_disable(this.Ptr), "Unable to disable connection");
}
}
/// <summary>
/// Destroy a connection. Release an acquired reference on a connection. Only actually destroys the connection when the last reference is
/// being released. The actual destruction of the connection will start by disabling it, if necessary. Any pool, queue, and so on owned by
/// the connection shall then be destroyed.
/// </summary>
public void Destroy()
{
// Cleaning port pools for sanity.
this.UpstreamComponent.CleanPortPools();
this.DownstreamComponent.CleanPortPools();
MMALCheck(MMALConnection.mmal_connection_destroy(this.Ptr), "Unable to destroy connection");
}
/// <summary>
/// Associates a <see cref="IConnectionCallbackHandler"/> for use with this connection instance. This will only be used
/// if callbacks have been enabled against this connection.
/// </summary>
/// <param name="handler">The callback handler to use.</param>
public void RegisterCallbackHandler(IConnectionCallbackHandler handler)
{
this.CallbackHandler = handler;
}
/// <summary>
/// Facility to create a connection between two port objects.
/// </summary>
/// <param name="output">The output port of the connection.</param>
/// <param name="input">The input port of the connection.</param>
/// <param name="inputComponent">The managed instance of the component we are connecting to.</param>
/// <param name="useCallback">When set to true, enable the connection callback delegate (adversely affects performance).</param>
/// <returns>A new managed connection object.</returns>
internal static MMALConnectionImpl CreateConnection(IOutputPort output, IInputPort input, IDownstreamComponent inputComponent, bool useCallback)
{
IntPtr ptr = IntPtr.Zero;
if (useCallback)
{
MMALCheck(MMALConnection.mmal_connection_create(&ptr, output.Ptr, input.Ptr, MMALConnection.MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT), "Unable to create connection");
}
else
{
MMALCheck(MMALConnection.mmal_connection_create(&ptr, output.Ptr, input.Ptr, MMALConnection.MMAL_CONNECTION_FLAG_TUNNELLING | MMALConnection.MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT), "Unable to create connection");
}
return new MMALConnectionImpl((MMAL_CONNECTION_T*)ptr, output, input, inputComponent, output.ComponentReference, useCallback);
}
/// <summary>
/// Represents the native callback method for a connection between two ports.
/// </summary>
/// <param name="connection">The native pointer to a MMAL_CONNECTION_T struct.</param>
/// <returns>The value of all flags set against this connection.</returns>
protected virtual int NativeConnectionCallback(MMAL_CONNECTION_T* connection)
{
if (MMALCameraConfig.Debug)
{
MMALLog.Logger.LogDebug("Inside native connection callback");
}
var queue = new MMALQueueImpl(connection->Queue);
var bufferImpl = queue.GetBuffer();
if (bufferImpl.CheckState())
{
if (MMALCameraConfig.Debug)
{
bufferImpl.PrintProperties();
}
if (bufferImpl.Length > 0)
{
this.CallbackHandler.InputCallback(bufferImpl);
}
this.InputPort.SendBuffer(bufferImpl);
}
else
{
queue = new MMALQueueImpl(connection->Pool->Queue);
bufferImpl = queue.GetBuffer();
if (bufferImpl.CheckState())
{
if (MMALCameraConfig.Debug)
{
bufferImpl.PrintProperties();
}
if (bufferImpl.Length > 0)
{
this.CallbackHandler.OutputCallback(bufferImpl);
}
this.OutputPort.SendBuffer(bufferImpl);
}
else
{
MMALLog.Logger.LogInformation("Buffer could not be obtained by connection callback");
}
}
return (int)connection->Flags;
}
private void ConfigureConnectionCallback(IOutputPort output, IInputPort input)
{
output.SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
input.SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
this.NativeCallback = new MMALConnection.MMAL_CONNECTION_CALLBACK_T(this.NativeConnectionCallback);
IntPtr ptrCallback = Marshal.GetFunctionPointerForDelegate(this.NativeCallback);
this.Ptr->Callback = ptrCallback;
this.ConnectionPool = new MMALPoolImpl(this.Ptr->Pool);
}
}
}