Sunday, May 3, 2020

Using swift to run blackmagic camera SDK in iOS Part1

This blog is a note to make a smartphone application to control Pocket Cinema Camera by blackmagicdesign with a smartphone (iPhone6S) & apple watch

Note: The content of this blog is NOT RELATED TO BLACKMAGICDESIGN. Please don't check with blackmagicdesign about what is written here.

The ultimate goal is to control Blackmagic's camera with an APPLE WATCH.
The port for the iPhone is in a separate part.


The work flow is as follows
  1. The application runs on macOS with swift5 fixing the SDK that black magic has published.(Chapter1&2)
  2. The UI included in the Blackmagic SDK is for MacOS, so it can't be used on iOS. So this time, I'm going to edit only the Blackmagic SDK API for iOS.(Chapter 3)
  3. Implementing the ability to connect the iPhone and Blackmagic's camera via Bluetooth(Chapter 3)
  4. Implement a feature that allows apple watch and iPhone to communicate, and allow apple watch to send recording commands to the camera.(Chapter 4)
First of all, I write the note when I modified the code of the camera control application published in blackmagicdesign's web site to work with the latest Swift5.

I don't know if the revised method is correct. Please understand that this is a revision at the level of "it works for the time being".

 Chapter 1: Procedures for running as a Mac app on MacOS 
  1. Download and extract the source code (published in Blackmagic Cameras Code Samples chapter).
  2. Modified the downloaded source code to load it into Xcode and run it on Swift5
  3. Confirmation of operation on the Mac.
  The environment is as follows
  • MacOS 10.15.4
  • Xcode 11.3.1
  • ios13.4.1(iPhone6S)
  • watchos 6.2(watch 5th gen)

Decompressing the downloaded sample code

 When you extract the downloaded sample code, it will be extracted into three folders as follows. The contents of the expanded Samples folder and Libraries folder are the target of the reorganization.


■Loading and checking for errors in Xcode
Open the CameraContorol.xcodeproj file in xcode, which is shown below.
Then the following dialog will pop up, and of course, press "OK" to open it.
Then xcode will show you a warning like the following
Of course, press "OK" to open it.
Then when I open it in Xcode, there are only three yellow warnings.
And when I tried to run it with Xcode...
SWIFT_VERSION '3.0' is unsupported, supported versions are: 4.0, 4.2, 5.0.  You will get a red error saying

So, as shown below, the Swift Compiler-Language at the bottom of the Project's Build Settings is set to Swift3.0(unsupported).
Change this to Swift5.
Then, if you run again, you will see the following
So, change the following settings of the other BluetoothControl project that will be loaded next to Swift5 in the same way.
So, when you run again...about 20 errors are displayed, but here's the main point!
First, there's a lot of simple 'characters' is unavailable: please use String directly'...
Remove the 'characters' from the code where the error occurs
Fixing the above fix in 15 places will reduce the error to 5!
Next, 'NSFontAttributeName' has been renamed to 'NSAttributedString.Key.font' because the error is happening in StringFunctions.swift...
Press the displayed Fix button and modify the code as follows.
We now have three errors left!
Next, the following three errors remain in ExpectedValues.shift...
I wasn't used to seeing <T: > after this class definition, but I googled it...
It's called Generic Functions (see here for details) 
Then, I tried to find out what type other than Integer is more versatile, and I found a setting called Numeric & Comparable. (The source of the story is here.).Implementing this change eliminated three errors at once.


 When I run again because the error is no longer there... Unfortunately, 45 other errors occur.

There are two kinds of errors, 'NSOffState' is unavailable in Swift: Use NSControl.StateValue.off and 'NSOnState' is unavailable in Swift: Use NSControl.StateValue.on.

I did some research on this error and found some helpful answers here. 
In accordance with this answer, amend it to read as follows

  • NSOnState  →  NSControl.StateValue.on  
  • NSOffState   → NSControl.StateValue.off
The actual modified situation looks like the following.

If you make this fix, the resulting errors are reduced to 19.
Next, some of the code encountered by Cannot call value of non-function type 'NSApplication' can be fixed with the Fix button, which is shown.
【Before the correction

After the correction

With this fix, the number of errors is reduced to 12.
Next, the "addChildViewController' has been renamed to 'addChild(_:)'  and 'removeChildViewController(at:)' has been renamed to 'removeChild(at:)'  that are occurring in BaseViewController.swift can be fixed with the "Fix" button that appears.
【Before the correction
After the correction
With this fix, there are now 8 errors remaining.
Next, the problem('NSFontAttributeName' has been renamed to 'NSAttributedString.Key.font'  and  'NSForegroundColorAttributeName' has been renamed to 'NSAttributedString.Key.foregroundColor' and  'NSParagraphStyleAttributeName' has been renamed to 'NSAttributedString.Key.paragraphStyle')  that occurred in  TransportViewController.swift was dealt with by pushing down the displayed Fix button in order from the bottom.

【Before the correction
After the correction
With this correction, there are now two errors left.
Next, the Argument of '#selector' refers to the instance method 'updateTracking' that is not exposed to Objective-C and 'RunLoopMode' has been renamed to 'RunLoop.Mode' in BlackmagicSlider.swift, which I'll fix with the Fix button...
【Before the correction
After the correction
As mentioned above, the first error can be corrected by auto-correction with Fix button, but after executing Auto Fix 3 times, the Fix button is lost in the above state and auto-correction is not possible, but because RunLoop.Mode is dubbed by the way I see it...

【Before the correction
RunLoop.main.add(self.m_timer, forMode:RunLoop.Mode.RunLoop.Mode.common)
After the correction by Manual correction
RunLoop.main.add(self.m_timer, forMode: RunLoop.Mode.common)

Fix the above and all the errors will be gone!

 Chapter 2: Confirmation of operation on MacOS as a Mac app
After removing all the errors, the following window will open on your Mac when you run Run and the app will start.
Make sure that Bluetooth is turned on on the Mac side. And when I turn on Pocket Cinema Camera near your Mac, it detects the camera as shown below.
Click on the camera displayed above and select it, then press the [Connect] button at the bottom, and the following window will appear.

In this case, the following window should appear when you push the menu button of Pocket Cinema Camera and open the setting screen of Bluetooth in the setup screen.(However, if you have previously paired a Mac with a camera, you may not see this screen)
Please input the number displayed on the camera side into the following window displayed on the Mac side and perform the pairing.
When pairing is successful, "Connected" will be displayed on the camera side, and the application on the Mac side will automatically transition to the following screen.

In this state, the application is connected to the camera side, so you can remotely control each parameter change and start recording with the black circle button below.

No comments:

Post a Comment