This release adds microphone processing controls and local chat controls, expands platform packages, and synchronizes the website and guides. It also fixes native H.264 input/format handling, signaling recovery, fresh STUN discovery, and UDP ownership during overlapping connections. This is **v1.8.0**. Browser/Server v23, Native control v9 and capture v7 remain unchanged, with the existing 1440p ceiling. Additional resolutions, capability negotiation and Magicsock are separate follow-up work. ## Validation - Full local Web check: 79 files / 1,317 tests, TypeScript and production build. - Full Go checks, platform builds and Windows native capture regressions/probe. - Native H.264 Host lifecycle, Browser-to-App fanout and App-exit recovery, and Native H.264 through embedded SFU: all passed with owned process/port/profile cleanup. - Website build, documentation and repository hygiene passed. Existing UI acceptance covers narrow screens, both languages/themes, architecture downloads and all 23 film checks; final packaging and Go race checks run on the merged revision in CI. - Physical macOS/Linux capture, representative public networks and unmatched field reports retain their documented evidence limits. No claim that every blur/disconnection report is resolved. ## Release notes ### 中文 这是一次功能与兼容性更新:麦克风和聊天增加了实用的小选项,App 与 Server 支持更多设备,同时修复了原生分享和连接恢复中的若干问题。现有 1.x 协议保持不变。 #### 新增与改进 - 浏览器麦克风新增「降噪与回声消除」开关,默认开启回声消除、降噪和自动增益;分享音乐或使用虚拟音频输入时,可按需关闭。 - 可调整弹幕字号和透明度,并将当前保留的聊天记录导出为 TXT。当前会话最多保留 1,000 条文字消息,刷新页面后清空;需要保存时请先导出。 - 新增 Intel Mac、Linux ARM64 App,以及 Linux ARM64 Server 和容器镜像。官网下载区可直接选择对应架构,并显示最新版本和发布时间。 - 更新官网演示、README 截图和中英文教程,明确浏览器分享也可选择窗口或屏幕;开始分享前显示房间号占位,保持底栏控件位置稳定。 - 官网新增直达问题排查的入口,补充浏览器 WebRTC 限制、校园网和 Windows 显卡相关的排查步骤。 - App 模式选择页显示免费开源说明、作者与贡献者署名,以及官方网站和源码链接。 #### 修复 - 修正部分 H.264 原生分享的格式兼容性校验,以及 Windows 编码器输入长度和输出缓冲区对齐问题。 - 清理已失效的重连任务和 Native 接收信令,避免旧连接继续影响当前会话。 - 修复 App 重连时可能复用过期公网地址的问题;重新探测使用当前地址,并处理探测丢包和过期响应,保留其他正常连接。 - 修复 App 新旧媒体连接重叠时共用端口造成的冲突,避免准备或撤销新连接打断仍在使用的画面;关闭连接时及时取消未完成的端口映射请求。 - 无站点密码时,连接恢复不再误引导用户输入站点密码。 - 房间失效或会话终止时,开播前后的原因提示统一显示在电视状态图标中,避免额外提示条。 - 修正 Linux 原生采集的时间戳与关键帧时间基准。感谢 Pedro Antonio 在 [#449](https://github.com/TNTcraftHIM/Piik/pull/449) 的贡献。 #### 使用说明 下载对应平台的完整压缩包,退出旧 App 后解压使用。Windows 继续提供 x64;macOS 提供 Apple 芯片与 Intel 版本,原生采集需要 macOS 13 或更新版本。Linux 提供 x64 与 ARM64,原生采集还需安装[系统组件](https://github.com/TNTcraftHIM/Piik/blob/main/native/capture/linux/README.md)。macOS 和 Linux 的实机覆盖仍有限,欢迎反馈。 ### English This update adds useful microphone and chat controls, brings Piik to more devices, and fixes several native-sharing and connection-recovery issues. The existing 1.x protocols remain unchanged. #### New and improved - Browser microphones now have a “Noise & echo reduction” toggle. Echo cancellation, noise suppression and automatic gain control are on by default; turn them off when appropriate for music or virtual audio inputs. - Adjust chat-overlay text size and opacity, and export retained messages as TXT. The current session keeps up to 1,000 text messages and clears on page refresh; export first if you want to keep them. - Added Intel Mac and Linux ARM64 App packages, plus Linux ARM64 Server and container images. The website provides architecture selectors and shows the latest release and publication date. - Updated the website demonstration, README screenshots and bilingual guides, clarifying that browser capture can also select windows or screens. A room-code placeholder keeps the bottom bar stable before sharing starts. - Added direct website links to troubleshooting, with steps for browser WebRTC restrictions, campus networks and Windows graphics issues. - The App mode-selection page now shows its free and open-source status, author and contributor credit, and links to the official website and source. #### Fixes - Corrected H.264 format compatibility checks and Windows encoder input-length and output-buffer alignment handling. - Retired stale reconnect tasks and Native receive signals so they cannot continue affecting the current session. - Fixed App reconnection reusing an expired public address. Fresh discovery handles lost probes and stale responses while preserving other working connections. - Fixed UDP socket conflicts between overlapping App media connections so preparing or canceling a replacement preserves the active picture. Closing a connection now cancels unfinished gateway mapping requests. - Connection recovery on passwordless sites no longer incorrectly asks for a site passphrase. - Expired-room and session-termination reasons now use the same TV status indicator before and during sharing, without an extra notice strip. - Corrected Linux native capture timestamps and keyframe time bases. Thanks to Pedro Antonio for [#449](https://github.com/TNTcraftHIM/Piik/pull/449). #### Updating Download the complete archive for your platform, quit the old App, and extract the new package. Windows remains x64-only. macOS packages support Apple silicon and Intel; native capture requires macOS 13 or newer. Linux supports x64 and ARM64 and requires additional [system components](https://github.com/TNTcraftHIM/Piik/blob/main/native/capture/linux/README.md) for native capture. Physical-device coverage on macOS and Linux remains limited; feedback is welcome. ## Attribution Co-authored-by: Pedro Antonio <wrenchwastaken@gmail.com>
623 lines
20 KiB
Go
623 lines
20 KiB
Go
package mediaedge
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/livekit/livekit-server/pkg/sfu"
|
|
"github.com/pion/rtp"
|
|
"github.com/pion/sdp/v3"
|
|
"github.com/pion/stun/v3"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
func TestOfferSendsCodecDistinguishesBundledAndRejectedMedia(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
port int
|
|
attributes string
|
|
want bool
|
|
}{
|
|
{"ordinary", 9, "a=sendonly\r\n", true},
|
|
{"bundled", 0, "a=bundle-only\r\na=sendonly\r\n", true},
|
|
{"rejected", 0, "a=sendonly\r\n", false},
|
|
{"bundled-inactive", 0, "a=bundle-only\r\na=inactive\r\n", false},
|
|
{"bundled-receive-only", 0, "a=bundle-only\r\na=recvonly\r\n", false},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
offer := "v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=group:BUNDLE 0 1\r\n" +
|
|
"m=video 9 UDP/TLS/RTP/SAVPF 96\r\na=mid:0\r\na=rtpmap:96 VP8/90000\r\n" +
|
|
fmt.Sprintf("m=audio %d UDP/TLS/RTP/SAVPF 111\r\na=mid:1\r\na=rtpmap:111 opus/48000/2\r\n%s", test.port, test.attributes)
|
|
got, err := offerSendsCodec(offer, "audio", "opus")
|
|
if err != nil || got != test.want {
|
|
t.Fatalf("audio=%v, wanted %v: %v", got, test.want, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReceiverAdvertisesLocalStereoPreference(t *testing.T) {
|
|
for _, remotePreference := range []string{"", ";stereo=0", ";stereo=1"} {
|
|
t.Run(remotePreference, func(t *testing.T) {
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = engine.Close() })
|
|
// Independent upstream capabilities: local registration must not make
|
|
// the fixture advertise the receive preference we are checking.
|
|
upstream, err := webrtc.NewPeerConnection(webrtc.Configuration{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = upstream.Close() })
|
|
for _, kind := range []webrtc.RTPCodecType{webrtc.RTPCodecTypeVideo, webrtc.RTPCodecTypeAudio} {
|
|
transceiver, addErr := upstream.AddTransceiverFromKind(kind, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
|
if addErr != nil {
|
|
t.Fatal(addErr)
|
|
}
|
|
if kind == webrtc.RTPCodecTypeAudio {
|
|
for _, codec := range transceiver.Sender().GetParameters().Codecs {
|
|
if codec.MimeType != webrtc.MimeTypeOpus {
|
|
continue
|
|
}
|
|
codec.SDPFmtpLine = "minptime=10;useinbandfec=1" + remotePreference
|
|
err = transceiver.SetCodecPreferences([]webrtc.RTPCodecParameters{codec})
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var receiver *Receiver
|
|
for attempt := 0; attempt < 2; attempt++ {
|
|
offer, err := upstream.CreateOffer(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = upstream.SetLocalDescription(offer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var answer webrtc.SessionDescription
|
|
if receiver == nil {
|
|
receiver, answer, err = engine.NewReceiver(ReceiverOptions{Offer: offer, EdgeCapacity: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = receiver.Close() })
|
|
} else {
|
|
var reused bool
|
|
answer, reused, err = receiver.Renegotiate(offer, nil)
|
|
if err != nil || !reused {
|
|
t.Fatalf("renegotiate: reused=%v, error=%v", reused, err)
|
|
}
|
|
}
|
|
var parsed sdp.SessionDescription
|
|
if err = parsed.UnmarshalString(answer.SDP); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found := false
|
|
for _, media := range parsed.MediaDescriptions {
|
|
if media.MediaName.Media != "audio" {
|
|
continue
|
|
}
|
|
for _, attribute := range media.Attributes {
|
|
if attribute.Key == "fmtp" && strings.Contains(attribute.Value, "stereo=1") && strings.Contains(attribute.Value, "maxaveragebitrate=192000") {
|
|
found = true
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("answer %d lost local stereo preference", attempt)
|
|
}
|
|
if err = upstream.SetRemoteDescription(answer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReceiverCodecMatchesTheSingleNegotiatedAnswer(t *testing.T) {
|
|
for _, preferVP8 := range []bool{false, true} {
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = engine.Close() })
|
|
upstream, err := testICESocket(t, engine).newPeerConnection()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = upstream.Close() })
|
|
transceiver, err := upstream.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo,
|
|
webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
preferences := []webrtc.RTPCodecParameters{videoCodecs["h264"], videoCodecs["vp8"]}
|
|
want := "h264"
|
|
if preferVP8 {
|
|
preferences[0], preferences[1] = preferences[1], preferences[0]
|
|
want = "vp8"
|
|
}
|
|
if err = transceiver.SetCodecPreferences(preferences); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
offer, err := upstream.CreateOffer(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
receiver, answer, err := engine.NewReceiver(ReceiverOptions{Offer: offer, EdgeCapacity: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = receiver.Close() })
|
|
if receiver.Codec() != want || !strings.Contains(strings.ToLower(answer.SDP), want+"/90000") ||
|
|
(strings.Contains(strings.ToLower(answer.SDP), "vp8/90000") && strings.Contains(strings.ToLower(answer.SDP), "h264/90000")) {
|
|
t.Fatalf("receiver codec %q does not match its single-codec answer", receiver.Codec())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReceiverNegotiatesForwardableH264(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
formats []string
|
|
want string
|
|
}{
|
|
{"browser-auto", []string{"42e01f", "vp8"}, "42e01f"},
|
|
{"safari-order", []string{"640c1f", "42e01f"}, "42e01f"},
|
|
{"main", []string{"4d001f"}, ""},
|
|
{"high", []string{"64001f"}, ""},
|
|
{"baseline", []string{"42001f"}, ""},
|
|
{"missing-profile", []string{""}, ""},
|
|
{"equivalent-baseline", []string{"42c01f"}, "42c01f"},
|
|
{"small-browser-output", []string{"42e00b"}, "42e00b"},
|
|
{"level-ceiling", []string{"42e033"}, "42e033"},
|
|
{"over-ceiling", []string{"42e034"}, ""},
|
|
{"invalid-level", []string{"42e035"}, ""},
|
|
{"invalid-constraints", []string{"42e11f"}, ""},
|
|
{"mode-zero", []string{"42e01f;packetization-mode=0"}, ""},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = engine.Close() })
|
|
upstream, err := webrtc.NewPeerConnection(webrtc.Configuration{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = upstream.Close() })
|
|
transceiver, err := upstream.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo,
|
|
webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var preferences []webrtc.RTPCodecParameters
|
|
for index, format := range test.formats {
|
|
codec := videoCodecs["h264"]
|
|
codec.SDPFmtpLine = "level-asymmetry-allowed=1;packetization-mode=1"
|
|
if format != "" {
|
|
codec.SDPFmtpLine += ";profile-level-id=" + format
|
|
}
|
|
if format == "vp8" {
|
|
codec = videoCodecs["vp8"]
|
|
}
|
|
codec.PayloadType = webrtc.PayloadType(96 + index)
|
|
preferences = append(preferences, codec)
|
|
}
|
|
if err = transceiver.SetCodecPreferences(preferences); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
offer, err := upstream.CreateOffer(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
receiver, answer, err := engine.NewReceiver(ReceiverOptions{Offer: offer, EdgeCapacity: 1})
|
|
if receiver != nil {
|
|
t.Cleanup(func() { _ = receiver.Close() })
|
|
}
|
|
if test.want == "" {
|
|
if err == nil {
|
|
t.Fatalf("unsupported format was admitted: %s", answer.SDP)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if receiver.Codec() != "h264" || !strings.Contains(answer.SDP, "profile-level-id="+test.want) {
|
|
t.Fatalf("wanted %s, got %s: %s", test.want, receiver.Codec(), answer.SDP)
|
|
}
|
|
// A profile-only change is still a changed media shape. Reject it before
|
|
// mutating the current peer/source, so the owner can prepare a replacement.
|
|
previous := receiver.connection.RemoteDescription().SDP
|
|
changed := offer
|
|
changed.SDP = strings.ReplaceAll(changed.SDP, "profile-level-id="+test.want, "profile-level-id=4d001f")
|
|
_, reused, renegotiateErr := receiver.Renegotiate(changed, nil)
|
|
if renegotiateErr != nil || reused || receiver.connection.RemoteDescription().SDP != previous {
|
|
t.Fatalf("unsupported profile altered the current source: reused=%v, error=%v", reused, renegotiateErr)
|
|
}
|
|
source := receiver.Source()
|
|
_, reused, renegotiateErr = receiver.Renegotiate(offer, nil)
|
|
if renegotiateErr != nil || !reused || receiver.Source() != source {
|
|
t.Fatalf("compatible renegotiation replaced the source: reused=%v, error=%v", reused, renegotiateErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReceiverForwardsEncodedVideoToANativeEdge(t *testing.T) {
|
|
for _, codec := range []string{"h264", "vp8"} {
|
|
t.Run(codec, func(t *testing.T) { testReceiverForwarding(t, codec) })
|
|
}
|
|
}
|
|
|
|
func testReceiverForwarding(t *testing.T, codec string) {
|
|
engine, err := NewEngine(EngineOptions{
|
|
BindAddress: "127.0.0.1:0",
|
|
IncludeLoopback: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = engine.Close() })
|
|
|
|
upstreamEngine, err := NewEngine(EngineOptions{
|
|
BindAddress: "127.0.0.1:0",
|
|
IncludeLoopback: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = upstreamEngine.Close() })
|
|
upstream, err := testICESocket(t, upstreamEngine).newPeerConnection()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = upstream.Close() })
|
|
track, err := webrtc.NewTrackLocalStaticRTP(
|
|
videoCodecs[codec].RTPCodecCapability, "screen", "upstream",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = upstream.AddTrack(track); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, transceiver := range upstream.GetTransceivers() {
|
|
if transceiver.Kind() == webrtc.RTPCodecTypeVideo {
|
|
if err = transceiver.SetCodecPreferences([]webrtc.RTPCodecParameters{videoCodecs[codec]}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
audioTrack, err := webrtc.NewTrackLocalStaticRTP(
|
|
opusCapability, "audio", "upstream",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = upstream.AddTrack(audioTrack); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var nativeReceiver *Receiver
|
|
var upstreamCandidates []*webrtc.ICECandidateInit
|
|
var receiverCandidates []*webrtc.ICECandidateInit
|
|
var candidateMu sync.Mutex
|
|
remoteReady := false
|
|
completed := make(chan struct{}, 4)
|
|
gatheringCurrent := make(chan func() bool, 4)
|
|
surveyCandidates := 0
|
|
var bindingRequests atomic.Int32
|
|
server := bindingServer(t, func(request *stun.Message, sender *net.UDPAddr, listener *net.UDPConn) {
|
|
bindingRequests.Add(1)
|
|
answerBinding(request, sender, listener, sender.Port)
|
|
})
|
|
servers := []webrtc.ICEServer{{URLs: []string{"stun:" + server.String()}}}
|
|
upstream.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
|
if candidate == nil {
|
|
return
|
|
}
|
|
value := candidate.ToJSON()
|
|
candidateMu.Lock()
|
|
ready := remoteReady
|
|
if !ready {
|
|
upstreamCandidates = append(upstreamCandidates, &value)
|
|
}
|
|
candidateMu.Unlock()
|
|
if ready {
|
|
_ = nativeReceiver.AddRemoteCandidate(&value)
|
|
}
|
|
})
|
|
upstreamGathered := webrtc.GatheringCompletePromise(upstream)
|
|
offer, err := upstream.CreateOffer(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = upstream.SetLocalDescription(offer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitSignal(t, upstreamGathered, "upstream ICE gathering")
|
|
|
|
nativeReceiver, answer, err := engine.NewReceiver(ReceiverOptions{
|
|
Offer: *upstream.LocalDescription(),
|
|
ICEServers: servers,
|
|
EdgeCapacity: 1,
|
|
Events: ReceiverEvents{
|
|
LocalCandidate: func(candidate *webrtc.ICECandidateInit, current func() bool) {
|
|
if candidate == nil {
|
|
gatheringCurrent <- current
|
|
completed <- struct{}{}
|
|
return
|
|
}
|
|
candidateMu.Lock()
|
|
if strings.Contains(candidate.Candidate, "candidate:ns") {
|
|
surveyCandidates++
|
|
}
|
|
ready := remoteReady
|
|
if !ready {
|
|
receiverCandidates = append(receiverCandidates, candidate)
|
|
}
|
|
candidateMu.Unlock()
|
|
if ready {
|
|
_ = upstream.AddICECandidate(*candidate)
|
|
}
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = nativeReceiver.Close() })
|
|
if nativeReceiver.Codec() != codec || nativeReceiver.Source().Codec() != codec {
|
|
t.Fatalf("native receiver/source selected %q instead of %q", nativeReceiver.Codec(), codec)
|
|
}
|
|
if err = upstream.SetRemoteDescription(answer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
candidateMu.Lock()
|
|
remoteReady = true
|
|
pendingUpstream, pendingReceiver := upstreamCandidates, receiverCandidates
|
|
upstreamCandidates, receiverCandidates = nil, nil
|
|
candidateMu.Unlock()
|
|
for _, candidate := range pendingUpstream {
|
|
_ = nativeReceiver.AddRemoteCandidate(candidate)
|
|
}
|
|
for _, candidate := range pendingReceiver {
|
|
_ = upstream.AddICECandidate(*candidate)
|
|
}
|
|
waitConnected(t, upstream, "native receiver")
|
|
|
|
downstream, packets, audioPackets, err := connectedReceiverForSources(
|
|
t, engine, nativeReceiver.Source(), nativeReceiver.AudioSource(), "native-relay-edge",
|
|
)
|
|
t.Cleanup(func() { _ = downstream.Close() })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := &rtp.Packet{
|
|
Header: rtp.Header{
|
|
Version: 2,
|
|
PayloadType: uint8(videoCodecs[codec].PayloadType),
|
|
SequenceNumber: 7,
|
|
Timestamp: 90_000,
|
|
SSRC: 42,
|
|
Marker: true,
|
|
},
|
|
Payload: []byte{0x65, 0x88, 0x84, 0x00},
|
|
}
|
|
if codec == "vp8" {
|
|
want.Payload = []byte{0x10, 0x10, 0, 0, 0x9d, 0x01, 0x2a, 0x80, 0x02, 0xe0, 0x01, 0}
|
|
} else {
|
|
want.Payload = []byte{0x78}
|
|
for _, nalu := range sfu.H264KeyFrame2x2 {
|
|
want.Payload = append(want.Payload, byte(len(nalu)>>8), byte(len(nalu)))
|
|
want.Payload = append(want.Payload, nalu...)
|
|
}
|
|
}
|
|
if err = want.SetExtension(9, []byte{0xde, 0xad}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = track.WriteRTP(want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := waitPacket(t, packets)
|
|
if string(got.Payload) != string(want.Payload) || !got.Marker ||
|
|
got.GetExtension(9) != nil {
|
|
t.Fatalf("forwarded packet = %#v", got)
|
|
}
|
|
wantAudio := &rtp.Packet{
|
|
Header: rtp.Header{
|
|
Version: 2, PayloadType: 111, SequenceNumber: 8,
|
|
Timestamp: 960, SSRC: 43, Marker: true,
|
|
},
|
|
Payload: []byte{0xf8, 0xff, 0xfe},
|
|
}
|
|
if err = wantAudio.SetExtension(9, []byte{0xbe, 0xef}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = audioTrack.WriteRTP(wantAudio); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gotAudio := waitPacket(t, audioPackets)
|
|
if string(gotAudio.Payload) != string(wantAudio.Payload) ||
|
|
gotAudio.GetExtension(9) != nil {
|
|
t.Fatalf("forwarded audio packet = %#v", gotAudio)
|
|
}
|
|
for index, input := range []struct {
|
|
track *webrtc.TrackLocalStaticRTP
|
|
media *rtp.Packet
|
|
packets <-chan *rtp.Packet
|
|
}{{track, want, packets}, {audioTrack, wantAudio, audioPackets}} {
|
|
padding := &rtp.Packet{Header: input.media.Header, PaddingSize: 4}
|
|
padding.Padding = true
|
|
padding.SequenceNumber++
|
|
padding.Marker = false
|
|
if err = input.track.WriteRTP(padding); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if index == 1 {
|
|
if packet := waitPacket(t, input.packets); !packet.Padding || packet.PaddingSize != padding.PaddingSize || len(packet.Payload) != 0 {
|
|
t.Fatal("audio lost RTP padding continuity")
|
|
}
|
|
}
|
|
next := input.media.Clone()
|
|
next.SequenceNumber += 2
|
|
next.Timestamp += 3_000
|
|
if err = input.track.WriteRTP(next); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
packet := waitPacket(t, input.packets)
|
|
if index == 0 && packet.SequenceNumber != got.SequenceNumber+1 {
|
|
t.Fatal("video projection did not close the filtered padding sequence gap")
|
|
}
|
|
if string(packet.Payload) != string(next.Payload) {
|
|
t.Fatalf("stream %d stopped delivering media after padding", index)
|
|
}
|
|
}
|
|
waitSignal(t, completed, "initial receiver gathering")
|
|
firstGatheringCurrent := <-gatheringCurrent
|
|
if !firstGatheringCurrent() {
|
|
t.Fatal("current receiver gathering lost delivery authority")
|
|
}
|
|
defer func() {
|
|
_ = nativeReceiver.Close()
|
|
if nativeReceiver.active() || firstGatheringCurrent() {
|
|
t.Error("retired receiver still authorizes queued events")
|
|
}
|
|
}()
|
|
source, audio := nativeReceiver.Source(), nativeReceiver.AudioSource()
|
|
socket := nativeReceiver.socket
|
|
for _, restart := range []bool{false, true} {
|
|
previousGathering := nativeReceiver.localCandidates
|
|
candidateMu.Lock()
|
|
remoteReady = false
|
|
candidateMu.Unlock()
|
|
offer, offerErr := upstream.CreateOffer(&webrtc.OfferOptions{ICERestart: restart})
|
|
if offerErr != nil {
|
|
t.Fatal(offerErr)
|
|
}
|
|
if err = upstream.SetLocalDescription(offer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
answer, reused, renegotiateErr := nativeReceiver.Renegotiate(offer, servers)
|
|
if renegotiateErr != nil || !reused {
|
|
t.Fatalf("receiver renegotiation: reused=%v, %v", reused, renegotiateErr)
|
|
}
|
|
if err = upstream.SetRemoteDescription(answer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
candidateMu.Lock()
|
|
remoteReady = true
|
|
pendingUpstream, pendingReceiver = upstreamCandidates, receiverCandidates
|
|
upstreamCandidates, receiverCandidates = nil, nil
|
|
candidateMu.Unlock()
|
|
for _, candidate := range pendingUpstream {
|
|
_ = nativeReceiver.AddRemoteCandidate(candidate)
|
|
}
|
|
for _, candidate := range pendingReceiver {
|
|
_ = upstream.AddICECandidate(*candidate)
|
|
}
|
|
waitConnected(t, upstream, "renegotiated receiver")
|
|
if (previousGathering != nativeReceiver.localCandidates) != restart {
|
|
t.Fatalf("collector replaced=%v, ICE restart=%v", previousGathering != nativeReceiver.localCandidates, restart)
|
|
}
|
|
if restart {
|
|
waitSignal(t, completed, "restarted receiver gathering")
|
|
if firstGatheringCurrent() || !(<-gatheringCurrent)() {
|
|
t.Fatal("queued candidates did not retain their gathering owner")
|
|
}
|
|
previousGathering.addPion(nil)
|
|
select {
|
|
case <-completed:
|
|
t.Fatal("retired collector emitted completion")
|
|
default:
|
|
}
|
|
candidateMu.Lock()
|
|
count := surveyCandidates
|
|
candidateMu.Unlock()
|
|
if count != 2 {
|
|
t.Fatalf("STUN survey candidates=%d, want one per gathering", count)
|
|
}
|
|
if bindingRequests.Load() < 2 {
|
|
t.Fatal("receiver ICE restart reused a completed STUN observation")
|
|
}
|
|
} else if bindingRequests.Load() != 1 {
|
|
t.Fatal("ordinary SDP renegotiation restarted STUN discovery")
|
|
}
|
|
if nativeReceiver.socket != socket || nativeReceiver.Source() != source || nativeReceiver.AudioSource() != audio ||
|
|
downstream.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
|
t.Fatal("renegotiation retired the source or its healthy downstream")
|
|
}
|
|
for _, input := range []struct {
|
|
track *webrtc.TrackLocalStaticRTP
|
|
media *rtp.Packet
|
|
packets <-chan *rtp.Packet
|
|
}{{track, want, packets}, {audioTrack, wantAudio, audioPackets}} {
|
|
input.media.SequenceNumber += 10
|
|
input.media.Timestamp += 90_000
|
|
if err = input.track.WriteRTP(input.media); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if packet := waitPacket(t, input.packets); string(packet.Payload) != string(input.media.Payload) {
|
|
t.Fatal("media did not resume on the retained downstream")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func connectedReceiverForSources(
|
|
t *testing.T,
|
|
engine *Engine,
|
|
source *Source,
|
|
audio *AudioSource,
|
|
connectionID string,
|
|
) (*Edge, <-chan *rtp.Packet, <-chan *rtp.Packet, error) {
|
|
options := EdgeOptions{ConnectionID: connectionID, Audio: audio}
|
|
edge, err := engine.NewEdge(source, options)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
receiver := newReceiverWithAudio(t, audio != nil, "127.0.0.1:0")
|
|
t.Cleanup(func() { _ = receiver.Close() })
|
|
packets := make(chan *rtp.Packet, 4)
|
|
audioPackets := make(chan *rtp.Packet, 4)
|
|
receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
|
go func() {
|
|
for {
|
|
packet, _, readErr := track.ReadRTP()
|
|
if readErr != nil {
|
|
return
|
|
}
|
|
if track.Kind() == webrtc.RTPCodecTypeAudio {
|
|
audioPackets <- packet
|
|
} else {
|
|
packets <- packet
|
|
}
|
|
}
|
|
}()
|
|
})
|
|
connectEdgeToReceiver(t, edge, receiver)
|
|
return edge, packets, audioPackets, nil
|
|
}
|
|
|
|
func waitConnected(t *testing.T, connection *webrtc.PeerConnection, label string) {
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("%s state = %s", label, connection.ConnectionState())
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|