0

I know I can use GCD or an OperationQueue to do concurrent workload but I've been trying to look for a way to call multiple functions and have them start at the same time.

My project here is to trigger multiple webcams at once to have synchronised photos coming from different cameras.

I had found this post about how to display multiple feeds from multiple webcams at once here: Run multiple AVCaptureSessions or add multiple inputs

I am unsure of how I will be able to trigger a photo capture from these as of yet, but I first wanted to see how I would go about synchronising the call to the function.

My theoretical solution would be something like this:

  • create concurrent operation queue
  • make it so I can start the operation queue manually rather than start each operation automatically as it would be added to the queue (how ?)
  • add an operation that would take a picture from the associated video input. Once for each camera
  • start operation queue
  • wait for operations to finish
  • continue workflow

Is this possible ?

If not is calling multiple methods truly at once even possible ?

If even this isn't possible how would I go about synchronising the photo capture, maybe recording a short video, use timestamp right before the start of the recording to adjust delay and capture frames at a specific time in the resulting videos ?

Also in the comments, what is the tag for a MacOS application built in swift ? It's my first time asking for this rather than iOS so it would help find people who might be able to help.

Thanks for any insight you might be able to give me !

James Woodrow
  • 365
  • 1
  • 14

1 Answers1

0

You are on the right track (NS)OperationQueue is made for this. Below is an example:

func operation1() {
    print("\(#function) starts")
    sleep(1)
    print("\(#function) ends")
}

func operation2() {
    print("\(#function) starts")
    sleep(2)
    print("\(#function) ends")
}

func operation3() {
    print("\(#function) starts")
    sleep(3)
    print("\(#function) ends")
}

@IBAction func start(_ sender: Any) {
    let operation = BlockOperation(block: {})
    operation.addExecutionBlock { self.operation1() }
    operation.addExecutionBlock { self.operation2() }
    operation.addExecutionBlock { self.operation3() }

    let endOperation = BlockOperation { self.allFinished() }
    endOperation.addDependency(operation)

    let queue = OperationQueue()
    queue.addOperations([operation, endOperation], waitUntilFinished: false)
}

func allFinished() {
    print("all finished")
}

operation1, 2, 3 can start in arbitrary order but the finishing order is always 1, 2, 3, and then allFinished is triggered.

Code Different
  • 90,614
  • 16
  • 144
  • 163