Skip to content

Commit

Permalink
Improvements to photo input item:
Browse files Browse the repository at this point in the history
 - Just one capture session per input component.
 - Avoids starting/stoping capture during collection reloads, which can make the live camera take longer to capture or even hang.
 - Moves creation/destruction of capture session to the background: when opening/closing the input component many times it was resulting in capture start / stop blocking the main thread for some seconds on iOS 8
 - Queues updates in the collection view: doing a reloadData right after reloadItemsAtIndexPaths (update video) results in the visible cells not being refreshed

Todo: tests
  • Loading branch information
diegosanchezr committed Jun 29, 2016
1 parent 25b32bb commit 4e2dfd2
Show file tree
Hide file tree
Showing 10 changed files with 640 additions and 454 deletions.
2 changes: 2 additions & 0 deletions Chatto/Source/SerialTaskQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public final class SerialTaskQueue: SerialTaskQueueProtocol {
private var isStopped = true
private var tasksQueue = [TaskClosure]()

public init() {}

public func addTask(task: TaskClosure) {
self.tasksQueue.append(task)
self.maybeExecuteNextTask()
Expand Down
8 changes: 8 additions & 0 deletions ChattoAdditions/ChattoAdditions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
3907DBAF27F9A201978EDDC1 /* BaseMessageCollectionViewCellDefaultStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3907D74597451AA0F6DAE924 /* BaseMessageCollectionViewCellDefaultStyle.swift */; };
C33DEA451D23F825002AAD26 /* LiveCameraCellPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C33DEA441D23F825002AAD26 /* LiveCameraCellPresenter.swift */; };
C33DEA471D23F8C8002AAD26 /* LiveCameraCaptureSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = C33DEA461D23F8C8002AAD26 /* LiveCameraCaptureSession.swift */; };
C35FE3C51C0331CF00D42980 /* TextMessagePresenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35FE3C41C0331CF00D42980 /* TextMessagePresenterTests.swift */; };
C35FE3C81C033E7800D42980 /* PhotoMessagePresenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C35FE3C71C033E7800D42980 /* PhotoMessagePresenterTests.swift */; };
C36F9C4B1BFE4A89001E9D8F /* ChattoAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = C3C0CC961BFE4A2A0052747C /* ChattoAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -95,6 +97,8 @@

/* Begin PBXFileReference section */
3907D74597451AA0F6DAE924 /* BaseMessageCollectionViewCellDefaultStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseMessageCollectionViewCellDefaultStyle.swift; sourceTree = "<group>"; };
C33DEA441D23F825002AAD26 /* LiveCameraCellPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiveCameraCellPresenter.swift; sourceTree = "<group>"; };
C33DEA461D23F8C8002AAD26 /* LiveCameraCaptureSession.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LiveCameraCaptureSession.swift; sourceTree = "<group>"; };
C35FE3C41C0331CF00D42980 /* TextMessagePresenterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TextMessagePresenterTests.swift; path = "Tests/Chat Items/TextMessages/TextMessagePresenterTests.swift"; sourceTree = SOURCE_ROOT; };
C35FE3C71C033E7800D42980 /* PhotoMessagePresenterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoMessagePresenterTests.swift; sourceTree = "<group>"; };
C3815CFF1C036B3000DF95CA /* PhotoMessagePresenterBuilderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoMessagePresenterBuilderTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -347,6 +351,8 @@
isa = PBXGroup;
children = (
C3C0CC0D1BFE496A0052747C /* LiveCameraCell.swift */,
C33DEA461D23F8C8002AAD26 /* LiveCameraCaptureSession.swift */,
C33DEA441D23F825002AAD26 /* LiveCameraCellPresenter.swift */,
C3C0CC0E1BFE496A0052747C /* Photos.xcassets */,
C3C0CC0F1BFE496A0052747C /* PhotosChatInputItem.swift */,
C3C0CC101BFE496A0052747C /* PhotosInputCameraPicker.swift */,
Expand Down Expand Up @@ -606,11 +612,13 @@
C3C0CC2E1BFE496A0052747C /* BaseMessageCollectionViewCell.swift in Sources */,
C3C0CC5A1BFE496A0052747C /* PhotosInputCameraPicker.swift in Sources */,
C3C0CC551BFE496A0052747C /* ExpandableTextView.swift in Sources */,
C33DEA451D23F825002AAD26 /* LiveCameraCellPresenter.swift in Sources */,
C3C0CC6B1BFE496A0052747C /* CircleProgressView.m in Sources */,
C3C0CC371BFE496A0052747C /* PhotoMessageViewModel.swift in Sources */,
C3C0CC5B1BFE496A0052747C /* PhotosInputCell.swift in Sources */,
C3C0CC561BFE496A0052747C /* HorizontalStackScrollView.swift in Sources */,
C3C0CC5F1BFE496A0052747C /* PhotosInputViewItemSizeCalculator.swift in Sources */,
C33DEA471D23F8C8002AAD26 /* LiveCameraCaptureSession.swift in Sources */,
C3C0CC671BFE496A0052747C /* CircleIconView.m in Sources */,
C3C0CC301BFE496A0052747C /* ViewDefinitions.swift in Sources */,
C3C0CC531BFE496A0052747C /* ChatInputItem.swift in Sources */,
Expand Down
101 changes: 101 additions & 0 deletions ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
The MIT License (MIT)

