|
| 1 | +import { Platform } from 'react-native'; |
| 2 | + |
1 | 3 | import { |
2 | 4 | AndroidAudioEncoder, |
3 | 5 | AndroidOutputFormat, |
4 | 6 | ExpoAudioRecordingConfiguration as AudioRecordingConfiguration, |
5 | 7 | IOSAudioQuality, |
6 | 8 | IOSOutputFormat, |
7 | 9 | ExpoRecordingOptions as RecordingOptions, |
| 10 | + RecordingStatus, |
8 | 11 | } from 'stream-chat-react-native-core'; |
9 | 12 |
|
10 | 13 | import { AudioComponent, RecordingObject } from './AudioVideo'; |
11 | 14 |
|
| 15 | +let ExpoAudioComponent; |
| 16 | +let ExpoRecordingComponent; |
| 17 | + |
| 18 | +try { |
| 19 | + const { AudioModule } = require('expo-audio'); |
| 20 | + ExpoAudioComponent = AudioModule; |
| 21 | + ExpoRecordingComponent = AudioModule.AudioRecorder; |
| 22 | +} catch (e) { |
| 23 | + // do nothing |
| 24 | +} |
| 25 | + |
12 | 26 | const sleep = (ms: number) => |
13 | 27 | new Promise<void>((resolve) => { |
14 | 28 | setTimeout(() => { |
15 | 29 | resolve(); |
16 | 30 | }, ms); |
17 | 31 | }); |
18 | 32 |
|
19 | | -class _Audio { |
| 33 | +class _AudioExpoAV { |
20 | 34 | recording: typeof RecordingObject | null = null; |
21 | 35 | audioRecordingConfiguration: AudioRecordingConfiguration = { |
22 | 36 | mode: { |
@@ -105,8 +119,233 @@ class _Audio { |
105 | 119 | }; |
106 | 120 | } |
107 | 121 |
|
| 122 | +class _AudioExpoAudio { |
| 123 | + recording: typeof RecordingObject | null = null; |
| 124 | + audioRecordingConfiguration: AudioRecordingConfiguration = { |
| 125 | + mode: { |
| 126 | + allowsRecordingIOS: true, |
| 127 | + playsInSilentModeIOS: true, |
| 128 | + }, |
| 129 | + options: { |
| 130 | + android: { |
| 131 | + audioEncoder: AndroidAudioEncoder.AAC, |
| 132 | + extension: '.aac', |
| 133 | + outputFormat: AndroidOutputFormat.AAC_ADTS, |
| 134 | + }, |
| 135 | + ios: { |
| 136 | + audioQuality: IOSAudioQuality.HIGH, |
| 137 | + bitRate: 128000, |
| 138 | + extension: '.aac', |
| 139 | + numberOfChannels: 2, |
| 140 | + outputFormat: IOSOutputFormat.MPEG4AAC, |
| 141 | + sampleRate: 44100, |
| 142 | + }, |
| 143 | + isMeteringEnabled: true, |
| 144 | + web: {}, |
| 145 | + }, |
| 146 | + }; |
| 147 | + |
| 148 | + startRecording = async (recordingOptions: RecordingOptions, onRecordingStatusUpdate) => { |
| 149 | + try { |
| 150 | + const permissions = await ExpoAudioComponent.getRecordingPermissionsAsync(); |
| 151 | + const permissionsStatus = permissions.status; |
| 152 | + let permissionsGranted = permissions.granted; |
| 153 | + |
| 154 | + // If permissions have not been determined yet, ask the user for permissions. |
| 155 | + if (permissionsStatus === 'undetermined') { |
| 156 | + const newPermissions = await ExpoAudioComponent.requestRecordingPermissionsAsync(); |
| 157 | + permissionsGranted = newPermissions.granted; |
| 158 | + } |
| 159 | + |
| 160 | + // If they are explicitly denied after this, exit early by throwing an error |
| 161 | + // that will be caught in the catch block below (as a single source of not |
| 162 | + // starting the player). The player would error itself anyway if we did not do |
| 163 | + // this, but there's no reason to run the asynchronous calls when we know |
| 164 | + // immediately that the player will not be run. |
| 165 | + if (!permissionsGranted) { |
| 166 | + throw new Error('Missing audio recording permission.'); |
| 167 | + } |
| 168 | + await ExpoAudioComponent.setAudioModeAsync( |
| 169 | + expoAvToExpoAudioModeAdapter(this.audioRecordingConfiguration.mode), |
| 170 | + ); |
| 171 | + const options = { |
| 172 | + ...recordingOptions, |
| 173 | + ...this.audioRecordingConfiguration.options, |
| 174 | + }; |
| 175 | + |
| 176 | + this.recording = new ExpoAudioRecordingAdapter(options); |
| 177 | + await this.recording.createAsync( |
| 178 | + Platform.OS === 'android' ? 100 : 60, |
| 179 | + onRecordingStatusUpdate, |
| 180 | + ); |
| 181 | + return { accessGranted: true, recording: this.recording }; |
| 182 | + } catch (error) { |
| 183 | + console.error('Failed to start recording', error); |
| 184 | + this.recording = null; |
| 185 | + return { accessGranted: false, recording: null }; |
| 186 | + } |
| 187 | + }; |
| 188 | + stopRecording = async () => { |
| 189 | + try { |
| 190 | + if (this.recording) { |
| 191 | + await this.recording.stopAndUnloadAsync(); |
| 192 | + } |
| 193 | + this.recording = null; |
| 194 | + } catch (error) { |
| 195 | + console.log('Error stopping recoding', error); |
| 196 | + } |
| 197 | + }; |
| 198 | +} |
| 199 | + |
| 200 | +class ExpoAudioRecordingAdapter { |
| 201 | + private recording; |
| 202 | + private recordingStateInterval; |
| 203 | + private uri; |
| 204 | + private options; |
| 205 | + |
| 206 | + constructor(options: RecordingOptions) { |
| 207 | + // Currently, expo-audio has a bug where isMeteringEnabled is not respected |
| 208 | + // whenever we pass it to the Recording class constructor - but rather it is |
| 209 | + // only respected whenever you pass it to prepareToRecordAsync. That in turn |
| 210 | + // however, means that all other audio related configuration will be overwritten |
| 211 | + // and forgotten. So, we snapshot the configuration whenever we create an instance |
| 212 | + // of a recorder and pass it to both places. Furthermore, the type of the options |
| 213 | + // in prepareToRecordAsync is wrong - it's supposed to be the flattened config; |
| 214 | + // otherwise none of the quality properties get respected either (only the top level |
| 215 | + // ones). |
| 216 | + this.options = flattenExpoAudioRecordingOptions(options); |
| 217 | + this.recording = new ExpoRecordingComponent(this.options); |
| 218 | + this.uri = null; |
| 219 | + } |
| 220 | + |
| 221 | + createAsync = async ( |
| 222 | + progressUpdateInterval: number = 500, |
| 223 | + onRecordingStatusUpdate: (status: RecordingStatus) => void, |
| 224 | + ) => { |
| 225 | + this.recordingStateInterval = setInterval(() => { |
| 226 | + const status = this.recording.getStatus(); |
| 227 | + onRecordingStatusUpdate(status); |
| 228 | + }, progressUpdateInterval); |
| 229 | + this.uri = null; |
| 230 | + await this.recording.prepareToRecordAsync(this.options); |
| 231 | + this.recording.record(); |
| 232 | + }; |
| 233 | + |
| 234 | + stopAndUnloadAsync = async () => { |
| 235 | + clearInterval(this.recordingStateInterval); |
| 236 | + await this.recording.stop(); |
| 237 | + this.uri = this.recording.uri; |
| 238 | + this.recording.release(); |
| 239 | + }; |
| 240 | + |
| 241 | + getURI = () => this.uri; |
| 242 | +} |
| 243 | + |
108 | 244 | export const overrideAudioRecordingConfiguration = ( |
109 | 245 | audioRecordingConfiguration: AudioRecordingConfiguration, |
110 | 246 | ) => audioRecordingConfiguration; |
111 | 247 |
|
112 | | -export const Audio = AudioComponent ? new _Audio() : null; |
| 248 | +const flattenExpoAudioRecordingOptions = ( |
| 249 | + options: RecordingOptions & { |
| 250 | + bitRate?: number; |
| 251 | + extension?: string; |
| 252 | + numberOfChannels?: number; |
| 253 | + sampleRate?: number; |
| 254 | + }, |
| 255 | +) => { |
| 256 | + let commonOptions = { |
| 257 | + bitRate: options.bitRate, |
| 258 | + extension: options.extension, |
| 259 | + isMeteringEnabled: options.isMeteringEnabled ?? false, |
| 260 | + numberOfChannels: options.numberOfChannels, |
| 261 | + sampleRate: options.sampleRate, |
| 262 | + }; |
| 263 | + |
| 264 | + if (Platform.OS === 'ios') { |
| 265 | + commonOptions = { |
| 266 | + ...commonOptions, |
| 267 | + ...options.ios, |
| 268 | + }; |
| 269 | + } else if (Platform.OS === 'android') { |
| 270 | + const audioEncoder = options.android.audioEncoder; |
| 271 | + const audioEncoderConfig = audioEncoder |
| 272 | + ? { audioEncoder: expoAvToExpoAudioAndroidEncoderAdapter(audioEncoder) } |
| 273 | + : {}; |
| 274 | + const outputFormat = options.android.outputFormat; |
| 275 | + const outputFormatConfig = outputFormat |
| 276 | + ? { outputFormat: expoAvToExpoAudioAndroidOutputAdapter(outputFormat) } |
| 277 | + : {}; |
| 278 | + commonOptions = { |
| 279 | + ...commonOptions, |
| 280 | + ...options.android, |
| 281 | + ...audioEncoderConfig, |
| 282 | + ...outputFormatConfig, |
| 283 | + }; |
| 284 | + } |
| 285 | + return commonOptions; |
| 286 | +}; |
| 287 | + |
| 288 | +const expoAvToExpoAudioModeAdapter = (mode: AudioRecordingConfiguration['mode']) => { |
| 289 | + const { |
| 290 | + allowsRecordingIOS, |
| 291 | + interruptionModeAndroid, |
| 292 | + interruptionModeIOS, |
| 293 | + playsInSilentModeIOS, |
| 294 | + playThroughEarpieceAndroid, |
| 295 | + staysActiveInBackground, |
| 296 | + } = mode; |
| 297 | + |
| 298 | + return { |
| 299 | + allowsRecording: allowsRecordingIOS, |
| 300 | + interruptionMode: interruptionModeIOS, |
| 301 | + interruptionModeAndroid, |
| 302 | + playsInSilentMode: playsInSilentModeIOS, |
| 303 | + shouldPlayInBackground: staysActiveInBackground, |
| 304 | + shouldRouteThroughEarpiece: playThroughEarpieceAndroid, |
| 305 | + }; |
| 306 | +}; |
| 307 | + |
| 308 | +const expoAvToExpoAudioAndroidEncoderAdapter = ( |
| 309 | + audioEncoder: AudioRecordingConfiguration['options']['android']['audioEncoder'], |
| 310 | +) => { |
| 311 | + const encoderMap = { |
| 312 | + 0: 'default', |
| 313 | + 1: 'amr_nb', |
| 314 | + 2: 'amr_wb', |
| 315 | + 3: 'aac', |
| 316 | + 4: 'he_aac', |
| 317 | + 5: 'aac_eld', |
| 318 | + }; |
| 319 | + |
| 320 | + return Object.keys(encoderMap).includes(audioEncoder.toString()) |
| 321 | + ? encoderMap[audioEncoder] |
| 322 | + : 'default'; |
| 323 | +}; |
| 324 | + |
| 325 | +const expoAvToExpoAudioAndroidOutputAdapter = ( |
| 326 | + outputFormat: AudioRecordingConfiguration['options']['android']['outputFormat'], |
| 327 | +) => { |
| 328 | + const outputFormatMap = { |
| 329 | + 0: 'default', |
| 330 | + 1: '3gp', |
| 331 | + 2: 'mpeg4', |
| 332 | + 3: 'amrnb', |
| 333 | + 4: 'amrwb', |
| 334 | + 5: 'default', |
| 335 | + 6: 'aac_adts', |
| 336 | + 7: 'default', |
| 337 | + 8: 'mpeg2ts', |
| 338 | + 9: 'webm', |
| 339 | + }; |
| 340 | + |
| 341 | + return Object.keys(outputFormatMap).includes(outputFormat.toString()) |
| 342 | + ? outputFormatMap[outputFormat] |
| 343 | + : 'default'; |
| 344 | +}; |
| 345 | + |
| 346 | +// Always try to prioritize expo-audio if it's there. |
| 347 | +export const Audio = ExpoRecordingComponent |
| 348 | + ? new _AudioExpoAudio() |
| 349 | + : AudioComponent |
| 350 | + ? new _AudioExpoAV() |
| 351 | + : null; |
0 commit comments