STKouyuEngine C core line coverage

Total 99.05% (2801 of 2828 lines)

filecoveredlinespercent
Sources/STKouyuEngine/core/ygst_api.c238238100.00%
Sources/STKouyuEngine/core/ygst_audio.c16716899.40%
Sources/STKouyuEngine/core/ygst_buf.c9710394.17%
Sources/STKouyuEngine/core/ygst_crypto.c140140100.00%
Sources/STKouyuEngine/core/ygst_envelope.c251251100.00%
Sources/STKouyuEngine/core/ygst_errors_table.c4040100.00%
Sources/STKouyuEngine/core/ygst_fields.c14114994.63%
Sources/STKouyuEngine/core/ygst_json.c34034897.70%
Sources/STKouyuEngine/core/ygst_multipart.c6969100.00%
Sources/STKouyuEngine/core/ygst_params.c16116597.58%
Sources/STKouyuEngine/core/ygst_pipeline.c4242100.00%
Sources/STKouyuEngine/core/ygst_realtime.c463463100.00%
Sources/STKouyuEngine/core/ygst_retry.c374374100.00%
Sources/STKouyuEngine/core/ygst_timing.c278278100.00%

Sources/STKouyuEngine/core/ygst_api.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * TtsClient and ReportClient (2.2.0): POST /api/v1/tts/generate and GET /api/v1/report/{recordId}
6  * (CONTRACT 2.2, 2.3, 4, 6, 7). The JSON body of a TTS request with its signed parameters, the
7  * report path, the absolute audio URL, the fields of the answers, the wait for the AI final draft and
8  * the log line of a call. ygst_classify_api (ygst_retry.c) classifies the answers; the Objective-C
9  * layer sends the requests with the session and the retry controller of the evaluations. Since 2.3.0
10  * the wait goes on through reportStatus partial as well as pending.
11  */
12 #include "ygst_core.h"
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 /* ------------------------------------------------------------------ TTS request */
18 
19 15void ygst_tts_request_init(ygst_tts_request *r) {
20 15    if (r) memset(r, 0, sizeof *r);
21 15}
22 
23 typedef struct {
24     ygst_buf *json;
25     ygst_fields *sign;
26     int first;
27     int rc;
28 } body_writer;
29 
30 /* "name":<literal> into the body; the signed parameter is the literal text of the value */
31 37static void put_member(body_writer *w, const char *name, const char *literal, int quoted) {
32 37    if (!w->first) ygst_buf_append_char(w->json, ',');
33 37    w->first = 0;
34 37    ygst_json_escape(w->json, name);
35 37    ygst_buf_append_char(w->json, ':');
36 37    if (quoted) {
37 28        ygst_json_escape(w->json, literal);
38     } else {
39 9        ygst_buf_append_str(w->json, literal);
40     }
41 37    if (ygst_fields_set(w->sign, name, literal) != 0) w->rc = -1;
42 37}
43 
44 112static void put_str(body_writer *w, const char *name, const char *value) {
45 112    if (value && value[0]) put_member(w, name, value, 1);
46 112}
47 
48 56static void put_int(body_writer *w, const char *name, int has, long long value) {
49     char tmp[32];
50 56    if (!has) return;
51 9    snprintf(tmp, sizeof tmp, "%lld", value);
52 9    put_member(w, name, tmp, 0);
53 }
54 
55 20int ygst_tts_body(const ygst_tts_request *r, ygst_buf *json, ygst_fields *sign) {
56     body_writer w;
57 20    if (!r || !json || !sign) return -1;
58 17    if (ygst_is_blank(r->text)) return YGST_LOCAL_INVALID_ARGUMENT;
59 14    w.json = json;
60 14    w.sign = sign;
61 14    w.first = 1;
62 14    w.rc = 0;
63 14    ygst_buf_append_char(json, '{');
64 14    put_str(&w, "text", r->text);
65 14    put_str(&w, "language", r->language);
66 14    put_str(&w, "voice", r->voice);
67 14    put_str(&w, "format", r->format);
68 14    put_int(&w, "speed", r->has_speed, r->speed);
69 14    put_int(&w, "pitch", r->has_pitch, r->pitch);
70 14    put_int(&w, "volume", r->has_volume, r->volume);
71 14    put_int(&w, "sampleRate", r->has_sample_rate, r->sample_rate);
72 14    put_str(&w, "emotion", r->emotion);
73 14    put_str(&w, "style", r->style);
74 14    put_str(&w, "pinyin", r->pinyin);
75 14    put_str(&w, "genre", r->genre);
76 14    ygst_buf_append_char(json, '}');
77 14    ygst_buf_cstr(json);
78 14    return (w.rc != 0 || json->oom || sign->oom) ? -1 : 0;
79 }
80 
81 /* ------------------------------------------------------------------ TTS answer */
82 
83 7void ygst_tts_answer_init(ygst_tts_answer *a) {
84 7    if (!a) return;
85 6    memset(a, 0, sizeof *a);
86 6    ygst_buf_init(&a->audio_url);
87 6    ygst_buf_init(&a->format);
88 }
89 
90 7void ygst_tts_answer_free(ygst_tts_answer *a) {
91 7    if (!a) return;
92 6    ygst_buf_free(&a->audio_url);
93 6    ygst_buf_free(&a->format);
94 }
95 
96 /* 1 when obj[key] is a string, its text in out (replaced) */
97 135static int string_member(ygst_span obj, const char *key, ygst_buf *out) {
98     ygst_span v;
99 135    ygst_buf_reset(out);
100 135    if (ygst_json_member(obj, key, &v) != 1 || ygst_json_type(v) != 's') return 0;
101 116    return ygst_json_string(v, out) == 0;
102 }
103 
104 13int ygst_tts_parse(ygst_span data, ygst_tts_answer *a) {
105     ygst_span v;
106 13    if (!a) return -1;
107 12    ygst_buf_reset(&a->audio_url);
108 12    ygst_buf_reset(&a->format);
109 12    a->has_duration = 0;
110 12    a->duration = 0;
111 12    if (ygst_json_type(data) != 'o') return -1;
112 11    string_member(data, "audioUrl", &a->audio_url);
113 11    string_member(data, "format", &a->format);
114 11    if (ygst_json_member(data, "duration", &v) == 1) {
115 11        if (ygst_json_type(v) == 'n') {
116 1            a->has_duration = ygst_json_number(v, &a->duration) == 0;
117 10        } else if (ygst_json_type(v) == 's') {
118             /* the platform writes the seconds as text, "1.348": read it as a JSON number */
119             ygst_buf text;
120             ygst_span num;
121 9            ygst_buf_init(&text);
122 9            if (ygst_json_string(v, &text) == 0 && text.len) {
123 8                num.p = ygst_buf_cstr(&text);
124 8                num.n = text.len;
125 8                a->has_duration = ygst_json_type(num) == 'n' && ygst_json_number(num, &a->duration) == 0;
126             }
127 9            ygst_buf_free(&text);
128         }
129 11        if (!a->has_duration) a->duration = 0;
130     }
131 11    ygst_buf_cstr(&a->audio_url);
132 11    ygst_buf_cstr(&a->format);
133 11    return (a->audio_url.oom || a->format.oom) ? -1 : 0;
134 }
135 
136 27static int starts_with_ci(const char *s, const char *prefix) {
137 46    for (; *prefix; s++, prefix++) {
138 44        char c = *s;
139 44        if (c >= 'A' && c <= 'Z') c = (char)(c + ('a' - 'A'));
140 44        if (c != *prefix) return 0;
141     }
142 2    return 1;
143 }
144 
145 17int ygst_tts_absolute_url(const char *base, const char *audio_url, ygst_buf *out) {
146     size_t n;
147 17    if (!out) return -1;
148 16    if (audio_url && audio_url[0]) {
149 14        if (starts_with_ci(audio_url, "http://") || starts_with_ci(audio_url, "https://")) {
150 2            ygst_buf_append_str(out, audio_url);
151         } else {
152 12            if (!base) base = "";
153 12            n = strlen(base);
154 13            while (n > 0 && base[n - 1] == '/') n--;
155 12            ygst_buf_append(out, base, n);
156 12            if (strncmp(audio_url, "/audio/", 7) == 0) {
157 9                ygst_buf_append_str(out, "/tts"); /* CONTRACT 4.1: /audio/.. is served under /tts */
158 3            } else if (audio_url[0] != '/') {
159 1                ygst_buf_append_char(out, '/');
160             }
161 12            ygst_buf_append_str(out, audio_url);
162         }
163     }
164 16    ygst_buf_cstr(out);
165 16    return out->oom ? -1 : 0;
166 }
167 
168 /* ------------------------------------------------------------------ report request */
169 
170 24int ygst_report_path(const char *record_id, ygst_buf *out) {
171     ygst_buf id;
172     int rc;
173 24    if (!out) return -1;
174 23    if (ygst_is_blank(record_id)) return YGST_LOCAL_INVALID_ARGUMENT;
175 20    ygst_buf_init(&id);
176 20    ygst_buf_append_trimmed(&id, record_id);
177 20    ygst_buf_append_str(out, "api/v1/report/");
178 20    ygst_url_encode(out, ygst_buf_cstr(&id)); /* recordId is one path segment */
179 20    rc = (id.oom || out->oom) ? -1 : 0;
180 20    ygst_buf_free(&id);
181 20    return rc;
182 }
183 
184 32int ygst_api_url(const char *base, const char *path, ygst_buf *out) {
185     size_t n;
186 32    if (!base || !path || !out) return -1;
187 29    n = strlen(base);
188 31    while (n > 0 && base[n - 1] == '/') n--;
189 29    ygst_buf_append(out, base, n);
190 29    ygst_buf_append_char(out, '/');
191 30    while (*path == '/') path++;
192 29    ygst_buf_append_str(out, path);
193 29    ygst_buf_cstr(out);
194 29    return out->oom ? -1 : 0;
195 }
196 
197 /* ------------------------------------------------------------------ report answer */
198 
199 22void ygst_report_init(ygst_report *r) {
200 22    if (!r) return;
201 21    memset(r, 0, sizeof *r);
202 21    ygst_buf_init(&r->record_id);
203 21    ygst_buf_init(&r->report_status);
204 21    ygst_buf_init(&r->summary);
205 }
206 
207 22void ygst_report_free(ygst_report *r) {
208 22    if (!r) return;
209 21    ygst_buf_free(&r->record_id);
210 21    ygst_buf_free(&r->report_status);
211 21    ygst_buf_free(&r->summary);
212 }
213 
214 120static ygst_span object_member(ygst_span obj, const char *key) {
215     ygst_span v;
216 120    if (ygst_json_member(obj, key, &v) == 1 && ygst_json_type(v) == 'o') return v;
217 48    v.p = NULL;
218 48    v.n = 0;
219 48    return v;
220 }
221 
222 42int ygst_report_parse(ygst_span data, ygst_report *r) {
223     ygst_span v;
224 42    if (!r) return -1;
225 41    ygst_buf_reset(&r->record_id);
226 41    ygst_buf_reset(&r->report_status);
227 41    ygst_buf_reset(&r->summary);
228 41    r->has_summary = 0;
229 41    r->has_overall = 0;
230 41    r->overall = 0;
231 41    r->report.p = r->score.p = r->timing.p = r->suggestions.p = NULL;
232 41    r->report.n = r->score.n = r->timing.n = r->suggestions.n = 0;
233 41    if (ygst_json_type(data) != 'o') return -1;
234 40    string_member(data, "recordId", &r->record_id);
235 40    string_member(data, "reportStatus", &r->report_status);
236 40    r->report = object_member(data, "report");
237 40    r->score = object_member(data, "score");
238 40    r->timing = object_member(data, "timing");
239 40    if (r->report.p) {
240 33        r->has_summary = string_member(r->report, "summary", &r->summary);
241 33        if (ygst_json_member(r->report, "suggestions", &v) == 1 && ygst_json_type(v) == 'a') r->suggestions = v;
242     }
243 40    if (r->score.p && ygst_json_member(r->score, "overall", &v) == 1 && ygst_json_type(v) == 'n') {
244 24        r->has_overall = ygst_json_number(v, &r->overall) == 0;
245 24        if (!r->has_overall) r->overall = 0;
246     }
247 40    ygst_buf_cstr(&r->record_id);
248 40    ygst_buf_cstr(&r->report_status);
249 40    ygst_buf_cstr(&r->summary);
250 40    return (r->record_id.oom || r->report_status.oom || r->summary.oom) ? -1 : 0;
251 }
252 
253 40int ygst_report_next_suggestion(ygst_span suggestions, size_t *pos, ygst_buf *out) {
254     ygst_span item;
255 40    if (!pos || !out || ygst_json_type(suggestions) != 'a') return 0;
256 41    while (ygst_json_array_next(suggestions, pos, &item) == 1) {
257 26        if (ygst_json_type(item) != 's') continue;
258 22        ygst_buf_reset(out);
259 22        if (ygst_json_string(item, out) != 0) continue;
260 22        ygst_buf_cstr(out);
261 22        return 1;
262     }
263 15    return 0;
264 }
265 
266 121static int status_is(const ygst_report *r, const char *status) {
267 121    size_t n = strlen(status);
268 121    return r && r->report_status.len == n && memcmp(r->report_status.data, status, n) == 0;
269 }
270 
271 54int ygst_report_is_pending(const ygst_report *r) {
272 54    return status_is(r, "pending");
273 }
274 
275 43int ygst_report_is_partial(const ygst_report *r) {
276 43    return status_is(r, "partial");
277 }
278 
279 34int ygst_report_is_waiting(const ygst_report *r) {
280     /* 2.3.0: partial has the AI summary and suggestions, the dimension comments are still written */
281 34    return ygst_report_is_pending(r) || ygst_report_is_partial(r);
282 }
283 
284 24int ygst_report_is_final(const ygst_report *r) {
285 24    return status_is(r, "final");
286 }
287 
288 42long ygst_report_poll_delay(long long wait_ms, int64_t started_ms, int64_t now_ms, int pending) {
289 42    if (!pending || wait_ms <= 0) return -1;
290 26    if ((long long)(now_ms - started_ms) + YGST_REPORT_POLL_MS > wait_ms) return -1;
291 19    return YGST_REPORT_POLL_MS;
292 }
293 
294 43ygst_rp_action ygst_report_after_lookup(long long wait_ms, int64_t started_ms, int64_t now_ms, int ok, int pending,
295                                         int answered) {
296     ygst_rp_action a;
297 43    a.kind = YGST_RP_RETURN;
298 43    a.delay_ms = 0;
299 43    a.degraded = 0;
300 43    if (!ok) {
301         /* the first lookup reports its error; a later one leaves the answer already received */
302 11        if (answered) {
303 5            a.degraded = 1;
304         } else {
305 6            a.kind = YGST_RP_FAIL;
306         }
307 11        return a;
308     }
309 32    a.delay_ms = ygst_report_poll_delay(wait_ms, started_ms, now_ms, pending);
310 32    if (a.delay_ms >= 0) {
311 15        a.kind = YGST_RP_AGAIN;
312     } else {
313 17        a.delay_ms = 0;
314     }
315 32    return a;
316 }
317 
318 /* ------------------------------------------------------------------ log line */
319 
320 /* a value of the log line: whitespace and control bytes become "_", so one value stays one token */
321 50static void put_token_value(ygst_buf *b, const char *s) {
322 50    const unsigned char *p = (const unsigned char *)s;
323 50    if (!p || !*p) {
324 4        ygst_buf_append_char(b, '-');
325 4        return;
326     }
327 450    for (; *p; p++) ygst_buf_append_char(b, (char)((*p <= 0x20 || *p == 0x7F) ? '_' : *p));
328 }
329 
330 6int ygst_report_lookup_failed_line(const char *record_id, int code, int lookups, ygst_buf *out) {
331 6    if (!out) return -1;
332 5    ygst_buf_append_str(out, "getReport recordId=");
333 5    put_token_value(out, record_id);
334 5    ygst_buf_append_str(out, " lookup failed while waiting: code=");
335 5    ygst_buf_append_int(out, code);
336 5    ygst_buf_append_str(out, " lookups=");
337 5    ygst_buf_append_int(out, lookups);
338 5    ygst_buf_append_str(out, ", returning the latest answer");
339 5    ygst_buf_cstr(out);
340 5    return out->oom ? -1 : 0;
341 }
342 
343 38int ygst_api_log_line(const char *op, const char *record_id, const char *status, int code, int lookups,
344                       int attempts, long long ms, ygst_buf *out) {
345 38    if (!out) return -1;
346 37    ygst_buf_append_str(out, op ? op : "");
347 37    if (record_id) {
348 22        ygst_buf_append_str(out, " recordId=");
349 22        put_token_value(out, record_id);
350     }
351 37    if (code) {
352 14        ygst_buf_append_str(out, " status=error code=");
353 14        ygst_buf_append_int(out, code);
354     } else {
355 23        ygst_buf_append_str(out, " status=");
356 23        put_token_value(out, status);
357     }
358 37    if (lookups >= 0) {
359 22        ygst_buf_append_str(out, " lookups=");
360 22        ygst_buf_append_int(out, lookups);
361     }
362 37    ygst_buf_append_str(out, " attempts=");
363 37    ygst_buf_append_int(out, attempts);
364 37    ygst_buf_append_str(out, " ms=");
365 37    ygst_buf_append_int(out, ms);
366 37    ygst_buf_cstr(out);
367 37    return out->oom ? -1 : 0;
368 }

Sources/STKouyuEngine/core/ygst_audio.c 99.40%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * WAV header writer and reader, local audio checks, a frame energy VAD with an adaptive noise
6  * floor, the 0..100 sound intensity scale and the countdown values of the tick callback.
7  */
8 #include "ygst_core.h"
9 
10 #include <math.h>
11 #include <string.h>
12 
13 80static void put_le32(unsigned char *p, uint32_t v) {
14 80    p[0] = (unsigned char)v;
15 80    p[1] = (unsigned char)(v >> 8);
16 80    p[2] = (unsigned char)(v >> 16);
17 80    p[3] = (unsigned char)(v >> 24);
18 80}
19 
20 64static void put_le16(unsigned char *p, uint16_t v) {
21 64    p[0] = (unsigned char)v;
22 64    p[1] = (unsigned char)(v >> 8);
23 64}
24 
25 473static uint32_t le32(const unsigned char *p) {
26 473    return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
27 }
28 
29 360static uint16_t le16(const unsigned char *p) {
30 360    return (uint16_t)(p[0] | (p[1] << 8));
31 }
32 
33 16void ygst_wav_header(unsigned char out[44], uint32_t data_len, int sample_rate, int channels, int bits) {
34 16    uint32_t block = (uint32_t)(channels * (bits / 8));
35 16    memcpy(out, "RIFF", 4);
36 16    put_le32(out + 4, data_len > 0xFFFFFFFFu - 36u ? 0xFFFFFFFFu : data_len + 36u);
37 16    memcpy(out + 8, "WAVE", 4);
38 16    memcpy(out + 12, "fmt ", 4);
39 16    put_le32(out + 16, 16);
40 16    put_le16(out + 20, 1);
41 16    put_le16(out + 22, (uint16_t)channels);
42 16    put_le32(out + 24, (uint32_t)sample_rate);
43 16    put_le32(out + 28, (uint32_t)sample_rate * block);
44 16    put_le16(out + 32, (uint16_t)block);
45 16    put_le16(out + 34, (uint16_t)bits);
46 16    memcpy(out + 36, "data", 4);
47 16    put_le32(out + 40, data_len);
48 16}
49 
50 129int ygst_is_riff_wave(const unsigned char *p, size_t n) {
51 129    return n >= 12 && memcmp(p, "RIFF", 4) == 0 && memcmp(p + 8, "WAVE", 4) == 0;
52 }
53 
54 127int ygst_wav_parse(const unsigned char *p, size_t n, ygst_wav_info *info) {
55 127    size_t pos = 12;
56 127    int have_fmt = 0;
57 127    memset(info, 0, sizeof *info);
58 127    if (!ygst_is_riff_wave(p, n)) return -1;
59 353    while (pos + 8 <= n) {
60 353        uint32_t size = le32(p + pos + 4);
61 353        const unsigned char *id = p + pos;
62 353        size_t body = pos + 8;
63 353        if (memcmp(id, "fmt ", 4) == 0) {
64 121            if (size < 16 || body + 16 > n) return -1;
65 120            info->format = le16(p + body);
66 120            info->channels = le16(p + body + 2);
67 120            info->sample_rate = (int)le32(p + body + 4);
68 120            info->bits = le16(p + body + 14);
69 120            have_fmt = 1;
70 232        } else if (memcmp(id, "data", 4) == 0) {
71 121            if (!have_fmt) return -1;
72 120            info->data_offset = body;
73             /* Streaming writers leave 0 or 0xFFFFFFFF; clamp to what the file holds. */
74 120            info->data_len = (size == 0 || size > n - body) ? n - body : size;
75 120            return 0;
76         }
77 231        if (size > n - body) return -1;
78 230        pos = body + size + (size & 1u);
79     }
80 0    return -1;
81 }
82 
83 83long ygst_pcm_duration_ms(size_t bytes, int sample_rate, int channels, int bits) {
84     double per_second;
85 83    if (sample_rate <= 0 || channels <= 0 || bits <= 0) return -1;
86 81    per_second = (double)sample_rate * channels * (bits / 8.0);
87 81    if (per_second <= 0) return -1;
88 81    return (long)((double)bytes * 1000.0 / per_second);
89 }
90 
91 /* Exact duration test without rounding: bytes * 1000 compared with ms * bytes per second. */
92 71static int pcm_longer_than(size_t bytes, int sample_rate, int channels, int bits, long ms) {
93 71    double per_second = (double)sample_rate * channels * (bits / 8.0);
94 71    if (per_second <= 0) return 0;
95 70    return (double)bytes * 1000.0 > (double)ms * per_second;
96 }
97 
98 28int ygst_audio_check_pcm(size_t pcm_len, int sample_rate, int channels, int bits) {
99     long ms;
100 28    if (pcm_len == 0) return YGST_ERRID_AUDIO_EMPTY;
101 27    if (pcm_len > YGST_MAX_AUDIO_BYTES - 44u) return YGST_ERRID_AUDIO_TOO_LARGE;
102 26    ms = ygst_pcm_duration_ms(pcm_len, sample_rate, channels, bits);
103 26    if (ms >= 0 && ms < YGST_MIN_AUDIO_MS) return YGST_ERRID_AUDIO_TOO_SHORT;
104 24    if (pcm_longer_than(pcm_len, sample_rate, channels, bits, YGST_MAX_AUDIO_MS)) return YGST_ERRID_AUDIO_TOO_LARGE;
105 22    return 0;
106 }
107 
108 56int ygst_audio_check_upload(const unsigned char *data, size_t len) {
109     ygst_wav_info info;
110 56    if (!data || len == 0) return YGST_ERRID_AUDIO_EMPTY;
111 54    if (len > YGST_MAX_AUDIO_BYTES) return YGST_ERRID_AUDIO_TOO_LARGE;
112 53    if (ygst_wav_parse(data, len, &info) == 0) {
113         long ms;
114 51        if (info.data_len == 0) return YGST_ERRID_AUDIO_EMPTY;
115 49        if (info.format == 1 || info.format == 3 || info.format == 0xFFFE) {
116 49            ms = ygst_pcm_duration_ms(info.data_len, info.sample_rate, info.channels, info.bits);
117 49            if (ms >= 0 && ms < YGST_MIN_AUDIO_MS) return YGST_ERRID_AUDIO_TOO_SHORT;
118 47            if (pcm_longer_than(info.data_len, info.sample_rate, info.channels, info.bits, YGST_MAX_AUDIO_MS))
119 1                return YGST_ERRID_AUDIO_TOO_LARGE;
120         }
121     }
122 48    return 0;
123 }
124 
125 /* ------------------------------------------------------------------ VAD */
126 
127 #define VAD_ABS_DB (-42.0)      /* a frame quieter than this is never speech */
128 #define VAD_OVER_NOISE_DB 10.0 /* speech must also exceed the noise floor by this much */
129 #define VAD_START_FRAMES 3     /* 30 ms of speech starts an utterance */
130 #define VAD_NOISE_MIN_DB (-90.0)
131 #define VAD_NOISE_MAX_DB (-35.0)
132 
133 10void ygst_vad_init(ygst_vad *v, int sample_rate, double seek_10ms, double ref_length_10ms) {
134 10    memset(v, 0, sizeof *v);
135 10    v->sample_rate = sample_rate > 0 ? sample_rate : 16000;
136 10    v->frame_samples = v->sample_rate / 100;
137 10    if (v->frame_samples <= 0) v->frame_samples = 1;
138 10    v->seek_ms = (int)((seek_10ms > 0 ? seek_10ms : 60) * 10.0);
139 10    v->ref_length_ms = ref_length_10ms > 0 ? (int)(ref_length_10ms * 10.0) : 0;
140 10    v->noise_db = -60.0;
141 10}
142 
143 1341int ygst_sound_intensity_db(double db) {
144 1341    double x = (db + 60.0) * 100.0 / 60.0;
145 1341    if (x < 0) x = 0;
146 1341    if (x > 100) x = 100;
147 1341    return (int)(x + 0.5);
148 }
149 
150 1338static double rms_db(double sumsq, int n) {
151     double rms;
152 1338    if (n <= 0) return -120.0;
153 1337    rms = sqrt(sumsq / n);
154 1337    if (rms < 1.0) return -120.0;
155 651    return 20.0 * log10(rms / 32768.0);
156 }
157 
158 3int ygst_sound_intensity(const int16_t *pcm, size_t samples) {
159 3    double sumsq = 0;
160     size_t i;
161 3203    for (i = 0; i < samples; i++) sumsq += (double)pcm[i] * pcm[i];
162 3    return ygst_sound_intensity_db(rms_db(sumsq, (int)samples));
163 }
164 
165 1335static void vad_frame(ygst_vad *v, double db) {
166 1335    double threshold = v->noise_db + VAD_OVER_NOISE_DB;
167     int speech;
168 1335    if (threshold < VAD_ABS_DB) threshold = VAD_ABS_DB;
169 1335    speech = db > threshold;
170 1335    v->intensity = ygst_sound_intensity_db(db);
171 1335    if (!speech) {
172 927        v->noise_db = v->noise_db * 0.95 + db * 0.05;
173 927        if (v->noise_db < VAD_NOISE_MIN_DB) v->noise_db = VAD_NOISE_MIN_DB;
174 927        if (v->noise_db > VAD_NOISE_MAX_DB) v->noise_db = VAD_NOISE_MAX_DB;
175     }
176 1335    v->speech_run = speech ? v->speech_run + 1 : 0;
177 1335    if (v->status != 1) {
178 644        if (v->speech_run >= VAD_START_FRAMES) {
179 7            v->status = 1;
180 7            v->speaking_ms = 0;
181 7            v->silence_ms = 0;
182         }
183 644        return;
184     }
185 691    v->speaking_ms += 10;
186 691    v->silence_ms = speech ? 0 : v->silence_ms + 10;
187 691    if (v->silence_ms >= v->seek_ms && v->speaking_ms >= v->ref_length_ms) {
188 4        v->status = 2;
189 4        v->ended_edge = 1;
190     }
191 }
192 
193 757int ygst_vad_process(ygst_vad *v, const int16_t *pcm, size_t samples) {
194     size_t i;
195 757    v->ended_edge = 0;
196 214357    for (i = 0; i < samples; i++) {
197 213600        v->acc_sumsq += (double)pcm[i] * pcm[i];
198 213600        v->acc_n++;
199 213600        if (v->acc_n >= v->frame_samples) {
200 1335            vad_frame(v, rms_db(v->acc_sumsq, v->acc_n));
201 1335            v->acc_sumsq = 0;
202 1335            v->acc_n = 0;
203         }
204     }
205 757    return v->status;
206 }
207 
208 131int ygst_vad_process_bytes(ygst_vad *v, const unsigned char *bytes, size_t n) {
209     int16_t chunk[256];
210 131    size_t used = 0;
211 131    int ended = 0;
212 131    size_t i = 0;
213 131    if (v->has_carry && n > 0) {
214 20        chunk[used++] = (int16_t)(uint16_t)(v->carry | (bytes[0] << 8));
215 20        v->has_carry = 0;
216 20        i = 1;
217     }
218 150831    for (; i + 1 < n; i += 2) {
219 150700        chunk[used++] = (int16_t)(uint16_t)(bytes[i] | (bytes[i + 1] << 8));
220 150700        if (used == sizeof chunk / sizeof chunk[0]) {
221 542            ygst_vad_process(v, chunk, used);
222 542            ended |= v->ended_edge;
223 542            used = 0;
224         }
225     }
226 131    if (i < n) {
227 20        v->carry = bytes[i];
228 20        v->has_carry = 1;
229     }
230 131    if (used) {
231 130        ygst_vad_process(v, chunk, used);
232 130        ended |= v->ended_edge;
233     }
234 131    v->ended_edge = ended;
235 131    return v->status;
236 }
237 
238 4void ygst_tick_compute(double duration_ms, double elapsed_ms, double *millis_left, double *percent_left) {
239     double left;
240 4    if (duration_ms <= 0) {
241 1        *millis_left = 0;
242 1        *percent_left = 0;
243 1        return;
244     }
245 3    left = duration_ms - (elapsed_ms > 0 ? elapsed_ms : 0);
246 3    if (left < 0) left = 0;
247 3    *millis_left = left;
248 3    *percent_left = left * 100.0 / duration_ms;
249 }