Copyright (c) 2015-present Badoo Trading Limited.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import Foundation
import Photos

class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol {

private enum OperationType: String {
case start
case stop
}

var isInitialized: Bool = false

var isCapturing: Bool {
return self.isInitialized && self.captureSession.running
}

deinit {
var layer = self.captureLayer
var session: AVCaptureSession? = self.captureSession
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// Analogously to AVCaptureSession creation, dealloc can take very long, so let's do it out of the main thread
if layer != nil { layer = nil }
if session != nil { session = nil }
}
}

func startCapturing(completion: () -> Void) {
let operation = NSBlockOperation()
operation.addExecutionBlock { [weak operation, weak self] in
guard let strongSelf = self, strongOperation = operation else { return }
if !strongOperation.cancelled && !strongSelf.captureSession.running {
strongSelf.captureSession.startRunning()
NSOperationQueue.mainQueue().addOperationWithBlock(completion)
}
}
self.queue.cancelOperation(forKey: "stopCapturingOperation")
self.queue.addOperation(operation, forKey: "startCapturingOperation")
}

func stopCapturing(completion: () -> Void) {
let operation = NSBlockOperation()
operation.addExecutionBlock { [weak operation, weak self] in
guard let strongSelf = self, strongOperation = operation else { return }
if !strongOperation.cancelled && strongSelf.captureSession.running {
strongSelf.captureSession.stopRunning()
NSOperationQueue.mainQueue().addOperationWithBlock(completion)
}
}
self.queue.cancelOperation(forKey: "startCapturingOperation")
self.queue.addOperation(operation, forKey: "stopCapturingOperation")
}

private (set) var captureLayer: AVCaptureVideoPreviewLayer?

private lazy var queue: KeyedOperationQueue = {
let queue = KeyedOperationQueue()
queue.qualityOfService = .UserInitiated
queue.maxConcurrentOperationCount = 1
return queue
}()

private lazy var captureSession: AVCaptureSession = {
assert(!NSThread.isMainThread(), "This can be very slow, make sure it happens in a background thread")
let session = AVCaptureSession()
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: device)
session.addInput(input)
} catch {

}
self.captureLayer = AVCaptureVideoPreviewLayer(session: session)
self.captureLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
self.isInitialized = true
return session
}()
}
194 changes: 38 additions & 156 deletions ChattoAdditions/Source/Input/Photos/LiveCameraCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Chatto

