kingslay / KSPlayer

A video player for iOS、macOS、tvOS、visionOS , based on AVPlayer and FFmpeg, support the horizontal, vertical screen. support adjust volume, brightness and seek by slide, support subtitles.
GNU General Public License v3.0
869 stars 184 forks source link

How can I write a UDP connection in Swiftui #733

Closed uguraltinsoy closed 4 months ago

uguraltinsoy commented 4 months ago

Since I don't know how to use swiftui, I am writing in this way, but I couldn't run it, can I watch UDP live broadcast and how should I use swiftui?

import SwiftUI
import KSPlayer

struct ContentView: View {
    let playerCoordinator = KSVideoPlayer.Coordinator()
    let options: KSOptions = {
        var option = KSOptions()

        return option
    }()

    var body: some View {
        VStack {            
            KSVideoPlayer(coordinator: playerCoordinator, url: URL(string: "udp://@127.0.0.1:6000")!, options: options)
                .frame(width: 300, height: 200)            
        }
    }
}
kingslay commented 4 months ago

你可以用我的swiftUI的demo试下。输入这个udp的url。看下行不行。

uguraltinsoy commented 4 months ago

The code I broadcast and the code I test the broadcast are as follows, but the application stays like this

Brodcast WebCam :

ffmpeg -f avfoundation -video_size 1280x720 -framerate 30 -i "0" -pixel_format uyvy422 -c:v libx264 -preset medium -tune zerolatency -f mpegts "udp://127.0.0.1:6000?pkt_size=1316"

Test Live Video

ffplay -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 1 -strict experimental -framedrop -f mpegts -vf setpts=0 udp://127.0.0.1:6000
import SwiftUI
import KSPlayer

struct ContentView: View {
    // udp://@127.0.0.1:6000

    let options: KSOptions = {
        var option = KSOptions()
        option.isAutoPlay = true
        option.cache = false
        option.probesize = 32
        return option
    }()

    var body: some View {
        VStack {
            KSVideoPlayerView(url: URL(string: "udp://127.0.0.1:6000")!, options: options)
        }
    }
}
Ekran Resmi 2024-02-15 13 31 33
kingslay commented 4 months ago

我的demo里面的Player可以播放你的udp吗?

uguraltinsoy commented 4 months ago

I'm not that good at Swift, your demo project seemed very confused to me, I tried to do as much as I could understand. I don't know how I can test with your demo project

kingslay commented 4 months ago

你直接运行我的demo。然后在app的界面上会有一个地方可以输入url。你把你的url给输入进去就可以进行测试了。

uguraltinsoy commented 4 months ago

The application in TestFlight is connecting, there is a lot of delay but it works

kingslay commented 4 months ago

如果我的demo是可以的,那就是你写的代码,有些地方的配置有问题了。你可以先自己排查下,如果还是找不到的原因的话,那我可以提供需要收费的技术支持服务。详细价格可以参考这个项目的Sponsor tiers。

uguraltinsoy commented 4 months ago

Paid support because I can't use the library you wrote? Could your library be giving an "unsupported URL" error?

warning KSPlayer: KSPlayerLayer.swift:423 finish(player:error:) | Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x600000cbe670 {Error Domain=NSOSStatusErrorDomain Code=-1002 "(null)"}}

Ekran Resmi 2024-02-15 14 51 38
uguraltinsoy commented 4 months ago

How can I contact you privately? I got a Monthly Sponsorship and I only have 1 problem

kingslay commented 4 months ago

Paid support because I can't use the library you wrote? Could your library be giving an "unsupported URL" error?

warning KSPlayer: KSPlayerLayer.swift:423 finish(player:error:) | Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x600000cbe670 {Error Domain=NSOSStatusErrorDomain Code=-1002 "(null)"}}

Ekran Resmi 2024-02-15 14 51 38

我看了你的日志。你需要加一行代码 KSOptions.secondPlayerType = KSMEPlayer.self

uguraltinsoy commented 4 months ago

Yes it worked thanks. I solved the latency problem in FFPLay in this way, do you have any suggestions for this? there is a lot of latency now. I added similar ones to Options but it was not enough

let options: KSOptions = {
        var option = KSOptions()
        option.isAutoPlay = true
        option.cache = false
        option.probesize = 32
        return option
    }()
ffplay -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 1 -strict experimental -framedrop -f mpegts -vf setpts=0 udp://127.0.0.1:6000

OR

mpv udp://127.0.0.1:6000 --no-cache --untimed --no-demuxer-thread --vd-lavc-threads=1
kingslay commented 4 months ago
option.codecLowDelay  = true
options.probesize = 32
options.maxAnalyzeDuration = 1
uguraltinsoy commented 4 months ago

Is it normal that there is so much difference? Isn't this much delay too much when running on localhost? FFPlay delay 10ms KSPlayer 17.75s

private let options: KSOptions = {
        var options = KSOptions()
        options.isAutoPlay = true
        options.codecLowDelay  = true
        options.probesize = 32
        options.maxAnalyzeDuration = 1
        return options
    }()
Ekran Resmi 2024-02-15 16 16 21 Ekran Resmi 2024-02-15 16 15 17
kingslay commented 4 months ago

在加一下 options.formatContextOptions["fflags"] = ["nobuffer"]

uguraltinsoy commented 4 months ago

Do you have any other ideas? 10 seconds doesn't seem normal in general.

Ekran Resmi 2024-02-15 17 21 22
kingslay commented 4 months ago
            options.preferredForwardBufferDuration = 0.02
option.codecLowDelay  = true
options.probesize = 32
options.maxAnalyzeDuration = 1
options.formatContextOptions["fflags"] = ["nobuffer"]
uguraltinsoy commented 4 months ago

Unfortunately it doesn't work, there is still a serious delay like 10s.

kingslay commented 4 months ago

你试下不要用模拟器,直接用mac或是真实的iPhone运行下。

uguraltinsoy commented 4 months ago

from phone hotspot to mac via wifi from real device to mac dropped to 3-4s. is it possible to reduce it even more?

uguraltinsoy commented 4 months ago

I solved the smoothness problem by downgrading the ffmpeg stream packet, but this error still occurs

ffmpeg -f avfoundation -video_size 1280x720 -framerate 30 -i "2" -pixel_format uyvy422 -vf hflip -c:v libx264 -preset ultrafast -tune zerolatency -vf "hflip,crop=in_w/1. 33333:in_h, scale=960:720" -f mpegts "udp://192.168.0.1:6000?pkt_size=32"

I get this error and the stream freezes, do you have any advice for this? "error KSPlayer: MEPlayerItem.swift:131 MEPlayerItem | hardware accelerator failed to decode picture"

Ekran Resmi 2024-02-16 23 54 30
kingslay commented 4 months ago

这个报错是说不能硬件解码,所以用的是软件解码。一般是流的编码格式有问题才会这样