Build verified live source snapshots in the core thread
This commit is contained in:
1 parent
a5fde68ebf
commit
884dcf73cc
2 files changed
+69
No files matched your search
@@ -1,11 +1,17 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../../lib/debug_config.h"
|
||||
#include "../../../lib/mem.h"
|
||||
#include "../../../lib/u_async.h"
|
||||
#include "../../../lib/sqlite3.h"
|
||||
#include "../../../lib/strbuf.h"
|
||||
#include "../../../src/utun_instance.h"
|
||||
#include "../../../src/chat/chat_core.h"
|
||||
#include "../../../src/routing_layer/topo_group.h"
|
||||
#include "../../../src/stream/stream.h"
|
||||
#include "../../../src/stream/stream_member.h"
|
||||
#include "../../../src/stream/stream_media.h"
|
||||
#include "stream_bridge.h"
|
||||
|
||||
@@ -37,6 +43,8 @@ static struct posted_task *media_post;
|
||||
static void *media_timer;
|
||||
static struct android_media_session publisher, viewer;
|
||||
static struct media_queue video_queue, audio_queue, output_queue;
|
||||
static uint64_t list_group, cached_group, list_updated;
|
||||
static char *source_list;
|
||||
static void media_process(void *arg);
|
||||
|
||||
/* Очереди обмена pthread: LL_QUEUE принадлежит uasync и сюда не подходит. */
|
||||
@@ -173,12 +181,52 @@ static int media_publish(void) {
|
||||
return STREAM_OK;
|
||||
}
|
||||
|
||||
/* Список содержит только проверенные публикации живых мемберов; SQL и BGP читаются в uasync. */
|
||||
static void media_list_sources(void) {
|
||||
uint64_t group_id = list_group; list_group = 0;
|
||||
struct TOPO_GROUP *group = media_inst->topo_groups ? topo_groups_find(media_inst->topo_groups, group_id) : NULL;
|
||||
struct strbuf json = strbuf_new();
|
||||
sqlite3_stmt *statement = NULL;
|
||||
int failed = strbuf_addf(&json, "[") < 0;
|
||||
if (group && !failed) {
|
||||
char query[256];
|
||||
snprintf(query, sizeof(query), "SELECT p.node_id,COALESCE(NULLIF(p.local_nick,''),n.name,'') "
|
||||
"FROM \"peers_%s\" p LEFT JOIN nodes n ON p.node_id=n.node_id", group->channel_id);
|
||||
sqlite3 *db = chat_core_get_db(media_inst);
|
||||
int result = sqlite3_prepare_v2(db, query, -1, &statement, NULL), count = 0;
|
||||
if (result == SQLITE_OK) {
|
||||
while ((result = sqlite3_step(statement)) == SQLITE_ROW) {
|
||||
uint64_t node = (uint64_t)sqlite3_column_int64(statement, 0);
|
||||
if (!stream_member_online(media_inst, group_id, node)) continue;
|
||||
struct stream_member_state state;
|
||||
if (stream_member_load(media_inst, group_id, node, &state, NULL) != STREAM_OK || !state.published) continue;
|
||||
if (count >= 256) { DEBUG_WARN(DEBUG_CATEGORY_STREAM, "Android stream source list truncated group=%llx", (unsigned long long)group_id); break; }
|
||||
if (strbuf_addf(&json, "%s{\"id\":\"%llu\",\"epoch\":\"%016llx\",\"name\":", count ? "," : "",
|
||||
(unsigned long long)node, (unsigned long long)state.source.source_epoch) < 0
|
||||
|| strbuf_add_json_string(&json, (const char *)sqlite3_column_text(statement, 1)) < 0
|
||||
|| strbuf_addf(&json, "}") < 0) { failed = 1; break; }
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (result != SQLITE_DONE && !(result == SQLITE_ROW && (failed || count == 256))) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "Android stream list query failed group=%llx: %s",
|
||||
(unsigned long long)group_id, sqlite3_errmsg(db)); failed = 1;
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
if (strbuf_addf(&json, "]") < 0) failed = 1;
|
||||
u_free(source_list); source_list = failed ? NULL : strbuf_detach(&json);
|
||||
strbuf_free(&json); cached_group = group_id; list_updated = get_time_us();
|
||||
if (failed) DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "Android stream source snapshot failed group=%llx", (unsigned long long)group_id);
|
||||
}
|
||||
|
||||
/* Один actor обслуживает команды и не больше двух FEC-пачек за вызов; idle не будит мобильное ядро. */
|
||||
static void media_process(void *arg) {
|
||||
(void)arg;
|
||||
pthread_mutex_lock(&media_lock);
|
||||
media_post = NULL; media_timer = NULL;
|
||||
if (!media_inst) { pthread_mutex_unlock(&media_lock); return; }
|
||||
if (list_group) media_list_sources();
|
||||
struct android_media_session *sessions[] = {&publisher, &viewer};
|
||||
for (unsigned i = 0; i < 2; i++) {
|
||||
struct android_media_session *s = sessions[i];
|
||||
@@ -217,6 +265,11 @@ static void media_process(void *arg) {
|
||||
}
|
||||
if (publisher.handle && !publisher.finished) {
|
||||
for (unsigned i = 0; i < 2 && (video_queue.head || audio_queue.head); i++) {
|
||||
size_t pending = video_queue.bytes + audio_queue.bytes - (video_queue.head ? video_queue.head->offset : 0);
|
||||
uint64_t oldest = video_queue.head ? video_queue.head->created : audio_queue.head->created;
|
||||
if (audio_queue.head && audio_queue.head->created < oldest) oldest = audio_queue.head->created;
|
||||
if (!publisher.stopping && pending + STREAM_MEDIA_HEADER_SIZE + STREAM_MEDIA_VIDEO_SIZE < 7080
|
||||
&& now - oldest < 20000) break;
|
||||
int result = media_publish();
|
||||
if (result < 0) { DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "Android media publish failed result=%d", result); media_close(&publisher, 6); break; }
|
||||
}
|
||||
@@ -250,6 +303,7 @@ void android_stream_detach(struct UTUN_INSTANCE *inst) {
|
||||
if (publisher.id) media_close(&publisher, 6);
|
||||
if (viewer.id) media_close(&viewer, 6);
|
||||
media_inst = NULL;
|
||||
u_free(source_list); source_list = NULL; list_group = cached_group = list_updated = 0;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_STREAM, "Android stream bridge detached; producers rejected");
|
||||
}
|
||||
pthread_mutex_unlock(&media_lock);
|
||||
@@ -337,3 +391,16 @@ uint8_t *android_stream_pull(uint64_t id, size_t *length) {
|
||||
}
|
||||
pthread_mutex_unlock(&media_lock); return data;
|
||||
}
|
||||
|
||||
/* Внешний поток не читает ни borrowed instance, ни SQLite; snapshot строится следующим actor-вызовом. */
|
||||
char *android_stream_sources(uint64_t group) {
|
||||
pthread_mutex_lock(&media_lock);
|
||||
if (group && media_inst && uasync_is_running(media_inst->ua)
|
||||
&& (group != cached_group || get_time_us() - list_updated >= 500000)) {
|
||||
list_group = group;
|
||||
if (media_schedule() < 0) DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "Android source snapshot schedule failed");
|
||||
}
|
||||
char *copy = u_strdup(group == cached_group && source_list ? source_list : "[]");
|
||||
if (!copy) DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "Android source snapshot copy allocation failed");
|
||||
pthread_mutex_unlock(&media_lock); return copy;
|
||||
}
|
||||
@@ -21,5 +21,7 @@ int android_stream_feed(uint64_t id, int video, uint64_t pts, int key, const uin
|
||||
/* feed: 1 accepted, 0 dropped (video: запросить KEY), -1 detached/stopped/invalid. */
|
||||
void android_stream_stop(uint64_t id);
|
||||
uint8_t *android_stream_pull(uint64_t id, size_t *length);
|
||||
/* Кеш live-снимка источников: запрос в actor, чтение только копии под mutex; caller освобождает u_free(). */
|
||||
char *android_stream_sources(uint64_t group);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user