Sources/STKouyuEngine/core/ygst_buf.c 94.17%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Growable byte buffer and small string helpers.
6  */
7 #include "ygst_core.h"
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 7320void ygst_buf_init(ygst_buf *b) {
14 7320    b->data = NULL;
15 7320    b->len = 0;
16 7320    b->cap = 0;
17 7320    b->oom = 0;
18 7320}
19 
20 3624void ygst_buf_free(ygst_buf *b) {
21 3624    free(b->data);
22 3624    ygst_buf_init(b);
23 3624}
24 
25 4880void ygst_buf_reset(ygst_buf *b) {
26 4880    b->len = 0;
27 4880    b->oom = 0;
28 4880    if (b->data) b->data[0] = 0;
29 4880}
30 
31 36449int ygst_buf_reserve(ygst_buf *b, size_t extra) {
32     size_t need;
33     size_t cap;
34     unsigned char *p;
35 36449    if (b->oom) return -1;
36 36449    if (extra > (size_t)-1 - b->len - 1) {
37 0        b->oom = 1;
38 0        return -1;
39     }
40 36449    need = b->len + extra + 1; /* room for the terminating NUL */
41 36449    if (need <= b->cap) return 0;
42 4189    cap = b->cap ? b->cap : 64;
43 8272    while (cap < need) {
44 4083        if (cap > ((size_t)-1) / 2) {
45 0            cap = need;
46 0            break;
47         }
48 4083        cap *= 2;
49     }
50 4189    p = (unsigned char *)realloc(b->data, cap);
51 4189    if (!p) {
52 0        b->oom = 1;
53 0        return -1;
54     }
55 4189    b->data = p;
56 4189    b->cap = cap;
57 4189    return 0;
58 }
59 
60 36232int ygst_buf_append(ygst_buf *b, const void *p, size_t n) {
61 36232    if (n == 0) return b->oom ? -1 : 0;
62 36213    if (ygst_buf_reserve(b, n) != 0) return -1;
63 36213    memcpy(b->data + b->len, p, n);
64 36213    b->len += n;
65 36213    b->data[b->len] = 0;
66 36213    return 0;
67 }
68 
69 8888int ygst_buf_append_str(ygst_buf *b, const char *s) {
70 8888    return ygst_buf_append(b, s ? s : "", s ? strlen(s) : 0);
71 }
72 
73 16675int ygst_buf_append_char(ygst_buf *b, char c) {
74 16675    return ygst_buf_append(b, &c, 1);
75 }
76 
77 1542int ygst_buf_append_int(ygst_buf *b, long long v) {
78     char tmp[32];
79 1542    int n = snprintf(tmp, sizeof tmp, "%lld", v);
80 1542    if (n < 0) return -1;
81 1542    return ygst_buf_append(b, tmp, (size_t)n);
82 }
83 
84 11312const char *ygst_buf_cstr(ygst_buf *b) {
85 11312    if (!b->data) {
86 236        if (ygst_buf_reserve(b, 0) != 0) return "";
87 236        b->data[0] = 0;
88     }
89 11312    return (const char *)b->data;
90 }
91 
92 1230char *ygst_strdup(const char *s) {
93     size_t n;
94     char *p;
95 1230    if (!s) return NULL;
96 1229    n = strlen(s);
97 1229    p = (char *)malloc(n + 1);
98 1229    if (!p) return NULL;
99 1229    memcpy(p, s, n + 1);
100 1229    return p;
101 }
102 
103 1044static int lower_ascii(int c) {
104 1044    return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
105 }
106 
107 251int ygst_strcaseeq(const char *a, const char *b) {
108 251    if (!a || !b) return 0;
109 649    while (*a && *b) {
110 522        if (lower_ascii((unsigned char)*a) != lower_ascii((unsigned char)*b)) return 0;
111 399        a++;
112 399        b++;
113     }
114 127    return *a == 0 && *b == 0;
115 }
116 
117 13int ygst_has_suffix_ci(const char *s, const char *suffix) {
118     size_t n;
119     size_t m;
120 13    if (!s || !suffix) return 0;
121 12    n = strlen(s);
122 12    m = strlen(suffix);
123 12    if (m > n) return 0;
124 11    return ygst_strcaseeq(s + (n - m), suffix);
125 }
126 
127 884static int is_space(unsigned char c) {
128 884    return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
129 }
130 
131 178size_t ygst_trim_copy(const char *s, char *out, size_t cap) {
132     const char *e;
133     size_t n;
134 178    if (!out || cap == 0) return 0;
135 178    out[0] = 0;
136 178    if (!s) return 0;
137 188    while (*s && is_space((unsigned char)*s)) s++;
138 176    e = s + strlen(s);
139 186    while (e > s && is_space((unsigned char)e[-1])) e--;
140 176    n = (size_t)(e - s);
141 176    if (n >= cap) n = cap - 1;
142 176    memcpy(out, s, n);
143 176    out[n] = 0;
144 176    return n;
145 }
146 
147 358int ygst_is_blank(const char *s) {
148 358    if (!s) return 1;
149 401    while (*s) {
150 365        if (!is_space((unsigned char)*s)) return 0;
151 60        s++;
152     }
153 36    return 1;
154 }
155 
156 60int ygst_buf_append_trimmed(ygst_buf *b, const char *s) {
157     const char *e;
158 60    if (!s) return 0;
159 76    while (*s && is_space((unsigned char)*s)) s++;
160 60    e = s + strlen(s);
161 75    while (e > s && is_space((unsigned char)e[-1])) e--;
162 60    return ygst_buf_append(b, s, (size_t)(e - s));
163 }

Sources/STKouyuEngine/core/ygst_crypto.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * SHA-256 (FIPS 180-4), HMAC-SHA256 (RFC 2104), Base64 (RFC 4648). Written from the
6  * specifications so that the core has no dependency on CommonCrypto or OpenSSL.
7  */
8 #include "ygst_core.h"
9 
10 #include <string.h>
11 
12 static const uint32_t K[64] = {
13     0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
14     0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
15     0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
16     0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
17     0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
18     0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
19     0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
20     0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u};
21 
22 #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
23 
24 16239static void sha256_block(ygst_sha256_ctx *c, const unsigned char *p) {
25     uint32_t w[64];
26     uint32_t a, b, cc, d, e, f, g, h, t1, t2;
27     int i;
28 276063    for (i = 0; i < 16; i++) {
29 259824        w[i] = ((uint32_t)p[i * 4] << 24) | ((uint32_t)p[i * 4 + 1] << 16) | ((uint32_t)p[i * 4 + 2] << 8) |
30 259824               (uint32_t)p[i * 4 + 3];
31     }
32 795711    for (i = 16; i < 64; i++) {
33 779472        uint32_t s0 = ROTR(w[i - 15], 7) ^ ROTR(w[i - 15], 18) ^ (w[i - 15] >> 3);
34 779472        uint32_t s1 = ROTR(w[i - 2], 17) ^ ROTR(w[i - 2], 19) ^ (w[i - 2] >> 10);
35 779472        w[i] = w[i - 16] + s0 + w[i - 7] + s1;
36     }
37 16239    a = c->h[0];
38 16239    b = c->h[1];
39 16239    cc = c->h[2];
40 16239    d = c->h[3];
41 16239    e = c->h[4];
42 16239    f = c->h[5];
43 16239    g = c->h[6];
44 16239    h = c->h[7];
45 1055535    for (i = 0; i < 64; i++) {
46 1039296        uint32_t S1 = ROTR(e, 6) ^ ROTR(e, 11) ^ ROTR(e, 25);
47 1039296        uint32_t ch = (e & f) ^ ((~e) & g);
48 1039296        uint32_t S0 = ROTR(a, 2) ^ ROTR(a, 13) ^ ROTR(a, 22);
49 1039296        uint32_t maj = (a & b) ^ (a & cc) ^ (b & cc);
50 1039296        t1 = h + S1 + ch + K[i] + w[i];
51 1039296        t2 = S0 + maj;
52 1039296        h = g;
53 1039296        g = f;
54 1039296        f = e;
55 1039296        e = d + t1;
56 1039296        d = cc;
57 1039296        cc = b;
58 1039296        b = a;
59 1039296        a = t1 + t2;
60     }
61 16239    c->h[0] += a;
62 16239    c->h[1] += b;
63 16239    c->h[2] += cc;
64 16239    c->h[3] += d;
65 16239    c->h[4] += e;
66 16239    c->h[5] += f;
67 16239    c->h[6] += g;
68 16239    c->h[7] += h;
69 16239}
70 
71 267void ygst_sha256_init(ygst_sha256_ctx *c) {
72 267    c->h[0] = 0x6a09e667u;
73 267    c->h[1] = 0xbb67ae85u;
74 267    c->h[2] = 0x3c6ef372u;
75 267    c->h[3] = 0xa54ff53au;
76 267    c->h[4] = 0x510e527fu;
77 267    c->h[5] = 0x9b05688cu;
78 267    c->h[6] = 0x1f83d9abu;
79 267    c->h[7] = 0x5be0cd19u;
80 267    c->total = 0;
81 267    c->used = 0;
82 267}
83 
84 1519void ygst_sha256_update(ygst_sha256_ctx *c, const void *data, size_t n) {
85 1519    const unsigned char *p = (const unsigned char *)data;
86 1519    c->total += (uint64_t)n;
87 1519    if (c->used) {
88 1008        size_t take = 64 - c->used;
89 1008        if (take > n) take = n;
90 1008        memcpy(c->block + c->used, p, take);
91 1008        c->used += take;
92 1008        p += take;
93 1008        n -= take;
94 1008        if (c->used < 64) return;
95 996        sha256_block(c, c->block);
96 996        c->used = 0;
97     }
98 16460    while (n >= 64) {
99 14953        sha256_block(c, p);
100 14953        p += 64;
101 14953        n -= 64;
102     }
103 1507    if (n) {
104 1239        memcpy(c->block, p, n);
105 1239        c->used = n;
106     }
107 }
108 
109 267void ygst_sha256_final(ygst_sha256_ctx *c, unsigned char out[32]) {
110 267    uint64_t bits = c->total * 8u;
111     int i;
112 267    c->block[c->used++] = 0x80;
113 267    if (c->used > 56) {
114 23        memset(c->block + c->used, 0, 64 - c->used);
115 23        sha256_block(c, c->block);
116 23        c->used = 0;
117     }
118 267    memset(c->block + c->used, 0, 56 - c->used);
119 2403    for (i = 0; i < 8; i++) c->block[56 + i] = (unsigned char)(bits >> (56 - 8 * i));
120 267    sha256_block(c, c->block);
121 2403    for (i = 0; i < 8; i++) {
122 2136        out[i * 4] = (unsigned char)(c->h[i] >> 24);
123 2136        out[i * 4 + 1] = (unsigned char)(c->h[i] >> 16);
124 2136        out[i * 4 + 2] = (unsigned char)(c->h[i] >> 8);
125 2136        out[i * 4 + 3] = (unsigned char)c->h[i];
126     }
127 267}
128 
129 5void ygst_sha256(const void *data, size_t n, unsigned char out[32]) {
130     ygst_sha256_ctx c;
131 5    ygst_sha256_init(&c);
132 5    ygst_sha256_update(&c, data, n);
133 5    ygst_sha256_final(&c, out);
134 5}
135 
136 114void ygst_hmac_sha256(const void *key, size_t key_len, const void *msg, size_t msg_len, unsigned char out[32]) {
137     unsigned char k[64];
138     unsigned char pad[64];
139     unsigned char inner[32];
140     ygst_sha256_ctx c;
141     int i;
142 114    memset(k, 0, sizeof k);
143 114    if (key_len > 64) {
144 1        ygst_sha256(key, key_len, k);
145 113    } else if (key_len) {
146 113        memcpy(k, key, key_len);
147     }
148 7410    for (i = 0; i < 64; i++) pad[i] = (unsigned char)(k[i] ^ 0x36);
149 114    ygst_sha256_init(&c);
150 114    ygst_sha256_update(&c, pad, 64);
151 114    ygst_sha256_update(&c, msg, msg_len);
152 114    ygst_sha256_final(&c, inner);
153 7410    for (i = 0; i < 64; i++) pad[i] = (unsigned char)(k[i] ^ 0x5c);
154 114    ygst_sha256_init(&c);
155 114    ygst_sha256_update(&c, pad, 64);
156 114    ygst_sha256_update(&c, inner, 32);
157 114    ygst_sha256_final(&c, out);
158 114}
159 
160 static const char B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
161 
162 143size_t ygst_base64_encode(const unsigned char *in, size_t n, char *out, size_t cap) {
163 143    size_t need = ((n + 2) / 3) * 4;
164     size_t i;
165 143    size_t o = 0;
166 143    if (!out || cap < need + 1) return 0;
167 1371    for (i = 0; i + 2 < n; i += 3) {
168 1230        uint32_t v = ((uint32_t)in[i] << 16) | ((uint32_t)in[i + 1] << 8) | (uint32_t)in[i + 2];
169 1230        out[o++] = B64[(v >> 18) & 63];
170 1230        out[o++] = B64[(v >> 12) & 63];
171 1230        out[o++] = B64[(v >> 6) & 63];
172 1230        out[o++] = B64[v & 63];
173     }
174 141    if (n - i == 1) {
175 25        uint32_t v = (uint32_t)in[i] << 16;
176 25        out[o++] = B64[(v >> 18) & 63];
177 25        out[o++] = B64[(v >> 12) & 63];
178 25        out[o++] = '=';
179 25        out[o++] = '=';
180 116    } else if (n - i == 2) {
181 113        uint32_t v = ((uint32_t)in[i] << 16) | ((uint32_t)in[i + 1] << 8);
182 113        out[o++] = B64[(v >> 18) & 63];
183 113        out[o++] = B64[(v >> 12) & 63];
184 113        out[o++] = B64[(v >> 6) & 63];
185 113        out[o++] = '=';
186     }
187 141    out[o] = 0;
188 141    return o;
189 }
190 
191 265void ygst_hex_lower(const unsigned char *in, size_t n, char *out) {
192     static const char H[] = "0123456789abcdef";
193     size_t i;
194 3577    for (i = 0; i < n; i++) {
195 3312        out[i * 2] = H[in[i] >> 4];
196 3312        out[i * 2 + 1] = H[in[i] & 15];
197     }
198 265    out[n * 2] = 0;
199 265}

