You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 line
8.4KB

  1. #ifndef _rtsp_server_h_
  2. #define _rtsp_server_h_
  3. #include "rtsp-header-transport.h"
  4. #include <stdint.h>
  5. #include <stddef.h>
  6. #if defined(__cplusplus)
  7. extern "C" {
  8. #endif
  9. typedef struct rtsp_server_t rtsp_server_t;
  10. struct rtsp_handler_t
  11. {
  12. /// rtsp_server_destroy will call this function
  13. /// @param[in] ptr2 user-defined parameter
  14. int (*close)(void* ptr2);
  15. /// Network send
  16. /// @param[in] ptr2 user-defined parameter
  17. /// @param[in] data send data
  18. /// @param[in] bytes data length in byte
  19. /// @return 0-ok, other-error
  20. int (*send)(void* ptr2, const void* data, size_t bytes);
  21. /// RTSP DESCRIBE request(call rtsp_server_reply_describe)
  22. /// @param[in] ptr user-defined parameter
  23. /// @param[in] uri request uri
  24. /// @return 0-ok, other-error
  25. int (*ondescribe)(void* ptr, rtsp_server_t* rtsp, const char* uri);
  26. /// RTSP SETUP request(call rtsp_server_reply_setup)
  27. /// @param[in] ptr user-defined parameter
  28. /// @param[in] uri request uri
  29. /// @param[in] session RTSP Session
  30. /// @param[in] transport RTSP Transport header
  31. /// @return 0-ok, other-error
  32. int (*onsetup)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const struct rtsp_header_transport_t transports[], size_t num);
  33. /// RTSP PLAY request(call rtsp_server_reply_play)
  34. /// @param[in] ptr user-defined parameter
  35. /// @param[in] session RTSP Session
  36. /// @param[in] npt request begin time, NULL if don't have Range parameter, 0 represent now
  37. /// @param[in] scale request scale, NULL if don't have Scale parameter
  38. /// @return 0-ok, other-error code
  39. int (*onplay)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const int64_t *npt, const double *scale);
  40. /// RTSP PAUSE request(call rtsp_server_reply_pause)
  41. /// @param[in] ptr user-defined parameter
  42. /// @param[in] session RTSP Session
  43. /// @param[in] npt request pause time, NULL if don't have Range parameter
  44. /// @return 0-ok, other-error code
  45. int (*onpause)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const int64_t *npt);
  46. /// RTSP TEARDOWN request(call rtsp_server_reply_teardown)
  47. /// @param[in] ptr user-defined parameter
  48. /// @param[in] session RTSP Session
  49. /// @param[in] uri request uri
  50. /// @return 0-ok, other-error code
  51. int (*onteardown)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session);
  52. /// RTSP ANNOUNCE request(call rtsp_server_reply_announce)
  53. /// @param[in] ptr user-defined parameter
  54. /// @param[in] uri request uri
  55. /// @param[in] sdp RTSP SDP
  56. /// @return 0-ok, other-error
  57. int (*onannounce)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* sdp, int len);
  58. /// RTSP RECORD request(call rtsp_server_reply_record)
  59. /// @param[in] ptr user-defined parameter
  60. /// @param[in] session RTSP Session
  61. /// @param[in] npt request begin time, NULL if don't have Range parameter, 0 represent now
  62. /// @param[in] scale request scale, NULL if don't have Scale parameter
  63. /// @return 0-ok, other-error code
  64. int (*onrecord)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const int64_t *npt, const double *scale);
  65. /// RTSP OPTIONS request
  66. /// @param[in] uri default is '*'
  67. /// @return 0-ok, other-error code
  68. int (*onoptions)(void* ptr, rtsp_server_t* rtsp, const char* uri);
  69. /// RTSP GET_PARAMETER/SET_PARAMETER request
  70. /// use rtsp_server_get_header("content-type") to if need
  71. /// @param[in] uri the presentation/stream uri
  72. /// @param[in] session RTSP session
  73. /// @param[in] content paramter(s), NULL for test client or server liveness ("ping")
  74. /// @return 0-ok, other-error code
  75. int (*ongetparameter)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const void* content, int bytes);
  76. int (*onsetparameter)(void* ptr, rtsp_server_t* rtsp, const char* uri, const char* session, const void* content, int bytes);
  77. };
  78. /// create (reuse-able) rtsp server
  79. /// param[in] ip peer(client) ip(IPv4/IPv6)
  80. /// param[in] port peer(client) port
  81. /// param[in] handler callbacks
  82. /// param[in] ptr callback(except send) parameter
  83. /// param[in] ptr2 send callback parameter
  84. /// @return NULL-error, other-rtsp server instance
  85. rtsp_server_t* rtsp_server_create(const char ip[65], unsigned short port, struct rtsp_handler_t* handler, void* ptr, void* ptr2);
  86. /// destroy rtsp server
  87. /// @param[in] server rtsp server instance
  88. /// @return 0-ok, other-error code
  89. int rtsp_server_destroy(rtsp_server_t* server);
  90. /// client request
  91. /// @param[in] data rtsp request
  92. /// @param[in,out] bytes input data length, output remain length
  93. /// @return 0-ok, 1-need more data, other-error
  94. int rtsp_server_input(rtsp_server_t* rtsp, const void* data, size_t* bytes);
  95. /// RTSP DESCRIBE reply
  96. /// @param[in] rtsp request handle
  97. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  98. /// @param[in] sdp RTSP SDP
  99. /// @return 0-ok, other-error code
  100. int rtsp_server_reply_describe(rtsp_server_t* rtsp, int code, const char* sdp);
  101. /// RTSP SETUP reply
  102. /// @param[in] rtsp request handle
  103. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  104. /// @param[in] session RTSP Session parameter
  105. /// @param[in] transport RTSP Transport parameter
  106. /// @return 0-ok, other-error code
  107. int rtsp_server_reply_setup(rtsp_server_t* rtsp, int code, const char* session, const char* transport);
  108. /// RTSP PLAY reply
  109. /// @param[in] rtsp request handle
  110. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  111. /// @param[in] nptstart Range start time(ms) [optional]
  112. /// @param[in] nptend Range end time(ms) [optional]
  113. /// @param[in] rtpinfo RTP-info [optional] e.g. url=rtsp://foo.com/bar.avi/streamid=0;seq=45102,url=rtsp://foo.com/bar.avi/streamid=1;seq=30211
  114. /// @return 0-ok, other-error code
  115. int rtsp_server_reply_play(rtsp_server_t* rtsp, int code, const int64_t *nptstart, const int64_t *nptend, const char* rtpinfo);
  116. /// RTSP PAUSE reply
  117. /// @param[in] rtsp request handle
  118. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  119. /// @return 0-ok, other-error code
  120. int rtsp_server_reply_pause(rtsp_server_t* rtsp, int code);
  121. /// RTSP PAUSE reply
  122. /// @param[in] rtsp request handle
  123. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  124. /// @return 0-ok, other-error code
  125. int rtsp_server_reply_teardown(rtsp_server_t* rtsp, int code);
  126. /// RTSP ANNOUNCE reply
  127. /// @param[in] rtsp request handle
  128. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  129. /// @return 0-ok, other-error code
  130. int rtsp_server_reply_announce(rtsp_server_t* rtsp, int code);
  131. /// RTSP RECORD reply
  132. /// @param[in] rtsp request handle
  133. /// @param[in] code RTSP status-code(200-OK, 301-Move Permanently, ...)
  134. /// @param[in] nptstart Range start time(ms) [optional]
  135. /// @param[in] nptend Range end time(ms) [optional]
  136. /// @return 0-ok, other-error code
  137. int rtsp_server_reply_record(rtsp_server_t* rtsp, int code, const int64_t *nptstart, const int64_t *nptend);
  138. /// RTSP OPTIONS reply
  139. /// @return 0-ok, other-error code
  140. int rtsp_server_reply_options(rtsp_server_t* rtsp, int code);
  141. /// RTSP GET_PARAMETER reply(content-type/content-encoding/content-language copy from request header)
  142. /// @param[in] rtsp request handle
  143. /// @param[in] code RTSP status-code(200-OK...)
  144. /// @return 0-ok, other-error code
  145. int rtsp_server_reply_get_parameter(rtsp_server_t* rtsp, int code, const void* content, int bytes);
  146. /// RTSP SET_PARAMETER reply
  147. /// @param[in] rtsp request handle
  148. /// @param[in] code RTSP status-code(200-OK...)
  149. /// @return 0-ok, other-error code
  150. int rtsp_server_reply_set_parameter(rtsp_server_t* rtsp, int code);
  151. /// RTSP send Embedded (Interleaved) Binary Data
  152. /// @param[in] rtsp request handle
  153. /// @param[in] data interleaved binary data, start with 1-byte $ + 1-byte CHANNEL + 2-bytes LEN + RTP/RTCP HEADER + PAYLOAD
  154. /// @param[in] bytes data length in bytes
  155. /// @return 0-ok, other-error code
  156. int rtsp_server_send_interleaved_data(rtsp_server_t* rtsp, const void* data, size_t bytes);
  157. /// find RTSP header
  158. /// @param[in] rtsp request handle
  159. /// @param[in] name header name
  160. /// @return header value, NULL if not found.
  161. /// NOTICE: call in rtsp_handler_t callback only
  162. const char* rtsp_server_get_header(rtsp_server_t* rtsp, const char* name);
  163. /// get client ip/port
  164. const char* rtsp_server_get_client(rtsp_server_t* rtsp, unsigned short* port);
  165. /// @timeout set session timeout in seconds, valid only in setup response(set before rtsp_server_reply_setup)
  166. void rtsp_server_set_session_timeout(rtsp_server_t* rtsp, int timeout);
  167. #if defined(__cplusplus)
  168. }
  169. #endif
  170. #endif /* !_rtsp_server_h_ */