From 038e46e4b7f869c0a65eb23f67f8e682481dec2b Mon Sep 17 00:00:00 2001 From: langhuihui <178529795@qq.com> Date: Sun, 4 Jun 2023 22:46:36 +0800 Subject: [PATCH] feat: add query to push and play --- README.en.md | 6 ++++++ README.md | 6 ++++++ main.go | 3 +++ publish.html | 11 +++++++---- publisher.go | 4 ++-- subscribe.html | 5 ++++- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.en.md b/README.en.md index ef5a87e..6f85e67 100644 --- a/README.en.md +++ b/README.en.md @@ -67,7 +67,13 @@ Response Body: `SDP` ### Push Test Page `/webrtc/test/publish` +- `?streamPath=xxx` The streamPath to publish, default is `live/webrtc` +- you can add other query parameters to the URL +### Play Test Page +`/webrtc/test/subscribe` +- `?streamPath=xxx` The streamPath to play, default is `live/webrtc` +- you can add other query parameters to the URL ## WHIP WebRTC-HTTP ingestion protocol diff --git a/README.md b/README.md index cf431c2..85e6722 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,13 @@ Response Body: `SDP` ### 推流测试页面 `/webrtc/test/publish` +- 可增加参数`?streamPath=xxx`指定推流地址,默认为`live/webrtc` +- 可以增加其他推流参数 +### 播放测试页面 +`/webrtc/test/subscribe` +- 可增加参数`?streamPath=xxx`指定播放地址,默认为`live/webrtc` +- 可以增加其他播放参数 ## WHIP WebRTC-HTTP ingestion protocol 用于WebRTC交换SDP信息的规范 diff --git a/main.go b/main.go index cecb854..73fdea8 100644 --- a/main.go +++ b/main.go @@ -155,6 +155,9 @@ func (conf *WebRTCConfig) Play_(w http.ResponseWriter, r *http.Request) { func (conf *WebRTCConfig) Push_(w http.ResponseWriter, r *http.Request) { streamPath := r.URL.Path[len("/push/"):] + if r.URL.RawQuery != "" { + streamPath += "?" + r.URL.RawQuery + } w.Header().Set("Content-Type", "application/sdp") bytes, err := ioutil.ReadAll(r.Body) var puber WebRTCPublisher diff --git a/publish.html b/publish.html index 2b72e8c..143fee6 100644 --- a/publish.html +++ b/publish.html @@ -12,7 +12,7 @@ -
+@@ -39,9 +39,12 @@ pc.onicecandidate = (e) => { console.log('onicecandidate', e.candidate); }; - mediaStream.id = 'live/webrtc'; + const searchParams = new URLSearchParams(location.search); + const streamPath = searchParams.get('streamPath') || 'live/webrtc'; + searchParams.delete('streamPath') + mediaStream.id = streamPath; mediaStream.getTracks().forEach((t) => { - pc.addTrack(t, mediaStream); + pc.addTrack(t, mediaStream); }); // const videoTransceiver = pc.addTransceiver(mediaStream.getVideoTracks()[0], { direction: 'sendonly' }); // const audioTransceiver = pc.addTransceiver(mediaStream.getAudioTracks()[0], { direction: 'sendonly' }); @@ -49,7 +52,7 @@ const offer = await pc.createOffer(); await pc.setLocalDescription(offer); const result = await fetch( - `/webrtc/push/live/webrtc`, + `/webrtc/push/${streamPath}${location.search?`?${searchParams.toString()}`:''}`, { method: 'POST', mode: 'cors', diff --git a/publisher.go b/publisher.go index d755a42..d6b8483 100644 --- a/publisher.go +++ b/publisher.go @@ -46,7 +46,7 @@ func (puber *WebRTCPublisher) onTrack(track *TrackRemote, receiver *RTPReceiver) return } rtpItem := puber.AudioTrack.GetRTPFromPool() - if i, _, err := track.Read(rtpItem.Value.Raw[:1460]); err == nil { + if i, _, err := track.Read(rtpItem.Value.Raw); err == nil { rtpItem.Value.Unmarshal(rtpItem.Value.Raw[:i]) puber.AudioTrack.WriteRTP(rtpItem) } else { @@ -66,7 +66,7 @@ func (puber *WebRTCPublisher) onTrack(track *TrackRemote, receiver *RTPReceiver) return } rtpItem := puber.VideoTrack.GetRTPFromPool() - if i, _, err := track.Read(rtpItem.Value.Raw[:1460]); err == nil { + if i, _, err := track.Read(rtpItem.Value.Raw); err == nil { rtpItem.Value.Unmarshal(rtpItem.Value.Raw[:i]) puber.VideoTrack.WriteRTP(rtpItem) } else { diff --git a/subscribe.html b/subscribe.html index 9127a78..c7fa742 100644 --- a/subscribe.html +++ b/subscribe.html @@ -40,8 +40,11 @@ // const dc = pc.createDataChannel('sdp'); const offer = await pc.createOffer(); await pc.setLocalDescription(offer); + const searchParams = new URLSearchParams(location.search); + const streamPath = searchParams.get('streamPath') || 'live/webrtc'; + searchParams.delete('streamPath') const result = await fetch( - `/webrtc/play/live/webrtc`, + `/webrtc/play/${streamPath}${location.search?`?${searchParams.toString()}`:''}`, { method: 'POST', mode: 'cors',