If the queue for the given stream (or all streams when stream_idx=-1) * is overflowing, trigger a fake heartbeat on lagging streams. * * @return 1 if heartbeat triggered, 0 otherwise */
| 270 | * @return 1 if heartbeat triggered, 0 otherwise |
| 271 | */ |
| 272 | static int overflow_heartbeat(SyncQueue *sq, int stream_idx) |
| 273 | { |
| 274 | SyncQueueStream *st; |
| 275 | SyncQueueFrame frame; |
| 276 | int64_t tail_ts = AV_NOPTS_VALUE; |
| 277 | |
| 278 | /* if no stream specified, pick the one that is most ahead */ |
| 279 | if (stream_idx < 0) { |
| 280 | int64_t ts = AV_NOPTS_VALUE; |
| 281 | |
| 282 | for (int i = 0; i < sq->nb_streams; i++) { |
| 283 | st = &sq->streams[i]; |
| 284 | if (st->head_ts != AV_NOPTS_VALUE && |
| 285 | (ts == AV_NOPTS_VALUE || |
| 286 | av_compare_ts(ts, sq->streams[stream_idx].tb, |
| 287 | st->head_ts, st->tb) < 0)) { |
| 288 | ts = st->head_ts; |
| 289 | stream_idx = i; |
| 290 | } |
| 291 | } |
| 292 | /* no stream has a timestamp yet -> nothing to do */ |
| 293 | if (stream_idx < 0) |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | st = &sq->streams[stream_idx]; |
| 298 | |
| 299 | /* get the chosen stream's tail timestamp */ |
| 300 | for (size_t i = 0; tail_ts == AV_NOPTS_VALUE && |
| 301 | av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++) |
| 302 | tail_ts = frame_end(sq, frame, 0); |
| 303 | |
| 304 | /* overflow triggers when the tail is over specified duration behind the head */ |
| 305 | if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts || |
| 306 | av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us) |
| 307 | return 0; |
| 308 | |
| 309 | /* signal a fake timestamp for all streams that prevent tail_ts from being output */ |
| 310 | tail_ts++; |
| 311 | for (unsigned int i = 0; i < sq->nb_streams; i++) { |
| 312 | const SyncQueueStream *st1 = &sq->streams[i]; |
| 313 | int64_t ts; |
| 314 | |
| 315 | if (st == st1 || st1->finished || |
| 316 | (st1->head_ts != AV_NOPTS_VALUE && |
| 317 | av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0)) |
| 318 | continue; |
| 319 | |
| 320 | ts = av_rescale_q(tail_ts, st->tb, st1->tb); |
| 321 | if (st1->head_ts != AV_NOPTS_VALUE) |
| 322 | ts = FFMAX(st1->head_ts + 1, ts); |
| 323 | |
| 324 | av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n", |
| 325 | i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb)); |
| 326 | |
| 327 | stream_update_ts(sq, i, ts); |
| 328 | } |
| 329 |
no test coverage detected