Sources/STKouyuEngine/core/ygst_envelope.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Shengtong style result, error and reading progress JSON (DESIGN 6.2, 14.3), the dtLastResponse
6  * clock text, the request params echo, base URL resolution and small identifiers.
7  */
8 #include "ygst_core.h"
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 /* civil_from_days (H. Hinnant) */
14 110static void civil_from_days(long long z, long long *y, unsigned *m, unsigned *d) {
15     long long era;
16     unsigned doe, yoe, doy, mp;
17 110    z += 719468;
18 110    era = (z >= 0 ? z : z - 146096) / 146097;
19 110    doe = (unsigned)(z - era * 146097);
20 110    yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
21 110    doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
22 110    mp = (5 * doy + 2) / 153;
23 110    *d = doy - (153 * mp + 2) / 5 + 1;
24 110    *m = mp < 10 ? mp + 3 : mp - 9;
25 110    *y = (long long)yoe + era * 400 + (*m <= 2);
26 110}
27 
28 110int ygst_format_dt(int64_t epoch_ms, int tz_offset_min, char out[32]) {
29 110    long long ms = (long long)epoch_ms + (long long)tz_offset_min * 60000LL;
30 110    long long days = ms >= 0 ? ms / 86400000LL : -((-ms + 86399999LL) / 86400000LL);
31 110    long long rem = ms - days * 86400000LL;
32     long long y;
33     unsigned m, d, hh, mi, ss, mss;
34 110    civil_from_days(days, &y, &m, &d);
35 110    if (y < 0) y = 0;
36 110    if (y > 9999) y = 9999;
37 110    hh = (unsigned)(rem / 3600000LL) % 24u;
38 110    mi = (unsigned)(rem / 60000LL) % 60u;
39 110    ss = (unsigned)(rem / 1000LL) % 60u;
40 110    mss = (unsigned)rem % 1000u;
41 110    snprintf(out, 32, "%04u-%02u-%02u %02u:%02u:%02u:%03u", (unsigned)y % 10000u, m % 13u, d % 32u, hh, mi, ss,
42              mss);
43 110    return 0;
44 }
45 
46 68int ygst_align_result(ygst_span result, ygst_buf *out) {
47     ygst_span sentences, sent, details, d, scores, v, so, sp;
48 68    size_t spos = 0;
49 68    size_t copied = 0; /* bytes of result already copied to out */
50 68    int added = 0;
51 68    if (!result.p) return -1;
52 122    if (ygst_json_type(result) != 'o' || ygst_json_member(result, "sentences", &sentences) != 1 ||
53 55        ygst_json_type(sentences) != 'a') {
54 13        return ygst_buf_append(out, result.p, result.n) == 0 ? 0 : -1;
55     }
56 113    while (ygst_json_array_next(sentences, &spos, &sent) == 1) {
57 59        size_t dpos = 0;
58 113        if (ygst_json_type(sent) != 'o' || ygst_json_member(sent, "details", &details) != 1 ||
59 54            ygst_json_type(details) != 'a') {
60 6            continue;
61         }
62 357        while (ygst_json_array_next(details, &dpos, &d) == 1) {
63             int add_overall, add_pron;
64             size_t close;
65 304            if (ygst_json_type(d) != 'o' || ygst_json_member(d, "overall", &v) == 1) continue;
66 302            if (ygst_json_member(d, "scores", &scores) != 1 || ygst_json_type(scores) != 'o') continue;
67 300            add_overall = ygst_json_member(scores, "overall", &so) == 1;
68 300            add_pron = ygst_json_member(d, "pronunciation", &v) != 1 && ygst_json_member(scores, "pronunciation", &sp) == 1;
69 300            if (!add_overall && !add_pron) continue;
70 299            close = (size_t)(d.p + d.n - 1 - result.p); /* the closing brace of the item */
71 299            ygst_buf_append(out, result.p + copied, close - copied);
72 299            if (add_overall) {
73 299                ygst_buf_append_str(out, ",\"overall\":");
74 299                ygst_buf_append(out, so.p, so.n);
75 299                added++;
76             }
77 299            if (add_pron) {
78 296                ygst_buf_append_str(out, ",\"pronunciation\":");
79 296                ygst_buf_append(out, sp.p, sp.n);
80 296                added++;
81             }
82 299            copied = close;
83         }
84     }
85 54    ygst_buf_append(out, result.p + copied, result.n - copied);
86 54    return out->oom ? -1 : added;
87 }
88 
89 1125static void key(ygst_buf *out, const char *k, int first) {
90 1125    if (!first) ygst_buf_append_char(out, ',');
91 1125    ygst_json_escape(out, k);
92 1125    ygst_buf_append_char(out, ':');
93 1125}
94 
95 58int ygst_envelope_result(ygst_buf *out, const char *token_id, const char *record_id_json, const char *app_key,
96                          const char *user_id, const char *ref_text, const char *dt_last_response,
97                          ygst_span result_raw, const char *params_json, const char *audio_url) {
98 58    if (!result_raw.p || result_raw.n == 0) return -1;
99 57    ygst_buf_append_char(out, '{');
100 57    key(out, "tokenId", 1);
101 57    ygst_json_escape(out, token_id);
102 57    if (record_id_json && record_id_json[0]) {
103 56        key(out, "recordId", 0);
104 56        ygst_buf_append_str(out, record_id_json);
105     }
106 57    key(out, "applicationId", 0);
107 57    ygst_json_escape(out, app_key);
108 57    key(out, "userId", 0);
109 57    ygst_json_escape(out, user_id);
110 57    key(out, "refText", 0);
111 57    ygst_json_escape(out, ref_text);
112 57    key(out, "eof", 0);
113 57    ygst_buf_append_char(out, '1');
114 57    key(out, "dtLastResponse", 0);
115 57    ygst_json_escape(out, dt_last_response);
116 57    key(out, "result", 0);
117 57    if (ygst_align_result(result_raw, out) < 0) return -1;
118 57    if (params_json && params_json[0]) {
119 3        key(out, "params", 0);
120 3        ygst_buf_append_str(out, params_json);
121     }
122 57    if (audio_url && audio_url[0]) {
123 3        key(out, "audioUrl", 0);
124 3        ygst_json_escape(out, audio_url);
125     }
126 57    ygst_buf_append_char(out, '}');
127 57    ygst_buf_cstr(out);
128 57    return out->oom ? -1 : 0;
129 }
130 
131 28int ygst_envelope_error(ygst_buf *out, const char *token_id, int err_id, const char *message,
132                         const char *app_key) {
133 28    ygst_buf_append_char(out, '{');
134 28    key(out, "tokenId", 1);
135 28    ygst_json_escape(out, token_id);
136 28    key(out, "errId", 0);
137 28    ygst_buf_append_int(out, err_id);
138 28    key(out, "error", 0);
139 28    ygst_json_escape(out, message);
140 28    key(out, "eof", 0);
141 28    ygst_buf_append_char(out, '1');
142 28    key(out, "applicationId", 0);
143 28    ygst_json_escape(out, app_key);
144 28    ygst_buf_append_char(out, '}');
145 28    ygst_buf_cstr(out);
146 28    return out->oom ? -1 : 0;
147 }
148 
149 /* The progress result as the platform sent it, without the first skip items of "words". */
150 60static int append_progress_result(ygst_buf *out, ygst_span result, int skip) {
151     ygst_span words, item;
152 60    size_t pos = 0;
153 60    int i = 0;
154 60    int first = 1;
155 62    if (skip <= 0 || ygst_json_type(result) != 'o' || ygst_json_member(result, "words", &words) != 1 ||
156 2        ygst_json_type(words) != 'a') {
157 58        return ygst_buf_append(out, result.p, result.n);
158     }
159 2    ygst_buf_append(out, result.p, (size_t)(words.p - result.p));
160 2    ygst_buf_append_char(out, '[');
161 8    while (ygst_json_array_next(words, &pos, &item) == 1) {
162 6        if (i++ < skip) continue;
163 1        if (!first) ygst_buf_append_char(out, ',');
164 1        first = 0;
165 1        ygst_buf_append(out, item.p, item.n);
166     }
167 2    ygst_buf_append_char(out, ']');
168 2    ygst_buf_append(out, words.p + words.n, (size_t)(result.p + result.n - (words.p + words.n)));
169 2    return out->oom ? -1 : 0;
170 }
171 
172 62int ygst_envelope_progress(ygst_buf *out, const char *token_id, const char *app_key, const char *user_id,
173                            const char *ref_text, const char *dt_last_response, ygst_span result_raw,
174                            int skip_words) {
175 62    if (!result_raw.p || result_raw.n == 0) return -1;
176 60    ygst_buf_append_char(out, '{');
177 60    key(out, "tokenId", 1);
178 60    ygst_json_escape(out, token_id);
179     /* Shengtong's intermediate results carry recordId; the platform names the record only in the final */
180 60    key(out, "recordId", 0);
181 60    ygst_buf_append_str(out, "\"\"");
182 60    key(out, "applicationId", 0);
183 60    ygst_json_escape(out, app_key);
184 60    key(out, "userId", 0);
185 60    ygst_json_escape(out, user_id);
186 60    key(out, "refText", 0);
187 60    ygst_json_escape(out, ref_text);
188 60    key(out, "eof", 0);
189 60    ygst_buf_append_char(out, '0');
190 60    key(out, "dtLastResponse", 0);
191 60    ygst_json_escape(out, dt_last_response);
192 60    key(out, "result", 0);
193 60    if (append_progress_result(out, result_raw, skip_words) != 0) return -1;
194 60    ygst_buf_append_char(out, '}');
195 60    ygst_buf_cstr(out);
196 60    return out->oom ? -1 : 0;
197 }
198 
199 4int ygst_params_json(ygst_buf *out, const char *app_key, const char *user_id, int64_t timestamp_sec,
200                      const char *audio_type, int sample_rate, int channel, int sample_bytes,
201                      const char *core_type, const char *token_id, const ygst_fields *request_fields) {
202     size_t i;
203     char ts[32];
204 4    snprintf(ts, sizeof ts, "%lld", (long long)timestamp_sec);
205 4    ygst_buf_append_str(out, "{\"app\":{");
206 4    key(out, "applicationId", 1);
207 4    ygst_json_escape(out, app_key);
208 4    key(out, "userId", 0);
209 4    ygst_json_escape(out, user_id);
210 4    key(out, "timestamp", 0);
211 4    ygst_json_escape(out, ts);
212 4    ygst_buf_append_str(out, "},\"audio\":{");
213 4    key(out, "audioType", 1);
214 4    ygst_json_escape(out, audio_type);
215 4    key(out, "sampleRate", 0);
216 4    ygst_buf_append_int(out, sample_rate);
217 4    key(out, "channel", 0);
218 4    ygst_buf_append_int(out, channel);
219 4    key(out, "sampleBytes", 0);
220 4    ygst_buf_append_int(out, sample_bytes);
221 4    ygst_buf_append_str(out, "},\"request\":{");
222 4    key(out, "coreType", 1);
223 4    ygst_json_escape(out, core_type);
224 4    key(out, "tokenId", 0);
225 4    ygst_json_escape(out, token_id);
226 4    if (request_fields) {
227 12        for (i = 0; i < request_fields->count; i++) {
228 9            const char *n = request_fields->items[i].name;
229 9            if (strcmp(n, "coreType") == 0 || strcmp(n, "tokenId") == 0) continue;
230 8            key(out, n, 0);
231 8            ygst_json_escape(out, request_fields->items[i].value);
232         }
233     }
234 4    ygst_buf_append_str(out, "}}");
235 4    ygst_buf_cstr(out);
236 4    return out->oom ? -1 : 0;
237 }
238 
239 /* ------------------------------------------------------------------ base URL */
240 
241 46static int prefix_ci(const char *s, const char *p) {
242 157    while (*p) {
243 144        char a = *s, b = *p;
244 144        if (a >= 'A' && a <= 'Z') a = (char)(a + 32);
245 144        if (a != b) return 0;
246 111        s++;
247 111        p++;
248     }
249 13    return 1;
250 }
251 
252 13static int host_is_shengtong(const char *host, size_t n) {
253     static const char D[] = "stkouyu.com";
254 13    size_t m = sizeof D - 1;
255     char h[256];
256     size_t i;
257 13    if (n == 0 || n >= sizeof h) return 0;
258 195    for (i = 0; i < n; i++) {
259 182        char c = host[i];
260 182        h[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
261     }
262 13    h[n] = 0;
263 13    if (n == m) return strcmp(h, D) == 0;
264 9    return n > m && strcmp(h + n - m, D) == 0 && h[n - m - 1] == '.';
265 }
266 
267 18ygst_base_kind ygst_resolve_base_url(const char *server, char *out, size_t cap) {
268     char s[1024];
269     const char *scheme;
270     const char *rest;
271     const char *host_start;
272     const char *host_end;
273     const char *end;
274     size_t n;
275     int written;
276 18    if (!out || cap == 0) return YGST_BASE_DEFAULT_EMPTY;
277 18    snprintf(out, cap, "%s", YGST_DEFAULT_BASE_URL);
278 18    n = ygst_trim_copy(server, s, sizeof s);
279 18    if (n == 0) return YGST_BASE_DEFAULT_EMPTY;
280 16    if (prefix_ci(s, "https://")) {
281 2        scheme = "https";
282 2        rest = s + 8;
283 14    } else if (prefix_ci(s, "http://")) {
284 5        scheme = "http";
285 5        rest = s + 7;
286 9    } else if (prefix_ci(s, "wss://")) {
287 2        scheme = "https";
288 2        rest = s + 6;
289 7    } else if (prefix_ci(s, "ws://")) {
290 4        scheme = "http";
291 4        rest = s + 5;
292     } else {
293 3        return host_is_shengtong(s, strcspn(s, "/:?#")) ? YGST_BASE_DEFAULT_SHENGTONG : YGST_BASE_ERR_SCHEME;
294     }
295 13    host_start = rest;
296 13    if (*host_start == '[') {
297 2        host_end = strchr(host_start, ']');
298 2        if (!host_end) return YGST_BASE_ERR_HOST;
299 1        host_end++;
300     } else {
301 11        host_end = host_start;
302 159        while (*host_end && *host_end != '/' && *host_end != ':' && *host_end != '?' && *host_end != '#') host_end++;
303     }
304 12    if (host_end == host_start) return YGST_BASE_ERR_HOST;
305 10    if (host_is_shengtong(host_start, (size_t)(host_end - host_start))) return YGST_BASE_DEFAULT_SHENGTONG;
306 7    end = rest;
307 145    while (*end && *end != '?' && *end != '#') end++;
308 9    while (end > host_end && end[-1] == '/') end--;
309 7    written = snprintf(out, cap, "%s://%.*s", scheme, (int)(end - rest), rest);
310 7    if (written < 0 || (size_t)written >= cap) {
311 1        snprintf(out, cap, "%s", YGST_DEFAULT_BASE_URL);
312 1        return YGST_BASE_ERR_HOST;
313     }
314 6    return YGST_BASE_CUSTOM;
315 }
316 
317 47int ygst_build_url(const char *base, const char *core_type, char *out, size_t cap) {
318     size_t n;
319     int w;
320 47    if (!base || !core_type || !out) return -1;
321 46    n = strlen(base);
322 47    while (n > 0 && base[n - 1] == '/') n--;
323 46    w = snprintf(out, cap, "%.*s/%s", (int)n, base, core_type);
324 46    return (w < 0 || (size_t)w >= cap) ? -1 : 0;
325 }
326 
327 75int ygst_token_id(const unsigned char rnd[16], char out[33]) {
328 75    ygst_hex_lower(rnd, 16, out);
329 75    return 0;
330 }
331 
332 78int ygst_idempotency_key_ok(const char *k) {
333 78    size_t n = 0;
334 78    if (!k) return 0;
335 2593    for (; k[n]; n++) {
336 2522        unsigned char c = (unsigned char)k[n];
337 2522        if (c < 0x21 || c > 0x7E) return 0;
338 2517        if (n >= 200) return 0;
339     }
340 71    return n >= 1;
341 }
342 
343 3void ygst_mask_app_key(const char *app_key, char out[16]) {
344 3    size_t n = app_key ? strlen(app_key) : 0;
345 3    if (n > 4) n = 4;
346 3    memcpy(out, app_key ? app_key : "", n);
347 3    memcpy(out + n, "***", 4);
348 3}

Sources/STKouyuEngine/core/ygst_errors_table.c 100.00%

1 /* Generated by tools/gen_c_errors.py from spec/errors.json version 2026-10-08. Do not edit. */
2 #include "ygst_core.h"
3 
4 const char *const ygst_table_version = "2026-10-08";
5 
6 const ygst_error_entry ygst_table_errors[] = {
7     {40001, "VALIDATION_ERROR", "INVALID_PARAM", 0, 400, "请求参数校验失败"},
8     {40100, "UNAUTHORIZED", "AUTH", 0, 401, "未认证,缺少或无效的凭据"},
9     {40300, "FORBIDDEN", "PERMISSION", 0, 403, "无权限,例如 API Key 未授权该 coreType"},
10     {40400, "NOT_FOUND", "NOT_FOUND", 0, 404, "资源不存在"},
11     {40900, "CONFLICT", "CONFLICT", 0, 409, "资源冲突"},
12     {40901, "IDEMPOTENCY_IN_PROGRESS", "CONFLICT", 1, 409, "同一幂等键的请求仍在处理中,稍后用同一个键重试"},
13     {40902, "QUOTA_INSUFFICIENT", "QUOTA", 0, 409, "套餐额度和账户余额均不足"},
14     {40903, "IDEMPOTENCY_KEY_REUSED", "CONFLICT", 0, 409, "幂等键已用于另一个不同的请求"},
15     {42900, "TOO_MANY_REQUESTS", "RATE_LIMIT", 1, 429, "请求过多或评测排队超时,按 Retry-After 稍后重试"},
16     {42901, "CONCURRENCY_LIMIT", "RATE_LIMIT", 1, 429, "并发评测数超出套餐层级限制"},
17     {42902, "TRIAL_REPORT_DAILY_LIMIT", "QUOTA", 0, 429, "试用层 AI 报告达到每日上限"},
18     {42903, "SANDBOX_DAILY_LIMIT", "QUOTA", 0, 429, "沙箱密钥当日调用次数达到上限"},
19     {50000, "INTERNAL_ERROR", "SERVER", 1, 500, "服务器内部错误"},
20     {50010, "FEATURE_NOT_IMPLEMENTED", "SERVER", 0, 500, "功能未实现"},
21     {50200, "UPSTREAM_SERVICE_ERROR", "UPSTREAM", 1, 502, "上游评测服务暂不可用"},
22     {2001, "TOKEN_EXPIRED", "AUTH", 0, 401, "token 已过期,兼容层早期引擎接口也用此码表示缺少鉴权三元组或签名不匹配"},
23     {2002, "TOKEN_INVALID", "AUTH", 0, 401, "token 无效,兼容层早期引擎接口也用此码表示时间戳超窗"},
24     {2003, "SIGNATURE_VERIFICATION_FAILED", "AUTH", 0, 401, "签名验证失败,兼容层早期引擎接口也用此码表示 appKey 不存在"},
25     {2004, "SESSION_EXPIRED", "AUTH", 0, 401, "会话已过期"},
26     {2005, "TOKEN_BLACKLISTED", "AUTH", 0, 401, "token 已被列入黑名单"},
27     {2006, "API_KEY_INVALID", "AUTH", 0, 401, "API Key 无效"},
28     {2007, "API_KEY_EXPIRED", "AUTH", 0, 401, "API Key 已过期"},
29     {2008, "TOKEN_METADATA_NOT_FOUND", "AUTH", 0, 401, "token 元数据丢失"},
30     {2009, "TOKEN_REFRESH_FAILED", "AUTH", 0, 401, "token 刷新失败"},
31     {2010, "API_KEY_NOT_FOUND", "AUTH", 0, 401, "API Key 不存在"},
32     {2011, "API_KEY_DISABLED", "AUTH", 0, 401, "API Key 已禁用"},
33     {2012, "API_KEY_QUOTA_EXCEEDED", "QUOTA", 0, 400, "API Key 配额已超限"},
34     {3001, "RATE_LIMIT_EXCEEDED_IP", "RATE_LIMIT", 1, 429, "请求过于频繁,按来源地址限流"},
35     {3002, "RATE_LIMIT_EXCEEDED_USER", "RATE_LIMIT", 1, 429, "请求过于频繁,按用户限流"},
36     {3003, "RATE_LIMIT_EXCEEDED_API_KEY", "RATE_LIMIT", 1, 429, "请求过于频繁,按 API Key 限流"},
37     {1004, "USER_DISABLED", "PERMISSION", 0, 400, "用户已禁用"},
38     {1005, "USER_LOCKED", "PERMISSION", 0, 400, "用户已锁定"},
39     {1012, "ACCOUNT_DISABLED", "PERMISSION", 0, 400, "账户已禁用"},
40     {1013, "ACCOUNT_FROZEN", "PERMISSION", 0, 400, "账户已冻结"},
41     {1310, "ACCOUNT_BALANCE_INSUFFICIENT", "QUOTA", 0, 400, "账户余额不足"},
42     {1311, "ACCOUNT_LEDGER_CONFLICT", "CONFLICT", 1, 400, "账目并发冲突,重试即可"},
43 };
44 const size_t ygst_table_errors_count = sizeof ygst_table_errors / sizeof ygst_table_errors[0];
45 
46 const ygst_error_entry ygst_table_warnings[] = {
47     {1001, "NO_VALID_AUDIO", "AUDIO", 0, 0, "No valid audio detected!"},
48     {1002, "VOLUME_TOO_LOW", "AUDIO", 0, 0, "Audio volume too low!"},
49     {1003, "VOLUME_TOO_HIGH", "AUDIO", 0, 0, "Audio volume too high!"},
50     {1004, "AUDIO_NOISY", "AUDIO", 0, 0, "Audio noisy!"},
51     {1005, "AUDIO_INCOMPLETE", "AUDIO", 0, 0, "Audio not complete!"},
52     {1009, "SCORER_DEGRADED", "AUDIO", 1, 0, "scorer degraded"},
53 };
54 const size_t ygst_table_warnings_count = sizeof ygst_table_warnings / sizeof ygst_table_warnings[0];
55 
56 const ygst_error_entry ygst_table_local[] = {
57     {90001, "NETWORK_ERROR", "NETWORK", 1, 0, "网络错误,连接失败或被重置"},
58     {90002, "TIMEOUT", "TIMEOUT", 1, 0, "连接或读取超时"},
59     {90003, "CANCELLED", "CANCELLED", 0, 0, "调用方取消了请求"},
60     {90004, "CLIENT_CLOSED", "STATE", 0, 0, "客户端已关闭"},
61     {90005, "PROTOCOL_ERROR", "PROTOCOL", 0, 0, "响应或帧无法解析"},
62     {90006, "RECONNECT_EXHAUSTED", "NETWORK", 0, 0, "实时连接重连次数用尽"},
63     {90007, "RESULT_TIMEOUT", "TIMEOUT", 1, 0, "结束后等待终评结果超时"},
64     {90008, "REPLAY_BUFFER_OVERFLOW", "STATE", 0, 0, "重连重放缓冲超过上限"},
65     {90009, "INVALID_STATE", "STATE", 0, 0, "当前会话状态不允许该操作"},
66     {90010, "INVALID_ARGUMENT", "INVALID_PARAM", 0, 0, "调用参数不合法"},
67     {90011, "TLS_ERROR", "NETWORK", 0, 0, "证书校验失败"},
68     {90101, "AUDIO_TOO_SHORT", "AUDIO", 0, 0, "音频短于 1 秒"},
69     {90102, "AUDIO_TOO_LONG", "AUDIO", 0, 0, "音频长于 300 秒,整段上传大于 50 MB,或实时评测一轮大于 10 MB"},
70     {90103, "AUDIO_SILENT", "AUDIO", 0, 0, "音频全程静音"},
71     {90104, "AUDIO_LOW_VOLUME", "AUDIO", 0, 0, "音量过低"},
72     {90105, "AUDIO_FORMAT_UNSUPPORTED", "AUDIO", 0, 0, "不是 16 位 PCM 或采样率低于 16000"},
73     {90201, "RECORDER_PERMISSION_DENIED", "PERMISSION", 0, 0, "没有麦克风权限"},
74     {90202, "RECORDER_UNAVAILABLE", "STATE", 0, 0, "麦克风被占用或不可用"},
75     {90203, "RECORDER_ERROR", "STATE", 0, 0, "录音过程出错"},
76 };
77 const size_t ygst_table_local_count = sizeof ygst_table_local / sizeof ygst_table_local[0];
78 
79 const ygst_compat_entry ygst_table_compat[] = {
80     {20009, "网络或服务端临时故障,可重评,autoRetry 默认重评此码"},
81     {60001, "音频文件不存在或不可读"},
82     {60002, "音频为空"},
83     {60003, "不支持的评测内核"},
84     {60004, "麦克风不可用或没有录音权限"},
85     {60005, "音频短于 1 秒"},
86     {60006, "refText 为空"},
87     {60007, "引擎未初始化"},
88     {60008, "引擎正忙,上一次评测未结束"},
89     {60009, "音频大于 50 MB 或长于 300 秒"},
90 };
91 const size_t ygst_table_compat_count = sizeof ygst_table_compat / sizeof ygst_table_compat[0];
92 
93 const int ygst_table_retryable_http[] = {408, 425, 429, 500, 502, 503, 504};
94 const size_t ygst_table_retryable_http_count = sizeof ygst_table_retryable_http / sizeof ygst_table_retryable_http[0];
95 
96 47const char *ygst_http_fallback_category(int status) {
97 47    switch (status) {
98 3    case 400:
99 3        return "INVALID_PARAM";
100 3    case 401:
101 3        return "AUTH";
102 3    case 403:
103 3        return "PERMISSION";
104 4    case 404:
105 4        return "NOT_FOUND";
106 1    case 405:
107 1        return "INVALID_PARAM";
108 2    case 408:
109 2        return "TIMEOUT";
110 2    case 409:
111 2        return "CONFLICT";
112 2    case 413:
113 2        return "INVALID_PARAM";
114 1    case 415:
115 1        return "INVALID_PARAM";
116 3    case 422:
117 3        return "INVALID_PARAM";
118 1    case 425:
119 1        return "RATE_LIMIT";
120 2    case 429:
121 2        return "RATE_LIMIT";
122 2    case 500:
123 2        return "SERVER";
124 2    case 501:
125 2        return "SERVER";
126 2    case 502:
127 2        return "UPSTREAM";
128 2    case 503:
129 2        return "UPSTREAM";
130 1    case 504:
131 1        return "UPSTREAM";
132 11    default:
133         /* DESIGN 2.4: statuses missing from httpFallback map by class */
134 11        if (status >= 400 && status <= 499) return "INVALID_PARAM";
135 8        if (status >= 500 && status <= 599) return "SERVER";
136 6        return "UNKNOWN";
137     }
138 }

Sources/STKouyuEngine/core/ygst_fields.c 94.63%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Ordered form field list and the platform request signature (CONTRACT.md 0.2):
6  * drop empty values, sort by key, join key=value with &, Base64(HMAC_SHA256(payload, secret)).
7  */
8 #include "ygst_core.h"
9 
10 #include <stdlib.h>
11 #include <string.h>
12 
13 906void ygst_fields_init(ygst_fields *f) {
14 906    f->items = NULL;
15 906    f->count = 0;
16 906    f->cap = 0;
17 906    f->oom = 0;
18 906}
19 
20 437void ygst_fields_free(ygst_fields *f) {
21     size_t i;
22 1045    for (i = 0; i < f->count; i++) {
23 608        free(f->items[i].name);
24 608        free(f->items[i].value);
25     }
26 437    free(f->items);
27 437    ygst_fields_init(f);
28 437}
29 
30 636static long find_index(const ygst_fields *f, const char *name) {
31     size_t i;
32 3016    for (i = 0; i < f->count; i++) {
33 2402        if (strcmp(f->items[i].name, name) == 0) return (long)i;
34     }
35 614    return -1;
36 }
37 
38 2int ygst_fields_remove(ygst_fields *f, const char *name) {
39 2    long i = name ? find_index(f, name) : -1;
40 2    if (i < 0) return 0;
41 1    free(f->items[i].name);
42 1    free(f->items[i].value);
43 1    memmove(&f->items[i], &f->items[i + 1], (f->count - (size_t)i - 1) * sizeof(ygst_field));
44 1    f->count--;
45 1    return 1;
46 }
47 
48 621int ygst_fields_set(ygst_fields *f, const char *name, const char *value) {
49     long i;
50     char *v;
51 621    if (!name || f->oom) return -1;
52 620    if (!value) {
53 1        ygst_fields_remove(f, name);
54 1        return 0;
55     }
56 619    i = find_index(f, name);
57 619    v = ygst_strdup(value);
58 619    if (!v) {
59 0        f->oom = 1;
60 0        return -1;
61     }
62 619    if (i >= 0) {
63 10        free(f->items[i].value);
64 10        f->items[i].value = v;
65 10        return 0;
66     }
67 609    if (f->count == f->cap) {
68 219        size_t cap = f->cap ? f->cap * 2 : 16;
69 219        ygst_field *p = (ygst_field *)realloc(f->items, cap * sizeof(ygst_field));
70 219        if (!p) {
71 0            free(v);
72 0            f->oom = 1;
73 0            return -1;
74         }
75 219        f->items = p;
76 219        f->cap = cap;
77     }
78 609    f->items[f->count].name = ygst_strdup(name);
79 609    if (!f->items[f->count].name) {
80 0        free(v);
81 0        f->oom = 1;
82 0        return -1;
83     }
84 609    f->items[f->count].value = v;
85 609    f->count++;
86 609    return 0;
87 }
88 
89 15const char *ygst_fields_get(const ygst_fields *f, const char *name) {
90 15    long i = name ? find_index(f, name) : -1;
91 15    return i < 0 ? NULL : f->items[i].value;
92 }
93 
94 47int ygst_fields_copy(const ygst_fields *src, ygst_fields *dst) {
95     size_t i;
96 193    for (i = 0; i < src->count; i++) {
97 146        if (ygst_fields_set(dst, src->items[i].name, src->items[i].value) != 0) return -1;
98     }
99 47    return 0;
100 }
101 
102 /* Decodes one UTF-8 sequence. Invalid bytes decode as themselves so that ordering stays total. */
103 1048static uint32_t next_cp(const unsigned char **s) {
104 1048    const unsigned char *p = *s;
105 1048    uint32_t c = p[0];
106 1048    int n = 0;
107     int i;
108 1048    if (c < 0x80) {
109 1038        *s = p + 1;
110 1038        return c;
111     }
112 10    if ((c & 0xE0) == 0xC0) {
113 3        n = 1;
114 3        c &= 0x1F;
115 7    } else if ((c & 0xF0) == 0xE0) {
116 2        n = 2;
117 2        c &= 0x0F;
118 5    } else if ((c & 0xF8) == 0xF0) {
119 4        n = 3;
120 4        c &= 0x07;
121     } else {
122 1        *s = p + 1;
123 1        return 0xFFFD;
124     }
125 27    for (i = 1; i <= n; i++) {
126 19        if ((p[i] & 0xC0) != 0x80) {
127 1            *s = p + 1;
128 1            return 0xFFFD;
129         }
130 18        c = (c << 6) | (p[i] & 0x3F);
131     }
132 8    *s = p + n + 1;
133 8    return c;
134 }
135 
136 /* Emits the UTF-16 code units of cp into u, returns how many (1 or 2). */
137 1048static int utf16_units(uint32_t cp, uint32_t u[2]) {
138 1048    if (cp >= 0x10000 && cp <= 0x10FFFF) {
139 4        cp -= 0x10000;
140 4        u[0] = 0xD800 + (cp >> 10);
141 4        u[1] = 0xDC00 + (cp & 0x3FF);
142 4        return 2;
143     }
144 1044    u[0] = cp;
145 1044    return 1;
146 }
147 
148 428int ygst_utf16_compare(const char *a, const char *b) {
149 428    const unsigned char *pa = (const unsigned char *)a;
150 428    const unsigned char *pb = (const unsigned char *)b;
151     uint32_t ua[2], ub[2];
152 428    int na = 0, nb = 0, ia = 0, ib = 0;
153 98    for (;;) {
154         uint32_t ca, cb;
155 526        if (ia == na) {
156 526            if (!*pa) {
157 2                na = 0;
158             } else {
159 524                na = utf16_units(next_cp(&pa), ua);
160             }
161 526            ia = 0;
162         }
163 526        if (ib == nb) {
164 526            if (!*pb) {
165 2                nb = 0;
166             } else {
167 524                nb = utf16_units(next_cp(&pb), ub);
168             }
169 526            ib = 0;
170         }
171 526        if (na == 0 || nb == 0) {
172 3            if (na == 0 && nb == 0) return 0;
173 2            return na == 0 ? -1 : 1;
174         }
175 523        ca = ua[ia++];
176 523        cb = ub[ib++];
177 523        if (ca != cb) return ca < cb ? -1 : 1;
178     }
179 }
180 
181 420static int cmp_field_ptr(const void *x, const void *y) {
182 420    const ygst_field *a = *(const ygst_field *const *)x;
183 420    const ygst_field *b = *(const ygst_field *const *)y;
184 420    return ygst_utf16_compare(a->name, b->name);
185 }
186 
187 121int ygst_sign_payload(const ygst_fields *f, ygst_buf *out) {
188     const ygst_field **v;
189 121    size_t n = 0;
190     size_t i;
191 121    int first = 1;
192 121    if (f->oom) return -1;
193 121    v = (const ygst_field **)malloc((f->count ? f->count : 1) * sizeof(*v));
194 121    if (!v) return -1;
195 480    for (i = 0; i < f->count; i++) {
196 359        if (f->items[i].value && f->items[i].value[0]) v[n++] = &f->items[i];
197     }
198 121    qsort(v, n, sizeof(*v), cmp_field_ptr);
199 458    for (i = 0; i < n; i++) {
200 337        if (!first) ygst_buf_append_char(out, '&');
201 337        first = 0;
202 337        ygst_buf_append_str(out, v[i]->name);
203 337        ygst_buf_append_char(out, '=');
204 337        ygst_buf_append_str(out, v[i]->value);
205     }
206 121    free(v);
207 121    ygst_buf_cstr(out);
208 121    return out->oom ? -1 : 0;
209 }
210 
211 112int ygst_sign(const ygst_fields *f, const char *secret, char out[45]) {
212     ygst_buf payload;
213     unsigned char mac[32];
214     int rc;
215 112    out[0] = 0;
216 112    if (!secret) return -1;
217 111    ygst_buf_init(&payload);
218 111    rc = ygst_sign_payload(f, &payload);
219 111    if (rc == 0) {
220 111        ygst_hmac_sha256(secret, strlen(secret), payload.data ? payload.data : (const unsigned char *)"",
221                          payload.len, mac);
222 111        if (ygst_base64_encode(mac, sizeof mac, out, 45) != 44) rc = -1;
223     }
224 111    ygst_buf_free(&payload);
225 111    return rc;
226 }

Sources/STKouyuEngine/core/ygst_json.c 97.70%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Minimal validating JSON scanner (RFC 8259). It never builds a tree: values are spans into the
6  * original text, so the platform result can be passed to the caller byte for byte.
7  */
8 #include "ygst_core.h"
9 
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #define YGST_JSON_MAX_DEPTH 128
14 
15 5764836static const char *ws(const char *p, const char *e) {
16 11747867    while (p < e && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++;
17 5764836    return p;
18 }
19 
20 21484static int hexval(char c) {
21 21484    if (c >= '0' && c <= '9') return c - '0';
22 5865    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
23 0    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
24 0    return -1;
25 }
26 
27 1584505static const char *skip_string(const char *p, const char *e) {
28 1584505    if (p >= e || *p != '"') return NULL;
29 1584502    p++;
30 11492745    while (p < e) {
31 11492743        unsigned char c = (unsigned char)*p;
32 11492743        if (c == '"') return p + 1;
33 9908246        if (c < 0x20) return NULL;
34 9908245        if (c == '\\') {
35 5871            p++;
36 5871            if (p >= e) return NULL;
37 5871            switch (*p) {
38 780            case '"':
39             case '\\':
40             case '/':
41             case 'b':
42             case 'f':
43             case 'n':
44             case 'r':
45             case 't':
46 780                p++;
47 780                break;
48 5090            case 'u': {
49                 int i;
50 5090                if (e - p < 5) return NULL;
51 25445                for (i = 1; i <= 4; i++) {
52 20356                    if (hexval(p[i]) < 0) return NULL;
53                 }
54 5089                p += 5;
55 5089                break;
56             }
57 1            default:
58 1                return NULL;
59             }
60 5869            continue;
61         }
62 9902374        p++;
63     }
64 2    return NULL;
65 }
66 
67 1524051static int is_digit(char c) {
68 1524051    return c >= '0' && c <= '9';
69 }
70 
71 463889static const char *skip_number(const char *p, const char *e) {
72 463889    if (p < e && *p == '-') p++;
73 463889    if (p >= e) return NULL;
74 463888    if (*p == '0') {
75 165520        p++;
76 298368    } else if (*p >= '1' && *p <= '9') {
77 980442        while (p < e && is_digit(*p)) p++;
78     } else {
79 0        return NULL;
80     }
81 463888    if (p < e && *p == '.') {
82 25810        p++;
83 25810        if (p >= e || !is_digit(*p)) return NULL;
84 62015        while (p < e && is_digit(*p)) p++;
85     }
86 463887    if (p < e && (*p == 'e' || *p == 'E')) {
87 8        p++;
88 8        if (p < e && (*p == '+' || *p == '-')) p++;
89 8        if (p >= e || !is_digit(*p)) return NULL;
90 19        while (p < e && is_digit(*p)) p++;
91     }
92 463886    return p;
93 }
94 
95 241431static const char *skip_lit(const char *p, const char *e, const char *lit) {
96 241431    size_t n = strlen(lit);
97 241431    if ((size_t)(e - p) < n || memcmp(p, lit, n) != 0) return NULL;
98 241427    return p + n;
99 }
100 
101 1422064static const char *skip_value(const char *p, const char *e, int depth) {
102 1422064    if (depth > YGST_JSON_MAX_DEPTH) return NULL;
103 1422063    p = ws(p, e);
104 1422063    if (p >= e) return NULL;
105 1422059    switch (*p) {
106 261982    case '{':
107 261982        p = ws(p + 1, e);
108 261982        if (p < e && *p == '}') return p + 1;
109         for (;;) {
110 1113630            p = skip_string(ws(p, e), e);
111 1113630            if (!p) return NULL;
112 1113627            p = ws(p, e);
113 1113627            if (p >= e || *p != ':') return NULL;
114 1113626            p = skip_value(p + 1, e, depth + 1);
115 1113626            if (!p) return NULL;
116 1113624            p = ws(p, e);
117 1113624            if (p >= e) return NULL;
118 1113624            if (*p == ',') {
119 851720                p++;
120 851720                continue;
121             }
122 261904            if (*p == '}') return p + 1;
123 1            return NULL;
124         }
125 93495    case '[':
126 93495        p = ws(p + 1, e);
127 93495        if (p < e && *p == ']') return p + 1;
128         for (;;) {
129 174613            p = skip_value(p, e, depth + 1);
130 174613            if (!p) return NULL;
131 174482            p = ws(p, e);
132 174482            if (p >= e) return NULL;
133 174482            if (*p == ',') {
134 106993                p++;
135 106993                continue;
136             }
137 67489            if (*p == ']') return p + 1;
138 1            return NULL;
139         }
140 363231    case '"':
141 363231        return skip_string(p, e);
142 112859    case 't':
143 112859        return skip_lit(p, e, "true");
144 128526    case 'f':
145 128526        return skip_lit(p, e, "false");
146 46    case 'n':
147 46        return skip_lit(p, e, "null");
148 461920    default:
149 461920        if (*p == '-' || is_digit(*p)) return skip_number(p, e);
150 10        return NULL;
151     }
152 }
153 
154 795int ygst_json_parse(const char *s, size_t n, ygst_span *root) {
155     const char *e;
156     const char *start;
157     const char *end;
158 795    if (!s) return -1;
159 794    e = s + n;
160 794    start = ws(s, e);
161 794    end = skip_value(start, e, 0);
162 794    if (!end) return -1;
163 762    if (ws(end, e) != e) return -1;
164 760    if (root) {
165 760        root->p = start;
166 760        root->n = (size_t)(end - start);
167     }
168 760    return 0;
169 }
170 
171 235271int ygst_json_type(ygst_span v) {
172 235271    if (!v.p || v.n == 0) return 0;
173 235269    switch (v.p[0]) {
174 155960    case '{':
175 155960        return 'o';
176 55398    case '[':
177 55398        return 'a';
178 7254    case '"':
179 7254        return 's';
180 6720    case 't':
181 6720        return 't';
182 6649    case 'f':
183 6649        return 'f';
184 11    case 'n':
185 11        return 'z';
186 3277    default:
187 3277        return (v.p[0] == '-' || is_digit(v.p[0])) ? 'n' : 0;
188     }
189 }
190 
191 126466int ygst_json_object_next(ygst_span obj, size_t *pos, ygst_span *key, ygst_span *value) {
192 126466    const char *e = obj.p + obj.n;
193     const char *p;
194     const char *k;
195     const char *v;
196 126466    if (ygst_json_type(obj) != 'o') return -1;
197 126465    p = obj.p + (*pos ? *pos : 1);
198 126465    p = ws(p, e);
199 126465    if (p >= e) return -1;
200 126465    if (*p == '}') return 0;
201 103193    if (*pos) {
202 79929        if (*p != ',') return -1;
203 79929        p = ws(p + 1, e);
204     }
205 103193    k = p;
206 103193    p = skip_string(p, e);
207 103193    if (!p) return -1;
208 103193    if (key) {
209 103193        key->p = k;
210 103193        key->n = (size_t)(p - k);
211     }
212 103193    p = ws(p, e);
213 103193    if (p >= e || *p != ':') return -1;
214 103192    v = ws(p + 1, e);
215 103192    p = skip_value(v, e, 1);
216 103192    if (!p) return -1;
217 103192    if (value) {
218 103192        value->p = v;
219 103192        value->n = (size_t)(p - v);
220     }
221 103192    *pos = (size_t)(p - obj.p);
222 103192    return 1;
223 }
224 
225 /* Compares a raw key token (with quotes, possibly escaped) with a plain key. */
226 101343static int key_equals(ygst_span tok, const char *key) {
227 101343    size_t klen = strlen(key);
228 101343    if (tok.n >= 2 && memchr(tok.p, '\\', tok.n) == NULL) {
229 101338        return tok.n - 2 == klen && memcmp(tok.p + 1, key, klen) == 0;
230     } else {
231         ygst_buf b;
232         int eq;
233 5        ygst_buf_init(&b);
234 5        eq = ygst_json_string(tok, &b) == 0 && b.len == klen && memcmp(ygst_buf_cstr(&b), key, klen) == 0;
235 5        ygst_buf_free(&b);
236 5        return eq;
237     }
238 }
239 
240 23002int ygst_json_member(ygst_span obj, const char *key, ygst_span *out) {
241 23002    size_t pos = 0;
242     ygst_span k, v;
243 23002    int found = 0;
244     int rc;
245 23002    if (ygst_json_type(obj) != 'o' || !key) return -1;
246 124344    while ((rc = ygst_json_object_next(obj, &pos, &k, &v)) == 1) {
247 101343        if (key_equals(k, key)) {
248 21411            found = 1;
249 21411            if (out) *out = v;
250         }
251     }
252 23001    if (rc < 0) return -1;
253 23000    return found;
254 }
255 
256 30377int ygst_json_array_next(ygst_span arr, size_t *pos, ygst_span *out) {
257 30377    const char *e = arr.p + arr.n;
258     const char *p;
259     const char *v;
260 30377    if (ygst_json_type(arr) != 'a') return -1;
261 30376    p = arr.p + (*pos ? *pos : 1);
262 30376    p = ws(p, e);
263 30376    if (p >= e) return -1;
264 30376    if (*p == ']') return 0;
265 29840    if (*pos) {
266 27223        if (*p != ',') return -1;
267 27222        p = ws(p + 1, e);
268     }
269 29839    v = p;
270 29839    p = skip_value(v, e, 1);
271 29839    if (!p) return -1;
272 29839    if (out) {
273 29839        out->p = v;
274 29839        out->n = (size_t)(p - v);
275     }
276 29839    *pos = (size_t)(p - arr.p);
277 29839    return 1;
278 }
279 
280 280static void put_utf8(ygst_buf *out, uint32_t cp) {
281     char b[4];
282 280    if (cp < 0x80) {
283 6        b[0] = (char)cp;
284 6        ygst_buf_append(out, b, 1);
285 274    } else if (cp < 0x800) {
286 1        b[0] = (char)(0xC0 | (cp >> 6));
287 1        b[1] = (char)(0x80 | (cp & 0x3F));
288 1        ygst_buf_append(out, b, 2);
289 273    } else if (cp < 0x10000) {
290 272        b[0] = (char)(0xE0 | (cp >> 12));
291 272        b[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
292 272        b[2] = (char)(0x80 | (cp & 0x3F));
293 272        ygst_buf_append(out, b, 3);
294     } else {
295 1        b[0] = (char)(0xF0 | (cp >> 18));
296 1        b[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
297 1        b[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
298 1        b[3] = (char)(0x80 | (cp & 0x3F));
299 1        ygst_buf_append(out, b, 4);
300     }
301 280}
302 
303 282static uint32_t hex4(const char *p) {
304 282    return (uint32_t)((hexval(p[0]) << 12) | (hexval(p[1]) << 8) | (hexval(p[2]) << 4) | hexval(p[3]));
305 }
306 
307 4452int ygst_json_string(ygst_span v, ygst_buf *out) {
308     const char *p;
309     const char *e;
310 4452    if (ygst_json_type(v) != 's' || skip_string(v.p, v.p + v.n) != v.p + v.n) return -1;
311 4450    p = v.p + 1;
312 4450    e = v.p + v.n - 1;
313 4801    while (p < e) {
314 4706        const char *run = p;
315 33970        while (p < e && *p != '\\') p++;
316 4706        if (p > run) ygst_buf_append(out, run, (size_t)(p - run));
317 4706        if (p >= e) break;
318 351        p++; /* backslash */
319 351        switch (*p) {
320 1        case 'b':
321 1            ygst_buf_append_char(out, '\b');
322 1            p++;
323 1            break;
324 1        case 'f':
325 1            ygst_buf_append_char(out, '\f');
326 1            p++;
327 1            break;
328 1        case 'n':
329 1            ygst_buf_append_char(out, '\n');
330 1            p++;
331 1            break;
332 1        case 'r':
333 1            ygst_buf_append_char(out, '\r');
334 1            p++;
335 1            break;
336 1        case 't':
337 1            ygst_buf_append_char(out, '\t');
338 1            p++;
339 1            break;
340 280        case 'u': {
341 280            uint32_t cp = hex4(p + 1);
342 280            p += 5;
343 280            if (cp >= 0xD800 && cp <= 0xDBFF) {
344 5                if (e - p >= 6 && p[0] == '\\' && p[1] == 'u') {
345 2                    uint32_t lo = hex4(p + 2);
346 2                    if (lo >= 0xDC00 && lo <= 0xDFFF) {
347 1                        cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
348 1                        p += 6;
349                     } else {
350 1                        cp = 0xFFFD;
351                     }
352                 } else {
353 1                    cp = 0xFFFD;
354                 }
355 277            } else if (cp >= 0xDC00 && cp <= 0xDFFF) {
356 1                cp = 0xFFFD;
357             }
358 280            put_utf8(out, cp);
359 280            break;
360         }
361 66        default: /* " \ / */
362 66            ygst_buf_append_char(out, *p);
363 66            p++;
364 66            break;
365         }
366     }
367 4450    ygst_buf_cstr(out);
368 4450    return out->oom ? -1 : 0;
369 }
370 
371 1981static int number_text(ygst_span v, char *tmp, size_t cap) {
372 1981    if (ygst_json_type(v) != 'n' || v.n >= cap) return -1;
373 1979    if (skip_number(v.p, v.p + v.n) != v.p + v.n) return -1;
374 1979    memcpy(tmp, v.p, v.n);
375 1979    tmp[v.n] = 0;
376 1979    return 0;
377 }
378 
379 52int ygst_json_number(ygst_span v, double *out) {
380     char tmp[64];
381 52    char *end = NULL;
382 52    if (number_text(v, tmp, sizeof tmp) != 0) return -1;
383 51    *out = strtod(tmp, &end);
384 51    if (end && *end) {
385         /* A locale with a decimal comma: retry with the separator it expects. */
386 0        char *dot = strchr(tmp, '.');
387 0        if (dot) {
388 0            *dot = ',';
389 0            *out = strtod(tmp, &end);
390         }
391 0        if (end && *end) return -1;
392     }
393 51    return 0;
394 }
395 
396 1929int ygst_json_int(ygst_span v, long long *out) {
397     char tmp[64];
398     size_t i;
399 1929    int neg = 0;
400 1929    long long acc = 0;
401 1929    if (number_text(v, tmp, sizeof tmp) != 0) return -1;
402 1928    if (strpbrk(tmp, ".eE")) {
403         double d;
404 4        if (ygst_json_number(v, &d) != 0) return -1;
405 4        if (d != d || d > 9.2e18 || d < -9.2e18 || d != (double)(long long)d) return -1;
406 2        *out = (long long)d;
407 2        return 0;
408     }
409 1924    i = 0;
410 1924    if (tmp[0] == '-') {
411 1        neg = 1;
412 1        i = 1;
413     }
414 6698    for (; tmp[i]; i++) {
415 4775        int dgt = tmp[i] - '0';
416 4775        if (acc > (9223372036854775807LL - dgt) / 10) return -1;
417 4774        acc = acc * 10 + dgt;
418     }
419 1923    *out = neg ? -acc : acc;
420 1923    return 0;
421 }
422 
423 13369int ygst_json_bool(ygst_span v, int *out) {
424 13369    int t = ygst_json_type(v);
425 13369    if (t == 't' && v.n == 4) {
426 6719        *out = 1;
427 6719        return 0;
428     }
429 6650    if (t == 'f' && v.n == 5) {
430 6649        *out = 0;
431 6649        return 0;
432     }
433 1    return -1;
434 }
435 
436 2435int ygst_json_escape_n(ygst_buf *out, const char *s, size_t n) {
437     static const char H[] = "0123456789abcdef";
438     size_t i;
439 2435    size_t run = 0;
440 2435    ygst_buf_append_char(out, '"');
441 60889    for (i = 0; i < n; i++) {
442 58454        unsigned char c = (unsigned char)s[i];
443 58454        const char *rep = NULL;
444         char u[7];
445 58454        if (c == '"') {
446 21            rep = "\\\"";
447 58433        } else if (c == '\\') {
448 4            rep = "\\\\";
449 58429        } else if (c == '\n') {
450 5            rep = "\\n";
451 58424        } else if (c == '\r') {
452 1            rep = "\\r";
453 58423        } else if (c == '\t') {
454 1            rep = "\\t";
455 58422        } else if (c == '\b') {
456 1            rep = "\\b";
457 58421        } else if (c == '\f') {
458 1            rep = "\\f";
459 58420        } else if (c < 0x20) {
460 2            u[0] = '\\';
461 2            u[1] = 'u';
462 2            u[2] = '0';
463 2            u[3] = '0';
464 2            u[4] = H[c >> 4];
465 2            u[5] = H[c & 15];
466 2            u[6] = 0;
467 2            rep = u;
468         }
469 58454        if (rep) {
470 36            if (i > run) ygst_buf_append(out, s + run, i - run);
471 36            ygst_buf_append_str(out, rep);
472 36            run = i + 1;
473         }
474     }
475 2435    if (n > run) ygst_buf_append(out, s + run, n - run);
476 2435    ygst_buf_append_char(out, '"');
477 2435    return out->oom ? -1 : 0;
478 }
479 
480 2435int ygst_json_escape(ygst_buf *out, const char *s) {
481 2435    if (!s) s = "";
482 2435    return ygst_json_escape_n(out, s, strlen(s));
483 }

Sources/STKouyuEngine/core/ygst_multipart.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * multipart/form-data assembly (RFC 7578) for POST /{coreType}. Text fields carry no filename,
6  * so the platform treats them as form parameters and they are exactly the signed set; the audio
7  * is a file part and is not signed.
8  */
9 #include "ygst_core.h"
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 45int ygst_multipart_boundary(const unsigned char rnd[16], char out[64]) {
15     char hex[33];
16 45    ygst_hex_lower(rnd, 16, hex);
17 45    snprintf(out, 64, "----YuguSTCompat%s", hex);
18 45    return 0;
19 }
20 
21 46int ygst_multipart_content_type(const char *boundary, char *out, size_t cap) {
22     int n;
23 46    if (!boundary || !out) return -1;
24 46    n = snprintf(out, cap, "multipart/form-data; boundary=%s", boundary);
25 46    return (n < 0 || (size_t)n >= cap) ? -1 : 0;
26 }
27 
28 146static int contains(const unsigned char *hay, size_t n, const char *needle) {
29 146    size_t m = strlen(needle);
30     size_t i;
31 146    if (m == 0 || n < m) return 0;
32 3168190    for (i = 0; i + m <= n; i++) {
33 3168140        if (hay[i] == (unsigned char)needle[0] && memcmp(hay + i, needle, m) == 0) return 1;
34     }
35 50    return 0;
36 }
37 
38 52int ygst_multipart_build(const ygst_fields *fields, const char *boundary, const char *file_field,
39                          const char *filename, const char *file_content_type, const unsigned char *file,
40                          size_t file_len, ygst_buf *out) {
41     size_t i;
42 52    if (!boundary || !boundary[0] || strlen(boundary) > 70 || !file_field || !filename) return -1;
43 50    if (!ygst_field_name_ok(file_field) || !ygst_field_name_ok(filename)) return -1;
44 148    for (i = 0; i < fields->count; i++) {
45 101        const char *v = fields->items[i].value ? fields->items[i].value : "";
46 101        if (!ygst_field_name_ok(fields->items[i].name)) return -1;
47 100        if (contains((const unsigned char *)v, strlen(v), boundary)) return -2;
48     }
49 47    if (file && contains(file, file_len, boundary)) return -2;
50 145    for (i = 0; i < fields->count; i++) {
51 99        ygst_buf_append_str(out, "--");
52 99        ygst_buf_append_str(out, boundary);
53 99        ygst_buf_append_str(out, "\r\nContent-Disposition: form-data; name=\"");
54 99        ygst_buf_append_str(out, fields->items[i].name);
55 99        ygst_buf_append_str(out, "\"\r\n\r\n");
56 99        ygst_buf_append_str(out, fields->items[i].value ? fields->items[i].value : "");
57 99        ygst_buf_append_str(out, "\r\n");
58     }
59 46    ygst_buf_append_str(out, "--");
60 46    ygst_buf_append_str(out, boundary);
61 46    ygst_buf_append_str(out, "\r\nContent-Disposition: form-data; name=\"");
62 46    ygst_buf_append_str(out, file_field);
63 46    ygst_buf_append_str(out, "\"; filename=\"");
64 46    ygst_buf_append_str(out, filename);
65 46    ygst_buf_append_str(out, "\"\r\nContent-Type: ");
66 46    ygst_buf_append_str(out, file_content_type ? file_content_type : "application/octet-stream");
67 46    ygst_buf_append_str(out, "\r\n\r\n");
68 46    if (file_len) ygst_buf_append(out, file, file_len);
69 46    ygst_buf_append_str(out, "\r\n--");
70 46    ygst_buf_append_str(out, boundary);
71 46    ygst_buf_append_str(out, "--\r\n");
72 46    return out->oom ? -1 : 0;
73 }
74 
75 /* Upload extension for an audioType or a file extension. Unknown types upload as-is. */
76 67const char *ygst_audio_ext(const char *audio_type) {
77     static const char *const KNOWN[] = {"wav", "mp3", "ogg", "amr", "flv", "opus", "aac",
78                                         "spx", "speex", "silk", "slk", "m4a", "pcm"};
79     char t[16];
80     size_t i;
81 67    if (ygst_is_blank(audio_type)) return "wav";
82 65    ygst_trim_copy(audio_type, t, sizeof t);
83 65    if (t[0] == '.') memmove(t, t + 1, strlen(t));
84 122    for (i = 0; i < sizeof KNOWN / sizeof KNOWN[0]; i++) {
85 119        if (ygst_strcaseeq(t, KNOWN[i])) return strcmp(KNOWN[i], "pcm") == 0 ? "wav" : KNOWN[i];
86     }
87 3    if (ygst_strcaseeq(t, "wave")) return "wav";
88 2    if (ygst_strcaseeq(t, "mpeg")) return "mp3";
89 1    return "bin";
90 }
91 
92 55const char *ygst_audio_content_type(const char *ext) {
93 55    if (!ext) return "application/octet-stream";
94 54    if (ygst_strcaseeq(ext, "wav")) return "audio/wav";
95 9    if (ygst_strcaseeq(ext, "mp3")) return "audio/mpeg";
96 8    if (ygst_strcaseeq(ext, "ogg") || ygst_strcaseeq(ext, "spx") || ygst_strcaseeq(ext, "speex")) return "audio/ogg";
97 6    if (ygst_strcaseeq(ext, "opus")) return "audio/opus";
98 5    if (ygst_strcaseeq(ext, "amr")) return "audio/amr";
99 4    if (ygst_strcaseeq(ext, "aac")) return "audio/aac";
100 3    if (ygst_strcaseeq(ext, "m4a")) return "audio/mp4";
101 2    if (ygst_strcaseeq(ext, "flv")) return "video/x-flv";
102 1    return "application/octet-stream";
103 }
104 
105 4int ygst_audio_type_is_pcm(const char *audio_type) {
106     char t[16];
107 4    if (ygst_is_blank(audio_type)) return 1;
108 3    ygst_trim_copy(audio_type, t, sizeof t);
109 6    return ygst_strcaseeq(t, "wav") || ygst_strcaseeq(t, "pcm") || ygst_strcaseeq(t, "wave") ||
110 6           ygst_strcaseeq(t, ".wav") || ygst_strcaseeq(t, ".pcm");
111 }

Sources/STKouyuEngine/core/ygst_params.c 97.58%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * coreType resolution and the mapping from KYTestConfig properties to the form fields of
6  * POST /{coreType}. The table mirrors DESIGN 6.1 so that iOS and Android send the same fields.
7  */
8 #include "ygst_core.h"
9 
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 /* KYTestType in declaration order (KYTestConfig.h). NULL: Shengtong publishes no string. */
16 static const char *const KY_TEST_TYPES[] = {
17     "word.eval",     /* KYTestType_Word */
18     "sent.eval",     /* KYTestType_Sentence */
19     "para.eval",     /* KYTestType_Paragraph */
20     "open.eval",     /* KYTestType_Open */
21     "choice.rec",    /* KYTestType_Choice */
22     "asr.rec",       /* KYTestType_Asr */
23     "align.eval",    /* KYTestType_Align */
24     "word.eval.pro", /* KYTestType_Word_Pro */
25     "sent.eval.pro", /* KYTestType_Sentence_Pro */
26     "word.eval.cn",  /* KYTestType_Word_Cn */
27     "sent.eval.cn",  /* KYTestType_Sentence_Cn */
28     "para.eval.cn",  /* KYTestType_Paragraph_Cn */
29     "asr.eval",      /* KYTestType_AsrEval */
30     "word.eval.fr",  /* KYTestType_Word_fr */
31     "sent.eval.fr",  /* KYTestType_Sentence_fr */
32     "para.eval.fr",  /* KYTestType_Paragraph_fr */
33     "word.eval.kr",  /* KYTestType_Word_kr */
34     "sent.eval.kr",  /* KYTestType_Sentence_kr */
35     "para.eval.kr",  /* KYTestType_Paragraph_kr */
36     "word.eval.jp",  /* KYTestType_Word_jp */
37     "sent.eval.jp",  /* KYTestType_Sentence_jp */
38     "para.eval.jp",  /* KYTestType_Paragraph_jp */
39     NULL             /* KYTestType_Wordspell */
40 };
41 
42 /* coreTypes the platform evaluates on POST /{coreType} (DESIGN 6.1). open.eval.cn since 2.1.2: Shengtong's
43  * Chinese open question, passed as coreTypeNS because KYTestType has no member for it. */
44 static const char *const SUPPORTED[] = {"word.eval",    "word.eval.pro", "sent.eval",    "sent.eval.pro",
45                                         "para.eval",    "word.eval.cn",  "sent.eval.cn", "para.eval.cn",
46                                         "alpha.eval",   "pinyin",        "open.eval.cn"};
47 
48 /* Shengtong coreTypes that are answered with errId 60003 without network I/O. */
49 static const char *const UNSUPPORTED[] = {"choice.rec",   "open.eval",    "asr.rec",      "asr.eval",
50                                           "align.eval",   "word.eval.fr", "sent.eval.fr", "para.eval.fr",
51                                           "word.eval.jp", "sent.eval.jp", "para.eval.jp", "word.eval.kr",
52                                           "sent.eval.kr", "para.eval.kr"};
53 
54 67const char *ygst_coretype_for_enum(unsigned long kytesttype) {
55 67    if (kytesttype >= sizeof KY_TEST_TYPES / sizeof KY_TEST_TYPES[0]) return NULL;
56 63    return KY_TEST_TYPES[kytesttype];
57 }
58 
59 25size_t ygst_coretype_enum_count(void) {
60 25    return sizeof KY_TEST_TYPES / sizeof KY_TEST_TYPES[0];
61 }
62 
63 132ygst_ct_status ygst_coretype_check(const char *core_type) {
64     size_t i;
65 132    if (!core_type || !core_type[0]) return YGST_CT_UNKNOWN;
66 1077    for (i = 0; i < sizeof SUPPORTED / sizeof SUPPORTED[0]; i++) {
67 1036        if (strcmp(core_type, SUPPORTED[i]) == 0) return YGST_CT_SUPPORTED;
68     }
69 316    for (i = 0; i < sizeof UNSUPPORTED / sizeof UNSUPPORTED[0]; i++) {
70 311        if (strcmp(core_type, UNSUPPORTED[i]) == 0) return YGST_CT_UNSUPPORTED;
71     }
72 5    return YGST_CT_UNKNOWN;
73 }
74 
75 86ygst_ct_status ygst_coretype_resolve(const char *core_type_ns, unsigned long kytesttype, char *out, size_t cap) {
76     const char *mapped;
77 86    if (!out || cap == 0) return YGST_CT_UNKNOWN;
78 85    out[0] = 0;
79 85    if (!ygst_is_blank(core_type_ns)) {
80 75        ygst_trim_copy(core_type_ns, out, cap);
81 75        return ygst_coretype_check(out);
82     }
83 10    mapped = ygst_coretype_for_enum(kytesttype);
84 10    if (!mapped) return YGST_CT_UNSUPPORTED;
85 7    if (strlen(mapped) >= cap) return YGST_CT_UNKNOWN;
86 6    strcpy(out, mapped);
87 6    return ygst_coretype_check(out);
88 }
89 
90 11int ygst_coretype_is_chinese(const char *core_type) {
91 11    return core_type && (ygst_has_suffix_ci(core_type, ".cn") || strcmp(core_type, "pinyin") == 0);
92 }
93 
94 8int ygst_coretype_is_open_question(const char *core_type) {
95 8    return core_type && strcmp(core_type, "open.eval.cn") == 0;
96 }
97 
98 204int ygst_coretype_is_paragraph(const char *core_type) {
99 204    return core_type && strncmp(core_type, "para.", 5) == 0;
100 }
101 
102 102int ygst_fields_apply_coretype_rules(ygst_fields *f, const char *core_type) {
103 102    if (ygst_coretype_is_paragraph(core_type)) return ygst_fields_set(f, "paragraph_need_word_score", "1");
104 95    return 0;
105 }
106 
107 85int ygst_validate_request(ygst_ct_status status, const char *core_type, const char *ref_text,
108                           const char *ref_pinyin) {
109 85    if (status != YGST_CT_SUPPORTED) return YGST_ERRID_CORETYPE_UNSUPPORTED;
110 72    if (!ygst_is_blank(ref_text)) return 0;
111     /* the refText of an open question, model answers or the question, cannot be replaced by a reference pinyin */
112 9    if (!ygst_is_blank(ref_pinyin) && ygst_coretype_is_chinese(core_type) && !ygst_coretype_is_open_question(core_type))
113 3        return 0;
114 6    return YGST_ERRID_REFTEXT_EMPTY;
115 }
116 
117 13ygst_ct_status ygst_coretype_resolve_text(const char *core_type, char *out, size_t cap) {
118 13    if (!out || cap == 0) return YGST_CT_UNKNOWN;
119 12    out[0] = 0;
120 12    if (ygst_is_blank(core_type)) return YGST_CT_UNKNOWN;
121 6    return ygst_coretype_resolve(core_type, 0, out, cap);
122 }
123 
124 18int ygst_coretype_requested(const char *core_type_ns, unsigned long kytesttype, ygst_buf *out) {
125     const char *mapped;
126 18    if (!out) return -1;
127 17    if (!ygst_is_blank(core_type_ns)) {
128 8        ygst_buf_append_trimmed(out, core_type_ns);
129 9    } else if ((mapped = ygst_coretype_for_enum(kytesttype)) != NULL) {
130 5        ygst_buf_append_str(out, mapped);
131     } else {
132         char tmp[48];
133 4        snprintf(tmp, sizeof tmp, "KYTestType %lu", kytesttype);
134 4        ygst_buf_append_str(out, tmp);
135     }
136 17    ygst_buf_cstr(out);
137 17    return out->oom ? -1 : 0;
138 }
139 
140 20int ygst_coretype_unsupported_message(const char *requested, ygst_buf *out) {
141 20    const char *base = ygst_compat_message(YGST_ERRID_CORETYPE_UNSUPPORTED);
142 20    if (!out) return -1;
143 19    ygst_buf_append_str(out, base ? base : "");
144 19    ygst_buf_append_str(out, "\xEF\xBC\x9A"); /* U+FF1A FULLWIDTH COLON */
145 19    if (ygst_is_blank(requested)) {
146 5        ygst_buf_append_str(out, "\xE6\x9C\xAA\xE6\x8C\x87\xE5\xAE\x9A"); /* 未指定 */
147     } else {
148 14        ygst_buf_append_trimmed(out, requested);
149     }
150 19    ygst_buf_cstr(out);
151 19    return out->oom ? -1 : 0;
152 }
153 
154 /* ------------------------------------------------------------------ number text */
155 
156 69static void fix_decimal_point(char *s) {
157 431    for (; *s; s++) {
158 362        if (*s == ',') *s = '.';
159     }
160 69}
161 
162 3static void trim_fraction_zeros(char *s) {
163 3    char *dot = strchr(s, '.');
164     char *end;
165 3    if (!dot) return;
166 3    end = s + strlen(s);
167 3    while (end > dot + 1 && end[-1] == '0') end--;
168 3    if (end == dot + 1) end = dot;
169 3    *end = 0;
170 }
171 
172 28int ygst_fmt_double(double v, char *out, size_t cap) {
173     char tmp[64];
174     int p;
175 28    if (!out || cap == 0) return -1;
176 27    out[0] = 0;
177 27    if (v != v || v > 1.7976931348623157e308 || v < -1.7976931348623157e308) return -1; /* NaN, inf */
178 25    if (v == 0) {
179 2        tmp[0] = '0';
180 2        tmp[1] = 0;
181 23    } else if (fabs(v) < 9.0e15 && v == floor(v)) {
182 11        snprintf(tmp, sizeof tmp, "%.0f", v);
183 12    } else if (fabs(v) >= 9.0e15) {
184 1        snprintf(tmp, sizeof tmp, "%.0f", v);
185     } else {
186 41        for (p = 1; p <= 17; p++) {
187 41            char *end = NULL;
188             double back;
189 41            snprintf(tmp, sizeof tmp, "%.*g", p, v);
190 41            fix_decimal_point(tmp);
191 41            back = strtod(tmp, &end);
192 41            if (end && *end) {
193                 /* decimal comma locale: compare through the locale's own separator */
194                 char alt[64];
195                 char *dot;
196 0                strcpy(alt, tmp);
197 0                dot = strchr(alt, '.');
198 0                if (dot) *dot = ',';
199 0                back = strtod(alt, NULL);
200             }
201 41            if (back == v) break;
202         }
203 11        if (strchr(tmp, 'e') || strchr(tmp, 'E')) {
204             /* plain decimal: enough fraction digits to keep the significant ones */
205 3            int exp10 = (int)floor(log10(fabs(v)));
206 3            int digits = (p > 17 ? 17 : p) - 1 - exp10;
207 3            if (digits < 0) digits = 0;
208 3            if (digits > 40) digits = 40;
209 3            snprintf(tmp, sizeof tmp, "%.*f", digits, v);
210 3            fix_decimal_point(tmp);
211 3            trim_fraction_zeros(tmp);
212         }
213     }
214 25    fix_decimal_point(tmp);
215 25    if (strcmp(tmp, "-0") == 0) strcpy(tmp, "0");
216 25    if (strlen(tmp) >= cap) return -1;
217 24    strcpy(out, tmp);
218 24    return 0;
219 }
220 
221 214int ygst_field_name_ok(const char *name) {
222 214    if (!name || !name[0]) return 0;
223 2054    for (; *name; name++) {
224 1847        if (*name == '"' || *name == '\r' || *name == '\n') return 0;
225     }
226 207    return 1;
227 }
228 
229 /* ------------------------------------------------------------------ mapping */
230 
231 95void ygst_test_params_init(ygst_test_params *p) {
232 95    memset(p, 0, sizeof *p);
233 95}
234 
235 1003static int set_str(ygst_fields *f, const char *name, const char *v) {
236 1003    if (!v || !v[0]) return 0;
237 81    return ygst_fields_set(f, name, v);
238 }
239 
240 700static int set_flag(ygst_fields *f, const char *name, int on) {
241 700    return on ? ygst_fields_set(f, name, "1") : 0;
242 }
243 
244 501static int set_num(ygst_fields *f, const char *name, double v) {
245     char tmp[64];
246 501    if (v == 0 || ygst_fmt_double(v, tmp, sizeof tmp) != 0) return 0;
247 12    return ygst_fields_set(f, name, tmp);
248 }
249 
250 3static int set_int(ygst_fields *f, const char *name, long v) {
251     char tmp[32];
252 3    snprintf(tmp, sizeof tmp, "%ld", v);
253 3    return ygst_fields_set(f, name, tmp);
254 }
255 
256 100int ygst_params_to_fields(const ygst_test_params *p, ygst_fields *out) {
257     static const char *const DICT[] = {NULL, "CMU", "KK", "IPA88"};
258 100    int rc = 0;
259 100    rc |= set_str(out, "refText", p->ref_text);
260 100    rc |= set_str(out, "refPinyin", p->ref_pinyin);
261 100    if (p->age_group >= 1 && p->age_group <= 3) rc |= set_int(out, "agegroup", p->age_group);
262 100    rc |= set_num(out, "scale", p->scale);
263 100    rc |= set_num(out, "precision", p->precision);
264 100    rc |= set_num(out, "slack", p->slack);
265 100    rc |= set_flag(out, "paragraph_need_word_score",
266 100                   p->paragraph_need_word_score || ygst_coretype_is_paragraph(p->core_type));
267 100    rc |= set_flag(out, "phoneme_output", p->phoneme_output);
268 100    rc |= set_flag(out, "attachAudioUrl", p->attach_audio_url);
269 100    if (p->phoneme_option >= 1 && p->phoneme_option <= 3) rc |= set_str(out, "dict_type", DICT[p->phoneme_option]);
270 100    rc |= set_str(out, "dict_dialect", p->dict_dialect);
271 100    rc |= set_str(out, "customized_lexicon", p->customized_lexicon_json);
272 100    rc |= set_str(out, "customized_pron", p->customized_pron_json);
273 100    if (p->readtype_diagnosis != 0) rc |= set_num(out, "readtype_diagnosis", p->readtype_diagnosis);
274 100    rc |= set_flag(out, "output_rawtext", p->output_rawtext);
275 100    rc |= set_flag(out, "punctuate", p->punctuate);
276 100    rc |= set_num(out, "itn", p->itn);
277 100    rc |= set_flag(out, "detect_nonscorable", p->detect_nonscorable);
278     /* KYTestConfig.vad_detection travels under the Android RecordSetting name. */
279 100    rc |= set_flag(out, "vad_detction", p->vad_detection);
280 100    rc |= set_str(out, "keywords", p->keywords);
281 100    rc |= set_str(out, "keypoints", p->keypoints_json);
282 100    rc |= set_num(out, "keypoints_weight", p->keypoints_weight);
283 100    rc |= set_str(out, "negative_keypoints", p->negative_keypoints_json);
284 100    rc |= set_str(out, "negativeReftext", p->negative_reftext);
285 100    if (p->mode == 1) rc |= ygst_fields_set(out, "mode", "home");
286 100    if (p->q_type != 0) rc |= set_int(out, "qType", p->q_type);
287     /* customParams in the order of the Android RecordSetting newParams: a name that is already
288      * mapped keeps its position and takes the new value; request comes last */
289 100    if (p->custom_params) {
290         size_t i;
291 72        for (i = 0; i < p->custom_params->count; i++) {
292 10            const ygst_field *cp = &p->custom_params->items[i];
293 10            if (!ygst_field_name_ok(cp->name) || !cp->value || !cp->value[0]) continue;
294 7            rc |= ygst_fields_set(out, cp->name, cp->value);
295         }
296     }
297 100    rc |= set_str(out, "request", p->request_json);
298     /* customParams cannot switch the paragraph word details off */
299 100    rc |= ygst_fields_apply_coretype_rules(out, p->core_type);
300 100    return (rc != 0 || out->oom) ? -1 : 0;
301 }

Sources/STKouyuEngine/core/ygst_pipeline.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Pipelined evaluation (2.3.0, YuguCompat.maxPendingEvaluations): the stored value of the switch, the
6  * busy rule of a start and the log lines, in the key=value form of the Android shim. The Objective-C
7  * engine (KYTestEngine.m) keeps the recording session and the sessions waiting for their results, and
8  * asks here what a start does.
9  */
10 #include "ygst_core.h"
11 
12 11int ygst_pipe_clamp(long long n) {
13 11    if (n <= 0) return 0;
14 7    if (n > YGST_PIPE_MAX_PENDING) return YGST_PIPE_MAX_PENDING;
15 4    return (int)n;
16 }
17 
18 16int ygst_pipe_hands_over(int max_pending) {
19 16    return max_pending > 0;
20 }
21 
22 440ygst_pipe_decision ygst_pipe_decide_start(int max_pending, int session_active, int session_stopping, int pending) {
23 440    int cap = max_pending > 0 ? max_pending : 0;
24 440    if (session_active) {
25         /* one at a time, the session is busy until its result; pipelined, a stop that is being handled
26          * is finished first, any other state (starting, recording, paused, waiting for stopEngine) is busy */
27 279        return (cap > 0 && session_stopping) ? YGST_PIPE_FINISH_STOP : YGST_PIPE_BUSY_SESSION;
28     }
29     /* up to cap evaluations wait for their results while the new session records */
30 161    return pending > cap ? YGST_PIPE_BUSY_PENDING : YGST_PIPE_START;
31 }
32 
33 /* " pending=<p> maxPendingEvaluations=<n>" */
34 6static void put_counts(ygst_buf *out, int pending, int max_pending) {
35 6    ygst_buf_append_str(out, "pending=");
36 6    ygst_buf_append_int(out, pending);
37 6    ygst_buf_append_str(out, " maxPendingEvaluations=");
38 6    ygst_buf_append_int(out, max_pending);
39 6}
40 
41 5int ygst_pipe_start_line(const char *token_id, int pending, int max_pending, int audio_path, ygst_buf *out) {
42 5    if (!out) return -1;
43 4    ygst_buf_append_str(out, "pipelined evaluation tokenId=");
44 4    ygst_buf_append_str(out, token_id && token_id[0] ? token_id : "-");
45 4    ygst_buf_append_char(out, ' ');
46 4    put_counts(out, pending, max_pending);
47 4    if (audio_path) ygst_buf_append_str(out, " (audioPath)");
48 4    ygst_buf_cstr(out);
49 4    return out->oom ? -1 : 0;
50 }
51 
52 3int ygst_pipe_busy_line(int pending, int max_pending, ygst_buf *out) {
53 3    if (!out) return -1;
54 2    ygst_buf_append_str(out, "start rejected: engine is busy (");
55 2    put_counts(out, pending, max_pending);
56 2    ygst_buf_append_char(out, ')');
57 2    ygst_buf_cstr(out);
58 2    return out->oom ? -1 : 0;
59 }
60 
61 4int ygst_pipe_setting_line(int old_value, int new_value, ygst_buf *out) {
62 4    if (!out) return -1;
63 3    ygst_buf_append_str(out, new_value > 0 ? "pipelined evaluation on: maxPendingEvaluations="
64                                            : "pipelined evaluation off: maxPendingEvaluations=");
65 3    ygst_buf_append_int(out, new_value);
66 3    ygst_buf_append_str(out, " (was ");
67 3    ygst_buf_append_int(out, old_value);
68 3    ygst_buf_append_char(out, ')');
69 3    ygst_buf_cstr(out);
70 3    return out->oom ? -1 : 0;
71 }

Sources/STKouyuEngine/core/ygst_realtime.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Realtime feedback (2.1.0, DESIGN 14.3, CONTRACT 5.2 and 5.5): the compat WebSocket round of one
6  * evaluation. Handshake URL and its signature, parameter frame, classification of platform
7  * frames, the words dedupe of the reconnect rule and the round controller that decides
8  * connecting, streaming, ending, falling back to REST before end and reconnecting after end.
9  * 2.1.1: the same round streams every recording by default; the parameter frame asks for reading
10  * progress only with realtime_feedback.
11  */
12 #include "ygst_core.h"
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 /* ------------------------------------------------------------------ URL */
19 
20 288int ygst_url_encode(ygst_buf *out, const char *s) {
21     static const char H[] = "0123456789ABCDEF";
22 288    const unsigned char *p = (const unsigned char *)(s ? s : "");
23 4699    for (; *p; p++) {
24 4411        unsigned char c = *p;
25 4411        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' ||
26 90            c == '_' || c == '~') {
27 4323            ygst_buf_append_char(out, (char)c);
28         } else {
29             char e[3];
30 88            e[0] = '%';
31 88            e[1] = H[c >> 4];
32 88            e[2] = H[c & 15];
33 88            ygst_buf_append(out, e, 3);
34         }
35     }
36 288    ygst_buf_cstr(out);
37 288    return out->oom ? -1 : 0;
38 }
39 
40 71static int starts_ci(const char *s, const char *prefix) {
41 407    while (*prefix) {
42 374        char a = *s;
43 374        if (a >= 'A' && a <= 'Z') a = (char)(a + 32);
44 374        if (a != *prefix) return 0;
45 336        s++;
46 336        prefix++;
47     }
48 33    return 1;
49 }
50 
51 37int ygst_ws_base_url(const char *rest_base, char *out, size_t cap) {
52     const char *scheme;
53     const char *rest;
54     size_t n;
55     int w;
56 37    if (!out || cap == 0) return -1;
57 36    out[0] = 0;
58 36    if (!rest_base) return -1;
59 35    if (starts_ci(rest_base, "https://")) {
60 6        scheme = "wss://";
61 6        rest = rest_base + 8;
62 29    } else if (starts_ci(rest_base, "http://")) {
63 25        scheme = "ws://";
64 25        rest = rest_base + 7;
65 4    } else if (starts_ci(rest_base, "wss://")) {
66 1        scheme = "wss://";
67 1        rest = rest_base + 6;
68 3    } else if (starts_ci(rest_base, "ws://")) {
69 1        scheme = "ws://";
70 1        rest = rest_base + 5;
71     } else {
72 2        return -1;
73     }
74 33    n = strlen(rest);
75 37    while (n > 0 && rest[n - 1] == '/') n--;
76 33    if (n == 0 || rest[0] == '/') return -1;
77 31    w = snprintf(out, cap, "%s%.*s", scheme, (int)n, rest);
78 31    if (w < 0 || (size_t)w >= cap) {
79 1        out[0] = 0;
80 1        return -1;
81     }
82 30    return 0;
83 }
84 
85 /* Query parameters the handshake sets itself. */
86 9static int own_query_name(const char *n) {
87 8    return strcmp(n, "appKey") == 0 || strcmp(n, "timestamp") == 0 || strcmp(n, "nonce") == 0 ||
88 17           strcmp(n, "idempotencyKey") == 0 || strcmp(n, "signature") == 0;
89 }
90 
91 32int ygst_ws_handshake_url(const char *rest_base, const char *core_type, const char *app_key, const char *secret,
92                           int64_t timestamp_sec, const char *nonce, const char *idempotency_key,
93                           const ygst_fields *extra, ygst_buf *out) {
94     char base[1024];
95     char ts[32];
96     char sig[45];
97     ygst_fields q;
98     size_t i;
99 32    int rc = -1;
100 32    if (!out || !core_type || !core_type[0] || ygst_is_blank(app_key) || !secret || !secret[0] || !nonce ||
101 28        !nonce[0] || !ygst_idempotency_key_ok(idempotency_key)) {
102 6        return -1;
103     }
104 26    if (ygst_ws_base_url(rest_base, base, sizeof base) != 0) return -1;
105 25    snprintf(ts, sizeof ts, "%lld", (long long)timestamp_sec);
106 25    ygst_fields_init(&q);
107 25    ygst_fields_set(&q, "appKey", app_key);
108 25    ygst_fields_set(&q, "timestamp", ts);
109 25    ygst_fields_set(&q, "nonce", nonce);
110 25    ygst_fields_set(&q, "idempotencyKey", idempotency_key);
111 25    if (extra) {
112 34        for (i = 0; i < extra->count; i++) {
113 10            const ygst_field *f = &extra->items[i];
114 10            if (!f->name || !f->name[0] || !f->value || !f->value[0] || own_query_name(f->name)) continue;
115 7            ygst_fields_set(&q, f->name, f->value);
116         }
117     }
118 25    if (q.oom || ygst_sign(&q, secret, sig) != 0) goto done;
119 25    ygst_buf_append_str(out, base);
120 25    ygst_buf_append_char(out, '/');
121 25    ygst_url_encode(out, core_type);
122 132    for (i = 0; i < q.count; i++) {
123 107        ygst_buf_append_char(out, i == 0 ? '?' : '&');
124 107        ygst_url_encode(out, q.items[i].name);
125 107        ygst_buf_append_char(out, '=');
126 107        ygst_url_encode(out, q.items[i].value);
127     }
128 25    ygst_buf_append_str(out, "&signature=");
129 25    ygst_url_encode(out, sig);
130 25    ygst_buf_cstr(out);
131 25    rc = out->oom ? -1 : 0;
132 25done:
133 25    ygst_fields_free(&q);
134 25    return rc;
135 }
136 
137 /* ------------------------------------------------------------------ parameter frame */
138 
139 /* The platform reads these names as commands (cmd, end, data) or the frame sets them itself. */
140 71static int reserved_frame_name(const char *n, int flags) {
141 70    return strcmp(n, "cmd") == 0 || strcmp(n, "end") == 0 || strcmp(n, "data") == 0 ||
142 206           strcmp(n, "realtime_feedback") == 0 || strcmp(n, "idempotencyKey") == 0 ||
143 65           ((flags & YGST_WSP_TIMING) && strcmp(n, "timing") == 0);
144 }
145 
146 33int ygst_ws_param_frame(const ygst_fields *fields, const char *idempotency_key, int flags, ygst_buf *out) {
147     size_t i;
148 33    int skipped = 0;
149 33    if (!fields || !out || fields->oom || !ygst_idempotency_key_ok(idempotency_key)) return -1;
150 31    ygst_buf_append_char(out, '{');
151 102    for (i = 0; i < fields->count; i++) {
152 71        const ygst_field *f = &fields->items[i];
153 71        if (!f->name || reserved_frame_name(f->name, flags)) {
154 7            skipped++;
155 7            continue;
156         }
157 64        ygst_json_escape(out, f->name);
158 64        ygst_buf_append_char(out, ':');
159 64        ygst_json_escape(out, f->value ? f->value : "");
160 64        ygst_buf_append_char(out, ',');
161     }
162     /* 2.1.1: progress frames only when the app asked for them, the timing object only for the log */
163 31    if (flags & YGST_WSP_REALTIME_FEEDBACK) ygst_buf_append_str(out, "\"realtime_feedback\":true,");
164 31    if (flags & YGST_WSP_TIMING) ygst_buf_append_str(out, "\"timing\":1,");
165 31    ygst_buf_append_str(out, "\"idempotencyKey\":");
166 31    ygst_json_escape(out, idempotency_key);
167 31    ygst_buf_append_char(out, '}');
168 31    ygst_buf_cstr(out);
169 31    return out->oom ? -1 : skipped;
170 }
171 
172 /* ------------------------------------------------------------------ frames */
173 
174 /* An integer member, also when a gateway quoted it. 1 found, 0 absent or not an integer. */
175 1136static int int_member(ygst_span obj, const char *key, long long *out) {
176     ygst_span v;
177 1136    if (ygst_json_member(obj, key, &v) != 1) return 0;
178 1070    if (ygst_json_type(v) == 's') {
179         ygst_buf b;
180 6        int ok = 0;
181 6        ygst_buf_init(&b);
182 6        if (ygst_json_string(v, &b) == 0 && b.len > 0 && b.len < 20) {
183 6            char *end = NULL;
184 6            long long x = strtoll(ygst_buf_cstr(&b), &end, 10);
185 6            if (end && *end == 0) {
186 6                *out = x;
187 6                ok = 1;
188             }
189         }
190 6        ygst_buf_free(&b);
191 6        return ok;
192     }
193 1064    return ygst_json_int(v, out) == 0;
194 }
195 
196 8592static int bool_member(ygst_span obj, const char *key) {
197     ygst_span v;
198 8592    int b = 0;
199 8592    if (ygst_json_member(obj, key, &v) != 1 || ygst_json_bool(v, &b) != 0) return 0;
200 8565    return b;
201 }
202 
203 614static int count_value(long long v) {
204 614    if (v < 0) return -1;
205 614    return v > 2147483647LL ? 2147483647 : (int)v;
206 }
207 
208 /* The event name when it is a short JSON string, else "". */
209 142static void event_name(ygst_span ev, char *out, size_t cap) {
210     ygst_buf b;
211 142    out[0] = 0;
212 142    if (ygst_json_type(ev) != 's') return;
213 141    ygst_buf_init(&b);
214 141    if (ygst_json_string(ev, &b) == 0 && b.len < cap) memcpy(out, ygst_buf_cstr(&b), b.len + 1);
215 141    ygst_buf_free(&b);
216 }
217 
218 216static void parse_progress(ygst_ws_frame *f) {
219     ygst_span r, list, item;
220     long long v;
221 216    size_t pos = 0;
222 216    int read = 0, matched = 0, total = 0;
223 216    if (ygst_json_member(f->root, "result", &r) != 1 || ygst_json_type(r) != 'o') return;
224 215    f->result = r;
225 215    if (int_member(r, "bytes", &v) && v >= 0) f->bytes = v;
226 215    f->read_units = int_member(r, "readUnits", &v) ? count_value(v) : -1;
227 215    f->matched_units = int_member(r, "matchedUnits", &v) ? count_value(v) : -1;
228 215    f->total_units = int_member(r, "totalUnits", &v) ? count_value(v) : -1;
229     /* every unit with its state */
230 215    if (ygst_json_member(r, "progress", &list) == 1 && ygst_json_type(list) == 'a') {
231 197        f->has_progress = 1;
232 4479        while (ygst_json_array_next(list, &pos, &item) == 1) {
233 4282            total++;
234 4282            if (ygst_json_type(item) != 'o') continue;
235 4281            if (bool_member(item, "read")) read++;
236 4281            if (bool_member(item, "matched")) matched++;
237         }
238 197        if (f->read_units < 0) f->read_units = read;
239 197        if (f->matched_units < 0) f->matched_units = matched;
240 197        if (f->total_units < 0) f->total_units = total;
241     }
242     /* Shengtong items of the units read since the previous frame */
243 215    if (ygst_json_member(r, "words", &list) == 1 && ygst_json_type(list) == 'a') {
244 175        f->has_words = 1;
245 175        f->words = list;
246 175        pos = 0;
247 506        while (ygst_json_array_next(list, &pos, &item) == 1) f->word_count++;
248     }
249 }
250 
251 30static void parse_final(ygst_ws_frame *f) {
252     ygst_span r;
253 30    if (ygst_json_member(f->root, "result", &r) == 1) f->result = r;
254 30    f->replayed = bool_member(f->root, "replayed");
255 30}
256 
257 392int ygst_ws_frame_parse(const char *text, size_t n, ygst_ws_frame *f) {
258     ygst_span root, v;
259 392    if (!f) return -1;
260 391    memset(f, 0, sizeof *f);
261 391    f->kind = YGST_WSF_INVALID;
262 391    f->bytes = -1;
263 391    f->read_units = -1;
264 391    f->matched_units = -1;
265 391    f->total_units = -1;
266 391    if (!text || ygst_json_parse(text, n, &root) != 0 || ygst_json_type(root) != 'o') return -1;
267 387    f->root = root;
268 387    if (ygst_json_member(root, "event", &v) == 1) {
269         char ev[16];
270 142        event_name(v, ev, sizeof ev);
271 142        if (strcmp(ev, "connected") == 0) {
272 59            f->kind = YGST_WSF_CONNECTED;
273 83        } else if (strcmp(ev, "started") == 0) {
274 59            f->kind = YGST_WSF_STARTED;
275 24        } else if (strcmp(ev, "pong") == 0) {
276 2            f->kind = YGST_WSF_PONG;
277 22        } else if (strcmp(ev, "error") == 0) {
278 18            long long code = 0;
279 18            f->kind = YGST_WSF_ERROR;
280 18            if (int_member(root, "code", &code) && code > 0) f->code = count_value(code);
281 4        } else if (strcmp(ev, "result") == 0) {
282 2            f->kind = YGST_WSF_FINAL;
283 2            parse_final(f);
284         } else {
285 2            f->kind = YGST_WSF_OTHER;
286         }
287 142        return 0;
288     }
289 245    if (ygst_json_member(root, "eof", &v) == 1) {
290 242        long long eof = -1;
291 242        if (int_member(root, "eof", &eof) && eof == 0) {
292 216            f->kind = YGST_WSF_PROGRESS;
293 216            parse_progress(f);
294         } else {
295 26            f->kind = YGST_WSF_FINAL;
296 26            parse_final(f);
297         }
298 242        return 0;
299     }
300 3    if (ygst_json_member(root, "result", NULL) == 1 || ygst_json_member(root, "recordId", NULL) == 1) {
301 2        f->kind = YGST_WSF_FINAL;
302 2        parse_final(f);
303 2        return 0;
304     }
305 1    f->kind = YGST_WSF_OTHER;
306 1    return 0;
307 }
308 
309 16int ygst_ws_classify_error(ygst_span frame, ygst_classification *c) {
310     static const char *const KEYS[] = {"message", "msg", "error", NULL};
311     const char *const *k;
312 16    long long code = 0;
313 16    c->success = 0;
314 16    c->http_status = 0;
315 16    c->local_code = 0;
316 16    c->biz_code = 0;
317 16    c->has_audio_url = 0;
318 16    c->has_record_id = 0;
319 16    c->record_id_raw.p = NULL;
320 16    c->record_id_raw.n = 0;
321 16    c->root.p = NULL;
322 16    c->root.n = 0;
323 16    c->result.p = NULL;
324 16    c->result.n = 0;
325 16    ygst_buf_reset(&c->record_id);
326 16    ygst_buf_reset(&c->audio_url);
327 16    ygst_buf_reset(&c->message);
328 16    if (ygst_json_type(frame) == 'o') {
329 16        if (int_member(frame, "code", &code) && code > 0) c->biz_code = count_value(code);
330 23        for (k = KEYS; *k; k++) {
331             ygst_span v;
332 21            if (ygst_json_member(frame, *k, &v) == 1 && ygst_json_type(v) == 's') {
333 14                ygst_json_string(v, &c->message);
334 14                break;
335             }
336         }
337     }
338 16    if (!c->message.len) ygst_buf_append_str(&c->message, "error frame without a message");
339 16    ygst_buf_cstr(&c->message);
340 16    ygst_buf_cstr(&c->record_id);
341 16    ygst_buf_cstr(&c->audio_url);
342     /* without a business code the platform failed while evaluating: replay like a 5xx */
343 16    c->retryable = c->biz_code ? ygst_is_retryable(0, c->biz_code, 0) : 1;
344 16    c->err_id = ygst_errid_for(0, c->biz_code, 0, c->retryable);
345 16    return c->message.oom ? -1 : 0;
346 }
347 
348 /* ------------------------------------------------------------------ words dedupe */
349 
350 48void ygst_words_dedupe_init(ygst_words_dedupe *d) {
351 48    memset(d, 0, sizeof *d);
352 48}
353 
354 103int ygst_words_dedupe_skip(ygst_words_dedupe *d, int read_units, int word_count) {
355     long start;
356     long skip;
357 103    if (word_count <= 0) return 0;
358 102    if (read_units < 0) read_units = d->delivered + word_count; /* position unknown: the next units */
359 102    start = (long)read_units - word_count;
360 102    skip = d->delivered > start ? (long)d->delivered - start : 0;
361 102    if (skip >= word_count) return word_count; /* every item was delivered already */
362 78    if (read_units > d->delivered) d->delivered = read_units;
363 78    return (int)skip;
364 }
365 
366 /* ------------------------------------------------------------------ round controller */
367 
368 46void ygst_rt_init(ygst_rt *rt) {
369 46    memset(rt, 0, sizeof *rt);
370 46    ygst_retry_policy_default(&rt->policy);
371 46    rt->connect_timeout_ms = 20000;
372 46    rt->attempt_timeout_ms = 60000;
373 46    rt->ping_interval_ms = 15000;
374 46    rt->pong_timeout_ms = 30000;
375 46    rt->max_frame_bytes = YGST_WS_MAX_FRAME_BYTES;
376 46    rt->state = YGST_RT_IDLE;
377 46    rt->frame.kind = YGST_WSF_INVALID;
378 46    ygst_words_dedupe_init(&rt->dedupe);
379 46    ygst_classification_init(&rt->cls);
380 46    ygst_buf_init(&rt->failure);
381 46}
382 
383 46void ygst_rt_free(ygst_rt *rt) {
384 46    ygst_classification_free(&rt->cls);
385 46    ygst_buf_free(&rt->failure);
386 46}
387 
388 1461static ygst_rt_actions no_actions(void) {
389     ygst_rt_actions a;
390 1461    memset(&a, 0, sizeof a);
391 1461    return a;
392 }
393 
394 960static int on_connection(ygst_rt_state s) {
395 960    return s == YGST_RT_CONNECTING || s == YGST_RT_STARTING || s == YGST_RT_STREAMING || s == YGST_RT_ENDING;
396 }
397 
398 190static void clear_timers(ygst_rt *rt) {
399 190    rt->deadline_ms = 0;
400 190    rt->next_ping_ms = 0;
401 190    rt->pong_deadline_ms = 0;
402 190}
403 
404 36static void note_failure(ygst_rt *rt, const char *why) {
405 36    ygst_buf_reset(&rt->failure);
406 36    ygst_buf_append_str(&rt->failure, why ? why : "");
407 36    ygst_buf_cstr(&rt->failure);
408 36}
409 
410 60static void open_connection(ygst_rt *rt, ygst_rt_actions *a, int64_t now) {
411 60    a->connect = 1;
412 60    rt->state = YGST_RT_CONNECTING;
413 60    rt->connections++;
414 60    rt->sent = 0;
415 60    clear_timers(rt);
416 60    rt->deadline_ms = now + rt->connect_timeout_ms;
417 60}
418 
419 274static void send_pending_audio(ygst_rt *rt, ygst_rt_actions *a) {
420 274    if (rt->total > rt->sent) {
421 247        a->audio_from = rt->sent;
422 247        a->audio_to = rt->total;
423 247        rt->sent = rt->total;
424     }
425 274}
426 
427 32static void start_streaming(ygst_rt *rt, ygst_rt_actions *a, int64_t now) {
428 32    rt->state = YGST_RT_STREAMING;
429 32    clear_timers(rt);
430 32    if (rt->ping_interval_ms > 0) rt->next_ping_ms = now + rt->ping_interval_ms;
431 32    send_pending_audio(rt, a);
432 32}
433 
434 /* All audio from the current offset, then end. The first end starts the attempts of the REST
435  * call controller; a reconnect only sends it again. */
436 43static void send_end(ygst_rt *rt, ygst_rt_actions *a, int64_t now) {
437 43    send_pending_audio(rt, a);
438 43    a->send_end = 1;
439 43    if (!rt->end_sent) {
440 31        rt->end_sent = 1;
441 31        ygst_call_init(&rt->call, &rt->policy, rt->seed, now);
442     }
443 43    rt->state = YGST_RT_ENDING;
444     /* no heartbeat while the platform evaluates: the result timeout bounds the wait, as a REST
445      * attempt's read timeout does */
446 43    clear_timers(rt);
447 43    rt->deadline_ms = now + rt->attempt_timeout_ms;
448 43}
449 
450 /* Before end: the platform never evaluated this round, REST evaluates it with the same tokenId. */
451 10static void fall_back(ygst_rt *rt, ygst_rt_actions *a, const char *why) {
452 10    a->close = 1;
453 10    rt->state = YGST_RT_FALLBACK;
454 10    clear_timers(rt);
455 10    note_failure(rt, why);
456 10    if (rt->end_requested) a->fallback = 1;
457 10}
458 
459 /* After end: rt->cls holds the outcome of this attempt; the REST call controller decides. */
460 26static void attempt_failed(ygst_rt *rt, ygst_rt_actions *a, int64_t now) {
461 26    ygst_action next = ygst_call_next(&rt->call, &rt->cls, -1, now);
462 26    a->close = 1;
463 26    clear_timers(rt);
464 26    note_failure(rt, ygst_buf_cstr(&rt->cls.message));
465 26    if (next.kind == YGST_ACT_RETRY) {
466 16        rt->state = YGST_RT_BACKOFF;
467 16        rt->reconnect_at_ms = now + next.delay_ms;
468 16        a->retry = 1;
469 16        a->delay_ms = next.delay_ms;
470 16        return;
471     }
472 10    rt->state = YGST_RT_DONE;
473 10    a->fail = 1;
474 10    a->err_id = next.kind == YGST_ACT_FAIL ? next.err_id : YGST_LOCAL_PROTOCOL;
475 }
476 
477 24static void connection_failed(ygst_rt *rt, ygst_rt_actions *a, int local_code, int http_status, const char *message,
478                               int64_t now) {
479     ygst_outcome o;
480 24    if (!rt->end_sent) {
481 8        fall_back(rt, a, message);
482 8        return;
483     }
484 16    memset(&o, 0, sizeof o);
485 16    o.local_code = local_code;
486 16    o.http_status = local_code ? 0 : http_status;
487 16    o.retry_after_ms = -1;
488 16    o.local_message = message;
489 16    ygst_classify(&o, &rt->cls);
490 16    if (!local_code && http_status > 0 && message && message[0]) {
491         /* a rejected handshake has no body: say so instead of repeating the status */
492 2        ygst_buf_reset(&rt->cls.message);
493 2        ygst_buf_append_str(&rt->cls.message, message);
494 2        ygst_buf_cstr(&rt->cls.message);
495     }
496 16    attempt_failed(rt, a, now);
497 }
498 
499 46ygst_rt_actions ygst_rt_start(ygst_rt *rt, int64_t now_ms) {
500 46    ygst_rt_actions a = no_actions();
501 46    if (rt->state != YGST_RT_IDLE) return a;
502 45    if (rt->max_frame_bytes == 0) rt->max_frame_bytes = YGST_WS_MAX_FRAME_BYTES;
503 45    open_connection(rt, &a, now_ms);
504 45    return a;
505 }
506 
507 356ygst_rt_actions ygst_rt_on_audio(ygst_rt *rt, size_t total_bytes, int64_t now_ms) {
508 356    ygst_rt_actions a = no_actions();
509     (void)now_ms;
510 356    if (rt->end_requested) return a; /* the round audio is complete */
511 355    if (total_bytes > rt->total) rt->total = total_bytes;
512 355    if (rt->state == YGST_RT_STREAMING) send_pending_audio(rt, &a);
513 355    return a;
514 }
515 
516 40ygst_rt_actions ygst_rt_on_stop(ygst_rt *rt, size_t total_bytes, int64_t now_ms) {
517 40    ygst_rt_actions a = no_actions();
518 40    if (rt->end_requested || rt->state == YGST_RT_DONE) return a;
519 38    if (total_bytes > rt->total) rt->total = total_bytes;
520 38    rt->end_requested = 1;
521 38    switch (rt->state) {
522 6    case YGST_RT_IDLE:
523     case YGST_RT_FALLBACK:
524 6        rt->state = YGST_RT_FALLBACK;
525 6        a.fallback = 1;
526 6        break;
527 24    case YGST_RT_STREAMING:
528 24        send_end(rt, &a, now_ms);
529 24        break;
530 8    default:
531 8        break; /* CONNECTING, STARTING: end goes out right after "started" */
532     }
533 38    return a;
534 }
535 
536 263ygst_rt_actions ygst_rt_on_text(ygst_rt *rt, const char *text, size_t n, int64_t now_ms) {
537 263    ygst_rt_actions a = no_actions();
538 263    if (!on_connection(rt->state)) return a;
539 259    if (ygst_ws_frame_parse(text, n, &rt->frame) != 0) return a;
540 258    switch (rt->frame.kind) {
541 53    case YGST_WSF_CONNECTED:
542 53        if (rt->state == YGST_RT_CONNECTING) {
543 52            a.send_params = 1;
544 52            rt->state = YGST_RT_STARTING;
545         }
546 53        break;
547 52    case YGST_WSF_STARTED:
548 52        if (rt->state == YGST_RT_STARTING) {
549 51            if (rt->end_sent || rt->end_requested) {
550 19                send_end(rt, &a, now_ms);
551             } else {
552 32                start_streaming(rt, &a, now_ms);
553             }
554         }
555 52        break;
556 123    case YGST_WSF_PROGRESS:
557         /* delivered only with new words; empty words and bytes-only frames are not */
558 123        if (rt->frame.word_count > 0) {
559 93            int skip = ygst_words_dedupe_skip(&rt->dedupe, rt->frame.read_units, rt->frame.word_count);
560 93            if (skip < rt->frame.word_count) {
561 71                a.progress = 1;
562 71                a.words_skip = skip;
563             } else {
564 22                a.progress_held = 1;
565             }
566         }
567 123        break;
568 19    case YGST_WSF_FINAL:
569 19        if (rt->state != YGST_RT_ENDING) {
570 1            if (!rt->end_sent) fall_back(rt, &a, "result frame before end");
571 1            break;
572         }
573         {
574             ygst_outcome o;
575 18            memset(&o, 0, sizeof o);
576 18            o.http_status = 200;
577 18            o.body = text;
578 18            o.body_len = n;
579 18            o.retry_after_ms = -1;
580 18            o.replayed = rt->frame.replayed;
581 18            ygst_classify(&o, &rt->cls);
582         }
583 18        if (rt->cls.success) {
584 17            (void)ygst_call_next(&rt->call, &rt->cls, -1, now_ms);
585 17            rt->state = YGST_RT_DONE;
586 17            clear_timers(rt);
587 17            a.close = 1;
588 17            a.final = 1;
589         } else {
590 1            attempt_failed(rt, &a, now_ms);
591         }
592 18        break;
593 10    case YGST_WSF_ERROR:
594 10        ygst_ws_classify_error(rt->frame.root, &rt->cls);
595 10        if (!rt->end_sent) {
596 1            fall_back(rt, &a, ygst_buf_cstr(&rt->cls.message));
597         } else {
598 9            attempt_failed(rt, &a, now_ms);
599         }
600 10        break;
601 1    default:
602 1        break; /* pong, other events */
603     }
604 258    return a;
605 }
606 
607 3ygst_rt_actions ygst_rt_on_pong(ygst_rt *rt, int64_t now_ms) {
608 3    ygst_rt_actions a = no_actions();
609 3    if (rt->state == YGST_RT_STREAMING && rt->pong_deadline_ms) {
610 1        rt->pong_deadline_ms = 0;
611 1        if (rt->ping_interval_ms > 0) rt->next_ping_ms = now_ms + rt->ping_interval_ms;
612     }
613 3    return a;
614 }
615 
616 20ygst_rt_actions ygst_rt_on_failure(ygst_rt *rt, int local_code, int http_status, const char *message,
617                                    int64_t now_ms) {
618 20    ygst_rt_actions a = no_actions();
619 20    if (!on_connection(rt->state)) return a;
620 18    connection_failed(rt, &a, local_code, http_status, message, now_ms);
621 18    return a;
622 }
623 
624 733ygst_rt_actions ygst_rt_on_tick(ygst_rt *rt, int64_t now_ms) {
625 733    ygst_rt_actions a = no_actions();
626     char msg[96];
627 733    if (rt->state == YGST_RT_BACKOFF) {
628 68        if (now_ms >= rt->reconnect_at_ms) open_connection(rt, &a, now_ms);
629 68        return a;
630     }
631 665    if (!on_connection(rt->state)) return a;
632 500    if (rt->deadline_ms && now_ms >= rt->deadline_ms) {
633 4        int ending = rt->state == YGST_RT_ENDING;
634 4        if (ending) {
635 2            snprintf(msg, sizeof msg, "timeout: no result within %d ms", rt->attempt_timeout_ms);
636         } else {
637 2            snprintf(msg, sizeof msg, "timeout: not started within %d ms", rt->connect_timeout_ms);
638         }
639 4        connection_failed(rt, &a, ending ? YGST_LOCAL_RESULT_TIMEOUT : YGST_LOCAL_TIMEOUT, 0, msg, now_ms);
640 4        return a;
641     }
642 496    if (rt->pong_deadline_ms && now_ms >= rt->pong_deadline_ms) {
643 2        snprintf(msg, sizeof msg, "timeout: no pong within %d ms", rt->pong_timeout_ms);
644 2        connection_failed(rt, &a, YGST_LOCAL_TIMEOUT, 0, msg, now_ms);
645 2        return a;
646     }
647 494    if (rt->state == YGST_RT_STREAMING && rt->next_ping_ms && !rt->pong_deadline_ms && now_ms >= rt->next_ping_ms) {
648 4        a.send_ping = 1;
649 4        rt->next_ping_ms = 0;
650 4        rt->pong_deadline_ms = now_ms + rt->pong_timeout_ms;
651     }
652 494    return a;
653 }
654 
655 2void ygst_rt_cancel(ygst_rt *rt) {
656 2    rt->state = YGST_RT_DONE;
657 2    clear_timers(rt);
658 2}
659 
660 12int ygst_rt_active(const ygst_rt *rt) {
661 12    return on_connection(rt->state) || rt->state == YGST_RT_BACKOFF;
662 }
663 
664 5int ygst_rt_error_message(const ygst_rt *rt, int err_id, ygst_buf *out) {
665 5    return ygst_error_message(&rt->cls, err_id, rt->call.attempt, out);
666 }

Sources/STKouyuEngine/core/ygst_retry.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Retry policy (DESIGN 2.3), Shengtong errId mapping (DESIGN 6.2), response classification and
6  * the per-call controller that decides between delivering, retrying and failing. The Objective-C
7  * layer executes the decisions with NSURLSession; the Linux tests execute them with sockets.
8  */
9 #include "ygst_core.h"
10 
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 /* ------------------------------------------------------------------ table lookups */
17 
18 307static const ygst_error_entry *find_in(const ygst_error_entry *t, size_t n, int code) {
19     size_t i;
20 4165    for (i = 0; i < n; i++) {
21 4148        if (t[i].code == code) return &t[i];
22     }
23 17    return NULL;
24 }
25 
26 223const ygst_error_entry *ygst_error_find(int code) {
27 223    return find_in(ygst_table_errors, ygst_table_errors_count, code);
28 }
29 
30 9const ygst_error_entry *ygst_warning_find(int code) {
31 9    return find_in(ygst_table_warnings, ygst_table_warnings_count, code);
32 }
33 
34 75const ygst_error_entry *ygst_local_find(int code) {
35 75    return find_in(ygst_table_local, ygst_table_local_count, code);
36 }
37 
38 58const char *ygst_compat_message(int err_id) {
39     size_t i;
40 252    for (i = 0; i < ygst_table_compat_count; i++) {
41 247        if (ygst_table_compat[i].err_id == err_id) return ygst_table_compat[i].message;
42     }
43 5    return NULL;
44 }
45 
46 49int ygst_http_retryable(int status) {
47     size_t i;
48 313    for (i = 0; i < ygst_table_retryable_http_count; i++) {
49 287        if (ygst_table_retryable_http[i] == status) return 1;
50     }
51 26    return 0;
52 }
53 
54 /* ------------------------------------------------------------------ RNG (splitmix64) */
55 
56 130void ygst_rng_seed(ygst_rng *r, uint64_t seed) {
57 130    r->s = seed;
58 130}
59 
60 1086uint64_t ygst_rng_next(ygst_rng *r) {
61 1086    uint64_t z = (r->s += 0x9E3779B97F4A7C15ull);
62 1086    z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
63 1086    z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
64 1086    return z ^ (z >> 31);
65 }
66 
67 1084double ygst_rng_uniform(ygst_rng *r) {
68 1084    return (double)(ygst_rng_next(r) >> 11) * (1.0 / 9007199254740992.0);
69 }
70 
71 /* ------------------------------------------------------------------ policy */
72 
73 155void ygst_retry_policy_default(ygst_retry_policy *p) {
74 155    memset(p, 0, sizeof *p);
75 155    p->max_retries = 2;
76 155    p->initial_delay_ms = 200;
77 155    p->multiplier = 2.0;
78 155    p->max_delay_ms = 4000;
79 155    p->jitter = 0.3;
80 155    p->respect_retry_after = 1;
81 155    p->max_retry_after_ms = 30000;
82 155    p->total_timeout_ms = 300000;
83 155    p->auto_retry = 0;
84 155    p->max_auto_retries = 2;
85 155    p->err_ids[0] = YGST_ERRID_RETRYABLE;
86 155    p->n_err_ids = 1;
87 155}
88 
89 28int ygst_retry_policy_add_err_id(ygst_retry_policy *p, long long err_id) {
90 28    if (err_id <= 0 || err_id > 2147483647LL) return -1;
91 25    if (ygst_retry_policy_has_err_id(p, (int)err_id)) return 0;
92 24    if (p->n_err_ids >= YGST_MAX_ERR_IDS) return -1;
93 17    p->err_ids[p->n_err_ids++] = (int)err_id;
94 17    return 0;
95 }
96 
97 40int ygst_retry_policy_has_err_id(const ygst_retry_policy *p, int err_id) {
98     int i;
99 280    for (i = 0; i < p->n_err_ids; i++) {
100 253        if (p->err_ids[i] == err_id) return 1;
101     }
102 27    return 0;
103 }
104 
105 93long ygst_backoff_delay_ms(const ygst_retry_policy *p, int n, ygst_rng *rng) {
106     double base;
107 93    double factor = 1.0;
108     double d;
109 93    if (n < 1) n = 1;
110 93    base = (double)p->initial_delay_ms * pow(p->multiplier > 0 ? p->multiplier : 1.0, (double)(n - 1));
111 93    if (base > p->max_delay_ms) base = p->max_delay_ms;
112 93    if (p->jitter > 0 && rng) factor = 1.0 + (ygst_rng_uniform(rng) * 2.0 - 1.0) * p->jitter;
113 93    d = base * factor;
114 93    if (d < 0) d = 0;
115 93    return (long)(d + 0.5);
116 }
117 
118 /* days since 1970-01-01 for a proleptic Gregorian date (H. Hinnant, days_from_civil) */
119 3static long long days_from_civil(long long y, unsigned m, unsigned d) {
120     long long era;
121     unsigned yoe, doy, doe;
122 3    y -= m <= 2;
123 3    era = (y >= 0 ? y : y - 399) / 400;
124 3    yoe = (unsigned)(y - era * 400);
125 3    doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
126 3    doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
127 3    return era * 146097 + (long long)doe - 719468;
128 }
129 
130 15int ygst_parse_retry_after(const char *value, int64_t now_ms, long *out_ms) {
131     static const char *const MON[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
132                                       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
133     char v[64];
134     size_t n;
135     size_t i;
136 15    if (!value || !out_ms) return -1;
137 14    n = ygst_trim_copy(value, v, sizeof v);
138 14    if (n == 0) return -1;
139 30    for (i = 0; i < n && v[i] >= '0' && v[i] <= '9'; i++) {
140     }
141 13    if (i == n) {
142 6        long secs = 0;
143 21        for (i = 0; i < n; i++) {
144 16            if (secs > 100000000L) return -1;
145 15            secs = secs * 10 + (v[i] - '0');
146         }
147 5        *out_ms = secs * 1000L;
148 5        return 0;
149     }
150     /* IMF-fixdate: "Sun, 06 Nov 1994 08:49:37 GMT" */
151 7    if (n == 29 && v[3] == ',' && v[4] == ' ' && v[7] == ' ' && v[11] == ' ' && v[16] == ' ' && v[19] == ':' &&
152 5        v[22] == ':' && v[25] == ' ' && strcmp(v + 26, "GMT") == 0) {
153 5        unsigned day, mon = 0, hh, mm, ss;
154         long long year;
155         long long t;
156 65        for (i = 0; i < 12; i++) {
157 60            if (strncmp(v + 8, MON[i], 3) == 0) mon = (unsigned)i + 1;
158         }
159 5        if (!mon) return -1;
160 4        day = (unsigned)((v[5] - '0') * 10 + (v[6] - '0'));
161 4        year = (v[12] - '0') * 1000 + (v[13] - '0') * 100 + (v[14] - '0') * 10 + (v[15] - '0');
162 4        hh = (unsigned)((v[17] - '0') * 10 + (v[18] - '0'));
163 4        mm = (unsigned)((v[20] - '0') * 10 + (v[21] - '0'));
164 4        ss = (unsigned)((v[23] - '0') * 10 + (v[24] - '0'));
165 4        if (day < 1 || day > 31 || hh > 23 || mm > 59 || ss > 60) return -1;
166 3        t = days_from_civil(year, mon, day) * 86400LL + hh * 3600LL + mm * 60LL + ss;
167 3        t = t * 1000LL - now_ms;
168 3        *out_ms = t > 0 ? (long)(t > 2000000000LL ? 2000000000LL : t) : 0;
169 3        return 0;
170     }
171 2    return -1;
172 }
173 
174 /* ------------------------------------------------------------------ retry and errId rules */
175 
176 269int ygst_is_retryable(int local_code, int biz_code, int http_status) {
177     const ygst_error_entry *e;
178 269    if (local_code) {
179 55        return local_code == YGST_LOCAL_NETWORK || local_code == YGST_LOCAL_TIMEOUT ||
180                local_code == YGST_LOCAL_RESULT_TIMEOUT;
181     }
182 214    if (biz_code) {
183 178        e = ygst_error_find(biz_code);
184 178        if (e) return e->retryable;
185     }
186 41    return ygst_http_retryable(http_status);
187 }
188 
189 24int ygst_http_fallback_code(int status) {
190 24    const char *cat = ygst_http_fallback_category(status);
191 24    if (strcmp(cat, "AUTH") == 0) return 40100;
192 22    if (strcmp(cat, "PERMISSION") == 0) return 40300;
193 20    if (strcmp(cat, "NOT_FOUND") == 0) return 40400;
194 17    if (strcmp(cat, "CONFLICT") == 0) return 40900;
195 16    if (strcmp(cat, "RATE_LIMIT") == 0) return 42900;
196 15    if (strcmp(cat, "SERVER") == 0) return status == 501 ? 50010 : 50000;
197 12    if (strcmp(cat, "UPSTREAM") == 0) return 50200;
198 10    if (strcmp(cat, "TIMEOUT") == 0) return YGST_ERRID_RETRYABLE;
199 9    if (strcmp(cat, "INVALID_PARAM") == 0) return 40001;
200 3    return YGST_LOCAL_PROTOCOL;
201 }
202 
203 239int ygst_errid_for(int local_code, int biz_code, int http_status, int retryable) {
204 239    if (local_code >= 60001 && local_code <= 60009) return local_code;
205 228    if (retryable) return YGST_ERRID_RETRYABLE;
206 115    if (biz_code) return biz_code;
207 46    if (local_code) return local_code;
208 24    return ygst_http_fallback_code(http_status);
209 }
210 
211 /* ------------------------------------------------------------------ classification */
212 
213 262void ygst_classification_init(ygst_classification *c) {
214 262    memset(c, 0, sizeof *c);
215 262    ygst_buf_init(&c->record_id);
216 262    ygst_buf_init(&c->audio_url);
217 262    ygst_buf_init(&c->message);
218 262}
219 
220 262void ygst_classification_free(ygst_classification *c) {
221 262    ygst_buf_free(&c->record_id);
222 262    ygst_buf_free(&c->audio_url);
223 262    ygst_buf_free(&c->message);
224 262}
225 
226 181static int member_int(ygst_span obj, const char *key, long long *out) {
227     ygst_span v;
228 181    if (ygst_json_member(obj, key, &v) != 1) return 0;
229 156    if (ygst_json_type(v) == 's') {
230         /* some gateways quote numeric codes */
231         ygst_buf b;
232 3        int ok = 0;
233 3        ygst_buf_init(&b);
234 3        if (ygst_json_string(v, &b) == 0 && b.len > 0 && b.len < 12) {
235 3            char *end = NULL;
236 3            long long x = strtoll(ygst_buf_cstr(&b), &end, 10);
237 3            if (end && *end == 0) {
238 3                *out = x;
239 3                ok = 1;
240             }
241         }
242 3        ygst_buf_free(&b);
243 3        return ok;
244     }
245 153    return ygst_json_int(v, out) == 0;
246 }
247 
248 /* First of the keys whose value is a JSON string. */
249 135static int first_string(ygst_span obj, const char *const *keys, ygst_buf *out) {
250 171    for (; *keys; keys++) {
251         ygst_span v;
252 161        if (ygst_json_member(obj, *keys, &v) == 1 && ygst_json_type(v) == 's') {
253 125            ygst_buf_reset(out);
254 125            return ygst_json_string(v, out) == 0;
255         }
256     }
257 10    return 0;
258 }
259 
260 /* "[2001] signature mismatch": the compat engine code in a FastAPI detail, 3 to 6 digits. */
261 7static int bracket_code(const char *msg) {
262 7    long v = 0;
263 7    int digits = 0;
264 7    if (!msg) return 0;
265 8    while (*msg == ' ' || *msg == '\t' || *msg == '\n' || *msg == '\r') msg++;
266 7    if (*msg != '[') return 0;
267 3    msg++;
268 14    while (*msg >= '0' && *msg <= '9' && digits < 7) {
269 11        v = v * 10 + (*msg - '0');
270 11        msg++;
271 11        digits++;
272     }
273 3    if (*msg != ']' || digits < 3 || digits > 6) return 0;
274 2    return (int)v;
275 }
276 
277 114static void set_message(ygst_buf *m, const char *s) {
278 114    ygst_buf_reset(m);
279 114    ygst_buf_append_str(m, s ? s : "");
280 114    ygst_buf_cstr(m);
281 114}
282 
283 305int ygst_classify(const ygst_outcome *o, ygst_classification *c) {
284     static const char *const MESSAGE_KEYS[] = {"message", "msg", "error", NULL};
285     ygst_span root;
286 305    int parsed = 0;
287 305    long long code = 0;
288 305    c->success = 0;
289 305    c->http_status = o->http_status;
290 305    c->local_code = o->local_code;
291 305    c->biz_code = 0;
292 305    c->has_audio_url = 0;
293 305    c->has_record_id = 0;
294 305    c->record_id_raw.p = NULL;
295 305    c->record_id_raw.n = 0;
296 305    c->root.p = NULL;
297 305    c->root.n = 0;
298 305    c->result.p = NULL;
299 305    c->result.n = 0;
300 305    ygst_buf_reset(&c->record_id);
301 305    ygst_buf_reset(&c->audio_url);
302 305    ygst_buf_reset(&c->message);
303 
304 305    if (o->local_code || o->http_status <= 0) {
305         const ygst_error_entry *e;
306 48        if (!c->local_code) c->local_code = YGST_LOCAL_NETWORK;
307 48        e = ygst_local_find(c->local_code);
308 72        set_message(&c->message, (o->local_message && o->local_message[0]) ? o->local_message
309 24                                                                            : (e ? e->message : "network error"));
310 48        c->retryable = ygst_is_retryable(c->local_code, 0, 0);
311 48        c->err_id = ygst_errid_for(c->local_code, 0, 0, c->retryable);
312 48        return 0;
313     }
314 
315 257    if (o->body && o->body_len) parsed = ygst_json_parse(o->body, o->body_len, &root) == 0 && ygst_json_type(root) == 'o';
316 
317 257    if (o->http_status >= 200 && o->http_status < 300) {
318         ygst_span result, v;
319 120        if (parsed && ygst_json_member(root, "result", &result) == 1) {
320             ygst_span inner;
321 76            c->success = 1;
322 76            c->root = root;
323 76            c->result = result;
324 76            if (ygst_json_member(root, "recordId", &v) == 1) {
325 73                c->has_record_id = 1;
326 73                c->record_id_raw = v;
327 73                if (ygst_json_type(v) == 's') {
328 72                    ygst_json_string(v, &c->record_id);
329                 } else {
330 1                    ygst_buf_append(&c->record_id, v.p, v.n);
331                 }
332             }
333 76            if (ygst_json_member(root, "audioUrl", &v) == 1) {
334 5                if (ygst_json_type(v) == 's') c->has_audio_url = ygst_json_string(v, &c->audio_url) == 0;
335 73            } else if (ygst_json_type(result) == 'o' && ygst_json_member(result, "audioUrl", &inner) == 1 &&
336 2                       ygst_json_type(inner) == 's') {
337 1                c->has_audio_url = ygst_json_string(inner, &c->audio_url) == 0;
338             }
339 76            if (c->has_audio_url && c->audio_url.len == 0) c->has_audio_url = 0;
340 76            ygst_buf_cstr(&c->record_id);
341 76            ygst_buf_cstr(&c->audio_url);
342 76            c->retryable = 0;
343 76            c->err_id = 0;
344 76            return 0;
345         }
346 44        c->local_code = YGST_LOCAL_PROTOCOL;
347 44        set_message(&c->message, parsed ? "invalid platform response: platform response has no result"
348                                         : "invalid platform response: not a JSON object");
349 44        c->retryable = 0;
350 44        c->err_id = YGST_LOCAL_PROTOCOL;
351 44        return 0;
352     }
353 
354 137    if (parsed) {
355         ygst_span detail;
356 130        if (member_int(root, "code", &code) && code != 0) c->biz_code = (int)code;
357 130        if (!c->biz_code && member_int(root, "errId", &code) && code != 0) c->biz_code = (int)code;
358 130        first_string(root, MESSAGE_KEYS, &c->message);
359 130        if (ygst_json_member(root, "detail", &detail) == 1) {
360 9            if (ygst_json_type(detail) == 's') {
361                 ygst_buf d;
362 7                ygst_buf_init(&d);
363 7                ygst_json_string(detail, &d);
364 7                if (!c->biz_code) c->biz_code = bracket_code(ygst_buf_cstr(&d));
365 7                if (!c->message.len) set_message(&c->message, ygst_buf_cstr(&d));
366 7                ygst_buf_free(&d);
367 2            } else if (!c->message.len) {
368 1                ygst_buf_reset(&c->message);
369 1                ygst_buf_append(&c->message, detail.p, detail.n > 300 ? 300 : detail.n);
370             }
371         }
372     }
373 137    if (!c->message.len) {
374         char tmp[32];
375 8        snprintf(tmp, sizeof tmp, "HTTP %d", o->http_status);
376 8        set_message(&c->message, tmp);
377     }
378 137    ygst_buf_cstr(&c->message);
379 137    c->retryable = ygst_is_retryable(0, c->biz_code, o->http_status);
380 137    c->err_id = ygst_errid_for(0, c->biz_code, o->http_status, c->retryable);
381 137    return 0;
382 }
383 
384 /* 2.2.0, TtsClient and ReportClient: the {code, message, data} envelope of the v1 API. Local failures
385  * and non-2xx answers are classified by ygst_classify; a 2xx answer by its envelope. */
386 71int ygst_classify_api(const ygst_outcome *o, ygst_classification *c) {
387     static const char *const MESSAGE_KEYS[] = {"message", "msg", "error", NULL};
388     ygst_span root, data;
389 71    long long code = 0;
390     int rc;
391 71    if (!o || !c) return -1;
392 70    rc = ygst_classify(o, c);
393 70    if (o->local_code || o->http_status < 200 || o->http_status >= 300) return rc;
394 42    c->success = 0;
395 42    c->local_code = 0;
396 42    c->biz_code = 0;
397 42    c->has_record_id = 0;
398 42    c->has_audio_url = 0;
399 42    c->record_id_raw.p = NULL;
400 42    c->record_id_raw.n = 0;
401 42    c->root.p = NULL;
402 42    c->root.n = 0;
403 42    c->result.p = NULL;
404 42    c->result.n = 0;
405 42    ygst_buf_reset(&c->record_id);
406 42    ygst_buf_reset(&c->audio_url);
407 42    ygst_buf_reset(&c->message);
408 42    ygst_buf_cstr(&c->record_id);
409 42    ygst_buf_cstr(&c->audio_url);
410 42    if (!o->body || !o->body_len || ygst_json_parse(o->body, o->body_len, &root) != 0 || ygst_json_type(root) != 'o') {
411 3        c->local_code = YGST_LOCAL_PROTOCOL;
412 3        set_message(&c->message, "invalid platform response: not a JSON object");
413 3        c->retryable = 0;
414 3        c->err_id = YGST_LOCAL_PROTOCOL;
415 3        return 0;
416     }
417 39    if (member_int(root, "code", &code) && code != 0) {
418         /* a platform error answered with a 2xx status */
419 5        c->biz_code = (int)code;
420 5        first_string(root, MESSAGE_KEYS, &c->message);
421 5        ygst_buf_cstr(&c->message);
422 5        c->retryable = ygst_is_retryable(0, c->biz_code, o->http_status);
423 5        c->err_id = ygst_errid_for(0, c->biz_code, o->http_status, c->retryable);
424 5        return 0;
425     }
426 34    if (ygst_json_member(root, "data", &data) == 1 && ygst_json_type(data) == 'o') {
427 30        c->success = 1;
428 30        c->root = root;
429 30        c->result = data;
430 30        c->retryable = 0;
431 30        c->err_id = 0;
432 30        ygst_buf_cstr(&c->message);
433 30        return 0;
434     }
435 4    c->local_code = YGST_LOCAL_PROTOCOL;
436 4    set_message(&c->message, "invalid platform response: response has no data");
437 4    c->retryable = 0;
438 4    c->err_id = YGST_LOCAL_PROTOCOL;
439 4    return 0;
440 }
441 
442 38int ygst_error_message(const ygst_classification *c, int err_id, int attempts, ygst_buf *out) {
443 38    const char *m = c->message.data ? (const char *)c->message.data : "";
444 38    if (err_id == YGST_ERRID_RETRYABLE) {
445 14        const char *base = ygst_compat_message(YGST_ERRID_RETRYABLE);
446         size_t mark;
447 14        ygst_buf_append_str(out, base ? base : "");
448 14        ygst_buf_append_str(out, ": ");
449 14        mark = out->len;
450 14        if (c->http_status > 0) {
451 7            ygst_buf_append_str(out, "HTTP ");
452 7            ygst_buf_append_int(out, c->http_status);
453 7            if (c->biz_code) {
454 5                ygst_buf_append_str(out, " code=");
455 5                ygst_buf_append_int(out, c->biz_code);
456             }
457 7        } else if (c->local_code) {
458 4            const ygst_error_entry *e = ygst_local_find(c->local_code);
459 4            if (e) {
460 3                ygst_buf_append_str(out, e->name);
461             } else {
462 1                ygst_buf_append_str(out, "code=");
463 1                ygst_buf_append_int(out, c->local_code);
464             }
465 3        } else if (c->biz_code) {
466             /* a WebSocket error frame: a business code without an HTTP status */
467 2            ygst_buf_append_str(out, "code=");
468 2            ygst_buf_append_int(out, c->biz_code);
469         }
470 14        if (m[0]) {
471 14            if (out->len > mark) ygst_buf_append_char(out, ' ');
472 14            ygst_buf_append_str(out, m);
473         }
474 14        if (attempts > 0) {
475 13            ygst_buf_append_str(out, " (attempts ");
476 13            ygst_buf_append_int(out, attempts);
477 13            ygst_buf_append_char(out, ')');
478         }
479 24    } else if (m[0]) {
480 19        ygst_buf_append_str(out, m);
481     } else {
482 5        const char *cm = ygst_compat_message(err_id);
483 5        const ygst_error_entry *e = cm ? NULL : ygst_error_find(err_id);
484 5        if (!cm && !e) e = ygst_local_find(err_id);
485 5        ygst_buf_append_str(out, cm ? cm : (e ? e->message : ""));
486     }
487 38    ygst_buf_cstr(out);
488 38    return out->oom ? -1 : 0;
489 }
490 
491 /* ------------------------------------------------------------------ controller */
492 
493 126void ygst_call_init(ygst_call *c, const ygst_retry_policy *p, uint64_t seed, int64_t now_ms) {
494 126    memset(c, 0, sizeof *c);
495 126    if (p) {
496 125        c->policy = *p;
497     } else {
498 1        ygst_retry_policy_default(&c->policy);
499     }
500 126    ygst_rng_seed(&c->rng, seed);
501 126    c->start_ms = now_ms;
502 126}
503 
504 72static long apply_retry_after(const ygst_retry_policy *p, long delay, long retry_after_ms) {
505 72    if (p->respect_retry_after && retry_after_ms >= 0) {
506 4        long ra = retry_after_ms > p->max_retry_after_ms ? p->max_retry_after_ms : retry_after_ms;
507 4        if (ra > delay) delay = ra;
508     }
509 72    return delay;
510 }
511 
512 189ygst_action ygst_call_next(ygst_call *c, const ygst_classification *cls, long retry_after_ms, int64_t now_ms) {
513     ygst_action a;
514 189    int64_t deadline = c->start_ms + c->policy.total_timeout_ms;
515 189    a.kind = YGST_ACT_FAIL;
516 189    a.delay_ms = 0;
517 189    a.err_id = cls->err_id;
518 189    a.auto_retry = 0;
519 189    c->attempt++;
520 189    c->total_attempts++;
521 189    if (cls->success) {
522 82        a.kind = YGST_ACT_SUCCESS;
523 82        a.err_id = 0;
524 82        return a;
525     }
526 107    if (cls->retryable && c->attempt <= c->policy.max_retries) {
527 61        long d = apply_retry_after(&c->policy, ygst_backoff_delay_ms(&c->policy, c->attempt, &c->rng), retry_after_ms);
528 61        if (now_ms + d <= deadline) {
529 59            a.kind = YGST_ACT_RETRY;
530 59            a.delay_ms = d;
531 59            return a;
532         }
533 2        return a;
534     }
535 59    if (c->policy.auto_retry && c->submission < c->policy.max_auto_retries &&
536 13        ygst_retry_policy_has_err_id(&c->policy, cls->err_id)) {
537 11        long d = apply_retry_after(&c->policy, ygst_backoff_delay_ms(&c->policy, c->submission + 1, &c->rng),
538                                    retry_after_ms);
539 11        if (now_ms + d <= deadline) {
540 10            c->submission++;
541 10            c->attempt = 0;
542 10            a.kind = YGST_ACT_RETRY;
543 10            a.delay_ms = d;
544 10            a.auto_retry = 1;
545 10            return a;
546         }
547     }
548 36    return a;
549 }

Sources/STKouyuEngine/core/ygst_timing.c 100.00%

1 /*
2  * Copyright 2026 优谷雅言 open.shengzhiai.com
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Per-phase timing of one evaluation (2.1.1, BRIEF C and D): the Server-Timing header of the compat
6  * REST responses, the "timing" object the platform adds to a final JSON on request, the INFO
7  * summary line and the DEBUG detail line. The Objective-C layer and the integration driver record
8  * the phases; everything that turns them into text is here, so that the Linux tests cover it.
9  */
10 #include "ygst_core.h"
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 79void ygst_timing_init(ygst_timing *t) {
16 79    if (!t) return;
17 78    memset(t, 0, sizeof *t);
18 78    t->mode = YGST_TM_NONE;
19 78    t->upload_ms = -1;
20 78    t->wait_ms = -1;
21 78    t->download_ms = -1;
22 78    t->audio_bytes = -1;
23 78    t->upload_bytes = -1;
24 78    t->queued_at_stop = -1;
25 78    t->wire_bytes = -1;
26 78    t->body_bytes = -1;
27 78    t->server_total_ms = -1;
28 78    t->server_engine_ms = -1;
29 78    t->server_platform_ms = -1;
30 }
31 
32 5int ygst_timing_wanted(int log_level) {
33 5    return log_level >= 2;
34 }
35 
36 /* ------------------------------------------------------------------ Server-Timing */
37 
38 536static int is_ows(char c) {
39 536    return c == ' ' || c == '\t';
40 }
41 
42 /* The n bytes at s equal word (lower case) ignoring ASCII case. */
43 67static int word_eq(const char *s, size_t n, const char *word) {
44     size_t i;
45 67    if (strlen(word) != n) return 0;
46 223    for (i = 0; i < n; i++) {
47 180        char c = s[i];
48 180        if (c >= 'A' && c <= 'Z') c = (char)(c + 32);
49 180        if (c != word[i]) return 0;
50     }
51 43    return 1;
52 }
53 
54 /* A plain non-negative decimal (5321, 12.75) in the n bytes at s, rounded half up to whole
55  * milliseconds without the C locale's strtod. -1 when it is not one. */
56 26static long long decimal_ms(const char *s, size_t n) {
57 26    long long whole = 0;
58 26    size_t i = 0;
59 26    int digits = 0;
60 26    int up = 0;
61 90    while (i < n && s[i] >= '0' && s[i] <= '9') {
62 65        if (whole > 900000000000000LL) return -1;
63 64        whole = whole * 10 + (s[i] - '0');
64 64        digits++;
65 64        i++;
66     }
67 25    if (i < n && s[i] == '.') {
68 6        i++;
69 6        if (i < n && s[i] >= '0' && s[i] <= '9') up = s[i] >= '5';
70 12        while (i < n && s[i] >= '0' && s[i] <= '9') {
71 6            digits++;
72 6            i++;
73         }
74     }
75 25    if (!digits || i != n) return -1;
76 20    return whole + up;
77 }
78 
79 20int ygst_server_timing_parse(const char *value, ygst_timing *t) {
80 20    const char *p = value;
81 20    unsigned found = 0;
82 20    if (!value || !t) return 0;
83 29    for (;;) {
84         const char *name;
85         size_t name_n;
86 47        long long dur = -1;
87 73        while (*p == ',' || is_ows(*p)) p++;
88 47        if (!*p) break;
89 29        name = p;
90 182        while (*p && *p != ';' && *p != ',' && !is_ows(*p)) p++;
91 29        name_n = (size_t)(p - name);
92 29        for (;;) { /* ;param=value, value a token or a quoted string */
93             const char *pn;
94             const char *pv;
95             size_t pn_n;
96             size_t pv_n;
97 60            while (is_ows(*p)) p++;
98 58            if (*p != ';') break;
99 29            p++;
100 30            while (is_ows(*p)) p++;
101 29            pn = p;
102 118            while (*p && *p != '=' && *p != ';' && *p != ',' && !is_ows(*p)) p++;
103 29            pn_n = (size_t)(p - pn);
104 30            while (is_ows(*p)) p++;
105 29            if (*p != '=') continue;
106 28            p++;
107 29            while (is_ows(*p)) p++;
108 28            if (*p == '"') {
109 3                pv = ++p;
110 16                while (*p && *p != '"') {
111 13                    if (*p == '\\' && p[1]) p++;
112 13                    p++;
113                 }
114 3                pv_n = (size_t)(p - pv);
115 3                if (*p == '"') p++;
116             } else {
117 25                pv = p;
118 108                while (*p && *p != ';' && *p != ',' && !is_ows(*p)) p++;
119 25                pv_n = (size_t)(p - pv);
120             }
121 28            if (word_eq(pn, pn_n, "dur")) dur = decimal_ms(pv, pv_n);
122         }
123 29        while (*p && *p != ',') p++; /* anything else of this metric */
124 29        if (dur < 0) continue;
125 20        if (word_eq(name, name_n, "total")) {
126 8            t->server_total_ms = dur;
127 8            found |= 1u;
128 12        } else if (word_eq(name, name_n, "engine")) {
129 5            t->server_engine_ms = dur;
130 5            found |= 2u;
131 7        } else if (word_eq(name, name_n, "platform")) {
132 4            t->server_platform_ms = dur;
133 4            found |= 4u;
134         }
135     }
136 18    return (int)((found & 1u) + ((found >> 1) & 1u) + ((found >> 2) & 1u));
137 }
138 
139 /* ------------------------------------------------------------------ timing object */
140 
141 15static void number_ms_member(ygst_span obj, const char *key, long long *out) {
142     ygst_span v;
143     double d;
144 15    if (ygst_json_member(obj, key, &v) != 1 || ygst_json_type(v) != 'n' || ygst_json_number(v, &d) != 0) return;
145 14    if (d >= 0 && d < 9.0e15) *out = (long long)(d + 0.5);
146 }
147 
148 58int ygst_timing_member(ygst_span root, ygst_timing *t, ygst_span *timing) {
149     ygst_span v;
150 58    if (timing) {
151 57        timing->p = NULL;
152 57        timing->n = 0;
153     }
154 58    if (!root.p || ygst_json_type(root) != 'o') return -1;
155 56    if (ygst_json_member(root, "timing", &v) != 1 || ygst_json_type(v) != 'o') return 0;
156 7    if (timing) *timing = v;
157 7    if (t) {
158 5        number_ms_member(v, "totalMs", &t->server_total_ms);
159 5        number_ms_member(v, "engineMs", &t->server_engine_ms);
160 5        number_ms_member(v, "platformMs", &t->server_platform_ms);
161     }
162 7    return 1;
163 }
164 
165 /* ------------------------------------------------------------------ log lines */
166 
167 9void ygst_timing_set_fallback(ygst_timing *t, const char *reason) {
168 9    size_t i = 0;
169 9    size_t n = 0;
170 9    if (!t) return;
171 251    while (reason && reason[i]) {
172 244        unsigned char lead = (unsigned char)reason[i];
173 244        size_t len = lead < 0x80 ? 1 : (lead >= 0xF0 ? 4 : (lead >= 0xE0 ? 3 : 2));
174         size_t k;
175 244        if (n + len >= sizeof t->fallback) break; /* whole UTF-8 sequences only */
176 592        for (k = 0; k < len && reason[i + k]; k++) {
177 349            char c = reason[i + k];
178 349            if (c == '"') c = '\'';
179 349            if (c == '\r' || c == '\n' || c == '\t') c = ' ';
180 349            t->fallback[n++] = c;
181         }
182 243        i += k;
183     }
184 8    t->fallback[n] = 0;
185 }
186 
187 311static long long between(int64_t from, int64_t to) {
188 311    if (!from || !to || to < from) return -1;
189 285    return (long long)(to - from);
190 }
191 
192 431static void put_ms(ygst_buf *b, const char *key, long long ms) {
193 431    ygst_buf_append_char(b, ' ');
194 431    ygst_buf_append_str(b, key);
195 431    ygst_buf_append_char(b, '=');
196 431    if (ms < 0) {
197 30        ygst_buf_append_char(b, '-');
198 30        return;
199     }
200 401    ygst_buf_append_int(b, ms);
201 401    ygst_buf_append_str(b, "ms");
202 }
203 
204 195static void put_bytes(ygst_buf *b, const char *key, long long n) {
205 195    ygst_buf_append_char(b, ' ');
206 195    ygst_buf_append_str(b, key);
207 195    ygst_buf_append_char(b, '=');
208 195    if (n < 0) {
209 11        ygst_buf_append_char(b, '-');
210 11        return;
211     }
212 184    ygst_buf_append_int(b, n);
213 184    ygst_buf_append_char(b, 'B');
214 }
215 
216 65static const char *mode_name(ygst_tm_mode m) {
217 65    switch (m) {
218 42    case YGST_TM_REST:
219 42        return "rest";
220 17    case YGST_TM_WS:
221 17        return "ws";
222 5    case YGST_TM_WS_REST:
223 5        return "ws>rest";
224 1    default:
225 1        return "local";
226     }
227 }
228 
229 65static void put_audio(ygst_buf *b, const ygst_timing *t) {
230     char s[48];
231 128    if (t->audio_bytes >= 0 && t->audio_bytes_per_sec > 0) {
232 63        long long bps = t->audio_bytes_per_sec;
233 63        long long hundredths = (t->audio_bytes * 100 + bps / 2) / bps;
234 63        snprintf(s, sizeof s, " audio=%lld.%02llds", hundredths / 100, hundredths % 100);
235 63        ygst_buf_append_str(b, s);
236     } else {
237 2        ygst_buf_append_str(b, " audio=-");
238     }
239 65}
240 
241 128static void put_token(ygst_buf *b, const char *token_id) {
242 128    ygst_buf_append_str(b, "timing tokenId=");
243 128    ygst_buf_append_str(b, token_id ? token_id : "");
244 128}
245 
246 67int ygst_timing_summary(const ygst_timing *t, const char *token_id, const char *record_id, ygst_buf *out) {
247     int ws;
248     int64_t done;
249     long long upload, wait, download;
250 67    if (!t || !out) return -1;
251 65    ws = t->mode == YGST_TM_WS;
252 65    if (ws) {
253 17        int64_t first = t->ws_final_first_ms;
254 17        done = t->ws_final_ms;
255 17        upload = between(t->stop_ms, t->ws_end_ms);
256 17        wait = between(t->ws_end_ms, first ? first : done);
257 17        download = between(first, done);
258     } else {
259         /* the split of the last attempt from the transport, placed after its start */
260 48        int64_t body_sent = t->rest_start_ms && t->upload_ms >= 0 ? t->rest_start_ms + t->upload_ms : 0;
261 48        done = t->rest_done_ms;
262 48        upload = between(t->stop_ms, body_sent);
263 48        wait = t->wait_ms;
264 48        download = t->download_ms;
265     }
266 65    put_token(out, token_id);
267     /* 2.2.0: the record of the result, to match the platform's per-record timing */
268 65    ygst_buf_append_str(out, " recordId=");
269 65    ygst_buf_append_str(out, record_id && record_id[0] ? record_id : "-");
270 65    ygst_buf_append_str(out, " mode=");
271 65    ygst_buf_append_str(out, mode_name(t->mode));
272 65    put_audio(out, t);
273 65    put_ms(out, "afterStop", between(t->stop_ms, t->delivered_ms));
274 65    put_ms(out, "upload", upload);
275 65    put_ms(out, "wait", wait);
276 65    put_ms(out, "download", download);
277 65    put_ms(out, "parse", between(done, t->parsed_ms));
278 65    put_ms(out, "deliver", between(t->parsed_ms, t->delivered_ms));
279 65    if (ws) {
280 17        put_ms(out, "handshake", between(t->ws_connect_ms, t->ws_handshake_ms));
281 17        put_bytes(out, "queued", t->queued_at_stop);
282 17        put_bytes(out, "final", t->body_bytes);
283 17        put_bytes(out, "wire", t->wire_bytes);
284 17        if (t->deflate) ygst_buf_append_str(out, " deflate");
285 17        if (t->attempts > 1) {
286 4            ygst_buf_append_str(out, " connections=");
287 4            ygst_buf_append_int(out, t->attempts);
288         }
289     } else {
290 48        ygst_buf_append_str(out, " attempts=");
291 48        ygst_buf_append_int(out, t->attempts);
292 48        put_bytes(out, "sent", t->upload_bytes);
293 48        put_bytes(out, "body", t->body_bytes);
294 48        put_bytes(out, "wire", t->wire_bytes);
295 48        if (t->encoding[0]) {
296 1            ygst_buf_append_char(out, ' ');
297 1            ygst_buf_append_str(out, t->encoding);
298         }
299     }
300 65    if (t->server_total_ms >= 0) put_ms(out, "server.total", t->server_total_ms);
301 65    if (t->server_engine_ms >= 0) put_ms(out, "server.engine", t->server_engine_ms);
302 65    if (t->server_platform_ms >= 0) put_ms(out, "server.platform", t->server_platform_ms);
303 65    if (t->replayed) ygst_buf_append_str(out, " replayed");
304 65    if (!t->ok && t->err_id) {
305 11        ygst_buf_append_str(out, " errId=");
306 11        ygst_buf_append_int(out, t->err_id);
307     }
308 65    if (t->mode == YGST_TM_WS_REST && t->fallback[0]) {
309 5        ygst_buf_append_str(out, " fallback=\"");
310 5        ygst_buf_append_str(out, t->fallback);
311 5        ygst_buf_append_char(out, '"');
312     }
313 65    ygst_buf_cstr(out);
314 65    return out->oom ? -1 : 0;
315 }
316 
317 61int ygst_timing_phases(const ygst_timing *t, const char *token_id, ygst_buf *out) {
318     static const char *const names[] = {"start",       "connect",      "handshake",    "params", "firstAudio",
319                                         "stop",        "endWritten",   "finalFirstByte", "final", "requestStart",
320                                         "responseDone", "parsed",      "delivered"};
321     int64_t at[sizeof names / sizeof names[0]];
322 61    int64_t origin = 0;
323     size_t i;
324 61    if (!t || !out) return -1;
325 60    at[0] = t->start_ms;
326 60    at[1] = t->ws_connect_ms;
327 60    at[2] = t->ws_handshake_ms;
328 60    at[3] = t->ws_params_ms;
329 60    at[4] = t->ws_first_audio_ms;
330 60    at[5] = t->stop_ms;
331 60    at[6] = t->ws_end_ms;
332 60    at[7] = t->ws_final_first_ms;
333 60    at[8] = t->ws_final_ms;
334 60    at[9] = t->rest_start_ms;
335 60    at[10] = t->rest_done_ms;
336 60    at[11] = t->parsed_ms;
337 60    at[12] = t->delivered_ms;
338 840    for (i = 0; i < sizeof names / sizeof names[0]; i++) {
339 780        if (at[i] && (!origin || at[i] < origin)) origin = at[i];
340     }
341 60    put_token(out, token_id);
342 60    ygst_buf_append_str(out, " phases");
343 840    for (i = 0; i < sizeof names / sizeof names[0]; i++) {
344 780        if (!at[i]) continue;
345 427        ygst_buf_append_char(out, ' ');
346 427        ygst_buf_append_str(out, names[i]);
347 427        ygst_buf_append_str(out, "=+");
348 427        ygst_buf_append_int(out, (long long)(at[i] - origin));
349 427        ygst_buf_append_str(out, "ms");
350     }
351 60    ygst_buf_cstr(out);
352 60    return out->oom ? -1 : 0;
353 }
354 
355 /* " key=<number as the platform wrote it>" */
356 12static void put_raw_number(ygst_buf *b, ygst_span obj, const char *key) {
357     ygst_span v;
358 12    if (ygst_json_member(obj, key, &v) != 1 || ygst_json_type(v) != 'n') return;
359 8    ygst_buf_append_char(b, ' ');
360 8    ygst_buf_append_str(b, key);
361 8    ygst_buf_append_char(b, '=');
362 8    ygst_buf_append(b, v.p, v.n);
363 }
364 
365 62int ygst_timing_stages(ygst_span timing, const char *token_id, ygst_buf *out) {
366     ygst_buf members;
367     ygst_span stages;
368     int rc;
369 62    if (!out) return -1;
370 61    if (!timing.p || ygst_json_type(timing) != 'o') return 0;
371 4    ygst_buf_init(&members);
372 4    if (ygst_json_member(timing, "engineStages", &stages) == 1 && ygst_json_type(stages) == 'o') {
373 3        size_t pos = 0;
374         ygst_span key, value;
375 13        while (ygst_json_object_next(stages, &pos, &key, &value) == 1) {
376 10            if (ygst_json_type(value) != 'n') continue;
377 8            ygst_buf_append_char(&members, ' ');
378 8            ygst_json_string(key, &members);
379 8            ygst_buf_append_char(&members, '=');
380 8            ygst_buf_append(&members, value.p, value.n);
381         }
382     }
383 4    put_raw_number(&members, timing, "receivedAt");
384 4    put_raw_number(&members, timing, "wsFirstAudioAt");
385 4    put_raw_number(&members, timing, "audioBytes");
386 4    rc = members.oom ? -1 : 0;
387 4    if (rc == 0 && members.len) {
388 3        put_token(out, token_id);
389 3        ygst_buf_append_str(out, " engineStages");
390 3        ygst_buf_append(out, members.data, members.len);
391 3        ygst_buf_cstr(out);
392 3        rc = out->oom ? -1 : 1;
393     }
394 4    ygst_buf_free(&members);
395 4    return rc;
396 }