Maison  >  Article  >  développement back-end  >  Pourquoi pion/webrtc génère-t-il une liste de candidats sans adresse TCP ?

Pourquoi pion/webrtc génère-t-il une liste de candidats sans adresse TCP ?

王林
王林avant
2024-02-08 20:48:13530parcourir

Pourquoi pion/webrtc génère-t-il une liste de candidats sans adresse TCP ?

Contenu de la question

Je souhaite utiliser golang pion/webrtc pour publier des fichiers vidéo locaux sur un serveur vidéo. Ce serveur vidéo ne prend en charge que les connexions TCP. Lorsque je génère une offre sdp via le code pion/webrtc, la liste de candidats le fait. Je n'ai pas de candidat TCP, donc la connexion Ice a échoué. Lorsque j'utilise Chrome comme client Webrtc pour publier une vidéo de caméra, cela fonctionne, je ne sais pas comment faire en sorte que Chrome désactive les candidats TCP, j'ai vérifié les candidats pour Chrome et Pion/Webrtc et j'ai trouvé que la différence réside dans l'adresse TCP du candidat, je J'ai essayé de penser que j'avais fait de mon mieux pour laisser pion/webrtc générer des adresses de candidats TCP, mais elles ont toutes échoué. S'il vous plaît, aidez-moi.

Voici mon code :

package main

import (
    "fmt"
    "io"
    "os"
    "path/filepath"
    "sync"
    "time"

    "github.com/google/uuid"
    "github.com/pion/webrtc/v3"
    "github.com/pion/webrtc/v3/pkg/media"
    "github.com/pion/webrtc/v3/pkg/media/h264reader"
)

func main() {

    // 创建peerconnection配置
    config := webrtc.configuration{
        iceservers: []webrtc.iceserver{
            {
                urls: []string{"stun:stun.l.google.com:19302"},
            },
        },
        icetransportpolicy: webrtc.icetransportpolicyall,
    }

    // 创建peerconnection
    peerconnection, err := webrtc.newpeerconnection(config)
    if err != nil {
        panic(err)
    }

    videotrack, err := newh264localstaticsamplevideotrack("test.h264", 25)
    if err != nil {
        panic(err)
    }

    if rtpsender, err := peerconnection.addtrack(videotrack); err != nil {
        panic(err)
    } else {
        go func() {
            for {

                buf := make([]byte, 1024*1024)
                var rtcperr error
                if _, _, rtcperr = rtpsender.read(buf); rtcperr != nil {
                    fmt.printf("read error: %v", rtcperr.error())
                    return
                }
            }
        }()
    }

    offer, err := peerconnection.createoffer(nil)
    if err != nil {
        panic(err)
    }
    err = peerconnection.setlocaldescription(offer)
    if err != nil {
        panic(err)
    }

    wait := webrtc.gatheringcompletepromise(peerconnection)
    <-wait

    fmt.printf("local sdp: %v", peerconnection.localdescription().sdp)
}

func newh264localstaticsamplevideotrack(videofile string, fps int) (videotrack *webrtc.tracklocalstaticsample, err error) {
    if videofile == "" {
        err = fmt.errorf("videofile is empty")
        return
    }

    var videofileabs string
    videofileabs, err = filepath.abs(videofile)
    if err != nil {
        return
    }

    _, err = os.stat(videofileabs)
    if err != nil {
        return
    }

    videotrack, err = webrtc.newtracklocalstaticsample(webrtc.rtpcodeccapability{
        mimetype: webrtc.mimetypeh264,
    }, "video", fmt.sprintf("rtckit/%s", uuid.new().string()))
    if err != nil {
        return
    }

    go h264videofileconsumer(videotrack, videofileabs, time.duration(1000/fps)*time.millisecond)

    return
}

