Repository: flutter
Issue: [camera][iOS]: NSInternalInconsistencyException:  [AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] Must start a session (using -[AVAssetWriter startSessionAtSourceTime:) before appending pixel buffers
Labels: c: crash,platform-ios,p: camera,package,a: release,P2,needs repro info,team-ios,triaged-ios

## Steps to Reproduce

I could not reproduce it and it happened once on a ```iPhone 12 Pro Max``` in ```release``` mode only.  
This may be related to #95459 but i don't know.

I believe according to the trace that it's related to the ```camera``` but i am not sure.

My camera main code is in #95459, i would add that i use ```cameraController.pausePreview()``` when ```AppLifecycleState.paused``` and ```cameraController.resumePreview()``` when ```AppLifecycleState.resumed```. 

I am calling ```cameraController.dispose()``` in the ```dispose()``` ```Widget``` method. 

Sometimes on iOS only, putting the app in background (```paused``` or ```inactive```) is extremely laggy and finally crashes.
I will try to find a way to show you this better with a video so you understand better.

**Expected results:** <!-- what did you want to see? -->

The app should not crash.

**Actual results:** <!-- what did you see? -->

The app is crashing.  
The error i am getting from ```crashlytics``` is : 

```
Fatal Exception: NSInternalInconsistencyException
*** -[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] Must start a session (using -[AVAssetWriter startSessionAtSourceTime:) before appending pixel buffers
-[AVFigAssetWriterTrack addPixelBuffer:atPresentationTime:error:]
```
![Capture d’écran 2021-12-18 à 12 16 33](https://user-images.githubusercontent.com/32631467/146639009-247a21d2-1f43-4826-9c2c-e184f760f10a.png)

<details>
  <summary>Logs</summary>

```
Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x9904c __exceptionPreprocess
1  libobjc.A.dylib                0x15f54 objc_exception_throw
2  AVFCore                        0xc1c18 -[AVFigAssetWriterTrack addPixelBuffer:atPresentationTime:error:]
3  AVFCore                        0xbd87c -[AVAssetWriterInputWritingHelper appendPixelBuffer:withPresentationTime:]
4  AVFCore                        0xbac54 -[AVAssetWriterInput _appendPixelBuffer:withPresentationTime:]
5  AVFCore                        0xc0be8 -[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:]
6  camera                         0x6088 (Missing)
7  AVFCapture                     0x1956c -[AVCaptureVideoDataOutput _processSampleBuffer:]
8  AVFCapture                     0x36c4 __47-[AVCaptureVideoDataOutput _updateRemoteQueue:]_block_invoke
9  CMCapture                      0x14df94 __FigRemoteOperationReceiverCreateMessageReceiver_block_invoke
10 CMCapture                      0x2fdccc __rqReceiverSetSource_block_invoke
11 libdispatch.dylib              0x4660 _dispatch_client_callout
12 libdispatch.dylib              0x7b34 _dispatch_continuation_pop
13 libdispatch.dylib              0x1ac38 _dispatch_source_invoke
14 libdispatch.dylib              0x12ab4 _dispatch_main_queue_callback_4CF
15 CoreFoundation                 0x51cd4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                 0xbeac __CFRunLoopRun
17 CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific
18 GraphicsServices               0x138c GSEventRunModal
19 UIKitCore                      0x5196a8 -[UIApplication _run]
20 UIKitCore                      0x2987f4 UIApplicationMain
21 Runner                         0x7ab0 main + 7 (AppDelegate.swift:7)
22 ???                            0x103789a24 (Missing)

Crashed: com.google.firebase.crashlytics.ios.exception
0  FirebaseCrashlytics            0x1f44c FIRCLSProcessRecordAllThreads + 184
1  FirebaseCrashlytics            0x1f82c FIRCLSProcessRecordAllThreads + 1176
2  FirebaseCrashlytics            0x167f0 FIRCLSHandler + 48
3  FirebaseCrashlytics            0x10c78 __FIRCLSExceptionRecord_block_invoke + 92
4  libdispatch.dylib              0x4660 _dispatch_client_callout + 20
5  libdispatch.dylib              0x138b4 _dispatch_lane_barrier_sync_invoke_and_complete + 56
6  FirebaseCrashlytics            0x10414 FIRCLSExceptionRecord + 216
7  FirebaseCrashlytics            0x10720 FIRCLSExceptionRecordNSException + 560
8  FirebaseCrashlytics            0xffec FIRCLSTerminateHandler() + 404
9  libc++abi.dylib                0x12f58 std::__terminate(void (*)()) + 20
10 libc++abi.dylib                0x12ef4 std::terminate() + 64
11 libobjc.A.dylib                0x2a4d4 objc::DenseMapBase<objc::DenseMap<objc_class*, PendingInitialize*, objc::DenseMapValueInfo<PendingInitialize*>, objc::DenseMapInfo<objc_class*>, objc::detail::DenseMapPair<objc_class*, PendingInitialize*> >, objc_class*, PendingInitialize*, objc::DenseMapValueInfo<PendingInitialize*>, objc::DenseMapInfo<objc_class*>, objc::detail::DenseMapPair<objc_class*, PendingInitialize*> >::FatalCorruptHashTables(objc::detail::DenseMapPair<objc_class*, PendingInitialize*> const*, unsigned int) const + 14
12 libdispatch.dylib              0x4674 _dispatch_client_callout + 40
13 libdispatch.dylib              0x7b34 _dispatch_continuation_pop + 504
14 libdispatch.dylib              0x1ac38 _dispatch_source_invoke + 1356
15 libdispatch.dylib              0x12ab4 _dispatch_main_queue_callback_4CF + 772
16 CoreFoundation                 0x51cd4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
17 CoreFoundation                 0xbeac __CFRunLoopRun + 2540
18 CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
19 GraphicsServices               0x138c GSEventRunModal + 164
20 UIKitCore                      0x5196a8 -[UIApplication _run] + 1100
21 UIKitCore                      0x2987f4 UIApplicationMain + 2092
22 Runner                         0x7ab0 main + 7 (AppDelegate.swift:7)
23 ???                            0x103789a24 (Missing)

com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  Foundation                     0x18354 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 236
6  Foundation                     0x59c28 -[NSRunLoop(NSRunLoop) runUntilDate:] + 92
7  UIKitCore                      0x4928a4 -[UIEventFetcher threadMain] + 524
8  Foundation                     0x6836c __NSThread__start__ + 808
9  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
10 libsystem_pthread.dylib        0xea0 thread_start + 8

com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  CFNetwork                      0x27a6a0 _CFURLStorageSessionCopyIdentifier + 59784
6  Foundation                     0x6836c __NSThread__start__ + 808
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

Thread
0  libsystem_kernel.dylib         0x1b2c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1140 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe94 start_wqthread + 8

io.flutter.1.ui
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  Flutter                        0x2ee560 (Missing)
6  Flutter                        0x2edd20 (Missing)
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

io.flutter.1.raster
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  Flutter                        0x2ee560 (Missing)
6  Flutter                        0x2edd20 (Missing)
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

io.flutter.1.io
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  Flutter                        0x2ee560 (Missing)
6  Flutter                        0x2edd20 (Missing)
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.1
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.2
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.3
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.4
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.5
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

io.worker.6
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8254 _pthread_cond_wait + 1228
2  libc++.1.dylib                 0x9ddc std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
3  Flutter                        0x2e8918 (Missing)
4  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
5  libsystem_pthread.dylib        0xea0 thread_start + 8

dart:io EventHandler
0  libsystem_kernel.dylib         0x2e7c kevent + 8
1  Flutter                        0x4b14cc (Missing)
2  Flutter                        0x4e04d8 (Missing)
3  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
4  libsystem_pthread.dylib        0xea0 thread_start + 8

Thread
0  libsystem_pthread.dylib        0xe8c start_wqthread + 258

Thread
0  libsystem_pthread.dylib        0xe8c start_wqthread + 258

Thread
0  libsystem_pthread.dylib        0xe8c start_wqthread + 258

com.google.firebase.crashlytics.MachExceptionServer
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  FirebaseCrashlytics            0x19140 FIRCLSMachExceptionServer + 104
3  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
4  libsystem_pthread.dylib        0xea0 thread_start + 8

AVAudioSession Notify Thread
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  AudioSession                   0x6308 CADeprecated::GenericRunLoopThread::Entry(void*) + 164
6  AudioSession                   0xfd64 CADeprecated::CAPThread::Entry(CADeprecated::CAPThread*) + 92
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

com.squareup.SocketRocket.NetworkThread
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  Foundation                     0x18354 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 236
6  FirebaseDatabase               0x52f5c -[_FSRRunLoopThread main] + 252
7  Foundation                     0x6836c __NSThread__start__ + 808
8  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
9  libsystem_pthread.dylib        0xea0 thread_start + 8

com.apple.CoreMotion.MotionThread
0  libsystem_kernel.dylib         0x1504 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1b9c mach_msg + 76
2  CoreFoundation                 0x7688 __CFRunLoopServiceMachPort + 372
3  CoreFoundation                 0xb97c __CFRunLoopRun + 1212
4  CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
5  CoreFoundation                 0x9fa78 CFRunLoopRun + 64
6  CoreMotion                     0x13544 CLMotionActivity::isTypeInVehicle(CLMotionActivity::Type) + 21264
7  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
8  libsystem_pthread.dylib        0xea0 thread_start + 8

com.apple.CFSocket.private
0  libsystem_kernel.dylib         0x21d0 select$DARWIN_EXTSN + 8
1  libsystem_kernel.dylib         0x21d0 __select + 8
2  CoreFoundation                 0xaa740 __CFSocketManager + 648
3  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
4  libsystem_pthread.dylib        0xea0 thread_start + 8

DartWorker
0  libsystem_kernel.dylib         0x1f90 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x8280 _pthread_cond_wait + 1272
2  Flutter                        0x5a220c (Missing)
3  Flutter                        0x5e8624 (Missing)
4  Flutter                        0x5a1b34 (Missing)
5  libsystem_pthread.dylib        0x19a4 _pthread_start + 148
6  libsystem_pthread.dylib        0xea0 thread_start + 8

Thread
0  libsystem_kernel.dylib         0x1b2c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1140 _pthread_wqthread + 364
2  libsystem_pthread.dylib        0xe94 start_wqthread + 8
```

```
[✓] Flutter (Channel stable, 2.8.1, on macOS 12.1 21C52 darwin-x64, locale fr-FR)
    • Flutter version 2.8.1 at /Users/foxtom/Desktop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (2 days ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/foxtom/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.63.2)
    • VS Code at /Users/foxtom/Desktop/Visual Studio Code.app/Contents
    • Flutter extension version 3.29.0

[✓] Connected device (3 available)
    • Now You See Me (mobile) • 00008020-001204401E78002E • ios            • iOS 15.2 19C56
    • macOS (desktop)         • macos                     • darwin-x64     • macOS 12.1 21C52 darwin-x64
    • Chrome (web)            • chrome                    • web-javascript • Google Chrome 96.0.4664.110

• No issues found!
```

</details>