From f1cea12e926fe958daf9b6397f4bf281b7a742a4 Mon Sep 17 00:00:00 2001 From: langhuihui <178529795@qq.com> Date: Tue, 7 Jul 2020 21:28:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=89=E5=BE=85=E5=88=97=E8=A1=A8=E5=8A=A0?= =?UTF-8?q?=E9=94=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 9b7f2f5..8e03916 100644 --- a/main.go +++ b/main.go @@ -53,8 +53,27 @@ var api *API var SSRC uint32 var SSRCMap = make(map[string]uint32) var ssrcLock sync.Mutex -var playWaitList sync.Map +var playWaitList WaitList +type WaitList struct { + m map[string]*WebRTC + l sync.Mutex +} + +func (wl *WaitList) Set(k string, v *WebRTC) { + wl.l.Lock() + defer wl.l.Unlock() + if wl.m == nil { + wl.m = make(map[string]*WebRTC) + } + wl.m[k] = v +} +func (wl *WaitList) Get(k string) *WebRTC { + wl.l.Lock() + defer wl.l.Unlock() + defer delete(wl.m, k) + return wl.m[k] +} func init() { m.RegisterCodec(NewRTPCodec(RTPCodecTypeVideo, H264, @@ -210,6 +229,7 @@ func (rtc *WebRTC) GetAnswer(localSdp SessionDescription) ([]byte, error) { return bytes, nil } } + func run() { http.HandleFunc("/webrtc/play", func(w http.ResponseWriter, r *http.Request) { streamPath := r.URL.Query().Get("streamPath") @@ -220,8 +240,7 @@ func run() { Println(err) return } - if value, ok := playWaitList.Load(streamPath); ok { - rtc := value.(*WebRTC) + if rtc := playWaitList.Get(streamPath); rtc != nil { if err := rtc.SetRemoteDescription(offer); err != nil { Println(err) return @@ -273,7 +292,7 @@ func run() { return } rtc.videoTrack = videoTrack - playWaitList.Store(streamPath, rtc) + playWaitList.Set(streamPath, rtc) rtc.RemoteAddr = r.RemoteAddr offer, err := rtc.CreateOffer(nil) if err != nil {