fix: device rotation

Switch input function does not work at the beginning on the call,
that cause  producing wrong video output if device orientation was
changed between calls.
This patch use call manager to change video output.

Change-Id: Ia3f33cf8dfbaaca6eacf0f601b1a8e098a6d0825
Gitlab: #28
diff --git a/Ring/Ring/Bridging/VideoAdapter.h b/Ring/Ring/Bridging/VideoAdapter.h
index 234ca28..35e2174 100644
--- a/Ring/Ring/Bridging/VideoAdapter.h
+++ b/Ring/Ring/Bridging/VideoAdapter.h
@@ -33,5 +33,6 @@
 - (void)writeOutgoingFrameWithImage:(UIImage*)image;
 - (void)setDecodingAccelerated:(BOOL)state;
 - (void)switchInput:(NSString*)deviceName;
+- (void)switchInput:(NSString*)deviceName forCall:(NSString*) callID;
 
 @end
diff --git a/Ring/Ring/Bridging/VideoAdapter.mm b/Ring/Ring/Bridging/VideoAdapter.mm
index bc7baf0..6bc6c58 100644
--- a/Ring/Ring/Bridging/VideoAdapter.mm
+++ b/Ring/Ring/Bridging/VideoAdapter.mm
@@ -21,6 +21,7 @@
 #import "VideoAdapter.h"
 #import "Utils.h"
 #import "dring/videomanager_interface.h"
+#import "dring/callmanager_interface.h"
 #import "Ring-Swift.h"
 #include <pthread.h>
 #include <functional>
@@ -199,6 +200,10 @@
     DRing::switchInput(std::string([deviceName UTF8String]));
 }
 
+- (void)switchInput:(NSString*)deviceName forCall:(NSString*) callID {
+    DRing::switchInput(std::string([callID UTF8String]), std::string([deviceName UTF8String]));
+}
+
 #pragma mark PresenceAdapterDelegate
 
 + (id <VideoAdapterDelegate>)delegate {
diff --git a/Ring/Ring/Calls/CallViewModel.swift b/Ring/Ring/Calls/CallViewModel.swift
index e1e030b..3b9dbe9 100644
--- a/Ring/Ring/Calls/CallViewModel.swift
+++ b/Ring/Ring/Calls/CallViewModel.swift
@@ -97,7 +97,7 @@
                 return call.state == .over || call.state == .failure
             }).map({ [weak self] hide in
                 if hide {
-                    self?.videoService.setCameraOrientation(orientation: UIDevice.current.orientation)
+                    self?.videoService.setCameraOrientation(orientation: UIDevice.current.orientation, callID: nil)
                 }
                 return hide
             })
@@ -301,8 +301,10 @@
             return call.callId == self?.call?.callId
         }).map({ call in
             return call.state == .current
-        }).subscribe(onNext: { [weak self] _ in
-            self?.videoService.setCameraOrientation(orientation: UIDevice.current.orientation)
+        }).subscribe(onNext: { [weak self] call in
+            self?.videoService
+                .setCameraOrientation(orientation: UIDevice.current.orientation,
+                                      callID: self?.call?.callId)
         }).disposed(by: self.disposeBag)
     }
 
@@ -411,6 +413,7 @@
     }
 
     func setCameraOrientation(orientation: UIDeviceOrientation) {
-        videoService.setCameraOrientation(orientation: orientation)
+        videoService.setCameraOrientation(orientation: orientation,
+                                          callID: self.call?.callId)
     }
 }
diff --git a/Ring/Ring/Services/VideoAdapterDelegate.swift b/Ring/Ring/Services/VideoAdapterDelegate.swift
index fd21a26..46749ca 100644
--- a/Ring/Ring/Services/VideoAdapterDelegate.swift
+++ b/Ring/Ring/Services/VideoAdapterDelegate.swift
@@ -25,5 +25,5 @@
     func stopCapture()
     func writeFrame(withImage image: UIImage?)
     func setDecodingAccelerated(withState state: Bool)
-    func switchInput(toDevice device: String)
+    func switchInput(toDevice device: String, callID: String?)
 }
diff --git a/Ring/Ring/Services/VideoService.swift b/Ring/Ring/Services/VideoService.swift
index fb3bb67..7ce9852 100644
--- a/Ring/Ring/Services/VideoService.swift
+++ b/Ring/Ring/Services/VideoService.swift
@@ -336,7 +336,7 @@
         }).disposed(by: self.disposeBag)
     }
 
-    func setCameraOrientation(orientation: UIDeviceOrientation) {
+    func setCameraOrientation(orientation: UIDeviceOrientation, callID: String?) {
         var newOrientation: AVCaptureVideoOrientation
         switch orientation {
         case .portrait:
@@ -358,7 +358,7 @@
         let deviceName: String =
             (orientation == .landscapeLeft || orientation == .landscapeRight) ?
                 self.camera.nameLandscape : self.camera.namePortrait
-        self.switchInput(toDevice: self.camera.nameCamera + deviceName)
+        self.switchInput(toDevice: self.camera.nameCamera + deviceName, callID: callID)
         self.camera.rotateCamera(orientation: newOrientation)
             .subscribe(onCompleted: { [unowned self] in
                 self.log.debug("new camera orientation isPortrait: \(orientation.isPortrait)")
@@ -369,7 +369,11 @@
 }
 
 extension VideoService: VideoAdapterDelegate {
-    func switchInput(toDevice device: String) {
+    func switchInput(toDevice device: String, callID: String?) {
+        if let call = callID {
+            videoAdapter.switchInput(device, forCall: call)
+            return
+        }
         videoAdapter.switchInput(device)
     }