Browse Source

Fix instant Meet recording lifecycle

Create a real Meet link via Calendar for Instant Meeting so we have a meeting code and can start/stop recording using the existing Meet API monitor.

Co-authored-by: Cursor <cursoragent@cursor.com>
huzaifahayat12 2 months ago
parent
commit
4a6bfcf649
1 changed files with 49 additions and 3 deletions
  1. 49 3
      meetings_app/ViewController.swift

+ 49 - 3
meetings_app/ViewController.swift

@@ -947,9 +947,55 @@ private extension ViewController {
             return
         }
 
-        guard let url = URL(string: "https://meet.google.com/new") else { return }
-        openInDefaultBrowser(url: url)
-        consumeNonPremiumJoinTrialIfNeeded()
+        // Meet blocks WKWebView. To support instant-stop recording we must create a real Meet space (with meeting code)
+        // before launching the user's browser.
+        guard hasGoogleSessionAvailable() else {
+            showSimpleAlert(
+                title: "Connect Google to start Instant Meeting",
+                message: "To auto-stop recording when the meeting ends, connect your Google account first."
+            )
+            guard let fallbackURL = URL(string: "https://meet.google.com/new") else { return }
+            openInDefaultBrowser(url: fallbackURL)
+            consumeNonPremiumJoinTrialIfNeeded()
+            return
+        }
+
+        let title = "Instant Meeting"
+        let consentChoice = requestMeetingRecordingConsent(title: title)
+        if consentChoice == .dismissed { return }
+
+        meetingRecordingMonitorTask?.cancel()
+        meetingRecordingMonitorTask = nil
+        if activeMeetingRecordingSession != nil { finishActiveMeetingRecording(cancelMonitoring: false) }
+
+        Task { [weak self] in
+            guard let self else { return }
+            do {
+                let now = Date()
+                let end = now.addingTimeInterval(60 * 60) // 1h placeholder; Meet lifecycle monitor will stop recording when call ends.
+                let token = try await self.googleOAuth.validAccessToken(presentingWindow: self.view.window)
+                let meeting = try await self.calendarClient.createEvent(
+                    accessToken: token,
+                    title: title,
+                    description: nil,
+                    start: now,
+                    end: end
+                )
+
+                DispatchQueue.main.async {
+                    if consentChoice == .allowAndContinue {
+                        // Start Meet API-based recording lifecycle monitoring using the real meeting URL (has code).
+                        self.startAutomaticRecordingFlow(meetingTitle: title, meetingURL: meeting.meetURL)
+                    }
+                    self.openInDefaultBrowser(url: meeting.meetURL)
+                    self.consumeNonPremiumJoinTrialIfNeeded()
+                }
+            } catch {
+                DispatchQueue.main.async {
+                    self.showSimpleAlert(title: "Could not create Instant Meeting", message: error.localizedDescription)
+                }
+            }
+        }
     }
 
     private func consumeNonPremiumJoinTrialIfNeeded() {