重启 Chromium 刷新推流并切换测试入口

parent 7919b3ec
......@@ -12,6 +12,7 @@
#define BROWSER_URL_SIZE 768
#define MD5_HEX_SIZE 33
#define BROWSER_TEST_PAGE "index-test.html" /* 临时推流测试入口 */
static char s_webcam[2048];//存放启动火狐网站命令
static char s_active_stream_url[BROWSER_URL_SIZE]; /* 最近一次启动 chromium 的推流网址 */
......@@ -35,6 +36,7 @@ static ThreadPool_t *s_refresh_pool = NULL;
#define BROWSER_AUTO_RESTART_FAIL_SEC 5
static int browser_launch_chromium(const char *url);
static int browser_kill_chromium(void);
static int browser_build_stream_urls(const char *url, char *base_url, size_t base_size,
char *launch_url, size_t launch_size);
static int browser_chromium_found_cb(int pid, const char *cmdline, void *ctx);
......@@ -52,12 +54,17 @@ int opencamsh() {
return -1;
}
if (is_browser_running()) {
pthread_mutex_unlock(&s_browser_ops_mutex);
return 0;
}
rc = browser_launch_chromium(g_mqtt_cam_config_t->videourl);
s_browser_retry_after = rc == 0 ? 0 : time(NULL) + BROWSER_AUTO_RESTART_FAIL_SEC;
pthread_mutex_unlock(&s_browser_ops_mutex);
if (rc != 0) {
s_open_cam_fail_streak++;
s_browser_retry_after = time(NULL) + BROWSER_AUTO_RESTART_FAIL_SEC;
if (s_open_cam_fail_streak >= 10 && (s_open_cam_fail_streak % 10) == 0) {
my_zlog_error("chromium start fail, continuous fail:%d, enter count:%d",
s_open_cam_fail_streak, s_open_cam_enter_count);
......@@ -72,7 +79,6 @@ int opencamsh() {
my_zlog_info("chromium start recovered, previous continuous fail:%d", s_open_cam_fail_streak);
}
s_open_cam_fail_streak = 0;
s_browser_retry_after = 0;
my_zlog_debug("open cam");
return 0;
}
......@@ -80,74 +86,50 @@ int opencamsh() {
/* 实际刷新逻辑:在后台线程执行,避免阻塞 MQTT 心跳线程 */
static void refresh_cam_impl(void)
{
int refreshed_windows = 0;
int failed_windows = 0;
int enter = 0;
char stream_url[BROWSER_URL_SIZE];
const char *failure = NULL;
bool running = false;
pthread_mutex_lock(&s_refresh_stat_mutex);
s_refresh_cam_enter_count++;
enter = s_refresh_cam_enter_count;
int enter = ++s_refresh_cam_enter_count;
pthread_mutex_unlock(&s_refresh_stat_mutex);
my_zlog_info("refresh_cam(async worker): start enter=%d", enter);
setenv("DISPLAY", ":0", 1); // 确保 xdotool 在图形会话中执行
setenv("XAUTHORITY", "/home/orangepi/.Xauthority", 1);//加入授权
const char *search_command = "xdotool search --class \"chromium-browser\"";//获取窗口id
FILE *fp = popen(search_command, "r");
if (!fp) {
pthread_mutex_lock(&s_refresh_stat_mutex);
s_refresh_cam_fail_count++;
int fc = s_refresh_cam_fail_count;
pthread_mutex_unlock(&s_refresh_stat_mutex);
my_zlog_error("refresh_cam outcome=FAIL reason=popen total_fail=%d enter=%d",
fc, enter);
return;
}
char window_id[32];
while (fgets(window_id, sizeof(window_id), fp) != NULL) {
window_id[strcspn(window_id, "\n")] = '\0';
char activate_command[128];
snprintf(activate_command, sizeof(activate_command), "xdotool windowactivate %s key Ctrl+r", window_id);
if (system(activate_command) == 0) {
refreshed_windows++;
pthread_mutex_lock(&s_browser_ops_mutex);
if (s_browser_external_switch) {
failure = "stream_switch_in_progress";
} else {
browser_get_stream_url_info(stream_url, sizeof(stream_url), &running);
if (!running) {
failure = "no_chromium";
} else if (!stream_url[0]) {
failure = "no_stream_url";
} else if (browser_kill_chromium() != 0) {
failure = "chromium_stop";
} else {
/* 等待旧信令连接释放,再使用重新签名的地址启动。 */
delay_s(1);
if (browser_launch_chromium(stream_url) == 0) {
s_browser_retry_after = 0;
} else {
failed_windows++;
my_zlog_warn("refresh_cam window command fail, window id:%s", window_id);
s_browser_retry_after = time(NULL) + BROWSER_AUTO_RESTART_FAIL_SEC;
failure = "chromium_launch";
}
}
pclose(fp);
if (refreshed_windows == 0) {
pthread_mutex_lock(&s_refresh_stat_mutex);
s_refresh_cam_fail_count++;
int fc = s_refresh_cam_fail_count;
pthread_mutex_unlock(&s_refresh_stat_mutex);
my_zlog_warn("refresh_cam outcome=FAIL reason=no_chromium_window total_fail=%d enter=%d",
fc, enter);
return;
}
pthread_mutex_unlock(&s_browser_ops_mutex);
if (failed_windows > 0) {
if (failure) {
pthread_mutex_lock(&s_refresh_stat_mutex);
s_refresh_cam_fail_count++;
int fc = s_refresh_cam_fail_count;
int fail_count = ++s_refresh_cam_fail_count;
pthread_mutex_unlock(&s_refresh_stat_mutex);
my_zlog_warn("refresh_cam outcome=FAIL reason=partial success=%d fail=%d total_fail=%d enter=%d",
refreshed_windows, failed_windows, fc, enter);
my_zlog_warn("refresh_cam outcome=FAIL reason=%s total_fail=%d enter=%d",
failure, fail_count, enter);
return;
}
my_zlog_info("refresh_cam outcome=OK windows=%d enter=%d", refreshed_windows, enter);
my_zlog_info("refresh_cam outcome=OK chromium_restarted enter=%d", enter);
}
static int ensure_refresh_pool_ready(void)
......@@ -585,11 +567,29 @@ static int browser_build_stream_urls(const char *url, char *base_url, size_t bas
const char *fragment = url + strcspn(url, "#");
const char *query = memchr(url, '?', fragment - url);
size_t used = (query ? query : fragment) - url;
if (used >= base_size) {
const char *prefix_end = query ? query : fragment;
const char *page = "";
if (launch_url) {
const char *scheme = strstr(url, "://");
if (!scheme || scheme + 3 >= prefix_end) {
return -1;
}
const char *authority = scheme + 3;
const char *path = memchr(authority, '/', prefix_end - authority);
if (path) {
while (prefix_end[-1] != '/') {
prefix_end--;
}
page = BROWSER_TEST_PAGE;
} else {
page = "/" BROWSER_TEST_PAGE;
}
}
int prefix_length = snprintf(base_url, base_size, "%.*s%s", (int)(prefix_end - url), url, page);
if (prefix_length < 0 || (size_t)prefix_length >= base_size) {
return -1;
}
memcpy(base_url, url, used);
size_t used = (size_t)prefix_length;
const char *dev = NULL;
size_t dev_len = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment