Commits
cristianoccazinsp authored and GitHub committed c7e92b29c81
feature(record): On recording start and end events (#2702)
* This update tries to improve audio recording interruptions on iOS due to phone calls or background music.
- Use a more generic event to handle session interruptions. This removes the need to listen to foreground/background events, and stopping the session this way was actually redundant/wrong (see https://forums.developer.apple.com/thread/61406). This also makes session stopping detection more reliable (calls, suspension due to a call or notification, etc., which would previously not set the recording interrupted flag on every case)
From the above docs: "No, incorrect. You _never_ need to stop your capture session. The capture session automatically stops itself when your app goes to the background and resumes itself when you come back to the foreground."
- Allow for `captureAudio` updates to also update the audio connections internally so the prop can be correctly updated on the fly without remounting.
- add onAudioInterrupted and onAudioConnected events so the UI can handle scenarios where audio is wanted but not available. This should also help in keeping the preview active even if audio is interrupted and we have captureAudio={true}. Lastly, it can be used to detect if we can record audio or not due to the dummy implementation of the audio permission on iOS always returning true.
- check, activate, and release audio sessions (if captureAudio) so we can detect early if audio is available before attempting to connect the input. This will also allow us to detect if we can record even if there was already a call before opening the camera.
- use proper observer for session error instead of of the strong self block. No benefit, but makes code more readable and allows access to instance variables
- getDeviceOrientationWithBlock might fire more than once under some circumstances, ending up taking a picture or video twice. Add a lock and additional check to prevent this.
* no need for change check,
* do not resume audio if we were hinted not to (e.g., music playback happening)
* Move heavy work to a dedicated background thread. Improves camera initial loading and resumes from background.
Details:
- Use a HandlerThread to delegate heavy tasks to background. The thread is managed by the view, and passed down to the implementation in case it also needs to use it. The view will fire start calls and other possibly heavy operations in this thread to avoid ANRs. Some code sent to this thread:
- start calls: start is extremely heavy and will cause ANRs on some devices, especially when coming back from background
- Camera1: some preset changes fire a stop/start sequence. These will now happen in the background thread
- take picture and start recording (from view class) will also start in this thread
- Add some extra null checks
- View was not properly cleaning up itself on destroy (host destroy event was never fired)
* Fix for a possible crash when changing devices and changing focus. If the new device resets the focus, "defocusing" might not be possible if the new device does not support auto focus. For this reason, we need to do a different cleanup on the focus and exposed flags and events.
* start session here also on session queue.
* check for session running before trying to record or capture.
This should fix a possible race condition where both the session start call happens at the same time as the record call
* set preview orientation also in session queue
* no need to set orientation on constructor, and set it on session queue to prevent race conditions
* move device init and checks also to session queue. This prevents possible double initializations.
* catch possible errors when starting camera preview. This might still randomly fail on some devices for some reason.
* delay capture in progress until we have resumed/paused preview.
* do not crash the app if set texture setup failed
* more synchronized checks to prevent crashes due to concurrent camera updates
* remove unused imports
* Handle audio interruption in session queue. This prevents the session isRunning flag from getting corrupted due to concurrent updates to the session.
* Fix possible crash when attempting to retrieve camera parameters.
* Preserve exif/metadata on photo capture. Add a few comments to each step of the capture process
* orientation must be fixed before mirroring
* x/y dimensions are redundantly updated (storing the image sets them automatically). However, orientation must be reset on any image change since the final stored image is automatically rotated when it is modified in place.
* revert mirrorImage order, has to be first since forceUpOrientation adjusts the image afterwards.
* Minor change: also implement `onPictureTaken` for iOS in case anyone needs the early event.
* - Improve Android code so skipProcessing is not needed, the code is more in line with iOS, and is "fast" by default. This means that skipProcessing is no longer needed (nor used), and adding additional options will "slow down" the capture as expected, rather than having always a lot of processing. This shouldn't be a breaking change.
- document the writeExif option, and implement it for iOS as well.
* This is a fix for events possibly being fired not on the main thread, which could cause the whole app to freeze.
* Fire events on the right thread. No need to use UI thread, and fix the still works
* Release CF object which could cause a memleak
* Fix Objects.equals that is only available after API 19
* allow for audio session to be kept even after unmounts
* readme typo
* readme typo
* Make camera ready events to fire also on camera/device change to be consistent with Android. Fire unmount error when session or device fails to start.
* update example app to properly use camera ready event
* Make camera ready events to fire also on camera/device change to be consistent with Android. Fire unmount error when session or device fails to start. Update advanced example app to use camera ready event instead.
* Android crash fixes. Make sure no unsupported aspect ratio is used, and do not crash when there are no cameras available.
* stop/release camera in non-UI thread so we prevent ANRs and UI freezing.
Some phones may take up to a second to release the camera and preview.
* move codec, max duration, and max file size settings to the session queue.
This might prevent a race condition when changing presets/quality.
* android crash fix
* Add onRecordingStart and onRecordingEnd events
* fix for surface destroy and resume events.
* add missing types
* fix for surface destroy and resume events.
* add missing types
Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>