Files
piik/native/capture/linux/main.test.c
T
AAA da7218bf44 feat(voice): add room voice and secure local invitations
## Problem and outcome / 问题与结果

Planned release: **v2.3.0**, preserving the published 2.x sharing contracts.

Friends can now talk in a Piik room before, during or after screen sharing. Room
voice, media forwarding and the picture retain separate lifetimes under the
existing room authority and route controller. This phase also improves direct LAN
entry, native capture startup, interaction controls and first-use documentation.

Related: #484, #437, #486, #488, #485 and #489. These references do not declare
all reported hardware or distribution problems fixed.

## Release notes

**Less typing. More talking.**

Piik now supports room voice, with or without screen sharing.

**聊不够?开麦吧!**

Piik 现已支持房间语音,不开播也能聊。

### English

This update adds room voice alongside sharing, chat and reactions, with clearer
controls and direct HTTPS invitations for local and virtual networks.

#### Highlights

- Create a room before sharing. The Host opens or closes room voice; friends
  can join, control their own microphone and listening, or mute someone locally.
  Joining voice keeps the microphone off until you enable it.
- Choose your microphone, adjust microphone, master and individual listening
  volumes from 0% to 200%, and enable voice processing in the interaction window.
  Audio icons offer quick volume sliders. The microphone icon shows live input
  level. Voice continues when the picture stops.
- Use a local HTTPS invitation for microphone and remote-control features on a
  LAN or virtual LAN. A temporary local certificate requires a browser warning
  confirmation; a self-hosted Server can also use supplied certificate files.
- Rotate your local picture, optionally volunteer more relay capacity, and
  download a native Windows x64 Server package.

#### Fixes and polish

- Align Windows automatic encoder probes with the surfaces used during actual
  capture; keep video capture progressing while optional source audio starts.
- Correct Linux desktop pixel-aspect negotiation and accept fitted native
  source dimensions without forcing a different aspect ratio.
- Avoid retaining false media queue congestion when producer and consumer
  clocks disagree.
- Distinguish chat authors with identical names, clarify remote permissions,
  and keep floating tools, contextual hints and microphone indicators aligned.
- Separate first-use guides, room tools and troubleshooting so help is easier
  to find.

Use updated Server/Web pages for room voice and update the App for the native
capture and local HTTPS improvements. Established 2.x sharing remains supported.
macOS builds do not yet carry Developer ID signing or Apple notarization; see the first-launch instructions.
The macOS audio-startup changes still need confirmation on an affected Mac.

### 中文

本次更新为分享、聊天和表情互动加入房间语音,同时打磨操作控件,并为局域网和虚拟局域网提供直接 HTTPS 邀请。

#### 主要变化

- 不开播也能创建房间。房主开启或关闭房间语音;朋友可以加入、自行开关麦克风和收听,或仅在自己这边静音某人。加入语音时默认不开麦。
- 在互动窗口选择麦克风、分别调整 0%–200% 的麦克风、总收听与个人音量,并设置语音处理。音频图标可展开快捷音量条,麦克风图标显示实时输入音量。停止画面分享后,语音仍可继续。
- 局域网和虚拟局域网可使用本地 HTTPS 邀请,启用麦克风和远程控制;临时本地证书需要在浏览器确认警告。自部署服务端也可使用自行提供的证书文件。
- 可旋转本地观看画面、自愿增加转发容量,并提供 Windows x64 服务端下载包。

#### 修复与打磨

- Windows 自动编码探测使用与实际采集一致的输入;可选来源音频启动期间,画面采集继续运行。
- 修正 Linux 桌面采集的像素比例协商;原生来源保留实际画幅,不再要求固定比例的尺寸。
- 避免生产者和消费者时钟不一致时,错误保留媒体队列拥塞状态。
- 区分同名聊天作者,明确远控授权,并统一浮窗、上下文提示与麦克风指示器的细节。
- 分开首次使用、房间工具和故障排查文档,让帮助更容易查找。

房间语音需使用更新后的服务端和网页;原生采集与本地 HTTPS 改进需更新 App。既有 2.x 分享仍保持兼容。
macOS 构建仍未正式签名和公证,首次启动请参照文档;音频启动调整仍需在受影响的 Mac 上确认。

## Verification / 验证

Full Web checks, Go tests/vet/build/native probes, reachable vulnerability checks,
normal-page P2P/SFU voice and compatible sharing, LAN HTTPS, detached interaction
tools, responsive controls and tooltip/pure-visual checks provide acceptance
evidence. Physical macOS/Linux capture and acoustic behavior on arbitrary devices
are not established by these controls.

Scoped contributions from #488 (Linux pixel aspect) and #485 (Windows Server
packaging) are retained as their original authors' patch commits in this PR.
The remaining implementation and integration belong to the maintainer.

## Impact / 影响

Preserve the published 2.x sharing contract. New room-voice behavior is opt-in;
Host voice activation, capture and picture lifetimes remain distinct. Local
certificate material remains in memory; no system trust installation is performed.
2026-10-21 06:51:22 -04:00

666 lines
30 KiB
C

// build.sh --check uses synthetic video/GStreamer elements; no capture or codec is used.
#define main capture_main
#include "main.c"
#undef main
#include <gst/video/video.h>
typedef struct { GstElement parent; guint bitrate; } FixtureEncoder;
typedef struct { GstElementClass parent; } FixtureEncoderClass;
G_DEFINE_TYPE(FixtureEncoder, fixture_encoder, GST_TYPE_ELEMENT)
static void fixture_set_property(GObject *object, guint id,
const GValue *value, GParamSpec *property) {
(void)id;
(void)property;
((FixtureEncoder *)object)->bitrate = g_value_get_uint(value);
}
static void fixture_get_property(GObject *object, guint id,
GValue *value, GParamSpec *property) {
(void)id;
(void)property;
g_value_set_uint(value, ((FixtureEncoder *)object)->bitrate);
}
static void fixture_encoder_init(FixtureEncoder *encoder) {
encoder->bitrate = 3000000;
}
static void fixture_encoder_class_init(FixtureEncoderClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->set_property = fixture_set_property;
object_class->get_property = fixture_get_property;
g_object_class_install_property(object_class, 1,
g_param_spec_uint("bitrate", "Bitrate", "Bitrate in bps", 1000, 50000000,
3000000, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING));
}
static void check_encoder_admission_and_control(void) {
GstElement *encoder = g_object_new(fixture_encoder_get_type(), NULL);
GParamSpec *property = g_object_class_find_property(G_OBJECT_GET_CLASS(encoder), "bitrate");
// Only this synthetic class is varied, before any pipeline owns an instance.
const GParamFlags flags = property->flags;
property->flags = G_PARAM_READWRITE;
g_assert_cmpuint(bitrate_divisor(encoder), ==, 0);
property->flags = G_PARAM_READABLE | GST_PARAM_MUTABLE_PLAYING;
g_assert_cmpuint(bitrate_divisor(encoder), ==, 0);
property->flags = flags;
g_assert_cmpuint(bitrate_divisor(encoder), ==, 1);
EncoderInfo info = {.bit_rate_divisor = bitrate_divisor(encoder)};
CaptureRun run = {.output_count = 1, .encoder_info = &info};
g_mutex_init(&run.lock);
run.outputs[0] = (VideoOutput){.run = &run, .encoder = encoder,
.profile = {.bit_rate = 3000000, .frame_rate = 30}};
g_assert_true(configure_encoder(&run.outputs[0]));
g_assert_true(apply_control(&run, "B 0 3000000"));
g_assert_true(apply_control(&run, "B 0 1000000"));
guint applied = 0;
g_object_get(encoder, "bitrate", &applied, NULL);
g_assert_cmpuint(applied, ==, 1000000);
g_assert_null(run.failure);
g_mutex_clear(&run.lock);
gst_object_unref(encoder);
}
static gboolean capture_failure_timeout(gpointer data) {
(void)data;
g_error("capture failure was lost before the main loop started");
return G_SOURCE_REMOVE;
}
static void check_bus_error_retains_primary_cause(void) {
CaptureRun run = {.loop = g_main_loop_new(NULL, FALSE), .output_count = 1,
.pipeline = gst_pipeline_new(NULL)};
g_mutex_init(&run.lock);
run.outputs[0].run = &run;
run.outputs[0].encoder = gst_element_factory_make("identity", NULL);
g_assert_nonnull(run.outputs[0].encoder);
GError *error = g_error_new_literal(GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
"device returned fixture failure 42");
GstMessage *message = gst_message_new_error(GST_OBJECT(run.outputs[0].encoder), error,
"fixture encoder write");
char *frame_path = NULL;
int frames = g_file_open_tmp("piik-native-test-XXXXXX", &frame_path, NULL);
g_assert_cmpint(frames, >=, 0);
int original_stdout = dup(STDOUT_FILENO);
g_assert_cmpint(original_stdout, >=, 0);
g_assert_cmpint(dup2(frames, STDOUT_FILENO), >=, 0);
g_assert_true(bus_message(NULL, message, &run));
g_assert_cmpint(dup2(original_stdout, STDOUT_FILENO), >=, 0);
close(original_stdout);
close(frames);
unlink(frame_path);
g_free(frame_path);
g_assert_cmpstr(run.failure, ==, error->message);
// Startup callbacks can fail before the bus watch/main loop is running.
// The recorded failure must still retire the pipeline once dispatch starts.
GstBus *bus = gst_element_get_bus(run.pipeline);
guint watch = gst_bus_add_watch(bus, bus_message, &run);
guint timeout = g_timeout_add(500, capture_failure_timeout, NULL);
g_main_loop_run(run.loop);
g_source_remove(timeout);
g_source_remove(watch);
gst_object_unref(bus);
gst_message_unref(message);
g_error_free(error);
gst_object_unref(run.outputs[0].encoder);
gst_object_unref(run.pipeline);
g_free(run.failure);
g_main_loop_unref(run.loop);
g_mutex_clear(&run.lock);
}
static void check_slot_profiles(void) {
char *arguments[23 + 7 * 5] = {
"capture", "--capture-video", "picker", "1", "0", "0",
"--adapter-index", "0", "--mft-index", "0", "--width", "1280",
"--height", "720", "--fps", "30", "--bitrate", "3000000",
"--preference", "balanced", "--codec", "h264", "--protocol-v7"};
for (guint index = 0; index < 7; ++index) {
guint offset = 23 + index * 5;
arguments[offset] = "--output";
arguments[offset + 1] = index == 1 ? "1280" : "854";
arguments[offset + 2] = index == 1 ? "720" : "480";
arguments[offset + 3] = "30";
arguments[offset + 4] = index == 1 ? "3000000" : "2000000";
}
CaptureRun run = {0};
guint encoder = 0;
g_assert_true(parse_profile(53, arguments, &run, &encoder));
g_assert_cmpuint(run.output_count, ==, 6);
g_assert_cmpuint(run.original_output, ==, 1);
for (guint index = 0; index < run.output_count; ++index) {
g_assert_cmpint(run.outputs[index].enabled, ==, index < 2);
}
g_assert_false(parse_profile(58, arguments, &run, &encoder));
arguments[23 + 5 * 5 + 1] = "1920";
g_assert_false(parse_profile(53, arguments, &run, &encoder));
arguments[23 + 5 * 5 + 1] = "854";
arguments[23 + 5 + 1] = "854";
g_assert_false(parse_profile(53, arguments, &run, &encoder));
arguments[23 + 5 + 1] = "1280";
char *encoded[11 + 6 * 5] = {
"capture", "--encoded-video", "--codec", "h264", "--adapter-index", "0",
"--mft-index", "0", "--preference", "balanced", "--protocol-v7"};
memcpy(encoded + 11, arguments + 23, 6 * 5 * sizeof(char *));
run = (CaptureRun){0};
g_assert_true(parse_profile(G_N_ELEMENTS(encoded), encoded, &run, &encoder));
g_assert_cmpuint(run.profile.width, ==, 1280);
g_assert_cmpuint(run.profile.height, ==, 720);
g_assert_cmpuint(run.original_output, ==, 0);
for (guint index = 0; index < run.output_count; ++index) {
g_assert_false(run.outputs[index].enabled);
}
char *numeric[] = {
"capture", "--capture-video", "picker", "1", "0", "0",
"--adapter-index", "0", "--mft-index", "0", "--width", "1920",
"--height", "1200", "--fps", "30", "--bitrate", "3000000",
"--preference", "balanced", "--codec", "h264", "--protocol-v7",
"--output", "1920", "1200", "30", "3000000"};
const struct { char *width; char *height; gboolean valid; } sizes[] = {
{"1920", "1200", TRUE}, {"720", "1280", TRUE}, {"4", "4", TRUE},
{"3840", "2160", TRUE}, {"2160", "3840", TRUE}, {"3000", "2764", TRUE},
{"0", "720", FALSE}, {"2", "720", FALSE}, {"1280", "2", FALSE},
{"1279", "720", FALSE}, {"1280", "719", FALSE},
{"5120", "1440", TRUE}, {"4096", "2304", TRUE},
{"8194", "100", FALSE}, {"5120", "2880", FALSE},
{"4096", "2306", FALSE}, {"6562", "1438", FALSE}};
for (guint index = 0; index < G_N_ELEMENTS(sizes); ++index) {
numeric[11] = numeric[24] = sizes[index].width;
numeric[13] = numeric[25] = sizes[index].height;
run = (CaptureRun){0};
g_assert_cmpint(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder), ==, sizes[index].valid);
}
numeric[11] = numeric[24] = "3840";
numeric[13] = numeric[25] = "2160";
numeric[15] = numeric[26] = "60";
run = (CaptureRun){0};
g_assert_true(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder));
numeric[15] = "30"; // Reject an excessive derived output independently.
g_assert_false(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder));
const struct { char *fps; char *bitrate; gboolean valid; } rates[] = {
{"1", "100000", TRUE}, {"5", "800000", TRUE}, {"60", "50000000", TRUE},
{"60", "240000000", TRUE}, {"65", "100000000", FALSE},
{"0", "100000", FALSE}, {"1", "99999", FALSE}, {"60", "240000001", FALSE}};
for (guint index = 0; index < G_N_ELEMENTS(rates); ++index) {
numeric[15] = numeric[26] = rates[index].fps;
numeric[17] = numeric[27] = rates[index].bitrate;
run = (CaptureRun){0};
g_assert_cmpint(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder), ==, rates[index].valid);
}
g_assert_false(valid_video_format(G_MAXUINT, G_MAXUINT, G_MAXUINT));
numeric[11] = numeric[24] = "1920";
numeric[13] = numeric[25] = "1080";
numeric[15] = numeric[26] = "240";
numeric[17] = numeric[27] = "240000000";
run = (CaptureRun){0};
g_assert_true(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder));
numeric[15] = numeric[26] = "241";
g_assert_false(parse_profile(G_N_ELEMENTS(numeric), numeric, &run, &encoder));
}
static GstPadProbeReturn record_key_request(GstPad *pad, GstPadProbeInfo *info,
gpointer data) {
(void)pad;
GstEvent *event = GST_PAD_PROBE_INFO_EVENT(info);
if (gst_video_event_is_force_key_unit(event)) {
GstEvent **request = data;
gst_event_replace(request, event);
}
return GST_PAD_PROBE_OK;
}
static void check_encoder_timestamp_segment(GstClockTime offset) {
// NVENC shifts both its PTS and segment by 1000 hours. Emit the source
// running time, including the segment base, rather than that private PTS.
const guint8 h264[] = {0, 0, 1, 0x67, 0x42, 0xc0, 0x1f,
0, 0, 1, 0x68, 0,
0, 0, 1, 0x65, 0};
GError *error = NULL;
GstElement *pipeline = gst_parse_launch(
"appsrc name=source format=time handle-segment-change=true ! "
"appsink name=output sync=false async=false", &error);
g_assert_no_error(error);
GstElement *source = gst_bin_get_by_name(GST_BIN(pipeline), "source");
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "output");
CaptureRun run = {.active = TRUE, .last_input_timestamp = GST_CLOCK_TIME_NONE,
.profile = {.frame_rate = 30}};
g_mutex_init(&run.lock);
VideoOutput output = {.run = &run, .enabled = TRUE,
.activation_timestamp = GST_CLOCK_TIME_NONE, .key_timestamp = GST_CLOCK_TIME_NONE,
.key_requested = 7,
.profile = {.width = 1280, .height = 720, .frame_rate = 30}};
GstPad *source_pad = gst_element_get_static_pad(source, "src");
GstPad *sink_pad = gst_element_get_static_pad(sink, "sink");
GstEvent *request = NULL;
gst_pad_add_probe(source_pad, GST_PAD_PROBE_TYPE_BUFFER, begin_input_frame, &run, NULL);
gst_pad_add_probe(source_pad, GST_PAD_PROBE_TYPE_BUFFER, output_input_frame, &output, NULL);
gst_pad_add_probe(sink_pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, record_key_request, &request, NULL);
GstCaps *caps = gst_caps_from_string(
"video/x-h264,stream-format=byte-stream,alignment=au,width=640,height=480");
GstBuffer *buffer = gst_buffer_new_allocate(NULL, sizeof(h264), NULL);
gst_buffer_fill(buffer, 0, h264, sizeof(h264));
GST_BUFFER_PTS(buffer) = offset + GST_SECOND;
GstSegment segment;
gst_segment_init(&segment, GST_FORMAT_TIME);
segment.start = offset;
segment.base = 2 * GST_SECOND;
GstSample *sample = gst_sample_new(buffer, caps, &segment, NULL);
char *frame_path = NULL;
int frames = g_file_open_tmp("piik-timestamp-test-XXXXXX", &frame_path, NULL);
int original_stdout = dup(STDOUT_FILENO);
g_assert_cmpint(frames, >=, 0);
g_assert_cmpint(original_stdout, >=, 0);
g_assert_cmpint(dup2(frames, STDOUT_FILENO), >=, 0);
g_assert_cmpint(gst_element_set_state(pipeline, GST_STATE_PLAYING), !=,
GST_STATE_CHANGE_FAILURE);
g_assert_cmpint(gst_app_src_push_sample(GST_APP_SRC(source), sample), ==, GST_FLOW_OK);
gst_sample_unref(sample);
gst_caps_unref(caps);
gst_buffer_unref(buffer);
g_assert_cmpint(video_sample(GST_APP_SINK(sink), &output), ==, GST_FLOW_OK);
g_assert_cmpint(dup2(original_stdout, STDOUT_FILENO), >=, 0);
close(original_stdout);
close(frames);
char *bytes = NULL;
gsize size = 0;
g_assert_true(g_file_get_contents(frame_path, &bytes, &size, NULL));
g_assert_cmpuint(size, ==, 64 + sizeof(h264));
g_assert_cmpint(bytes[5], ==, 5);
g_assert_cmpuint(read_be((guint8 *)bytes + 8, 8), ==, 3 * GST_SECOND / 100);
g_assert_cmpint(bytes[37], ==, 2);
g_assert_cmpint(bytes[38], ==, 1);
g_assert_cmpuint(read_be((guint8 *)bytes + 40, 8), ==, 3 * GST_SECOND / 100);
g_assert_cmpuint(read_be((guint8 *)bytes + 56, 2), ==, 640);
g_assert_cmpuint(read_be((guint8 *)bytes + 58, 2), ==, 480);
g_assert_cmpmem(bytes + 64, sizeof(h264), h264, sizeof(h264));
g_assert_cmpuint(output.activation_timestamp, ==, 3 * GST_SECOND);
g_assert_nonnull(request);
GstClockTime pts, running_time;
guint count;
g_assert_true(gst_video_event_parse_downstream_force_key_unit(
request, &pts, NULL, &running_time, NULL, &count));
g_assert_cmpuint(pts, ==, offset + GST_SECOND);
g_assert_cmpuint(running_time, ==, 3 * GST_SECOND);
g_assert_cmpuint(count, ==, 7);
g_free(bytes);
unlink(frame_path);
g_free(frame_path);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_event_unref(request);
gst_object_unref(source_pad);
gst_object_unref(sink_pad);
gst_object_unref(source);
gst_object_unref(sink);
gst_object_unref(pipeline);
g_mutex_clear(&run.lock);
}
static GstState branch_state(VideoOutput *output) {
GstState state = GST_STATE_VOID_PENDING;
g_assert_cmpint(gst_element_get_state(output->branch, &state, NULL, GST_SECOND),
!=, GST_STATE_CHANGE_FAILURE);
return state;
}
static void check_desktop_source_pixel_aspect(void) {
const VideoProfile profile = {.width = 1920, .height = 1080};
GString *description = g_string_new("appsrc name=source format=time ! ");
g_string_append_printf(description, "%s ! ", desktop_source_caps);
append_video_scale(description, &profile);
g_string_append(description, "appsink name=output sync=false async=false");
GError *error = NULL;
GstElement *pipeline = gst_parse_launch(description->str, &error);
g_string_free(description, TRUE);
g_assert_no_error(error);
GstElement *source = gst_bin_get_by_name(GST_BIN(pipeline), "source");
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "output");
GstPad *pad = gst_element_get_static_pad(source, "src");
GstCaps *peer = gst_pad_peer_query_caps(pad, NULL);
// Reproduce PipeWire choosing from a downstream scaler's broad PAR range.
GstCaps *offer = gst_caps_from_string(
"video/x-raw,format=BGRA,width=3840,height=2160,framerate=30/1,"
"pixel-aspect-ratio=(fraction)[1/2147483647,2147483647/1]");
GstCaps *caps = gst_caps_fixate(gst_caps_intersect_full(
offer, peer, GST_CAPS_INTERSECT_FIRST));
GstVideoInfo input;
g_assert_true(gst_video_info_from_caps(&input, caps));
g_assert_cmpint(input.par_n, ==, 1);
g_assert_cmpint(input.par_d, ==, 1);
gst_app_src_set_caps(GST_APP_SRC(source), caps);
g_assert_cmpint(gst_element_set_state(pipeline, GST_STATE_PLAYING), !=,
GST_STATE_CHANGE_FAILURE);
GstBuffer *buffer = gst_buffer_new_allocate(NULL, input.size, NULL);
GST_BUFFER_PTS(buffer) = 0;
GST_BUFFER_DURATION(buffer) = GST_SECOND / 30;
g_assert_cmpint(gst_app_src_push_buffer(GST_APP_SRC(source), buffer), ==, GST_FLOW_OK);
GstSample *sample = gst_app_sink_try_pull_sample(GST_APP_SINK(sink), GST_SECOND);
g_assert_nonnull(sample);
GstVideoInfo output;
g_assert_true(gst_video_info_from_caps(&output, gst_sample_get_caps(sample)));
g_assert_cmpint(output.width, ==, 1920);
g_assert_cmpint(output.height, ==, 1080);
g_assert_cmpint(output.par_n, ==, 1);
g_assert_cmpint(output.par_d, ==, 1);
gst_sample_unref(sample);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_caps_unref(caps);
gst_caps_unref(offer);
gst_caps_unref(peer);
gst_object_unref(pad);
gst_object_unref(source);
gst_object_unref(sink);
gst_object_unref(pipeline);
}
static void check_source_geometry(guint max_width, guint max_height) {
const VideoProfile profile = {.width = max_width, .height = max_height};
GString *description = g_string_new("appsrc name=source format=time ! ");
append_video_scale(description, &profile);
g_string_append(description, "appsink name=output sync=false async=false");
GError *error = NULL;
GstElement *pipeline = gst_parse_launch(description->str, &error);
g_string_free(description, TRUE);
g_assert_no_error(error);
GstElement *source = gst_bin_get_by_name(GST_BIN(pipeline), "source");
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "output");
g_assert_cmpint(gst_element_set_state(pipeline, GST_STATE_PLAYING), !=,
GST_STATE_CHANGE_FAILURE);
// Keep one pipeline while input caps change, as a resized captured window or
// decoded relay source does. Small sources must not be enlarged to the ceiling.
const guint sizes[][2] = {{1080, 1920}, {3440, 1440}, {640, 480},
{1920, 1080}, {641, 479}, {600, 600}};
for (guint index = 0; index < G_N_ELEMENTS(sizes); ++index) {
GstVideoInfo input;
g_assert_true(gst_video_info_set_format(&input, GST_VIDEO_FORMAT_NV12,
sizes[index][0], sizes[index][1]));
GstCaps *caps = gst_video_info_to_caps(&input);
gst_app_src_set_caps(GST_APP_SRC(source), caps);
gst_caps_unref(caps);
GstBuffer *buffer = gst_buffer_new_allocate(NULL, input.size, NULL);
gst_buffer_memset(buffer, 0, 128, input.size);
gst_buffer_memset(buffer, 0, 235, input.offset[1]);
GST_BUFFER_PTS(buffer) = index * GST_SECOND / 30;
GST_BUFFER_DURATION(buffer) = GST_SECOND / 30;
g_assert_cmpint(gst_app_src_push_buffer(GST_APP_SRC(source), buffer), ==, GST_FLOW_OK);
GstSample *sample = gst_app_sink_try_pull_sample(GST_APP_SINK(sink), GST_SECOND);
g_assert_nonnull(sample);
GstVideoInfo actual;
g_assert_true(gst_video_info_from_caps(&actual, gst_sample_get_caps(sample)));
g_assert_true(valid_video_size(actual.width, actual.height));
g_assert_cmpuint(actual.width, <=, max_width);
g_assert_cmpuint(actual.height, <=, max_height);
g_assert_cmpint(actual.par_n, ==, actual.par_d);
double scale = MIN(1.0, MIN((double)max_width / input.width,
(double)max_height / input.height));
g_assert_cmpfloat(ABS(actual.width - input.width * scale), <=, 2);
g_assert_cmpfloat(ABS(actual.height - input.height * scale), <=, 2);
GstVideoFrame frame;
g_assert_true(gst_video_frame_map(&frame, &actual, gst_sample_get_buffer(sample), GST_MAP_READ));
const guint8 *luma = GST_VIDEO_FRAME_PLANE_DATA(&frame, 0);
const gint stride = GST_VIDEO_FRAME_PLANE_STRIDE(&frame, 0);
// No encoded letterbox: even the four corners still contain the source.
for (gint y = 0; y < actual.height; y += actual.height - 1) {
g_assert_cmpint(luma[y * stride], >=, 230);
g_assert_cmpint(luma[y * stride + actual.width - 1], >=, 230);
}
gst_video_frame_unmap(&frame);
gst_sample_unref(sample);
}
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(source);
gst_object_unref(sink);
gst_object_unref(pipeline);
}
static void check_slot_retirement(void) {
CaptureRun run = {.output_count = 6};
g_mutex_init(&run.lock);
GString *description = g_string_new("fakesrc is-live=true num-buffers=8 ! tee name=frames ");
for (guint index = 0; index < run.output_count; ++index) {
g_string_append_printf(description,
"frames. ! valve name=gate%u drop=true ! ( identity name=encoder%u ! "
"fakesink sync=false async=false ) ", index, index);
}
GError *error = NULL;
run.pipeline = gst_parse_launch(description->str, &error);
g_string_free(description, TRUE);
g_assert_no_error(error);
g_assert_nonnull(run.pipeline);
for (guint index = 0; index < run.output_count; ++index) {
VideoOutput *output = &run.outputs[index];
output->run = &run;
output->profile.bit_rate = 2000000;
char *name = g_strdup_printf("encoder%u", index);
output->encoder = gst_bin_get_by_name(GST_BIN(run.pipeline), name);
g_free(name);
g_assert_nonnull(output->encoder);
output->branch = GST_ELEMENT(gst_object_get_parent(GST_OBJECT(output->encoder)));
g_assert_true(output->branch != run.pipeline);
name = g_strdup_printf("gate%u", index);
output->gate = gst_bin_get_by_name(GST_BIN(run.pipeline), name);
g_free(name);
g_assert_nonnull(output->gate);
gst_element_set_locked_state(output->branch, TRUE);
}
g_assert_cmpint(gst_element_set_state(run.pipeline, GST_STATE_PLAYING), !=, GST_STATE_CHANGE_FAILURE);
g_assert_true(apply_control(&run, "A 1 1"));
g_assert_true(apply_control(&run, "A 5 1"));
g_assert_cmpint(branch_state(&run.outputs[1]), ==, GST_STATE_PLAYING);
g_assert_cmpint(branch_state(&run.outputs[5]), ==, GST_STATE_PLAYING);
g_assert_cmpint(branch_state(&run.outputs[2]), ==, GST_STATE_NULL);
g_assert_true(apply_control(&run, "A 1 0"));
g_assert_cmpint(branch_state(&run.outputs[1]), ==, GST_STATE_NULL);
g_assert_cmpint(branch_state(&run.outputs[5]), ==, GST_STATE_PLAYING);
g_assert_true(apply_control(&run, "A 1 1"));
g_assert_cmpint(branch_state(&run.outputs[1]), ==, GST_STATE_PLAYING);
g_assert_true(apply_control(&run, "K 5"));
g_assert_false(apply_control(&run, "A 2"));
g_assert_false(apply_control(&run, "A 6 1"));
g_assert_false(apply_control(&run, "A 0 2"));
g_assert_false(apply_control(&run, "B 5 999"));
gst_element_set_state(run.pipeline, GST_STATE_NULL);
for (guint index = 0; index < run.output_count; ++index) {
gst_object_unref(run.outputs[index].encoder);
gst_object_unref(run.outputs[index].branch);
gst_object_unref(run.outputs[index].gate);
}
gst_object_unref(run.pipeline);
g_mutex_clear(&run.lock);
}
typedef struct {
CaptureRun run;
gint frames;
gint frames_at_failure;
guint checks;
} FailureFixture;
static void count_healthy_frame(GstElement *sink, GstBuffer *buffer,
GstPad *pad, gpointer data) {
(void)sink;
(void)buffer;
(void)pad;
g_atomic_int_inc(&((FailureFixture *)data)->frames);
}
static GstFlowReturn fail_without_bus(GstPad *pad, GstObject *parent, GstBuffer *buffer) {
(void)pad;
(void)parent;
gst_buffer_unref(buffer);
return GST_FLOW_ERROR;
}
static gboolean verify_sibling_survives(gpointer data) {
FailureFixture *fixture = data;
CaptureRun *run = &fixture->run;
g_assert_cmpuint(++fixture->checks, <, 200);
g_assert_null(run->failure);
if (!run->outputs[0].failed) return G_SOURCE_CONTINUE;
g_assert_false(run->outputs[0].enabled);
g_assert_cmpint(branch_state(&run->outputs[0]), ==, GST_STATE_NULL);
gint frames = g_atomic_int_get(&fixture->frames);
if (fixture->frames_at_failure < 0) fixture->frames_at_failure = frames;
if (frames - fixture->frames_at_failure < 10) return G_SOURCE_CONTINUE;
// A failed slot cannot be revived by a later demand or bitrate update.
g_assert_true(apply_control(run, "A 0 1"));
g_assert_true(apply_control(run, "B 0 1000000"));
g_assert_false(run->outputs[0].enabled);
g_assert_cmpint(branch_state(&run->outputs[1]), ==, GST_STATE_PLAYING);
GstElement *source = gst_bin_get_by_name(GST_BIN(run->pipeline), "source");
GError *error = g_error_new_literal(GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ,
"shared source fixture failure");
gst_element_post_message(source, gst_message_new_error(GST_OBJECT(source), error, NULL));
g_error_free(error);
gst_object_unref(source);
return G_SOURCE_REMOVE;
}
static void check_output_failure_isolation(gboolean post_error, const char *error_detail) {
FailureFixture fixture = {.run = {.output_count = 2}, .frames_at_failure = -1};
CaptureRun *run = &fixture.run;
g_mutex_init(&run->lock);
run->loop = g_main_loop_new(NULL, FALSE);
char *description = g_strdup_printf(
"fakesrc name=source is-live=true sizetype=fixed sizemax=4 ! "
"identity sleep-time=1000 ! tee name=frames "
"frames. ! valve name=gate0 drop=true ! ( queue max-size-buffers=1 "
"max-size-bytes=0 max-size-time=0 leaky=downstream ! "
"identity name=encoder0 error-after=%d ! fakesink name=bad sync=false async=false ) "
"frames. ! valve name=gate1 drop=true ! ( queue max-size-buffers=1 "
"max-size-bytes=0 max-size-time=0 leaky=downstream ! "
"identity name=encoder1 ! fakesink name=good signal-handoffs=true sync=false async=false )",
post_error && error_detail == NULL ? 3 : -1);
GError *error = NULL;
run->pipeline = gst_parse_launch(description, &error);
g_free(description);
g_assert_no_error(error);
for (guint index = 0; index < run->output_count; ++index) {
VideoOutput *output = &run->outputs[index];
output->run = run;
output->layer = index;
output->profile.bit_rate = 2000000;
char *name = g_strdup_printf("encoder%u", index);
output->encoder = gst_bin_get_by_name(GST_BIN(run->pipeline), name);
g_free(name);
output->branch = GST_ELEMENT(gst_object_get_parent(GST_OBJECT(output->encoder)));
name = g_strdup_printf("gate%u", index);
output->gate = gst_bin_get_by_name(GST_BIN(run->pipeline), name);
g_free(name);
g_assert_true(isolate_output(output));
}
GstElement *sink = gst_bin_get_by_name(GST_BIN(run->pipeline), post_error ? "good" : "bad");
if (!post_error) {
GstPad *pad = gst_element_get_static_pad(sink, "sink");
gst_pad_set_chain_function(pad, fail_without_bus);
gst_object_unref(pad);
gst_object_unref(sink);
sink = gst_bin_get_by_name(GST_BIN(run->pipeline), "good");
}
g_signal_connect(sink, "handoff", G_CALLBACK(count_healthy_frame), &fixture);
gst_object_unref(sink);
GstBus *bus = gst_element_get_bus(run->pipeline);
guint watch = gst_bus_add_watch(bus, bus_message, run);
gst_object_unref(bus);
char *frame_path = NULL;
int frames = g_file_open_tmp("piik-output-test-XXXXXX", &frame_path, NULL);
int original_stdout = dup(STDOUT_FILENO);
g_assert_cmpint(frames, >=, 0);
g_assert_cmpint(original_stdout, >=, 0);
g_assert_cmpint(dup2(frames, STDOUT_FILENO), >=, 0);
g_assert_cmpint(gst_element_set_state(run->pipeline, GST_STATE_PLAYING), !=, GST_STATE_CHANGE_FAILURE);
g_assert_true(set_output_active(&run->outputs[0], TRUE));
g_assert_true(set_output_active(&run->outputs[1], TRUE));
if (error_detail != NULL) fail_output(&run->outputs[0], error_detail);
g_timeout_add(10, verify_sibling_survives, &fixture);
g_main_loop_run(run->loop);
g_assert_cmpstr(run->failure, ==, "shared source fixture failure");
for (guint index = 0; index < run->output_count; ++index) {
g_assert_true(set_output_active(&run->outputs[index], FALSE));
}
gst_element_set_state(run->pipeline, GST_STATE_NULL);
g_source_remove(watch);
g_assert_cmpint(dup2(original_stdout, STDOUT_FILENO), >=, 0);
close(original_stdout);
close(frames);
char *bytes = NULL;
gsize size = 0;
g_assert_true(g_file_get_contents(frame_path, &bytes, &size, NULL));
g_assert_cmpuint(size, >, 32);
g_assert_cmpint(bytes[5], ==, 6);
g_assert_cmpint(bytes[7], ==, 0);
g_assert_cmpuint(size, ==, 32 + read_be((guint8 *)bytes + 28, 4));
g_assert_true(g_utf8_validate(bytes + 32, size - 32, NULL));
if (error_detail != NULL) {
g_assert_cmpuint(size, <=, 32 + 512 + 2); // A partial final byte becomes U+FFFD.
} else if (post_error) g_assert_nonnull(strstr(bytes + 32, "Failed after iterations as requested"));
g_free(bytes);
unlink(frame_path);
g_free(frame_path);
for (guint index = 0; index < run->output_count; ++index) {
gst_object_unref(run->outputs[index].encoder);
gst_object_unref(run->outputs[index].branch);
gst_object_unref(run->outputs[index].gate);
}
gst_object_unref(run->pipeline);
g_free(run->failure);
g_main_loop_unref(run->loop);
g_mutex_clear(&run->lock);
}
static void check_pipewire_target_identity(void) {
PiikPortalCapture portal = {.node_id = 42};
char *target = pipewire_target(&portal);
g_assert_cmpstr(target, ==, "path=42");
g_free(target);
portal.target_object = "9001";
target = pipewire_target(&portal);
g_assert_cmpstr(target, ==, "target-object=9001");
g_free(target);
}
static void check_h264_profile_admission(void) {
const struct { guint profile; gboolean supported; } cases[] = {
{0x42c034, TRUE}, {0x42e034, TRUE}, {0x42c00a, TRUE},
{0x4d8034, TRUE}, {0x58c034, TRUE},
{0x4d0034, FALSE}, {0x640034, FALSE}, {0x42c035, FALSE}, {0x42c134, FALSE}};
for (guint index = 0; index < G_N_ELEMENTS(cases); ++index) {
const guint profile = cases[index].profile;
const guint8 bytes[] = {0, 0, 0, 1, 0x67, profile >> 16, profile >> 8, profile,
0, 0, 0, 1, 0x68, 0x80, 0, 0, 1, 0x65, 0x80};
const NalSummary nal = inspect_h264(bytes, sizeof(bytes));
g_assert_true(nal.sps && nal.pps && nal.idr);
char expected[7] = {0};
if (cases[index].supported) g_snprintf(expected, sizeof(expected), "%06x", profile);
g_assert_cmpstr(nal.profile_level_id, ==, expected);
}
}
int main(int argc, char **argv) {
gst_init(&argc, &argv);
check_h264_profile_admission();
check_pipewire_target_identity();
check_encoder_admission_and_control();
check_bus_error_retains_primary_cause();
check_slot_profiles();
check_desktop_source_pixel_aspect();
check_source_geometry(1920, 1080);
check_source_geometry(2, 2);
check_encoder_timestamp_segment(1000ULL * 60 * 60 * GST_SECOND);
check_encoder_timestamp_segment(0);
check_slot_retirement();
check_output_failure_isolation(TRUE, NULL);
check_output_failure_isolation(FALSE, NULL);
char *prefix = g_strnfill(511, 'x');
char *localized_error = g_strconcat(prefix, "\xe4\xb8\xad\xe6\x96\x87", NULL);
check_output_failure_isolation(TRUE, localized_error);
g_free(localized_error);
g_free(prefix);
return 0;
}