protocol LiveCameraCaptureSessionProtocol {
var captureLayer: AVCaptureVideoPreviewLayer? { get }
var isInitialized: Bool { get }
var isCapturing: Bool { get }
func startCapturing(completion: () -> Void)
func stopCapturing(completion: () -> Void)
Expand All @@ -42,10 +43,6 @@ class LiveCameraCell: UICollectionViewCell {
static let lockedCameraImageName = "camera_lock"
}

lazy var captureSession: LiveCameraCaptureSessionProtocol = {
return LiveCameraCaptureSession()
}()

private var iconImageView: UIImageView!

override init(frame: CGRect) {
Expand All @@ -58,31 +55,53 @@ class LiveCameraCell: UICollectionViewCell {
self.commonInit()
}

deinit {
self.unsubscribeFromAppNotifications()
}

private func commonInit() {
self.configureIcon()
self.contentView.backgroundColor = Constants.backgroundColor
}

private func configureIcon() {
self.iconImageView = UIImageView()
self.iconImageView.contentMode = .Center
self.contentView.addSubview(self.iconImageView)
var captureLayer: CALayer? {
didSet {
if oldValue !== self.captureLayer {
oldValue?.removeFromSuperlayer()
if let captureLayer = self.captureLayer {
self.contentView.layer.insertSublayer(captureLayer, below: self.iconImageView.layer)
let animation = CABasicAnimation.bma_fadeInAnimationWithDuration(0.25)
let animationKey = "fadeIn"
captureLayer.removeAnimationForKey(animationKey)
captureLayer.addAnimation(animation, forKey: animationKey)
}
}
}
}

typealias CellCallback = (cell: LiveCameraCell) -> Void

var onWillBeAddedToWindow: CellCallback?
override func willMoveToWindow(newWindow: UIWindow?) {
if newWindow != nil {
self.onWillBeAddedToWindow?(cell: self)
}
}

var onWasRemovedFromWindow: CellCallback?
override func didMoveToWindow() {
if self.window == nil {
self.onWasRemovedFromWindow?(cell: self)
}
}

private var authorizationStatus: AVAuthorizationStatus = .NotDetermined
func updateWithAuthorizationStatus(status: AVAuthorizationStatus) {
self.authorizationStatus = status
self.updateIcon()
}

if self.isCaptureAvailable {
self.subscribeToAppNotifications()
} else {
self.unsubscribeFromAppNotifications()
}
private var authorizationStatus: AVAuthorizationStatus = .NotDetermined

private func configureIcon() {
self.iconImageView = UIImageView()
self.iconImageView.contentMode = .Center
self.contentView.addSubview(self.iconImageView)
}

private func updateIcon() {
Expand All @@ -95,147 +114,10 @@ class LiveCameraCell: UICollectionViewCell {
self.setNeedsLayout()
}

private var isCaptureAvailable: Bool {
switch self.authorizationStatus {
case .NotDetermined, .Restricted, .Denied:
return false
case .Authorized:
return true
}
}

func startCapturing() {
guard self.isCaptureAvailable else { return }
self.captureSession.startCapturing() { [weak self] in
self?.addCaptureLayer()
}
}

private func addCaptureLayer() {
guard let captureLayer = self.captureSession.captureLayer else { return }
self.contentView.layer.insertSublayer(captureLayer, below: self.iconImageView.layer)
let animation = CABasicAnimation.bma_fadeInAnimationWithDuration(0.25)
let animationKey = "fadeIn"
captureLayer.removeAnimationForKey(animationKey)
captureLayer.addAnimation(animation, forKey: animationKey)
}

func stopCapturing() {
guard self.isCaptureAvailable else { return }
self.captureSession.stopCapturing() { [weak self] in
self?.removeCaptureLayer()
}
}

private func removeCaptureLayer() {
self.captureSession.captureLayer?.removeFromSuperlayer()
}

override func layoutSubviews() {
super.layoutSubviews()

if self.isCaptureAvailable {
self.captureSession.captureLayer?.frame = self.contentView.bounds
}

self.captureLayer?.frame = self.contentView.bounds
self.iconImageView.sizeToFit()
self.iconImageView.center = self.contentView.bounds.bma_center
}

override func didMoveToWindow() {
if self.window == nil {
self.stopCapturing()
}
}

// MARK: - App Notifications
lazy var notificationCenter = {
return NSNotificationCenter.defaultCenter()
}()

private func subscribeToAppNotifications() {
self.notificationCenter.addObserver(self, selector: #selector(LiveCameraCell.handleWillResignActiveNotification), name: UIApplicationWillResignActiveNotification, object: nil)
self.notificationCenter.addObserver(self, selector: #selector(LiveCameraCell.handleDidBecomeActiveNotification), name: UIApplicationDidBecomeActiveNotification, object: nil)
}

private func unsubscribeFromAppNotifications() {
self.notificationCenter.removeObserver(self)
}

private var needsRestoreCaptureSession = false
func handleWillResignActiveNotification() {
if self.captureSession.isCapturing {
self.needsRestoreCaptureSession = true
self.stopCapturing()
}
}

func handleDidBecomeActiveNotification() {
if self.needsRestoreCaptureSession {
self.needsRestoreCaptureSession = false
self.startCapturing()
}
}
}

private class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol {
init() {
self.configureCaptureSession()
}

private var captureSession: AVCaptureSession!
private (set) var captureLayer: AVCaptureVideoPreviewLayer?

private func configureCaptureSession() {
self.captureSession = AVCaptureSession()
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: device)
self.captureSession.addInput(input)
} catch {

}

self.captureLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.captureLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
}

private lazy var queue: KeyedOperationQueue = {
let queue = KeyedOperationQueue()
queue.qualityOfService = .UserInteractive
queue.maxConcurrentOperationCount = 1
return queue
}()

func startCapturing(completion: () -> Void) {
let operation = NSBlockOperation()
operation.addExecutionBlock { [weak operation, weak self] in
guard let strongSelf = self, strongOperation = operation else { return }
if !strongOperation.cancelled && !strongSelf.captureSession.running {
strongSelf.captureSession.startRunning()
NSOperationQueue.mainQueue().addOperationWithBlock({
completion()
})
}
}
self.queue.addOperation(operation, forKey: "startCapturingOperation")
}

func stopCapturing(completion: () -> Void) {
let operation = NSBlockOperation()
operation.addExecutionBlock { [weak operation, weak self] in
guard let strongSelf = self, strongOperation = operation else { return }
if !strongOperation.cancelled && strongSelf.captureSession.running {
strongSelf.captureSession.stopRunning()
NSOperationQueue.mainQueue().addOperationWithBlock({
completion()
})
}
}
self.queue.addOperation(operation, forKey: "stopCapturingOperation")
}

var isCapturing: Bool {
return self.captureSession.running
}
}
Loading

0 comments on commit 4e2dfd2

Please sign in to comment.