func h264videofileconsumer(videotrack *webrtc.tracklocalstaticsample, videofile string, videofps time.duration) {
    for {
        file, h264err := os.open(videofile)
        if h264err != nil {

            continue
        }

        h264, h264err := h264reader.newreader(file)
        if h264err != nil {

            continue
        }

        buf := make(chan []byte, 1024*1024) // 1mb?

        var wg sync.waitgroup

        wg.add(2)
        go func() {
            defer wg.done()
            for data := range buf {
                sample := media.sample{data: data, duration: videofps}

                if h264err1 := videotrack.writesample(sample); h264err1 != nil {

                    continue
                }
            }
        }()

        go func() {
            defer wg.done()
            ticker := time.newticker(videofps)
            for ; true; <-ticker.c {
                nal, h264err := h264.nextnal()
                if h264err == io.eof {

                    break
                }
                if h264err != nil {

                    break
                }

                buf <- nal.data
            }

            close(buf)
        }()

        wg.wait()
        _ = file.close()
    }
}

Voici la citation que j'ai obtenue :

offer2: v=0
o=- 3976125905671053001 1681720949 IN IP4 0.0.0.0
s=-
t=0 0
a=fingerprint:sha-256 31:A1:C7:4B:05:14:A3:7E:FE:CA:9C:4D:91:F0:29:6C:01:BF:20:67:DF:AA:EA:06:29:A4:DD:3F:F0:AA:5B:BD
a=extmap-allow-mixed
a=group:BUNDLE 0
m=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 100 101 102 121 127 120 125 107 108 109 123 118 116
c=IN IP4 0.0.0.0
a=setup:actpass
a=mid:0
a=ice-ufrag:ZjDGAGZtPHgWMfWa
a=ice-pwd:bJJRYUMfPcshoPCHTSfkyfrzwiJIiQkm
a=rtcp-mux
a=rtcp-rsize
a=rtpmap:96 VP8/90000
a=rtcp-fb:96 goog-remb 
a=rtcp-fb:96 ccm fir
a=rtcp-fb:96 nack 
a=rtcp-fb:96 nack pli
a=rtcp-fb:96 nack 
a=rtcp-fb:96 nack pli
a=rtcp-fb:96 transport-cc 
a=rtpmap:97 rtx/90000
a=fmtp:97 apt=96
a=rtcp-fb:97 nack 
a=rtcp-fb:97 nack pli
a=rtcp-fb:97 transport-cc 
a=rtpmap:98 VP9/90000
a=fmtp:98 profile-id=0
a=rtcp-fb:98 goog-remb 
a=rtcp-fb:98 ccm fir
a=rtcp-fb:98 nack 
a=rtcp-fb:98 nack pli
a=rtcp-fb:98 nack 
a=rtcp-fb:98 nack pli
a=rtcp-fb:98 transport-cc 
a=rtpmap:99 rtx/90000
a=fmtp:99 apt=98
a=rtcp-fb:99 nack 
a=rtcp-fb:99 nack pli
a=rtcp-fb:99 transport-cc 
a=rtpmap:100 VP9/90000
a=fmtp:100 profile-id=1
a=rtcp-fb:100 goog-remb 
a=rtcp-fb:100 ccm fir
a=rtcp-fb:100 nack 
a=rtcp-fb:100 nack pli
a=rtcp-fb:100 nack 
a=rtcp-fb:100 nack pli
a=rtcp-fb:100 transport-cc 
a=rtpmap:101 rtx/90000
a=fmtp:101 apt=100
a=rtcp-fb:101 nack 
a=rtcp-fb:101 nack pli
a=rtcp-fb:101 transport-cc 
a=rtpmap:102 H264/90000
a=fmtp:102 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f
a=rtcp-fb:102 goog-remb 
a=rtcp-fb:102 ccm fir
a=rtcp-fb:102 nack 
a=rtcp-fb:102 nack pli
a=rtcp-fb:102 nack 
a=rtcp-fb:102 nack pli
a=rtcp-fb:102 transport-cc 
a=rtpmap:121 rtx/90000
a=fmtp:121 apt=102
a=rtcp-fb:121 nack 
a=rtcp-fb:121 nack pli
a=rtcp-fb:121 transport-cc 
a=rtpmap:127 H264/90000
a=fmtp:127 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f
a=rtcp-fb:127 goog-remb 
a=rtcp-fb:127 ccm fir
a=rtcp-fb:127 nack 
a=rtcp-fb:127 nack pli
a=rtcp-fb:127 nack 
a=rtcp-fb:127 nack pli
a=rtcp-fb:127 transport-cc 
a=rtpmap:120 rtx/90000
a=fmtp:120 apt=127
a=rtcp-fb:120 nack 
a=rtcp-fb:120 nack pli
a=rtcp-fb:120 transport-cc 
a=rtpmap:125 H264/90000
a=fmtp:125 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f
a=rtcp-fb:125 goog-remb 
a=rtcp-fb:125 ccm fir
a=rtcp-fb:125 nack 
a=rtcp-fb:125 nack pli
a=rtcp-fb:125 nack 
a=rtcp-fb:125 nack pli
a=rtcp-fb:125 transport-cc 
a=rtpmap:107 rtx/90000
a=fmtp:107 apt=125
a=rtcp-fb:107 nack 
a=rtcp-fb:107 nack pli
a=rtcp-fb:107 transport-cc 
a=rtpmap:108 H264/90000
a=fmtp:108 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f
a=rtcp-fb:108 goog-remb 
a=rtcp-fb:108 ccm fir
a=rtcp-fb:108 nack 
a=rtcp-fb:108 nack pli
a=rtcp-fb:108 nack 
a=rtcp-fb:108 nack pli
a=rtcp-fb:108 transport-cc 
a=rtpmap:109 rtx/90000
a=fmtp:109 apt=108
a=rtcp-fb:109 nack 
a=rtcp-fb:109 nack pli
a=rtcp-fb:109 transport-cc 
a=rtpmap:123 H264/90000
a=fmtp:123 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640032
a=rtcp-fb:123 goog-remb 
a=rtcp-fb:123 ccm fir
a=rtcp-fb:123 nack 
a=rtcp-fb:123 nack pli
a=rtcp-fb:123 nack 
a=rtcp-fb:123 nack pli
a=rtcp-fb:123 transport-cc 
a=rtpmap:118 rtx/90000
a=fmtp:118 apt=123
a=rtcp-fb:118 nack 
a=rtcp-fb:118 nack pli
a=rtcp-fb:118 transport-cc 
a=rtpmap:116 ulpfec/90000
a=rtcp-fb:116 nack 
a=rtcp-fb:116 nack pli
a=rtcp-fb:116 transport-cc 
a=extmap:1 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
a=ssrc:554659860 cname:rtckit/53bd9691-40c4-4a58-9165-c7579c4b5031
a=ssrc:554659860 msid:rtckit/53bd9691-40c4-4a58-9165-c7579c4b5031 video
a=ssrc:554659860 mslabel:rtckit/53bd9691-40c4-4a58-9165-c7579c4b5031
a=ssrc:554659860 label:video
a=msid:rtckit/53bd9691-40c4-4a58-9165-c7579c4b5031 video
a=sendrecv
a=candidate:4113284106 1 udp 2130706431 101.4.122.16 41267 typ host
a=candidate:4113284106 2 udp 2130706431 101.4.122.16 41267 typ host
a=candidate:3528925834 1 udp 2130706431 172.18.0.1 33655 typ host
a=candidate:3528925834 2 udp 2130706431 172.18.0.1 33655 typ host
a=candidate:233762139 1 udp 2130706431 172.17.0.1 53508 typ host
a=candidate:233762139 2 udp 2130706431 172.17.0.1 53508 typ host
a=candidate:300762037 1 udp 1694498815 178.173.224.70 56107 typ srflx raddr 0.0.0.0 rport 56107
a=candidate:300762037 2 udp 1694498815 178.173.224.70 56107 typ srflx raddr 0.0.0.0 rport 56107
a=end-of-candidates

La meilleure combinaison~

J'ai essayé d'ajouter un serveur Ice dans localhost et d'utiliser stun:stun.l.google.com:19302 et de demander à chatgpt, rien de tout cela n'a fonctionné.


Bonne réponse


Par défaut, Pion n'active pas les candidats ICE-TCP.

Vous pouvez passer SettingEngine.SetICETCPMux一个>

Voir examples/ice-tcp pour des informations complètes sur des exemples sélectionnés sur la façon d'utiliser cette API.

Bonne chance et merci d'utiliser Pion !

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer