Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

32.8 KB
Raw Blame
// 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;
  gint gop_size;
  guint bframes;
  gint rc_mode;
} 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)property;
  FixtureEncoder *encoder = (FixtureEncoder *)object;
  switch (id) {
    case 1: encoder->bitrate = g_value_get_uint(value); break;
    case 2: encoder->gop_size = g_value_get_int(value); break;
    case 3: encoder->bframes = g_value_get_uint(value); break;
    case 4: encoder->rc_mode = g_value_get_enum(value); break;
  }
}

static void fixture_get_property(GObject *object, guint id,
                                 GValue *value, GParamSpec *property) {
  (void)property;
  FixtureEncoder *encoder = (FixtureEncoder *)object;
  switch (id) {
    case 1: g_value_set_uint(value, encoder->bitrate); break;
    case 2: g_value_set_int(value, encoder->gop_size); break;
    case 3: g_value_set_uint(value, encoder->bframes); break;
    case 4: g_value_set_enum(value, encoder->rc_mode); break;
  }
}

static void fixture_encoder_init(FixtureEncoder *encoder) {
  encoder->bitrate = 3000000;
  encoder->gop_size = 75;
  encoder->bframes = 2;
}

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));
  g_object_class_install_property(object_class, 2,
      g_param_spec_int("gop-size", "GOP size", "Number of frames between intra frames",
                      -1, G_MAXINT, 75, G_PARAM_READWRITE));
  g_object_class_install_property(object_class, 3,
      g_param_spec_uint("bframes", "B frames", "Number of B frames", 0, 4, 2,
                       G_PARAM_READWRITE));
  static const GEnumValue modes[] = {{0, "Default", "default"}, {1, "CBR", "cbr"}, {0, NULL, NULL}};
  g_object_class_install_property(object_class, 4,
      g_param_spec_enum("rc-mode", "RC mode", "Rate control mode",
          g_enum_register_static("PiikFixtureRateControl", modes), 0, G_PARAM_READWRITE));
}

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]));
  gint gop_size = 0, rc_mode = 0;
  guint bframes = 0;
  g_object_get(encoder, "gop-size", &gop_size, "bframes", &bframes, "rc-mode", &rc_mode, NULL);
  g_assert_cmpint(gop_size, ==, 60);
  g_assert_cmpuint(bframes, ==, 0);
  g_assert_cmpint(rc_mode, ==, 1);
  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);
  // The same rejected codec setting retires an optional slot independently,
  // but cannot admit a run whose required original encoder failed.
  run.original_output = 1;
  run.outputs[0].profile.bit_rate = 50000001;
  run.outputs[0].enabled = TRUE;
  g_assert_true(prepare_encoder(&run.outputs[0]));
  g_assert_true(run.outputs[0].failed);
  g_assert_false(run.outputs[0].enabled);
  run.original_output = 0;
  g_assert_false(prepare_encoder(&run.outputs[0]));
  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_startup_error_retains_primary_cause(void) {
  char *missing = NULL;
  int file = g_file_open_tmp("piik-missing-source-XXXXXX", &missing, NULL);
  g_assert_cmpint(file, >=, 0);
  close(file);
  g_assert_cmpint(unlink(missing), ==, 0);
  CaptureRun run = {.loop = g_main_loop_new(NULL, FALSE),
      .pipeline = gst_parse_launch("filesrc name=source ! fakesink", NULL)};
  g_assert_nonnull(run.pipeline);
  g_mutex_init(&run.lock);
  GstElement *source = gst_bin_get_by_name(GST_BIN(run.pipeline), "source");
  g_assert_nonnull(source);
  g_object_set(source, "location", missing, NULL);
  gst_object_unref(source);
  g_assert_cmpint(run_pipeline(&run, NULL), ==, 2);
  char *expected = gst_error_get_message(GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND);
  g_assert_cmpstr(run.failure, ==, expected);
  g_free(expected);
  g_free(missing);
  g_free(run.failure);
  gst_object_unref(run.pipeline);
  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 = {.last_input_timestamp = GST_CLOCK_TIME_NONE,
                    .profile = {.width = 1280, .height = 720, .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, >, 96 + 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_cmpint(bytes[64 + sizeof(h264) + 5], ==, 3);
  g_assert_nonnull(strstr(bytes + 96 + sizeof(h264), "\"width\":640,\"height\":480"));
  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_startup_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;
}