didOutputSampleBuffer Delegate Never Gets Called (iOS / Swift 3) - ios

I am trying to keep track of the sample buffer rate of a video recording.
I have a view controller with AVCaptureFileOutputRecordingDelegate and AVCaptureVideoDataOutputSampleBufferDelegate and then setting the buffer output like so:
sessionQueue.async { [weak self] in
if let `self` = self {
let movieFileOutput = AVCaptureMovieFileOutput()
let bufferQueue = DispatchQueue(label: "bufferRate", qos: .userInteractive, attributes: .concurrent)
let theOutput = AVCaptureVideoDataOutput()
theOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value:kCVPixelFormatType_32BGRA)]
theOutput.alwaysDiscardsLateVideoFrames = true
theOutput.setSampleBufferDelegate(self, queue: bufferQueue)
if self.session.canAddOutput(theOutput) {
self.session.addOutput(theOutput)
print("ADDED BUFFER OUTPUT")
}
if self.session.canAddOutput(movieFileOutput) {
self.session.beginConfiguration()
self.session.addOutput(movieFileOutput)
self.session.sessionPreset = AVCaptureSessionPresetHigh
if let connection = movieFileOutput.connection(withMediaType: AVMediaTypeVideo) {
if connection.isVideoStabilizationSupported {
connection.preferredVideoStabilizationMode = .auto
}
}
self.session.commitConfiguration()
self.movieFileOutput = movieFileOutput
DispatchQueue.main.async { [weak self] in
if let `self` = self {
self.recordButton.isEnabled = true
}
}
}
}
}
Additionally, I have the function put into place to read the buffer:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
print("captured \(sampleBuffer)")
}
The problem is that when running the camera, it records correctly like it is supposed to (code not shown as it is working like normal) but the captureOutput sample buffer never gets called. What am I doing wrong? I assume it has to do with the way I am setting it up?

Make sure the delegate signature is correct with Swift 3 syntax.
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
I was using Swift 2.3 syntax and compiler did not warn me of the issue. try to type out the didOut sampleBuffer and check if XCode autocorrects the proper syntax for that delegate method.

Related

Swift - captureOutput is not being executed

I am currently trying to implement a camera live feed to my app. I've got it setup but somehow it's not working as expected. As far as I understand, captureOutput should be executed every time a frame is recognized and the print message should be output in console, but somehow it's not - the console won't show the print command.
Does anybody see any possible mistake inside of the code?
I don't know whether that's connected to my problem, but at start of the app the console shows the following:
[BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x106b24530] get output frames failed, state 8196
import UIKit
import AVKit
import Vision
class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let captureSession = AVCaptureSession()
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else{ return }
captureSession.addInput(input)
captureSession.startRunning()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)
// let request = VNCoreMLRequest
// VNImageRequestHandler(cgImage: <#T##CGImage#>, options: [:]).perform(request)
}
func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
print("Es hat funktioniert")
}
}
You need to implement captureOutput(_:didOutput:from:) not captureOutput(_:didDrop:from:)
func captureOutput(_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection) {
print("Es hat funktioniert")
}

Ios recording Live mic Data cant record and send?

import UIKit
import AVFoundation
class RecViewController: UIViewController,AVCaptureAudioDataOutputSampleBufferDelegate {
#IBOutlet weak var recBtn: UIButton!
let session = AVCaptureSession()
var result: NSData?
override func viewDidLoad() {
super.viewDidLoad()
self.recBtn.setTitle("RECORDING", for: .normal)
}
#IBAction func recBtnAction(_ sender: Any) {
session.sessionPreset = .medium
let mic = AVCaptureDevice.default(for: .audio)
var mic_input: AVCaptureDeviceInput!
let audio_output = AVCaptureAudioDataOutput()
audio_output.setSampleBufferDelegate(self, queue: DispatchQueue.main)
do {
mic_input = try AVCaptureDeviceInput(device: mic!)
} catch {
return
}
if session.inputs.isEmpty {
self.session.addInput(mic_input)
session.addOutput(audio_output)
}
session.startRunning()
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
let block = CMSampleBufferGetDataBuffer(sampleBuffer)
var length = 0
var data: UnsafeMutablePointer<Int8>? = nil
var status = CMBlockBufferGetDataPointer(block!, 0, nil, &length, &data) // TODO: check for errors
result = NSData(bytesNoCopy: data!, length: length, freeWhenDone: false)
}
#IBAction func playBtnAction(_ sender: Any) {
// Storing the ns data and trying to play with following code but it dosent play cant hear a thing
do {
let audioPlayer = try AVAudioPlayer(data: result as! Data )
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print("Error")
}
}
}
i am using this code to record live data from mic but when i press the rec Button nothing happens it neither records nor it goes to delegate and print so how to solve this ? (Solved)
I have updated the code and when i trying to play that ns data via av audio players it dosen't plays anything how to solve this?
It doesn't look like your audio_output is actually retained by anything outside the scope of the recBtnAction method. It's quite likely that your session is being discarded by the time you leave the method, therefore not capturing anything for you.
Edit
Having had a play with the sample code that you provided, it seems that your actual issue is that the delegate method you have used is of the wrong signature.
It should be:
func captureOutput(_ output: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
}

