srs_rtmp_dump.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013-2015 SRS(ossrs)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. /**
  20. gcc srs_rtmp_dump.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_rtmp_dump
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <getopt.h>
  26. #include <assert.h>
  27. #include "../../objs/include/srs_librtmp.h"
  28. void parse_amf0_object(char* p, srs_amf0_t args)
  29. {
  30. char opvt = 0; // object property value type.
  31. const char* opnp = NULL; // object property name ptr.
  32. const char* opvp = NULL; // object property value ptr.
  33. while (*p) {
  34. switch (*p++) {
  35. case 'O':
  36. while (*p && *p++ != ':') {
  37. }
  38. if (*p++ == '1') {
  39. printf("amf0 object start\n");
  40. } else {
  41. printf("amf0 object end\n");
  42. }
  43. break;
  44. case 'N':
  45. opvt = *p++;
  46. if (*p++ != ':') {
  47. printf("object property must split by :.\n");
  48. exit(-1);
  49. }
  50. opnp = p++;
  51. while (*p && *p++ != ':') {
  52. }
  53. p[-1] = 0;
  54. opvp = p;
  55. printf("amf0 %c property[%s]=%s\n", opvt, opnp, opvp);
  56. switch(opvt) {
  57. case 'S':
  58. srs_amf0_object_property_set(args, opnp, srs_amf0_create_string(opvp));
  59. break;
  60. default:
  61. printf("unsupported object property.\n");
  62. exit(-1);
  63. }
  64. *p=0;
  65. break;
  66. default:
  67. printf("only supports an object arg.\n");
  68. exit(-1);
  69. }
  70. }
  71. }
  72. int main(int argc, char** argv)
  73. {
  74. srs_flv_t flv = NULL;
  75. srs_rtmp_t rtmp = NULL;
  76. printf("dump rtmp stream to flv file\n");
  77. printf("srs(ossrs) client librtmp library.\n");
  78. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  79. printf("@refer to http://rtmpdump.mplayerhq.hu/rtmpdump.1.html\n");
  80. struct option long_options[] = {
  81. {"rtmp", required_argument, 0, 'r'},
  82. {"flv", required_argument, 0, 'o'},
  83. {"swfUrl", required_argument, 0, 's'},
  84. {"tcUrl", required_argument, 0, 't'},
  85. {"pageUrl", required_argument, 0, 'p'},
  86. {"conn", required_argument, 0, 'C'},
  87. {"complex", no_argument, 0, 'x'},
  88. {"help", no_argument, 0, 'h'},
  89. {0, 0, 0, 0}
  90. };
  91. int show_help = 0;
  92. int complex_handshake = 0;
  93. const char* rtmp_url = NULL;
  94. const char* output_flv = NULL;
  95. const char* swfUrl = NULL;
  96. const char* tcUrl = NULL;
  97. const char* pageUrl = NULL;
  98. srs_amf0_t args = NULL;
  99. int opt = 0;
  100. int option_index = 0;
  101. while((opt = getopt_long(argc, argv, "hxr:o:s:t:p:C:", long_options, &option_index)) != -1){
  102. switch(opt){
  103. case 'r':
  104. rtmp_url = optarg;
  105. break;
  106. case 'o':
  107. output_flv = optarg;
  108. break;
  109. case 's':
  110. swfUrl = optarg;
  111. break;
  112. case 't':
  113. tcUrl = optarg;
  114. break;
  115. case 'p':
  116. pageUrl = optarg;
  117. break;
  118. case 'C':
  119. if (!args) {
  120. args = srs_amf0_create_object();
  121. }
  122. char* p = (char*)optarg;
  123. parse_amf0_object(p, args);
  124. break;
  125. case 'x':
  126. complex_handshake = 1;
  127. break;
  128. case 'h':
  129. show_help = 1;
  130. break;
  131. default:
  132. printf("unsupported opt.\n");
  133. exit(-1);
  134. }
  135. }
  136. if (!rtmp_url || show_help) {
  137. printf("Usage: %s -r url [-o output] [-s swfUrl] [-t tcUrl] [-p pageUrl] [-C conndata] [--complex] [-h]\n"
  138. "Options:\n"
  139. " --rtmp -r url\n"
  140. " URL of the server and media content.\n"
  141. " --flv -o output\n"
  142. " Specify the output file name. If the name is − or is omitted, the stream is written to stdout.\n"
  143. " --complex\n"
  144. " Whether use complex handshake(srs-librtmp with ssl required).\n"
  145. " --swfUrl -s url\n"
  146. " URL of the SWF player for the media. By default no value will be sent.\n"
  147. " --tcUrl -t url\n"
  148. " URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath.\n"
  149. " --pageUrl -p url\n"
  150. " URL of the web page in which the media was embedded. By default no value will be sent.\n"
  151. " −−conn −C type:data\n"
  152. " Append arbitrary AMF data to the Connect message. The type must be B for Boolean, N for number, S for string, O for object, or Z for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, respectively. Likewise for Objects the data must be 0 or 1 to end or begin an object, respectively. Data items in subobjects may be named, by prefixing the type with 'N' and specifying the name before the value, e.g. NB:myFlag:1. This option may be used multiple times to construct arbitrary AMF sequences. E.g.\n"
  153. " −C B:1 −C S:authMe −C O:1 −C NN:code:1.23 −C NS:flag:ok −C O:0\n"
  154. " -C O:1 -C NS:CONN:\" -C B:4Rg9vr0\" -C O:0\n"
  155. " @remark, support a object args only.\n"
  156. " --help -h\n"
  157. " Print a summary of command options.\n"
  158. "For example:\n"
  159. " %s -r rtmp://127.0.0.1:1935/live/livestream -o output.flv\n"
  160. " %s -h\n",
  161. argv[0], argv[0], argv[0]);
  162. exit(-1);
  163. }
  164. srs_human_trace("rtmp url: %s", rtmp_url);
  165. srs_human_trace("handshake: %s", (complex_handshake? "complex" : "simple"));
  166. srs_human_trace("swfUrl: %s", swfUrl);
  167. srs_human_trace("pageUrl: %s", pageUrl);
  168. srs_human_trace("tcUrl: %s", tcUrl);
  169. if (output_flv) {
  170. srs_human_trace("flv output path: %s", output_flv);
  171. } else {
  172. srs_human_trace("output to console");
  173. }
  174. rtmp = srs_rtmp_create(rtmp_url);
  175. if (srs_rtmp_dns_resolve(rtmp) != 0) {
  176. srs_human_trace("dns resolve failed.");
  177. goto rtmp_destroy;
  178. }
  179. if (srs_rtmp_connect_server(rtmp) != 0) {
  180. srs_human_trace("connect to server failed.");
  181. goto rtmp_destroy;
  182. }
  183. if (complex_handshake) {
  184. if (srs_rtmp_do_complex_handshake(rtmp) != 0) {
  185. srs_human_trace("complex handshake failed.");
  186. goto rtmp_destroy;
  187. }
  188. srs_human_trace("do complex handshake success");
  189. } else {
  190. if (srs_rtmp_do_simple_handshake(rtmp) != 0) {
  191. srs_human_trace("simple handshake failed.");
  192. goto rtmp_destroy;
  193. }
  194. srs_human_trace("do simple handshake success");
  195. }
  196. if (srs_rtmp_set_connect_args(rtmp, tcUrl, swfUrl, pageUrl, args) != 0) {
  197. srs_human_trace("set connect args failed.");
  198. goto rtmp_destroy;
  199. }
  200. if (srs_rtmp_connect_app(rtmp) != 0) {
  201. srs_human_trace("connect vhost/app failed.");
  202. goto rtmp_destroy;
  203. }
  204. srs_human_trace("connect vhost/app success");
  205. if (srs_rtmp_play_stream(rtmp) != 0) {
  206. srs_human_trace("play stream failed.");
  207. goto rtmp_destroy;
  208. }
  209. srs_human_trace("play stream success");
  210. if (output_flv) {
  211. flv = srs_flv_open_write(output_flv);
  212. }
  213. if (flv) {
  214. // flv header
  215. char header[9];
  216. // 3bytes, signature, "FLV",
  217. header[0] = 'F';
  218. header[1] = 'L';
  219. header[2] = 'V';
  220. // 1bytes, version, 0x01,
  221. header[3] = 0x01;
  222. // 1bytes, flags, UB[5] 0, UB[1] audio present, UB[1] 0, UB[1] video present.
  223. header[4] = 0x03; // audio + video.
  224. // 4bytes, dataoffset
  225. header[5] = 0x00;
  226. header[6] = 0x00;
  227. header[7] = 0x00;
  228. header[8] = 0x09;
  229. if (srs_flv_write_header(flv, header) != 0) {
  230. srs_human_trace("write flv header failed.");
  231. goto rtmp_destroy;
  232. }
  233. }
  234. int64_t nb_packets = 0;
  235. u_int32_t pre_timestamp = 0;
  236. int64_t pre_now = -1;
  237. int64_t start_time = -1;
  238. for (;;) {
  239. int size;
  240. char type;
  241. char* data;
  242. u_int32_t timestamp;
  243. if (srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
  244. srs_human_trace("read rtmp packet failed.");
  245. goto rtmp_destroy;
  246. }
  247. if (pre_now == -1) {
  248. pre_now = srs_utils_time_ms();
  249. }
  250. if (start_time == -1) {
  251. start_time = srs_utils_time_ms();
  252. }
  253. if (srs_human_print_rtmp_packet4(type, timestamp, data, size, pre_timestamp, pre_now, start_time, nb_packets++) != 0) {
  254. srs_human_trace("print rtmp packet failed.");
  255. goto rtmp_destroy;
  256. }
  257. pre_timestamp = timestamp;
  258. pre_now = srs_utils_time_ms();
  259. // we only write some types of messages to flv file.
  260. int is_flv_msg = type == SRS_RTMP_TYPE_AUDIO
  261. || type == SRS_RTMP_TYPE_VIDEO || type == SRS_RTMP_TYPE_SCRIPT;
  262. // for script data, ignore except onMetaData
  263. if (type == SRS_RTMP_TYPE_SCRIPT) {
  264. if (!srs_rtmp_is_onMetaData(type, data, size)) {
  265. is_flv_msg = 0;
  266. }
  267. }
  268. if (flv) {
  269. if (is_flv_msg) {
  270. if (srs_flv_write_tag(flv, type, timestamp, data, size) != 0) {
  271. srs_human_trace("dump rtmp packet failed.");
  272. goto rtmp_destroy;
  273. }
  274. } else {
  275. srs_human_trace("drop message type=%#x, size=%dB", type, size);
  276. }
  277. }
  278. free(data);
  279. }
  280. rtmp_destroy:
  281. srs_rtmp_destroy(rtmp);
  282. srs_flv_close(flv);
  283. srs_human_trace("completed");
  284. return 0;
  285. }