Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

161 lignes
5.1KB

  1. #ifndef _rtmp_internal_h_
  2. #define _rtmp_internal_h_
  3. #include "rtmp-chunk-header.h"
  4. #include "rtmp-netconnection.h"
  5. #include "rtmp-netstream.h"
  6. #define N_CHUNK_STREAM 8 // maximum chunk stream count
  7. #define N_STREAM_NAME 256
  8. #define RTMP_STREAM_LIVE "live"
  9. #define RTMP_STREAM_RECORD "record"
  10. #define RTMP_STREAM_APPEND "append"
  11. #define RTMP_LEVEL_WARNING "warning"
  12. #define RTMP_LEVEL_STATUS "status"
  13. #define RTMP_LEVEL_ERROR "error"
  14. #define RTMP_LEVEL_FINISH "finish" // ksyun cdn
  15. // Chunk Stram Id
  16. enum rtmp_channel_t
  17. {
  18. RTMP_CHANNEL_PROTOCOL = 2, // Protocol Control Messages (1,2,3,5,6) & User Control Messages Event (4)
  19. RTMP_CHANNEL_INVOKE, // RTMP_TYPE_INVOKE (20) & RTMP_TYPE_FLEX_MESSAGE (17)
  20. RTMP_CHANNEL_AUDIO, // RTMP_TYPE_AUDIO (8)
  21. RTMP_CHANNEL_VIDEO, // RTMP_TYPE_VIDEO (9)
  22. RTMP_CHANNEL_DATA, // RTMP_TYPE_DATA (18) & RTMP_TYPE_FLEX_STREAM (15)
  23. RTMP_CHANNEL_MAX = 65599, // The protocol supports up to 65597 streams with IDs 3-65599(65535 + 64)
  24. };
  25. enum rtmp_state_t
  26. {
  27. RTMP_STATE_UNINIT = 0,
  28. RTMP_STATE_HANDSHAKE,
  29. RTMP_STATE_CONNECTED,
  30. RTMP_STATE_CREATE_STREAM,
  31. RTMP_STATE_START,
  32. RTMP_STATE_STOP,
  33. RTMP_STATE_DELETE_STREAM,
  34. };
  35. enum rtmp_transaction_id_t
  36. {
  37. RTMP_TRANSACTION_CONNECT = 1,
  38. RTMP_TRANSACTION_CREATE_STREAM,
  39. RTMP_TRANSACTION_GET_STREAM_LENGTH,
  40. };
  41. enum rtmp_notify_t
  42. {
  43. RTMP_NOTIFY_START = 1,
  44. RTMP_NOTIFY_STOP,
  45. RTMP_NOTIFY_PAUSE,
  46. RTMP_NOTIFY_SEEK,
  47. };
  48. struct rtmp_packet_t
  49. {
  50. struct rtmp_chunk_header_t header;
  51. uint32_t delta; // delta / timestamp
  52. uint32_t clock; // timestamp
  53. uint8_t* payload;
  54. size_t capacity; // only for network read
  55. size_t bytes; // only for network read
  56. };
  57. // 5.3.1. Chunk Format (p11)
  58. /* 3-bytes basic header + 11-bytes message header + 4-bytes extended timestamp */
  59. #define MAX_CHUNK_HEADER 18
  60. enum rtmp_parser_state_t
  61. {
  62. RTMP_PARSE_INIT = 0,
  63. RTMP_PARSE_BASIC_HEADER,
  64. RTMP_PARSE_MESSAGE_HEADER,
  65. RTMP_PARSE_EXTENDED_TIMESTAMP,
  66. RTMP_PARSE_PAYLOAD,
  67. };
  68. struct rtmp_parser_t
  69. {
  70. uint8_t buffer[MAX_CHUNK_HEADER];
  71. uint32_t basic_bytes; // basic header length
  72. uint32_t bytes;
  73. enum rtmp_parser_state_t state;
  74. struct rtmp_packet_t* pkt;
  75. };
  76. struct rtmp_t
  77. {
  78. uint32_t in_chunk_size; // read from network
  79. uint32_t out_chunk_size; // write to network
  80. uint32_t sequence_number; // bytes read report
  81. uint32_t window_size; // server bandwidth (2500000)
  82. uint32_t peer_bandwidth; // client bandwidth
  83. uint32_t buffer_length_ms; // s -> c
  84. uint8_t limit_type; // client bandwidth limit
  85. // chunk header
  86. struct rtmp_packet_t in_packets[N_CHUNK_STREAM]; // receive from network
  87. struct rtmp_packet_t out_packets[N_CHUNK_STREAM]; // send to network
  88. struct rtmp_parser_t parser;
  89. void* param;
  90. /// @return 0-ok, other-error
  91. int (*send)(void* param, const uint8_t* header, uint32_t headerBytes, const uint8_t* payload, uint32_t payloadBytes);
  92. int (*onaudio)(void* param, const uint8_t* data, size_t bytes, uint32_t timestamp);
  93. int (*onvideo)(void* param, const uint8_t* data, size_t bytes, uint32_t timestamp);
  94. int (*onscript)(void* param, const uint8_t* data, size_t bytes, uint32_t timestamp);
  95. void (*onabort)(void* param, uint32_t chunk_stream_id);
  96. struct
  97. {
  98. // server side
  99. int (*onconnect)(void* param, int r, double transaction, const struct rtmp_connect_t* connect);
  100. int (*oncreate_stream)(void* param, int r, double transaction);
  101. int (*onplay)(void* param, int r, double transaction, const char* stream_name, double start, double duration, uint8_t reset);
  102. int (*ondelete_stream)(void* param, int r, double transaction, double stream_id);
  103. int (*onreceive_audio)(void* param, int r, double transaction, uint8_t audio);
  104. int (*onreceive_video)(void* param, int r, double transaction, uint8_t video);
  105. int (*onpublish)(void* param, int r, double transaction, const char* stream_name, const char* stream_type);
  106. int (*onseek)(void* param, int r, double transaction, double milliSeconds);
  107. int (*onpause)(void* param, int r, double transaction, uint8_t pause, double milliSeconds);
  108. int (*onget_stream_length)(void* param, int r, double transaction, const char* stream_name);
  109. } server;
  110. struct
  111. {
  112. // client side
  113. int (*onconnect)(void* param);
  114. int (*oncreate_stream)(void* param, double stream_id);
  115. int (*onnotify)(void* param, enum rtmp_notify_t notify);
  116. int (*oneof)(void* param, uint32_t stream_id); // EOF event
  117. int (*onping)(void* param, uint32_t stream_id); // send pong
  118. int (*onbandwidth)(void* param); // send window acknowledgement size
  119. } client;
  120. };
  121. /// @return 0-ok, other-error
  122. int rtmp_chunk_read(struct rtmp_t* rtmp, const uint8_t* data, size_t bytes);
  123. /// @return 0-ok, other-error
  124. int rtmp_chunk_write(struct rtmp_t* rtmp, const struct rtmp_chunk_header_t* header, const uint8_t* payload);
  125. int rtmp_handler(struct rtmp_t* rtmp, struct rtmp_chunk_header_t* header, const uint8_t* payload);
  126. int rtmp_event_handler(struct rtmp_t* rtmp, const struct rtmp_chunk_header_t* header, const uint8_t* data);
  127. int rtmp_invoke_handler(struct rtmp_t* rtmp, const struct rtmp_chunk_header_t* header, const uint8_t* data);
  128. /// @return >0-ok, 0-error
  129. int rtmp_control_handler(struct rtmp_t* rtmp, const struct rtmp_chunk_header_t* header, const uint8_t* data);
  130. #endif /* !_rtmp_internal_h_ */