srs_publish.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_publish
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include "../../objs/include/srs_librtmp.h"
  26. int main(int argc, char** argv)
  27. {
  28. printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\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 publish\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. // warn it .
  40. // @see: https://github.com/ossrs/srs/issues/126
  41. srs_human_trace("\033[33m%s\033[0m",
  42. "[warning] it's only a sample to use librtmp. "
  43. "please never use it to publish and test forward/transcode/edge/HLS whatever. "
  44. "you should refer to this tool to use the srs-librtmp to publish the real media stream."
  45. "read about: https://github.com/ossrs/srs/issues/126");
  46. srs_human_trace("rtmp url: %s", argv[1]);
  47. srs_rtmp_t rtmp = srs_rtmp_create(argv[1]);
  48. if (srs_rtmp_handshake(rtmp) != 0) {
  49. srs_human_trace("simple handshake failed.");
  50. goto rtmp_destroy;
  51. }
  52. srs_human_trace("simple handshake success");
  53. if (srs_rtmp_connect_app(rtmp) != 0) {
  54. srs_human_trace("connect vhost/app failed.");
  55. goto rtmp_destroy;
  56. }
  57. srs_human_trace("connect vhost/app success");
  58. if (srs_rtmp_publish_stream(rtmp) != 0) {
  59. srs_human_trace("publish stream failed.");
  60. goto rtmp_destroy;
  61. }
  62. srs_human_trace("publish stream success");
  63. u_int32_t timestamp = 0;
  64. for (;;) {
  65. char type = SRS_RTMP_TYPE_VIDEO;
  66. int size = 4096;
  67. char* data = (char*)malloc(4096);
  68. timestamp += 40;
  69. if (srs_rtmp_write_packet(rtmp, type, timestamp, data, size) != 0) {
  70. goto rtmp_destroy;
  71. }
  72. srs_human_trace("sent packet: type=%s, time=%d, size=%d",
  73. 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. }