Fixed crash on iOS13, methods beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications.

1. On iOS13 the implementation of methods begin- and endGeneratingDeviceOrientationNotifications changed and now are looks like "not threadsafe" (in specific sence) - they should be called only on the main thread. This fact is not documented. And may be a mistake.

2. By the Apple official documentation methods begin- and endGeneratingDeviceOrientationNotifications should be balanced. (Each begin- method should be balanced with end- method.)

By the reason two above facts they consequences merged and produced the "floating" NSInternalInconsistencyException crash.

Bug: webrtc:11216
Change-Id: Ibedd5bba7476cc687de3b9b04be49e3cceac1d27
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/162205
Reviewed-by: Anders Carlsson <andersc@webrtc.org>
Commit-Queue: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30121}
diff --git a/AUTHORS b/AUTHORS
index 5f8113f..8b63772 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -6,6 +6,7 @@
 Alexander Brauckmann <a.brauckmann@gmail.com>
 Alexandre Gouaillard <agouaillard@gmail.com>
 Andrew MacDonald <andrew@webrtc.org>
+Andrey Efremov <yoklmnprst@ya.ru>
 Anil Kumar <an1kumar@gmail.com>
 Ben Strong <bstrong@gmail.com>
 Bob Withers <bwit@pobox.com>
diff --git a/sdk/objc/components/capturer/RTCCameraVideoCapturer.m b/sdk/objc/components/capturer/RTCCameraVideoCapturer.m
index 744edf6..f83c03e 100644
--- a/sdk/objc/components/capturer/RTCCameraVideoCapturer.m
+++ b/sdk/objc/components/capturer/RTCCameraVideoCapturer.m
@@ -41,6 +41,7 @@
   RTCVideoRotation _rotation;
 #if TARGET_OS_IPHONE
   UIDeviceOrientation _orientation;
+  BOOL _generatingOrientationNotifications;
 #endif
 }
 
@@ -158,7 +159,12 @@
                       RTCLogInfo("startCaptureWithDevice %@ @ %ld fps", format, (long)fps);
 
 #if TARGET_OS_IPHONE
-                      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
+                      dispatch_async(dispatch_get_main_queue(), ^{
+                        if (!self->_generatingOrientationNotifications) {
+                          [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
+                          self->_generatingOrientationNotifications = YES;
+                        }
+                      });
 #endif
 
                       self.currentDevice = device;
@@ -200,7 +206,12 @@
                       [self.captureSession stopRunning];
 
 #if TARGET_OS_IPHONE
-                      [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
+                      dispatch_async(dispatch_get_main_queue(), ^{
+                        if (self->_generatingOrientationNotifications) {
+                          [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
+                          self->_generatingOrientationNotifications = NO;
+                        }
+                      });
 #endif
                       self.isRunning = NO;
                       if (completionHandler) {