132 lines
5.7 KiB
C#
132 lines
5.7 KiB
C#
using System.Net;
|
|||
|
|
using System.Text.Json;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.E2E.Tests.Pages;
|
||
|
|
|
||
|
|
[Collection("E2E")]
|
||
|
|
public sealed class LiveViewTests : E2ETestBase
|
||
|
|
{
|
||
|
|
public LiveViewTests(TestCommon.PostgreSqlContainerFixture postgres) : base(postgres) { }
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Live_view_negotiates_audio_and_video_and_starts_muted_for_autoplay()
|
||
|
|
{
|
||
|
|
var cameraId = Guid.NewGuid();
|
||
|
|
var realWhepUrl = Environment.GetEnvironmentVariable("HEIMDALL_TEST_LIVE_WHEP_URL");
|
||
|
|
var useRealMedia = !string.IsNullOrWhiteSpace(realWhepUrl);
|
||
|
|
await Page.RouteAsync("**/api/live-media/**", async route =>
|
||
|
|
{
|
||
|
|
var request = route.Request;
|
||
|
|
if (request.Url.EndsWith("/session", StringComparison.Ordinal))
|
||
|
|
{
|
||
|
|
await route.FulfillAsync(new()
|
||
|
|
{
|
||
|
|
Status = (int)HttpStatusCode.OK,
|
||
|
|
ContentType = "application/json",
|
||
|
|
Body = JsonSerializer.Serialize(new
|
||
|
|
{
|
||
|
|
url = useRealMedia ? realWhepUrl : $"{BaseUrl}/media/{cameraId:D}/whep",
|
||
|
|
token = "synthetic-read-ticket",
|
||
|
|
sessionId = Guid.NewGuid(),
|
||
|
|
iceServers = Array.Empty<object>()
|
||
|
|
})
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
await route.FulfillAsync(new() { Status = (int)HttpStatusCode.NoContent });
|
||
|
|
});
|
||
|
|
if (!useRealMedia)
|
||
|
|
{
|
||
|
|
await Page.RouteAsync("**/media/**/whep", route => route.FulfillAsync(new()
|
||
|
|
{
|
||
|
|
Status = (int)HttpStatusCode.Created,
|
||
|
|
ContentType = "application/sdp",
|
||
|
|
Headers = new Dictionary<string, string> { ["Location"] = "/media/session-resource" },
|
||
|
|
Body = "v=0\r\n"
|
||
|
|
}));
|
||
|
|
await Page.RouteAsync("**/media/session-resource", route => route.FulfillAsync(new()
|
||
|
|
{
|
||
|
|
Status = (int)HttpStatusCode.NoContent
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
|
||
|
|
await Page.GotoAsync($"{BaseUrl}/login");
|
||
|
|
if (!useRealMedia)
|
||
|
|
await Page.EvaluateAsync("""
|
||
|
|
() => {
|
||
|
|
window.__liveViewTransceivers = [];
|
||
|
|
window.__liveViewTracks = [];
|
||
|
|
window.RTCPeerConnection = class {
|
||
|
|
constructor() { this.iceGatheringState = 'complete'; }
|
||
|
|
addTransceiver(kind, options) { window.__liveViewTransceivers.push({ kind, direction: options.direction }); }
|
||
|
|
async createOffer() { return { type: 'offer', sdp: 'v=0\\r\\n' }; }
|
||
|
|
async setLocalDescription(description) { this.localDescription = description; }
|
||
|
|
async setRemoteDescription() {
|
||
|
|
const stream = new MediaStream();
|
||
|
|
for (const kind of ['video', 'audio']) {
|
||
|
|
window.__liveViewTracks.push(kind);
|
||
|
|
this.ontrack({ streams: [stream], track: { kind, addEventListener() {} } });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
addEventListener() {}
|
||
|
|
close() {}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
""");
|
||
|
|
|
||
|
|
var result = await Page.EvaluateAsync<JsonElement>("""
|
||
|
|
async options => {
|
||
|
|
const { cameraId, useRealMedia } = options;
|
||
|
|
const video = document.createElement('video');
|
||
|
|
video.dataset.liveCamera = cameraId;
|
||
|
|
if (!useRealMedia)
|
||
|
|
video.play = async () => { window.__liveViewPlayCalled = true; };
|
||
|
|
document.body.append(video);
|
||
|
|
const live = await import('/js/live-view.js');
|
||
|
|
await live.start(cameraId, video);
|
||
|
|
if (useRealMedia) {
|
||
|
|
await new Promise((resolve, reject) => {
|
||
|
|
const deadline = setTimeout(() => reject(new Error('Timed out waiting for decoded WebRTC audio/video')), 20000);
|
||
|
|
const timer = setInterval(() => {
|
||
|
|
if (video.videoWidth > 0 && video.srcObject?.getAudioTracks().some(track => track.readyState === 'live')) {
|
||
|
|
clearInterval(timer);
|
||
|
|
clearTimeout(deadline);
|
||
|
|
resolve();
|
||
|
|
}
|
||
|
|
}, 100);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
const value = {
|
||
|
|
transceivers: window.__liveViewTransceivers,
|
||
|
|
tracks: window.__liveViewTracks,
|
||
|
|
audioTracks: video.srcObject?.getAudioTracks().length ?? 0,
|
||
|
|
videoTracks: video.srcObject?.getVideoTracks().length ?? 0,
|
||
|
|
videoWidth: video.videoWidth,
|
||
|
|
muted: video.muted,
|
||
|
|
playCalled: window.__liveViewPlayCalled
|
||
|
|
};
|
||
|
|
await live.stop(cameraId);
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
""", new { cameraId = cameraId.ToString("D"), useRealMedia });
|
||
|
|
|
||
|
|
if (useRealMedia)
|
||
|
|
{
|
||
|
|
result.GetProperty("audioTracks").GetInt32().ShouldBeGreaterThan(0);
|
||
|
|
result.GetProperty("videoTracks").GetInt32().ShouldBeGreaterThan(0);
|
||
|
|
result.GetProperty("videoWidth").GetInt32().ShouldBeGreaterThan(0);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
result.GetProperty("transceivers").EnumerateArray().Select(x => x.GetProperty("kind").GetString())
|
||
|
|
.ShouldBe(new[] { "video", "audio" });
|
||
|
|
result.GetProperty("transceivers").EnumerateArray().ShouldAllBe(x =>
|
||
|
|
x.GetProperty("direction").GetString() == "recvonly");
|
||
|
|
result.GetProperty("tracks").EnumerateArray().Select(x => x.GetString()).ShouldBe(new[] { "video", "audio" });
|
||
|
|
result.GetProperty("playCalled").GetBoolean().ShouldBeTrue();
|
||
|
|
}
|
||
|
|
result.GetProperty("muted").GetBoolean().ShouldBeTrue();
|
||
|
|
}
|
||
|
|
}
|