stop videoCapture until video track is enabled to save some battery

Instruments showed a reduction from ~65% -> ~45% while on audio only
call on iPhone5c.

// FREEBIE
This commit is contained in:
Michael Kirk 2017-03-06 12:40:22 -05:00
parent 337c408810
commit e3eca4db7b

View file

@ -108,6 +108,7 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
// Video
private var videoCaptureSession: AVCaptureSession?
private var videoSender: RTCRtpSender?
private var localVideoTrack: RTCVideoTrack?
// RTCVideoTrack is fragile and prone to throwing exceptions and/or
@ -192,7 +193,9 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
// TODO: Revisit the cameraConstraints.
let videoSource = factory.avFoundationVideoSource(with: cameraConstraints)
self.videoCaptureSession = videoSource.captureSession
videoSource.useBackCamera = false
let localVideoTrack = factory.videoTrack(with: videoSource, trackId: Identifiers.videoTrack.rawValue)
self.localVideoTrack = localVideoTrack
@ -220,9 +223,22 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
Logger.error("\(self.TAG)) trying to \(action) videoTrack which doesn't exist")
return
}
guard let videoCaptureSession = self.videoCaptureSession else {
Logger.error("\(self.TAG) videoCaptureSession was unexpectedly nil")
assertionFailure()
return
}
localVideoTrack.isEnabled = enabled
if enabled {
Logger.debug("\(self.TAG) in \(#function) starting videoCaptureSession")
videoCaptureSession.startRunning()
} else {
Logger.debug("\(self.TAG) in \(#function) stopping videoCaptureSession")
videoCaptureSession.stopRunning()
}
if let delegate = self.delegate {
DispatchQueue.main.async { [weak self, weak localVideoTrack] in
guard let strongSelf = self else { return }