AVCaptureMetadataOutputObjectsDelegate method not called

I have a ViewController that continuously scans for QR codes and implements AVCaptureMetadataOutputObjectsDelegate to retrieve the metadata output.
class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession = AVCaptureSession()
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
override func viewDidLoad() {
super.viewDidLoad()
// Get the back-facing camera for capturing videos
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaTypeVideo, position: .back)
guard let captureDevice = deviceDiscoverySession?.devices.first else {
print("Failed to get the camera device")
return
}
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Set the input device on the capture session.
captureSession.addInput(input)
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession.addOutput(captureMetadataOutput)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
// Start video capture.
captureSession.startRunning()
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
print("in function of extension")
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects.count == 0 {
print("nada qr code")
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObjectTypeQRCode {
if metadataObj.stringValue != nil {
print(metadataObj.stringValue)
}
}
}
}
But for some reason the delegate callback doesn't get called. In the tutorial I'm following, it runs perfectly.
Hopefully someone can provide any words of assistance. Anything would be immensely helpful to get me on the right track. Thanks so much in advance.
The reason for not running is that metadataOutput(...) is technically a built-in function of the AV framework, but only for Swift 4. In Swift 3.0 and 3.2, the correct function is:
captureOutput(_ output: _AVCaptureOutput!, didOutputMetadataObjects: [Any]!, from connection: AVCaptureConnection!)
That should run!
The extension code you posted does not compile for me. I had to add public to the method declaration:
public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
I am using Swift 4.
Change the code as follows. It may work, at least it worked for me. I am using Swift 4.2
captureMetadataOutput.metadataObjectTypes = captureDeviceOutput.availableMetadataObjectTypes

Using AVCaptureVideoDataOutput and AVCaptureAudioDataOutput

I have an AVCaptureVideoDataOutput session running set up as below, which works great and records the buffer to a file.
I want to also record the audio but there doesn't seem to be any in the buffer even though I've added the microphone as an input to the captureSession.
I suspect I need to also use AVCaptureAudioDataOutput.
func setupCaptureSession()
{
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSessionPreset1280x720
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample
buffer delegate", attributes: []))
videoOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)]
videoOutput.alwaysDiscardsLateVideoFrames = true
captureSession.addOutput(videoOutput)
captureSession.addInput(deviceInputFromDevice(backCameraDevice))
captureSession.addInput(deviceInputFromDevice(micDevice))
captureSession.commitConfiguration()
captureSession.startRunning()
}
and then here's how I get the video buffer and send it off to be written to a file
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
{
writeVideoFromData(sampleBuffer) // a function that writes the buffer to disk
}
Check func captureOutput(AVCaptureOutput, didOutput: CMSampleBuffer, from: AVCaptureConnection)
of AVCaptureAudioDataOutputSampleBufferDelegate if you want grab sample data of audio from AVCaptureAudioDataOutput
https://developer.apple.com/documentation/avfoundation/avcaptureaudiodataoutputsamplebufferdelegate

Swift: captureOutput not being called

So I am working on my first Swift app which was going fine until I got stuck here. Please take a look at the code below.
//
// CameraFrames.swift
// Explore
//
// Created by Kushagra Agarwal on 13/08/15.
// Copyright © 2015 Kushagra Agarwal. All rights reserved.
//
import Foundation
import AVFoundation
protocol CameraFramesDelegate {
func processCameraFrames(sampleBuffer : CMSampleBufferRef)
}
class CameraFrames : NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
var previewLayer : AVCaptureVideoPreviewLayer?
var delegate : CameraFramesDelegate?
let captureSession = AVCaptureSession();
var captureDevice : AVCaptureDevice?
override init() {
super.init()
captureSession.beginConfiguration()
// Capture the session with High settings preset
captureSession.sessionPreset = AVCaptureSessionPresetHigh;
self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo);
// Because the old error handling method is deprecated
do {
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice));
} catch {
print("Error in getting input from camera");
}
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
let output = AVCaptureVideoDataOutput();
var outputQueue : dispatch_queue_t?
outputQueue = dispatch_queue_create("outputQueue", DISPATCH_QUEUE_SERIAL);
output.setSampleBufferDelegate(self, queue: outputQueue)
output.alwaysDiscardsLateVideoFrames = true;
output.videoSettings = nil;
captureSession.addOutput(output);
captureSession.commitConfiguration()
captureSession.startRunning();
}
func captureOutput(captureOutput: AVCaptureOutput!, didDropSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
print("frame dropped")
}
func captureOutput(captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBufferRef, fromConnection connection: AVCaptureConnection) {
print("frame received")
}
}
The preview shows up fine on the phone but the captureOutput is never called for some reason. I looked at some old SO threads, but none helped me resolve this issue. Any idea what can be the reason behind it?
Edit: I forgot to mention that I have a view controller in which I am previewing the AVCaptureVideoPreviewLayer layer, which works well. I have a feeling that the issue is somewhere in setting up the outputQueue but I can't figure out what.

Resources