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.

238 lines
8.8KB

  1. #include "rtmp-netstream.h"
  2. #include "amf0.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. // @param[in] streamName flv:sample, mp3:sample, H.264/AAC: mp4:sample.m4v
  6. // @param[in] start -2-live/vod, -1-live only, >=0-seek position
  7. // @param[in] duration <=-1-all, 0-single frame, >0-period
  8. // @param[in] reset 1-flush any previous playlist, 0-don't flush
  9. uint8_t* rtmp_netstream_play(uint8_t* out, size_t bytes, double transactionId, const char* name, double start, double duration, int reset)
  10. {
  11. uint8_t* end = out + bytes;
  12. const char* command = "play";
  13. if (NULL == name)
  14. return NULL;
  15. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  16. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  17. out = AMFWriteNull(out, end); // command object
  18. out = AMFWriteString(out, end, name, strlen(name)); // Stream Name
  19. out = AMFWriteDouble(out, end, start); // Start Number
  20. out = AMFWriteDouble(out, end, duration); // Duration Number
  21. out = AMFWriteBoolean(out, end, (uint8_t)reset); // Reset Boolean
  22. return out;
  23. }
  24. // reponse: none
  25. uint8_t* rtmp_netstream_delete_stream(uint8_t* out, size_t bytes, double transactionId, double streamId)
  26. {
  27. uint8_t* end = out + bytes;
  28. const char* command = "deleteStream";
  29. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  30. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  31. out = AMFWriteNull(out, end); // command object
  32. out = AMFWriteDouble(out, end, streamId); // Stream ID
  33. return out;
  34. }
  35. uint8_t* rtmp_netconnection_close_stream(uint8_t* out, size_t bytes, double transactionId, double streamId)
  36. {
  37. uint8_t* end = out + bytes;
  38. const char* command = "closeStream";
  39. out = AMFWriteString(out, end, command, strlen(command));
  40. out = AMFWriteDouble(out, end, transactionId);
  41. out = AMFWriteNull(out, end);
  42. out = AMFWriteDouble(out, end, streamId);
  43. return out;
  44. }
  45. // reponse: enable-false: none, enable-true: onStatus with NetStream.Seek.Notify/NetStream.Play.Start
  46. uint8_t* rtmp_netstream_receive_audio(uint8_t* out, size_t bytes, double transactionId, int enable)
  47. {
  48. uint8_t* end = out + bytes;
  49. const char* command = "receiveAudio";
  50. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  51. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  52. out = AMFWriteNull(out, end); // command object
  53. out = AMFWriteBoolean(out, end, (uint8_t)enable); // Bool Flag
  54. return out;
  55. }
  56. // reponse: enable-false: none, enable-true: onStatus with NetStream.Seek.Notify/NetStream.Play.Start
  57. uint8_t* rtmp_netstream_receive_video(uint8_t* out, size_t bytes, double transactionId, int enable)
  58. {
  59. uint8_t* end = out + bytes;
  60. const char* command = "receiveVideo";
  61. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  62. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  63. out = AMFWriteNull(out, end); // command object
  64. out = AMFWriteBoolean(out, end, (uint8_t)enable); // Bool Flag
  65. return out;
  66. }
  67. // response: onStatus beginning of publish
  68. uint8_t* rtmp_netstream_publish(uint8_t* out, size_t bytes, double transactionId, const char* stream_name, const char* stream_type)
  69. {
  70. uint8_t* end = out + bytes;
  71. const char* command = "publish";
  72. if (NULL == stream_name || NULL == stream_type)
  73. return NULL;
  74. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  75. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  76. out = AMFWriteNull(out, end); // command object
  77. out = AMFWriteString(out, end, stream_name, strlen(stream_name)); // Publishing Name
  78. out = AMFWriteString(out, end, stream_type, strlen(stream_type)); // Publishing Type
  79. return out;
  80. }
  81. // response: success: onStatus-NetStream.Seek.Notify, failure: _error message
  82. uint8_t* rtmp_netstream_seek(uint8_t* out, size_t bytes, double transactionId, double ms)
  83. {
  84. uint8_t* end = out + bytes;
  85. const char* command = "seek";
  86. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  87. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  88. out = AMFWriteNull(out, end); // command object
  89. out = AMFWriteDouble(out, end, ms); // milliSeconds Number
  90. return out;
  91. }
  92. // @param[in] pause 1-pausing, 0-resuing
  93. // response: success: onStatus-NetStream.Pause.Notify/NetStream.Unpause.Notify, failure: _error message
  94. uint8_t* rtmp_netstream_pause(uint8_t* out, size_t bytes, double transactionId, int pause, double ms)
  95. {
  96. uint8_t* end = out + bytes;
  97. const char* command = "pause";
  98. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  99. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  100. out = AMFWriteNull(out, end); // command object
  101. out = AMFWriteBoolean(out, end, (uint8_t)pause); // pause/unpause
  102. out = AMFWriteDouble(out, end, ms); // milliSeconds Number
  103. return out;
  104. }
  105. uint8_t* rtmp_netstream_release_stream(uint8_t* out, size_t bytes, double transactionId, const char* stream_name)
  106. {
  107. uint8_t* end = out + bytes;
  108. const char* command = "releaseStream";
  109. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  110. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  111. out = AMFWriteNull(out, end); // command object
  112. out = AMFWriteString(out, end, stream_name, strlen(stream_name)); // playpath
  113. return out;
  114. }
  115. uint8_t* rtmp_netstream_fcpublish(uint8_t* out, size_t bytes, double transactionId, const char* stream_name)
  116. {
  117. uint8_t* end = out + bytes;
  118. const char* command = "FCPublish";
  119. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  120. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  121. out = AMFWriteNull(out, end); // command object
  122. out = AMFWriteString(out, end, stream_name, strlen(stream_name)); // playpath
  123. return out;
  124. }
  125. uint8_t* rtmp_netstream_fcunpublish(uint8_t* out, size_t bytes, double transactionId, const char* stream_name)
  126. {
  127. uint8_t* end = out + bytes;
  128. const char* command = "FCUnpublish";
  129. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  130. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  131. out = AMFWriteNull(out, end); // command object
  132. out = AMFWriteString(out, end, stream_name, strlen(stream_name)); // playpath
  133. return out;
  134. }
  135. uint8_t* rtmp_netstream_fcsubscribe(uint8_t* out, size_t bytes, double transactionId, const char* subscribepath)
  136. {
  137. uint8_t* end = out + bytes;
  138. const char* command = "FCSubscribe";
  139. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  140. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  141. out = AMFWriteNull(out, end); // command object
  142. out = AMFWriteString(out, end, subscribepath, strlen(subscribepath)); // subscribepath
  143. return out;
  144. }
  145. uint8_t* rtmp_netstream_fcunsubscribe(uint8_t* out, size_t bytes, double transactionId, const char* subscribepath)
  146. {
  147. uint8_t* end = out + bytes;
  148. const char* command = "FCUnsubscribe";
  149. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  150. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  151. out = AMFWriteNull(out, end); // command object
  152. out = AMFWriteString(out, end, subscribepath, strlen(subscribepath)); // subscribepath
  153. return out;
  154. }
  155. uint8_t* rtmp_netstream_onbwdone(uint8_t* out, size_t bytes, double transactionId, double bandwidth)
  156. {
  157. uint8_t* end = out + bytes;
  158. const char* command = "onBWDone";
  159. out = AMFWriteString(out, end, command, strlen(command));
  160. out = AMFWriteDouble(out, end, transactionId);
  161. out = AMFWriteNull(out, end);
  162. out = AMFWriteDouble(out, end, bandwidth);
  163. return out;
  164. }
  165. // c -> s
  166. uint8_t* rtmp_netstream_checkbw(uint8_t* out, size_t bytes, double transactionId)
  167. {
  168. uint8_t* end = out + bytes;
  169. const char* command = "_checkbw";
  170. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  171. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  172. out = AMFWriteNull(out, end); // command object
  173. return out;
  174. }
  175. uint8_t* rtmp_netstream_onstatus(uint8_t* out, size_t bytes, double transactionId, const char* level, const char* code, const char* description)
  176. {
  177. uint8_t* end = out + bytes;
  178. const char* command = "onStatus";
  179. if (NULL == level || NULL == code || NULL == description)
  180. return NULL;
  181. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  182. out = AMFWriteDouble(out, end, transactionId); // Transaction ID
  183. out = AMFWriteNull(out, end); // command object
  184. out = AMFWriteObject(out, end);
  185. out = AMFWriteNamedString(out, end, "level", 5, level, strlen(level));
  186. out = AMFWriteNamedString(out, end, "code", 4, code, strlen(code));
  187. out = AMFWriteNamedString(out, end, "description", 11, description, strlen(description));
  188. out = AMFWriteObjectEnd(out, end);
  189. return out;
  190. }
  191. uint8_t* rtmp_netstream_rtmpsampleaccess(uint8_t* out, size_t bytes)
  192. {
  193. uint8_t* end = out + bytes;
  194. const char* command = "|RtmpSampleAccess";
  195. out = AMFWriteString(out, end, command, strlen(command)); // Command Name
  196. out = AMFWriteBoolean(out, end, 1);
  197. out = AMFWriteBoolean(out, end, 1);
  198. return out;
  199. }