audio_receiver_internal.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include "lwip/sockets.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "audio_buffer.h"
  9. #include "audio_decoder.h"
  10. #include "audio_receiver.h"
  11. #include "audio_stream.h"
  12. #include "audio_timing.h"
  13. #define MAX_RTP_PACKET_SIZE 2048
  14. typedef struct {
  15. audio_stream_t *stream;
  16. audio_stream_t *realtime_stream;
  17. audio_stream_t *buffered_stream;
  18. audio_decoder_t *decoder;
  19. audio_buffer_t buffer;
  20. audio_timing_t timing;
  21. audio_stats_t stats;
  22. int data_socket;
  23. int control_socket;
  24. TaskHandle_t task_handle;
  25. TaskHandle_t control_task_handle;
  26. uint16_t data_port;
  27. uint16_t control_port;
  28. int buffered_listen_socket;
  29. int buffered_client_socket;
  30. uint16_t buffered_port;
  31. TaskHandle_t buffered_task_handle;
  32. uint8_t *buffered_recv_buffer;
  33. uint8_t *decrypt_buffer;
  34. size_t decrypt_buffer_size;
  35. uint64_t blocks_read;
  36. uint64_t blocks_read_in_sequence;
  37. // NACK retransmission support
  38. struct sockaddr_in client_control_addr; // Client's control address for NACKs
  39. bool retransmit_enabled; // True when client address is set
  40. int64_t last_resend_error_time_us; // Backoff timer on sendto failure
  41. bool rtp_sequence_valid;
  42. uint16_t resend_window_first;
  43. uint64_t resend_missing_mask;
  44. int64_t resend_last_request_time_us;
  45. // Post-seek RTP gates: together they form a window [discard_before_rtp,
  46. // discard_above_rtp] around the new anchor. Frames outside the window are
  47. // discarded in audio_stream_process_frame before they enter the ring buffer,
  48. // preventing the stale-frame / repeated-bulk-flush loop.
  49. //
  50. // discard_before_rtp — drop frames with RTP < anchor (forward-seek stale)
  51. // discard_above_rtp — drop frames with RTP > anchor+10s (backward-seek
  52. // stale, e.g. seek-to-start where old pre-buffer
  53. // frames have much higher RTP than the new anchor)
  54. //
  55. // Both are always armed together; whichever direction the seek went, one
  56. // gate fires and the other is harmless. Each self-disarms on the first
  57. // frame that passes it (FIFO TCP order guarantees stale frames drain first).
  58. // Written by the RTSP task, read by the TCP buffered task — uint32_t write
  59. // is atomic on Xtensa; arm bool last so reader never sees stale threshold.
  60. uint32_t discard_before_rtp;
  61. bool discard_before_rtp_valid;
  62. uint32_t discard_above_rtp;
  63. bool discard_above_rtp_valid;
  64. // Set by audio_receiver_seek_flush() to ensure the gates are armed on the
  65. // next SETRATEANCHORTIME even when the buffer was already empty (forward
  66. // seek: flush empties buffer before anchor arrives, so seek detection in
  67. // set_anchor_time would otherwise find no oldest_rtp and skip arming).
  68. bool arm_gate_on_next_anchor;
  69. // Set by audio_receiver_seek_flush() to reject ALL incoming frames until
  70. // the next SETRATEANCHORTIME provides a valid anchor. Without this, stale
  71. // TCP data (from the old track still draining the socket buffer) fills the
  72. // ring buffer between FLUSHBUFFERED and the anchor, causing a second flush
  73. // and doubling the startup delay.
  74. bool discard_all_until_anchor;
  75. // Snapshot of the expected RTP position taken the moment the sender signals
  76. // PAUSE (SETRATEANCHORTIME rate=0). Path B in audio_receiver_set_anchor_time
  77. // uses this as the reference when comparing the new anchor on RESUME, so
  78. // that a long pause does not make the wall-clock-elapsed estimate overshoot
  79. // by (pause_duration × sample_rate) and false-trigger a seek flush.
  80. // Cleared on flush/reset and consumed after one use.
  81. uint32_t paused_rtp;
  82. bool paused_rtp_valid;
  83. } audio_receiver_state_t;
  84. bool audio_stream_process_frame(audio_receiver_state_t *state,
  85. uint32_t timestamp, const uint8_t *audio_data,
  86. size_t audio_len);
  87. static inline audio_receiver_state_t *
  88. audio_stream_state(audio_stream_t *stream) {
  89. return (audio_receiver_state_t *)stream->ctx;
  90. }