feat: add query to push and play

This commit is contained in:
langhuihui
2023-06-04 22:46:36 +08:00
parent 5d3f632c15
commit 038e46e4b7
6 changed files with 28 additions and 7 deletions

View File

@@ -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

View File

@@ -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信息的规范

View File

@@ -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

View File

@@ -12,7 +12,7 @@
<video id="video" width="640" height="480" autoplay muted>
</video>
<!-- <button id="sw" onclick="action()" type="button" style="width:100px;height:30px;display: block;">unpublish</button> -->
<pre>
<pre>
<code id="remoteSdp">
</code>
@@ -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',

View File

@@ -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 {

View File

@@ -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',