@@ -100,48 +100,56 @@ protected override Task OnInitializeAsync(VideoCharacteristics characteristics,
100100 $ "FlashCap: Couldn't find device: UniqueID={ this . uniqueID } ") ;
101101
102102 this . device . LockForConfiguration ( ) ;
103- var formatSelected = this . device . Formats
104- . FirstOrDefault ( format =>
105- format . FormatDescription . Dimensions is var dimensions &&
106- format . FormatDescription . MediaType == CMMediaType . Video &&
107- dimensions . Width == characteristics . Width &&
108- dimensions . Height == characteristics . Height )
109- ?? throw new InvalidOperationException (
110- $ "FlashCap: Couldn't set video format: UniqueID={ this . uniqueID } ") ;
111-
112- this . device . ActiveFormat = formatSelected ;
113-
114- var frameDuration = CMTimeMake (
115- characteristics . FramesPerSecond . Denominator ,
116- characteristics . FramesPerSecond . Numerator ) ;
103+ try
104+ {
105+ var formatSelected = this . device . Formats
106+ . FirstOrDefault ( format =>
107+ format . FormatDescription . Dimensions is var dimensions &&
108+ format . FormatDescription . MediaType == CMMediaType . Video &&
109+ dimensions . Width == characteristics . Width &&
110+ dimensions . Height == characteristics . Height )
111+ ?? throw new InvalidOperationException (
112+ $ "FlashCap: Couldn't set video format: UniqueID={ this . uniqueID } ") ;
113+
114+ this . device . ActiveFormat = formatSelected ;
115+
116+ var frameDuration = CMTimeMake (
117+ characteristics . FramesPerSecond . Denominator ,
118+ characteristics . FramesPerSecond . Numerator ) ;
117119
118- device . ActiveVideoMinFrameDuration = frameDuration ;
119- device . ActiveVideoMaxFrameDuration = frameDuration ;
120+ device . ActiveVideoMinFrameDuration = frameDuration ;
121+ device . ActiveVideoMaxFrameDuration = frameDuration ;
120122
121- this . deviceInput = new AVCaptureDeviceInput ( device ) ;
123+ this . deviceInput = new AVCaptureDeviceInput ( device ) ;
122124
123- this . deviceOutput = new AVCaptureVideoDataOutput ( ) ;
125+ this . deviceOutput = new AVCaptureVideoDataOutput ( ) ;
124126
125- if ( this . deviceOutput . AvailableVideoCVPixelFormatTypes ? . Any ( ) == true )
126- {
127- var validPixelFormat = this . deviceOutput . AvailableVideoCVPixelFormatTypes . FirstOrDefault ( p => p == pixelFormatType ) ;
128- this . deviceOutput . SetPixelFormatType ( validPixelFormat ) ;
127+ if ( this . deviceOutput . AvailableVideoCVPixelFormatTypes ? . Any ( ) == true )
128+ {
129+ var validPixelFormat = this . deviceOutput . AvailableVideoCVPixelFormatTypes . FirstOrDefault ( p => p == pixelFormatType ) ;
130+ this . deviceOutput . SetPixelFormatType ( validPixelFormat ) ;
131+ }
132+ else
133+ {
134+ // Fallback to the mapped pixel format if no available list is provided
135+ this . deviceOutput . SetPixelFormatType ( pixelFormatType ) ;
136+ }
137+
138+ this . deviceOutput . SetSampleBufferDelegate ( new VideoBufferHandler ( this ) , this . queue ) ;
139+ this . deviceOutput . AlwaysDiscardsLateVideoFrames = true ;
129140 }
130- else
141+ finally
131142 {
132- // Fallback to the mapped pixel format if no available list is provided
133- this . deviceOutput . SetPixelFormatType ( pixelFormatType ) ;
143+ this . device . UnlockForConfiguration ( ) ;
134144 }
135145
136- this . deviceOutput . SetSampleBufferDelegate ( new VideoBufferHandler ( this ) , this . queue ) ;
137- this . deviceOutput . AlwaysDiscardsLateVideoFrames = true ;
138-
139- this . device . UnlockForConfiguration ( ) ;
140-
141146 this . session = new AVCaptureSession ( ) ;
142147 this . session . AddInput ( this . deviceInput ) ;
143-
144- if ( session . CanAddOutput ( deviceOutput ) ) session . AddOutput ( this . deviceOutput ) ;
148+
149+ if ( this . session . CanAddOutput ( deviceOutput ) )
150+ {
151+ this . session . AddOutput ( this . deviceOutput ) ;
152+ }
145153 else
146154 {
147155 throw new Exception ( "Can't add video output" ) ;
0 commit comments