Commit 82328c18d0 for freeswitch.com

commit 82328c18d0f3e00c4568ad837af1ec32509c9db0
Author: Andrey Volk <andywolk@gmail.com>
Date:   Tue Nov 24 23:16:10 2026 +0300

    [Core] Keep the VPX encoder pts base across bitrate changes, with a unit-test (#3307)

    init_encoder() restarted the pts base on every call, including a bitrate
    change that only reconfigures the existing encoder with
    vpx_codec_enc_config_set(). libvpx keeps the first pts of the encoder, so
    the next frame could get a pts below it, which libvpx v1.17 rejects with
    "pts is smaller than initial pts". Restart the pts base only when a new
    encoder is created.

diff --git a/src/switch_vpx.c b/src/switch_vpx.c
index 6c5d6475ce..a9b2fb0c49 100644
--- a/src/switch_vpx.c
+++ b/src/switch_vpx.c
@@ -602,7 +602,10 @@ static switch_status_t init_encoder(switch_codec_t *codec)
 		context->codec_settings.video.width, context->codec_settings.video.height, context->bandwidth);

 	context->pkt = NULL;
-	context->start_time = switch_micro_time_now();
+
+	if (!context->encoder_init) {
+		context->start_time = switch_micro_time_now();
+	}

 	*config = my_cfg->enc_cfg; // reset whole config to current defaults

diff --git a/tests/unit/switch_vpx.c b/tests/unit/switch_vpx.c
index 0c42fd8665..92ce3c96fe 100644
--- a/tests/unit/switch_vpx.c
+++ b/tests/unit/switch_vpx.c
@@ -5,6 +5,44 @@
  */
 #include <test/switch_test.h>

+/* Waits for the next soft timer tick (the time switch_micro_time_now() returns),
+ * then for offset_us more of real time. Fails when the timer does not tick within
+ * a second or ticks again before offset_us pass */
+static switch_status_t wait_into_tick(switch_time_t offset_us)
+{
+	switch_time_t deadline = switch_time_now() + 1000000;
+	switch_time_t tick = switch_micro_time_now();
+
+	while (switch_micro_time_now() == tick) {
+		if (switch_time_now() > deadline) {
+			return SWITCH_STATUS_TIMEOUT;
+		}
+
+		switch_os_yield();
+	}
+
+	tick = switch_micro_time_now();
+
+	while (switch_time_now() - tick < offset_us) {
+		switch_os_yield();
+	}
+
+	return switch_micro_time_now() == tick ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+}
+
+/* Encodes frame->img and reads out all of its packets */
+static switch_status_t encode_picture(switch_codec_t *codec, switch_frame_t *frame)
+{
+	switch_status_t status;
+
+	do {
+		frame->datalen = SWITCH_DEFAULT_VIDEO_SIZE;
+		status = switch_core_codec_encode_video(codec, frame);
+	} while (status == SWITCH_STATUS_MORE_DATA);
+
+	return status;
+}
+
 FST_CORE_BEGIN("./conf")
 {
 	FST_SUITE_BEGIN()
@@ -86,6 +124,58 @@ FST_CORE_BEGIN("./conf")
 		}
 		FST_TEST_END()

+		/* A bitrate change reconfigures the running VP8 encoder. The first picture
+		 * is encoded 5 ms into a soft timer tick, the picture after the bitrate
+		 * change right at the start of a tick, and both must encode */
+		FST_TEST_BEGIN(vp8_bandwidth_change)
+		{
+			uint8_t buf[SWITCH_DEFAULT_VIDEO_SIZE + 12];
+			switch_codec_t codec = { 0 };
+			switch_codec_settings_t codec_settings = { { 0 } };
+			switch_frame_t frame = { 0 };
+			switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_I420, 640, 360, 1);
+			switch_rgb_color_t color = { 0 };
+			switch_status_t status;
+			int bandwidth = 500;
+
+			codec_settings.video.width = 640;
+			codec_settings.video.height = 360;
+
+			status = switch_core_codec_init(&codec, "VP8", NULL, NULL, 0, 0, 1, SWITCH_CODEC_FLAG_ENCODE, &codec_settings, fst_pool);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(img != NULL);
+
+			if (status == SWITCH_STATUS_SUCCESS && img) {
+				color.r = 255;
+				switch_img_fill(img, 0, 0, img->d_w, img->d_h, &color);
+
+				frame.packet = buf;
+				frame.data = buf + 12;
+				frame.img = img;
+
+				status = wait_into_tick(5000);
+				fst_check(status == SWITCH_STATUS_SUCCESS);
+				status = encode_picture(&codec, &frame);
+				fst_check(status == SWITCH_STATUS_SUCCESS);
+
+				switch_sleep(40000);
+
+				status = wait_into_tick(0);
+				fst_check(status == SWITCH_STATUS_SUCCESS);
+				status = switch_core_codec_control(&codec, SCC_VIDEO_BANDWIDTH, SCCT_INT, &bandwidth, SCCT_NONE, NULL, NULL, NULL);
+				fst_check(status == SWITCH_STATUS_SUCCESS);
+				status = encode_picture(&codec, &frame);
+				fst_check(status == SWITCH_STATUS_SUCCESS);
+			}
+
+			if (switch_core_codec_ready(&codec)) {
+				switch_core_codec_destroy(&codec);
+			}
+
+			switch_img_free(&img);
+		}
+		FST_TEST_END()
+
 		FST_TEARDOWN_BEGIN()
 		{
 			switch_sleep(1000000);