srs_play.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2018 Winlin
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "../../objs/include/srs_librtmp.h"
  26. int main(int argc, char** argv)
  27. {
  28. printf("suck rtmp stream like rtmpdump\n");
  29. printf("srs(ossrs) client librtmp library.\n");
  30. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  31. if (argc <= 1) {
  32. printf("Usage: %s <rtmp_url>\n"
  33. " rtmp_url RTMP stream url to play\n"
  34. "For example:\n"
  35. " %s rtmp://127.0.0.1:1935/live/livestream\n",
  36. argv[0], argv[0]);
  37. exit(-1);
  38. }
  39. srs_human_trace("rtmp url: %s", argv[1]);
  40. srs_rtmp_t rtmp = srs_rtmp_create(argv[1]);
  41. if (srs_rtmp_handshake(rtmp) != 0) {
  42. srs_human_trace("simple handshake failed.");
  43. goto rtmp_destroy;
  44. }
  45. srs_human_trace("simple handshake success");
  46. if (srs_rtmp_connect_app(rtmp) != 0) {
  47. srs_human_trace("connect vhost/app failed.");
  48. goto rtmp_destroy;
  49. }
  50. srs_human_trace("connect vhost/app success");
  51. if (srs_rtmp_play_stream(rtmp) != 0) {
  52. srs_human_trace("play stream failed.");
  53. goto rtmp_destroy;
  54. }
  55. srs_human_trace("play stream success");
  56. char buffer[1024];
  57. for (;;) {
  58. int size;
  59. char type;
  60. char* data;
  61. uint32_t timestamp;
  62. if (srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
  63. goto rtmp_destroy;
  64. }
  65. if (srs_human_format_rtmp_packet(buffer, sizeof(buffer), type, timestamp, data, size) != 0) {
  66. goto rtmp_destroy;
  67. }
  68. srs_human_trace("%s", buffer);
  69. free(data);
  70. }
  71. rtmp_destroy:
  72. srs_rtmp_destroy(rtmp);
  73. return 0;
  74. }