VideoDecoder icon indicating copy to clipboard operation
VideoDecoder copied to clipboard

Help for my use

Open uguraltinsoy opened this issue 1 year ago • 1 comments

I'm broadcasting udp with FFMPeg and I'm trying to connect to it and show the video stream on the phone, I'm doing it as follows, but it didn't work, can someone with knowledge help me with my code? I can read UDP data but I can't process it, I can't convert it to video, do I need to use it with AVPlayer?

ffmpeg -f avfoundation -video_size 1280x720 -framerate 30 -i "0" -pixel_format uyvy422 -vf hflip -c:v libx264 -preset medium -tune zerolatency -f mpegts "udp://127.0.0.1:6000?pkt_size=1316"
import Foundation
import CoreMedia
import VideoDecoder
import SwiftUI
import Network
import Combine
import CocoaAsyncSocket

class UDPManager: NSObject, ObservableObject, GCDAsyncUdpSocketDelegate, VideoDecoderDelegate {
    private let host: String
    private let port: UInt16
    private var socket: GCDAsyncUdpSocket?
    var decoder:H264Decoder?
    
    init(host: String, port: UInt16) {
        self.host = host
        self.port = port
    }
    
    func connectUDP() {
        decoder = H264Decoder(delegate: self)
        
        do {
            socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: .global())
            try socket?.bind(toPort: port)
            try socket?.enableBroadcast(true)
            try socket?.enableReusePort(true)
            try socket?.beginReceiving()
            
        } catch {
            print("UDP soketi oluşturma hatası: \(error)")
        }
    }
    
    func closeUDP() {
        socket?.close()
    }
    
    func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
        print("UDP Bağlandı.")
    }
    
    func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
        print("UDP soketi bağlantı hatası: \(error?.localizedDescription ?? "Bilinmeyen hata")")
    }
    
    func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
        if !data.isEmpty {
            DispatchQueue.main.async {
                self.decoder?.decodeOnePacket(VideoPacket(data, fps: 30, isIFrame: true, type: .h264, videoSize: CGSize(width: 1920, height: 1080)))
            }
        }
    }
    
    func decodeOutput(video: CMSampleBuffer) {
        print("Work")
    }
    
    func decodeOutput(error: DecodeError) {
        print("\(error)")
    }
}

uguraltinsoy avatar Feb 13 '24 09:02 uguraltinsoy

You need to confirm whether the data transmitted by Socket is a complete video frame

songbihai avatar Feb 21 '24 06:02 songbihai