@@ -13,6 +13,28 @@ import Photos
1313
1414extension RemoteCamSession {
1515
16+ // MARK: - Camera Capabilities Retry Helper
17+ private func attemptToSendCapabilities( ctrl: CameraViewController , peer: MCPeerID , attempt: Int , maxAttempts: Int ) {
18+ print ( " 🔍 DEBUG: Attempt \( attempt) / \( maxAttempts) to gather camera capabilities " )
19+
20+ ctrl. gatherAllCameraCapabilities ( )
21+ if let capabilities = ctrl. gatherCurrentCameraCapabilities ( ) {
22+ print ( " ✅ DEBUG: Successfully gathered capabilities on attempt \( attempt) " )
23+ print ( " 🔍 DEBUG: Sending camera capabilities - Available lenses: \( capabilities. getCurrentCameraInfo ( ) ? . availableLenses ?? [ ] ) " )
24+ self . mailbox. addOperation ( BlockOperation {
25+ self . sendCommandOrGoToScanning ( peer: [ peer] , msg: capabilities)
26+ } )
27+ } else if attempt < maxAttempts {
28+ let delay = Double ( attempt) * 0.2 // 0.2s, 0.4s, 0.6s, 0.8s delays
29+ print ( " ⏳ DEBUG: Attempt \( attempt) failed, retrying in \( delay) s " )
30+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + delay) { [ weak self] in
31+ self ? . attemptToSendCapabilities ( ctrl: ctrl, peer: peer, attempt: attempt + 1 , maxAttempts: maxAttempts)
32+ }
33+ } else {
34+ print ( " ❌ DEBUG: Failed to gather camera capabilities after \( maxAttempts) attempts " )
35+ }
36+ }
37+
1638 func savePicture( _ imageData: Data ) {
1739 PHPhotoLibrary . requestAuthorization { status in
1840 guard status == . authorized else {
@@ -90,69 +112,32 @@ extension RemoteCamSession {
90112 func camera( peer: MCPeerID ,
91113 ctrl: CameraViewController ,
92114 lobbyWrapper: Weak < DeviceScannerViewController > ) -> Receive {
93- var capabilitiesSent = false
94115
95116 return { [ unowned self] ( msg: Actor . Message ) in
96117 guard lobbyWrapper. value != nil else {
97118 popAndStartScanning ( )
98119 return
99120 }
100121
101- // Helper function to send capabilities if not already sent
102- func sendCapabilitiesIfNeeded( ) {
103- if !capabilitiesSent {
104- print ( " 🔍 DEBUG: First command received - gathering camera capabilities " )
105- ctrl. gatherAllCameraCapabilities ( )
106- if let capabilities = ctrl. gatherCurrentCameraCapabilities ( ) {
107- print ( " 🔍 DEBUG: Sending camera capabilities to monitor " )
108- print ( " 🔍 DEBUG: Available lenses: \( capabilities. getCurrentCameraInfo ( ) ? . availableLenses ?? [ ] ) " )
109- self . sendCommandOrGoToScanning ( peer: [ peer] , msg: capabilities)
110- capabilitiesSent = true
111- }
112- }
113- }
114-
115122 switch msg {
116123 case is OnEnter :
117124 print ( " 🔍 DEBUG: Camera starting up " )
118125 getFrameSender ( ) ? . tell ( msg: SetSession ( peer: peer, session: self ) )
119- // Send capabilities after a brief delay to ensure camera is fully initialized
120- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.5 ) {
121- print ( " 🔍 DEBUG: Camera entered - gathering and sending capabilities (delayed) " )
122- ctrl. gatherAllCameraCapabilities ( )
123- if let capabilities = ctrl. gatherCurrentCameraCapabilities ( ) {
124- print ( " 🔍 DEBUG: Sending camera capabilities to monitor " )
125- print ( " 🔍 DEBUG: Available lenses: \( capabilities. getCurrentCameraInfo ( ) ? . availableLenses ?? [ ] ) " )
126- self . mailbox. addOperation ( BlockOperation {
127- self . sendCommandOrGoToScanning ( peer: [ peer] , msg: capabilities)
128- capabilitiesSent = true
129- } )
130- } else {
131- print ( " ❌ DEBUG: Failed to gather current camera capabilities in OnEnter " )
132- }
133- }
134126
135- // MARK: - Monitor Join Handling
136127 case is RemoteCmd . PeerBecameMonitor :
137128 // When a new monitor joins, immediately send camera capabilities
138- print ( " 🔍 DEBUG: Camera received PeerBecameMonitor - sending capabilities immediately " )
139- ctrl. gatherAllCameraCapabilities ( )
140- if let capabilities = ctrl. gatherCurrentCameraCapabilities ( ) {
141- print ( " 🔍 DEBUG: Sending camera capabilities to new monitor " )
142- print ( " 🔍 DEBUG: Available lenses: \( capabilities. getCurrentCameraInfo ( ) ? . availableLenses ?? [ ] ) " )
143- self . sendCommandOrGoToScanning ( peer: [ peer] , msg: capabilities)
144- } else {
145- print ( " ❌ DEBUG: Failed to gather current camera capabilities for new monitor " )
146- }
129+ print ( " 🔍 DEBUG: Camera received PeerBecameMonitor - attempting to send capabilities " )
130+ self . attemptToSendCapabilities ( ctrl: ctrl, peer: peer, attempt: 1 , maxAttempts: 5 )
131+
132+ case is RemoteCmd . RequestCameraCapabilities :
133+ // When monitor explicitly requests capabilities
134+ print ( " 🔍 DEBUG: Camera received RequestCameraCapabilities - attempting to gather capabilities " )
135+ self . attemptToSendCapabilities ( ctrl: ctrl, peer: peer, attempt: 1 , maxAttempts: 5 )
147136
148137 case is RemoteCmd . RequestFrame :
149- // Send capabilities on first frame request, then handle normally
150- sendCapabilitiesIfNeeded ( )
151138 self . receive ( msg: msg)
152139
153140 case is RemoteCmd . SendFrame :
154- // Send capabilities on first frame send, then handle normally
155- sendCapabilitiesIfNeeded ( )
156141 self . receive ( msg: msg)
157142
158143 case let m as UICmd . ToggleCameraResp :
@@ -179,7 +164,7 @@ extension RemoteCamSession {
179164 print ( " 🔍 DEBUG: Camera received ToggleCamera command " )
180165 let result = ctrl. toggleCamera ( )
181166 var resp : Message ?
182- if let ( flashMode , position) = result. toOptional ( ) {
167+ if let ( _ , position) = result. toOptional ( ) {
183168 print ( " ✅ DEBUG: Camera toggle success - new position: \( position) " )
184169 // Send camera capabilities as part of the response
185170 let capabilities = ctrl. gatherCurrentCameraCapabilities ( )
@@ -191,7 +176,6 @@ extension RemoteCamSession {
191176 self . sendCommandOrGoToScanning ( peer: [ peer] , msg: resp!)
192177
193178 case is RemoteCmd . ToggleFlash :
194- sendCapabilitiesIfNeeded ( ) // Ensure capabilities are sent
195179 let result = ctrl. toggleFlash ( )
196180 var resp : Message ?
197181 if let flashMode = result. toOptional ( ) {
@@ -203,7 +187,6 @@ extension RemoteCamSession {
203187
204188 // MARK: - Zoom Command Handling
205189 case let zoomCmd as RemoteCmd . SetZoom :
206- sendCapabilitiesIfNeeded ( ) // Ensure capabilities are sent
207190 print ( " 🔍 DEBUG: Camera received SetZoom: \( zoomCmd. zoomFactor) " )
208191 let result = ctrl. setZoom ( zoomFactor: zoomCmd. zoomFactor)
209192 var resp : Message ?
@@ -217,7 +200,6 @@ extension RemoteCamSession {
217200
218201 // MARK: - Lens Switching Command Handling
219202 case let lensCmd as RemoteCmd . SwitchLens :
220- sendCapabilitiesIfNeeded ( ) // Ensure capabilities are sent
221203 print ( " 🔍 DEBUG: Camera received SwitchLens command to \( lensCmd. lensType. displayName) " )
222204 let result = ctrl. switchLens ( to: lensCmd. lensType)
223205 var resp : Message ?
0 commit comments