@@ -104,6 +104,8 @@ public class LivekitReactNativeModule: RCTEventEmitter {
104104 }
105105 }
106106
107+ private static let kModuleErrorDomain = " LivekitReactNativeModule "
108+
107109 private func syncAudioDeviceModule( ) -> Bool {
108110 guard let webRTCModule = self . bridge. module ( for: WebRTCModule . self) as? WebRTCModule else {
109111 return false
@@ -114,44 +116,45 @@ public class LivekitReactNativeModule: RCTEventEmitter {
114116 return true
115117 }
116118
119+ private func sendRecordingStateEvent( _ stage: String , error: Error ? = nil ) {
120+ var body : [ String : Any ] = [
121+ " stage " : stage,
122+ " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
123+ ]
124+ if let error = error {
125+ body [ " error " ] = error. localizedDescription
126+ }
127+ self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: body)
128+ }
129+
130+ private func bridgeUnavailableError( context: String ) -> NSError {
131+ NSError (
132+ domain: Self . kModuleErrorDomain,
133+ code: - 1 ,
134+ userInfo: [
135+ NSLocalizedDescriptionKey:
136+ " WebRTCModule is unavailable while \( context) local recording " ,
137+ ]
138+ )
139+ }
140+
117141 @objc ( startLocalRecording: withRejecter: )
118142 public func startLocalRecording( resolve: RCTPromiseResolveBlock , reject: RCTPromiseRejectBlock ) {
119- self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: [
120- " stage " : " local_recording_start_requested " ,
121- " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
122- ] )
143+ sendRecordingStateEvent ( " local_recording_start_requested " )
123144
124145 guard syncAudioDeviceModule ( ) else {
125- let bridgeError = NSError (
126- domain: " LivekitReactNativeModule " ,
127- code: - 1 ,
128- userInfo: [
129- NSLocalizedDescriptionKey:
130- " WebRTCModule is unavailable while starting local recording " ,
131- ]
132- )
133- self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: [
134- " stage " : " local_recording_start_failed " ,
135- " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
136- " error " : bridgeError. localizedDescription,
137- ] )
138- reject ( " startLocalRecording " , bridgeError. localizedDescription, bridgeError)
146+ let err = bridgeUnavailableError ( context: " starting " )
147+ sendRecordingStateEvent ( " local_recording_start_failed " , error: err)
148+ reject ( " startLocalRecording " , err. localizedDescription, err)
139149 return
140150 }
141151
142152 do {
143153 try LKAudioProcessingManager . sharedInstance ( ) . startLocalRecording ( )
144- self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: [
145- " stage " : " local_recording_started " ,
146- " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
147- ] )
154+ sendRecordingStateEvent ( " local_recording_started " )
148155 resolve ( nil )
149156 } catch {
150- self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: [
151- " stage " : " local_recording_start_failed " ,
152- " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
153- " error " : error. localizedDescription,
154- ] )
157+ sendRecordingStateEvent ( " local_recording_start_failed " , error: error)
155158 reject (
156159 " startLocalRecording " ,
157160 " Error starting local recording: \( error. localizedDescription) " ,
@@ -162,27 +165,21 @@ public class LivekitReactNativeModule: RCTEventEmitter {
162165
163166 @objc ( stopLocalRecording: withRejecter: )
164167 public func stopLocalRecording( resolve: RCTPromiseResolveBlock , reject: RCTPromiseRejectBlock ) {
168+ sendRecordingStateEvent ( " local_recording_stop_requested " )
169+
165170 guard syncAudioDeviceModule ( ) else {
166- let bridgeError = NSError (
167- domain: " LivekitReactNativeModule " ,
168- code: - 1 ,
169- userInfo: [
170- NSLocalizedDescriptionKey:
171- " WebRTCModule is unavailable while stopping local recording " ,
172- ]
173- )
174- reject ( " stopLocalRecording " , bridgeError. localizedDescription, bridgeError)
171+ let err = bridgeUnavailableError ( context: " stopping " )
172+ sendRecordingStateEvent ( " local_recording_stop_failed " , error: err)
173+ reject ( " stopLocalRecording " , err. localizedDescription, err)
175174 return
176175 }
177176
178177 do {
179178 try LKAudioProcessingManager . sharedInstance ( ) . stopLocalRecording ( )
180- self . sendEvent ( withName: LKEvents . kEventAudioRecordingState, body: [
181- " stage " : " local_recording_stopped " ,
182- " timestampMs " : Int ( Date ( ) . timeIntervalSince1970 * 1000 ) ,
183- ] )
179+ sendRecordingStateEvent ( " local_recording_stopped " )
184180 resolve ( nil )
185181 } catch {
182+ sendRecordingStateEvent ( " local_recording_stop_failed " , error: error)
186183 reject (
187184 " stopLocalRecording " ,
188185 " Error stopping local recording: \( error. localizedDescription) " ,
0 commit comments