rtsp_conn.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include "hap.h"
  5. /**
  6. * RTSP Connection State Management
  7. * Consolidates all session state for an AirPlay connection
  8. */
  9. // Forward declaration
  10. typedef struct rtsp_conn rtsp_conn_t;
  11. /**
  12. * Connection state struct - consolidates all session state
  13. */
  14. struct rtsp_conn {
  15. // HAP session for pairing/encryption
  16. hap_session_t *hap_session;
  17. bool encrypted_mode;
  18. // Volume control: Q15 fixed-point (0-32768)
  19. // 32768 = 0 dB (unity), 0 = mute
  20. volatile int32_t volume_q15;
  21. float volume_db;
  22. // Audio streaming state
  23. bool stream_active;
  24. bool stream_paused;
  25. int64_t stream_type; // 96=UDP realtime, 103=TCP buffered
  26. uint16_t data_port; // UDP port for audio data (type 96)
  27. uint16_t control_port; // UDP port for control (retransmit requests)
  28. uint16_t timing_port; // UDP port for timing (our local port)
  29. uint16_t event_port; // TCP port for server->client events
  30. uint16_t buffered_port; // TCP port for buffered audio (type 103)
  31. int data_socket;
  32. int control_socket;
  33. int event_socket; // TCP listener for event port
  34. // Client address for AirPlay 1 timing requests
  35. uint32_t client_ip; // Client IP (network byte order)
  36. uint16_t client_timing_port; // Client's timing port (for sending requests)
  37. uint16_t client_control_port; // Client's control port
  38. // Codec info from ANNOUNCE/SETUP
  39. char codec[32];
  40. int sample_rate;
  41. int channels;
  42. int bits_per_sample;
  43. // DACP identifiers for sending commands back to the client
  44. char dacp_id[32]; // DACP-ID header (hex string)
  45. char active_remote[32]; // Active-Remote header (token string)
  46. // AirPlay protocol version detected from request shape:
  47. // 0 = unknown (handshake not complete)
  48. // 1 = classic RAOP (Apple-Challenge / rsaaeskey / Transport: header)
  49. // 2 = AirPlay 2 (HAP / bplist streams)
  50. uint8_t protocol_version;
  51. };
  52. /**
  53. * Create a new connection state
  54. * @return Allocated connection state, or NULL on failure
  55. */
  56. rtsp_conn_t *rtsp_conn_create(void);
  57. /**
  58. * Free connection state and associated resources
  59. */
  60. void rtsp_conn_free(rtsp_conn_t *conn);
  61. /**
  62. * Reset stream-related state (called on stream teardown)
  63. * Keeps session alive but clears audio stream state
  64. */
  65. void rtsp_conn_reset_stream(rtsp_conn_t *conn);
  66. /**
  67. * Full cleanup when connection closes
  68. * Stops audio, closes sockets, clears PTP
  69. */
  70. void rtsp_conn_cleanup(rtsp_conn_t *conn);
  71. /**
  72. * Set volume in dB (converts to Q15 internally)
  73. * @param conn Connection state
  74. * @param volume_db Volume in dB (0 = max, -30 = mute)
  75. */
  76. void rtsp_conn_set_volume(rtsp_conn_t *conn, float volume_db);
  77. /**
  78. * Get volume as Q15 scale factor
  79. * @param conn Connection state
  80. * @return Q15 fixed-point multiplier (0 = mute, 32768 = unity)
  81. */
  82. int32_t rtsp_conn_get_volume_q15(rtsp_conn_t *conn);