33// Licensed under the MIT License. Please see LICENSE.txt for License info.
44// </copyright>
55
6+ using System ;
7+ using System . Drawing ;
68using System . Threading . Tasks ;
79using MMALSharp . Native ;
810using System . Text ;
11+ using MMALSharp . Callbacks . Providers ;
912using MMALSharp . Common . Utility ;
1013using MMALSharp . Handlers ;
1114using MMALSharp . Ports ;
@@ -34,14 +37,14 @@ public MMALImageFileEncoder(ICaptureHandler handler)
3437 /// </summary>
3538 public static MMALQueueImpl WorkingQueue { get ; set ; }
3639
37- /// <inheritdoc />>
38- public override unsafe MMALDownstreamComponent ConfigureInputPort ( MMALEncoding encodingType , MMALEncoding pixelFormat , int width , int height , bool zeroCopy = false )
40+ /// <inheritdoc />
41+ public override unsafe MMALDownstreamComponent ConfigureInputPort ( MMALPortConfig config )
3942 {
4043 this . InitialiseInputPort ( 0 ) ;
4144
42- if ( encodingType != null )
45+ if ( config . EncodingType != null )
4346 {
44- this . Inputs [ 0 ] . Ptr ->Format ->Encoding = encodingType . EncodingVal ;
47+ this . Inputs [ 0 ] . Ptr ->Format ->Encoding = config . EncodingType . EncodingVal ;
4548 }
4649
4750 /*if (pixelFormat != null)
@@ -50,13 +53,13 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
5053 }*/
5154
5255 this . Inputs [ 0 ] . Ptr ->Format ->Type = MMALFormat . MMAL_ES_TYPE_T . MMAL_ES_TYPE_VIDEO ;
53- this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Height = height ;
54- this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Width = width ;
56+ this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Height = config . Height ;
57+ this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Width = config . Width ;
5558 this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . FrameRate = new MMAL_RATIONAL_T ( 0 , 1 ) ;
5659 this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Par = new MMAL_RATIONAL_T ( 1 , 1 ) ;
57- this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Crop = new MMAL_RECT_T ( 0 , 0 , width , height ) ;
60+ this . Inputs [ 0 ] . Ptr ->Format ->Es ->Video . Crop = new MMAL_RECT_T ( 0 , 0 , config . Width , config . Height ) ;
5861
59- this . Inputs [ 0 ] . EncodingType = encodingType ;
62+ this . Inputs [ 0 ] . EncodingType = config . EncodingType ;
6063
6164 this . Inputs [ 0 ] . Commit ( ) ;
6265
@@ -68,15 +71,60 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
6871 this . Inputs [ 0 ] . Ptr ->BufferNum = this . Inputs [ 0 ] . Ptr ->BufferNumMin ;
6972 this . Inputs [ 0 ] . Ptr ->BufferSize = this . Inputs [ 0 ] . Ptr ->BufferSizeMin ;
7073
71- if ( zeroCopy )
74+ if ( config . ZeroCopy )
7275 {
7376 this . Inputs [ 0 ] . ZeroCopy = true ;
7477 this . Inputs [ 0 ] . SetParameter ( MMALParametersCommon . MMAL_PARAMETER_ZERO_COPY , true ) ;
7578 }
7679
7780 return this ;
7881 }
79-
82+
83+ /// <inheritdoc />
84+ public override unsafe MMALDownstreamComponent ConfigureOutputPort ( int outputPort , MMALPortConfig config )
85+ {
86+ this . Outputs [ outputPort ] . Ptr ->Format ->Es ->Video . Par = new MMAL_RATIONAL_T ( 1 , 1 ) ;
87+
88+ this . InitialiseOutputPort ( outputPort ) ;
89+
90+ if ( this . ProcessingPorts . ContainsKey ( outputPort ) )
91+ {
92+ this . ProcessingPorts . Remove ( outputPort ) ;
93+ }
94+
95+ this . ProcessingPorts . Add ( outputPort , this . Outputs [ outputPort ] ) ;
96+
97+ if ( config . EncodingType != null )
98+ {
99+ this . Outputs [ outputPort ] . Ptr ->Format ->Encoding = config . EncodingType . EncodingVal ;
100+ }
101+
102+ if ( config . PixelFormat != null )
103+ {
104+ this . Outputs [ outputPort ] . Ptr ->Format ->EncodingVariant = config . PixelFormat . EncodingVal ;
105+ }
106+
107+ if ( config . ZeroCopy )
108+ {
109+ this . Outputs [ outputPort ] . ZeroCopy = true ;
110+ this . Outputs [ outputPort ] . SetParameter ( MMALParametersCommon . MMAL_PARAMETER_ZERO_COPY , true ) ;
111+ }
112+
113+ this . Outputs [ outputPort ] . Resolution = new Resolution ( config . Width , config . Height ) . Pad ( ) ;
114+ this . Outputs [ outputPort ] . Crop = new Rectangle ( 0 , 0 , config . Width , config . Height ) ;
115+
116+ this . Outputs [ outputPort ] . Commit ( ) ;
117+
118+ this . Outputs [ outputPort ] . EncodingType = config . EncodingType ;
119+
120+ this . Outputs [ outputPort ] . ManagedOutputCallback = OutputCallbackProvider . FindCallback ( this . Outputs [ outputPort ] ) ;
121+
122+ this . Outputs [ outputPort ] . Ptr ->BufferNum = Math . Max ( this . Outputs [ outputPort ] . Ptr ->BufferNumMin , this . Outputs [ outputPort ] . Ptr ->BufferNumRecommended ) ;
123+ this . Outputs [ outputPort ] . Ptr ->BufferSize = Math . Max ( this . Outputs [ outputPort ] . Ptr ->BufferSizeMin , this . Outputs [ outputPort ] . Ptr ->BufferSizeRecommended ) ;
124+
125+ return this ;
126+ }
127+
80128 /// <summary>
81129 /// Encodes/decodes user provided image data.
82130 /// </summary>
@@ -203,8 +251,8 @@ private unsafe void ConfigureOutputPortWithoutInit(int outputPort, MMALEncoding
203251
204252 this . Outputs [ outputPort ] . EncodingType = encodingType ;
205253
206- this . Outputs [ outputPort ] . Ptr ->BufferNum = 2 ;
207- this . Outputs [ outputPort ] . Ptr ->BufferSize = this . Outputs [ outputPort ] . Ptr ->BufferSizeRecommended ;
254+ this . Outputs [ outputPort ] . Ptr ->BufferNum = Math . Max ( this . Outputs [ outputPort ] . Ptr -> BufferNumMin , this . Outputs [ outputPort ] . Ptr -> BufferNumRecommended ) ;
255+ this . Outputs [ outputPort ] . Ptr ->BufferSize = Math . Max ( this . Outputs [ outputPort ] . Ptr ->BufferSizeMin , this . Outputs [ outputPort ] . Ptr -> BufferSizeRecommended ) ;
208256
209257 MMALLog . Logger . Info ( $ "New buffer number { this . Outputs [ outputPort ] . Ptr ->BufferNum } ") ;
210258 MMALLog . Logger . Info ( $ "New buffer size { this . Outputs [ outputPort ] . Ptr ->BufferSize } ") ;
@@ -291,18 +339,15 @@ private void GetAndSendOutputBuffer()
291339
292340 private void ProcessFormatChangedEvent ( MMALBufferImpl buffer )
293341 {
294- MMALLog . Logger . Debug ( "Received MMAL_EVENT_FORMAT_CHANGED event" ) ;
342+ MMALLog . Logger . Info ( "Received MMAL_EVENT_FORMAT_CHANGED event" ) ;
295343
296344 var ev = MMALEventFormat . GetEventFormat ( buffer ) ;
297345
298- MMALLog . Logger . Debug ( "-- Event format changed from -- " ) ;
346+ MMALLog . Logger . Info ( "-- Event format changed from -- " ) ;
299347 this . LogFormat ( new MMALEventFormat ( this . Outputs [ 0 ] . Format ) , this . Outputs [ 0 ] ) ;
300348
301- MMALLog . Logger . Debug ( "-- To -- " ) ;
349+ MMALLog . Logger . Info ( "-- To -- " ) ;
302350 this . LogFormat ( ev , null ) ;
303-
304- // Port format changed
305- this . Outputs [ 0 ] . ManagedOutputCallback . Callback ( buffer ) ;
306351
307352 lock ( OutputPort . OutputLock )
308353 {
0 commit comments