rtsp_message.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include "rtsp_conn.h"
  5. /**
  6. * RTSP Message Parsing and Response Building
  7. */
  8. /**
  9. * Parsed RTSP request structure
  10. */
  11. typedef struct {
  12. char method[32];
  13. char path[256];
  14. char protocol[16];
  15. int cseq;
  16. char content_type[64];
  17. size_t content_length;
  18. const uint8_t *body;
  19. size_t body_len;
  20. } rtsp_request_t;
  21. /**
  22. * Parse RTSP request from buffer
  23. * @param data Raw request data
  24. * @param len Length of data
  25. * @param req Output: parsed request (body pointer references original data)
  26. * @return 0 on success, -1 on parse error
  27. */
  28. int rtsp_request_parse(const uint8_t *data, size_t len, rtsp_request_t *req);
  29. /**
  30. * Find end of HTTP/RTSP headers (\r\n\r\n)
  31. * @param data Request data
  32. * @param len Length of data
  33. * @return Pointer to first \r\n of header end, or NULL if not found
  34. */
  35. const uint8_t *rtsp_find_header_end(const uint8_t *data, size_t len);
  36. /**
  37. * Parse CSeq header from request
  38. * @param request Request string
  39. * @return CSeq value, or 1 if not found
  40. */
  41. int rtsp_parse_cseq(const char *request);
  42. /**
  43. * Parse Content-Length header from request
  44. * @param request Request string
  45. * @return Content length, or 0 if not found
  46. */
  47. int rtsp_parse_content_length(const char *request);
  48. /**
  49. * Get body pointer from request
  50. * @param request Request string
  51. * @param request_len Total request length
  52. * @param body_len Output: body length
  53. * @return Pointer to body, or NULL if none
  54. */
  55. const uint8_t *rtsp_get_body(const char *request, size_t request_len,
  56. size_t *body_len);
  57. /**
  58. * Send RTSP response (handles encryption automatically)
  59. * @param socket Client socket
  60. * @param conn Connection state
  61. * @param status_code HTTP status code (200, 404, etc.)
  62. * @param status_text Status text ("OK", "Not Found", etc.)
  63. * @param cseq CSeq value from request
  64. * @param extra_headers Additional headers (NULL if none, must end with \r\n)
  65. * @param body Response body (NULL if none)
  66. * @param body_len Body length
  67. * @return 0 on success, -1 on error
  68. */
  69. int rtsp_send_response(int socket, rtsp_conn_t *conn, int status_code,
  70. const char *status_text, int cseq,
  71. const char *extra_headers, const char *body,
  72. size_t body_len);
  73. /**
  74. * Send simple 200 OK response
  75. */
  76. int rtsp_send_ok(int socket, rtsp_conn_t *conn, int cseq);
  77. /**
  78. * Send HTTP response (for GET /info before RTSP mode)
  79. * @param socket Client socket
  80. * @param conn Connection state
  81. * @param status_code HTTP status code
  82. * @param status_text Status text
  83. * @param content_type Content-Type header value
  84. * @param body Response body
  85. * @param body_len Body length
  86. * @return 0 on success, -1 on error
  87. */
  88. int rtsp_send_http_response(int socket, rtsp_conn_t *conn, int status_code,
  89. const char *status_text, const char *content_type,
  90. const char *body, size_t body_len);
  91. /**
  92. * Parse Transport header for client ports (AirPlay 1)
  93. * @param request Request string
  94. * @param control_port Output: client's control port (or 0)
  95. * @param timing_port Output: client's timing port (or 0)
  96. */
  97. void rtsp_parse_transport(const char *request, uint16_t *control_port,
  98. uint16_t *timing_port);