반응형
import AVFoundation
import VideoCore
class RTMPSender {
private let session = AVCaptureSession()
private let videoDataOutput = AVCaptureVideoDataOutput()
private var videoPreviewLayer: AVCaptureVideoPreviewLayer!
private var rtmpSession: VCSimpleSession!
func startSending(to outputURL: String, on view: UIView, completion: @escaping (Result<Void, Error>) -> Void) {
do {
let device = AVCaptureDevice.default(for: .video)
let input = try AVCaptureDeviceInput(device: device!)
session.addInput(input)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
videoPreviewLayer.frame = view.layer.bounds
videoPreviewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(videoPreviewLayer)
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]
videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "video_queue"))
session.addOutput(videoDataOutput)
rtmpSession = try VCSimpleSession(videoSize: CGSize(width: 720, height: 1280), frameRate: 30, bitrate: 1000000, useInterfaceOrientation: false)
rtmpSession.startRtmpSession(outputURL)
rtmpSession.attachVideo(AVCaptureDevice.default(for: .video))
rtmpSession.attachAudio(AVCaptureDevice.default(for: .audio))
session.startRunning()
} catch {
completion(.failure(TransmissionError.sessionInitializationFailed))
}
}
}
extension RTMPSender: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
rtmpSession.processVideoSampleBuffer(pixelBuffer)
}
}
enum TransmissionError: Error {
case sessionInitializationFailed
}
반응형
'Spring Boot' 카테고리의 다른 글
[flask] 멀티채팅룸생성, 채팅창 상세화면 구현. flask chat serveer (0) | 2023.02.08 |
---|---|
springboot로 소켓통신 멀티룸 채팅 소스 (0) | 2023.02.08 |
[spring boot] PDF 출력 - iText7_02(TEXT) (0) | 2023.02.08 |
[JAVA] POI 를 이용하지않고 pdfbox를 이용한 pdf 출력[초간단] (0) | 2023.02.07 |
[Nginx] nginx에서 rtmp를 트랜스코딩 하는 방법 480p, 720p, 1080p (0) | 2023.02.07 |