2
0

srs_publish.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <unistd.h>
  26. #include "../../objs/include/srs_librtmp.h"
  27. int main(int argc, char** argv)
  28. {
  29. printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\n");
  30. printf("srs(ossrs) client librtmp library.\n");
  31. printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
  32. if (argc <= 1) {
  33. printf("Usage: %s <rtmp_url>\n"
  34. " rtmp_url RTMP stream url to publish\n"
  35. "For example:\n"
  36. " %s rtmp://127.0.0.1:1935/live/livestream\n",
  37. argv[0], argv[0]);
  38. exit(-1);
  39. }
  40. // warn it .
  41. // @see: https://github.com/ossrs/srs/issues/126
  42. srs_human_trace("\033[33m%s\033[0m",
  43. "[warning] it's only a sample to use librtmp. "
  44. "please never use it to publish and test forward/transcode/edge/HLS whatever. "
  45. "you should refer to this tool to use the srs-librtmp to publish the real media stream."
  46. "read about: https://github.com/ossrs/srs/issues/126");
  47. srs_human_trace("rtmp url: %s", argv[1]);
  48. srs_rtmp_t rtmp = srs_rtmp_create(argv[1]);
  49. if (srs_rtmp_handshake(rtmp) != 0) {
  50. srs_human_trace("simple handshake failed.");
  51. goto rtmp_destroy;
  52. }
  53. srs_human_trace("simple handshake success");
  54. if (srs_rtmp_connect_app(rtmp) != 0) {
  55. srs_human_trace("connect vhost/app failed.");
  56. goto rtmp_destroy;
  57. }
  58. srs_human_trace("connect vhost/app success");
  59. if (srs_rtmp_publish_stream(rtmp) != 0) {
  60. srs_human_trace("publish stream failed.");
  61. goto rtmp_destroy;
  62. }
  63. srs_human_trace("publish stream success");
  64. uint32_t timestamp = 0;
  65. for (;;) {
  66. char type = SRS_RTMP_TYPE_VIDEO;
  67. int size = 4096;
  68. char* data = (char*)malloc(4096);
  69. timestamp += 40;
  70. if (srs_rtmp_write_packet(rtmp, type, timestamp, data, size) != 0) {
  71. goto rtmp_destroy;
  72. }
  73. srs_human_trace("sent packet: type=%s, time=%d, size=%d", srs_human_flv_tag_type2string(type), timestamp, size);
  74. usleep(40 * 1000);
  75. }
  76. rtmp_destroy:
  77. srs_rtmp_destroy(rtmp);
  78. return 0;
  79. }