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.

80 lines
2.4KB

  1. #include "rtmp-control-message.h"
  2. #include "rtmp-msgtypeid.h"
  3. #include "rtmp-util.h"
  4. #define N_CHUNK_HEADER 12
  5. static void rtmp_protocol_control_message_header(uint8_t* out, uint8_t id, size_t payload)
  6. {
  7. // 5.4. Protocol Control Messages (p18)
  8. // These protocol control messages MUST have message stream ID 0 (known
  9. // as the control stream) and be sent in chunk stream ID 2.
  10. out[0] = (0x00 << 6) /*fmt*/ | 0x02 /*cs id*/;
  11. /* timestamp */
  12. out[1] = 0x00;
  13. out[2] = 0x00;
  14. out[3] = 0x00;
  15. /* message length */
  16. out[4] = (uint8_t)(payload >> 16);
  17. out[5] = (uint8_t)(payload >> 8);
  18. out[6] = (uint8_t)payload;
  19. /* message type id */
  20. out[7] = id;
  21. /* message stream id */
  22. out[8] = 0x00;
  23. out[9] = 0x00;
  24. out[10] = 0x00;
  25. out[11] = 0x00;
  26. }
  27. // 5.4.1. Set Chunk Size (1) (p19)
  28. int rtmp_set_chunk_size(uint8_t* out, size_t size, uint32_t chunkSize)
  29. {
  30. if (size < N_CHUNK_HEADER + 4) return 0;
  31. rtmp_protocol_control_message_header(out, RTMP_TYPE_SET_CHUNK_SIZE, 4);
  32. be_write_uint32(out + N_CHUNK_HEADER, chunkSize & 0x7FFFFFFF); // first bit MUST be zero
  33. return N_CHUNK_HEADER + 4;
  34. }
  35. // 5.4.2. Abort Message (2) (p19)
  36. int rtmp_abort_message(uint8_t* out, size_t size, uint32_t chunkStreamId)
  37. {
  38. if (size < N_CHUNK_HEADER + 4) return 0;
  39. rtmp_protocol_control_message_header(out, RTMP_TYPE_ABORT, 4);
  40. be_write_uint32(out + N_CHUNK_HEADER, chunkStreamId);
  41. return N_CHUNK_HEADER + 4;
  42. }
  43. // 5.4.3. Acknowledgement (3) (p20)
  44. int rtmp_acknowledgement(uint8_t* out, size_t size, uint32_t sequenceNumber)
  45. {
  46. if (size < N_CHUNK_HEADER + 4) return 0;
  47. rtmp_protocol_control_message_header(out, RTMP_TYPE_ACKNOWLEDGEMENT, 4);
  48. be_write_uint32(out + N_CHUNK_HEADER, sequenceNumber);
  49. return N_CHUNK_HEADER + 4;
  50. }
  51. // 5.4.4. Window Acknowledgement Size (5) (p20)
  52. int rtmp_window_acknowledgement_size(uint8_t* out, size_t size, uint32_t windowSize)
  53. {
  54. if (size < N_CHUNK_HEADER + 4) return 0;
  55. rtmp_protocol_control_message_header(out, RTMP_TYPE_WINDOW_ACKNOWLEDGEMENT_SIZE, 4);
  56. be_write_uint32(out + N_CHUNK_HEADER, windowSize);
  57. return N_CHUNK_HEADER + 4;
  58. }
  59. // 5.4.5. Set Peer Bandwidth (6) (p21)
  60. int rtmp_set_peer_bandwidth(uint8_t* out, size_t size, uint32_t windowSize, uint8_t limitType)
  61. {
  62. if (size < N_CHUNK_HEADER + 5) return 0;
  63. rtmp_protocol_control_message_header(out, RTMP_TYPE_SET_PEER_BANDWIDTH, 5);
  64. be_write_uint32(out + N_CHUNK_HEADER, windowSize);
  65. out[N_CHUNK_HEADER + 4] = limitType;
  66. return N_CHUNK_HEADER + 5;
  67. }