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.

rtmp-publish-aio-test.cpp 3.1KB

10 maanden geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "sys/sock.h"
  2. #include "sys/thread.h"
  3. #include "sys/system.h"
  4. #include "sys/sync.hpp"
  5. #include "aio-connect.h"
  6. #include "aio-timeout.h"
  7. #include "aio-rtmp-client.h"
  8. #include "flv-reader.h"
  9. #include "flv-proto.h"
  10. #include <stdio.h>
  11. #include <assert.h>
  12. #include <list>
  13. static struct
  14. {
  15. int code; // rtmp error code
  16. const char* file;
  17. char tcurl[4 * 1024];
  18. const char* app;
  19. const char* stream;
  20. aio_rtmp_client_t* rtmp;
  21. } s_param;
  22. static int STDCALL rtmp_client_push(void* flv)
  23. {
  24. int r, type;
  25. size_t taglen;
  26. uint32_t timestamp;
  27. uint64_t clock0 = system_clock();
  28. void* f = flv_reader_create((const char*)flv);
  29. static char packet[2 * 1024 * 1024];
  30. while (0 == s_param.code && 1 == flv_reader_read(f, &type, &timestamp, &taglen, packet, sizeof(packet)))
  31. {
  32. uint64_t t = system_clock();
  33. if(clock0 + timestamp > t && clock0 + timestamp < t + 3 * 1000)
  34. system_sleep(clock0 + timestamp - t);
  35. else if (t + timestamp > t + 3 * 1000)
  36. t = t - timestamp;
  37. while (s_param.rtmp && aio_rtmp_client_get_unsend(s_param.rtmp) > 8 * 1024 * 1024)
  38. system_sleep(1000); // can't send?
  39. switch (type)
  40. {
  41. case FLV_TYPE_AUDIO:
  42. r = aio_rtmp_client_send_audio(s_param.rtmp, packet, taglen, timestamp);
  43. break;
  44. case FLV_TYPE_VIDEO:
  45. r = aio_rtmp_client_send_video(s_param.rtmp, packet, taglen, timestamp);
  46. break;
  47. case FLV_TYPE_SCRIPT:
  48. r = aio_rtmp_client_send_script(s_param.rtmp, packet, taglen, timestamp);
  49. break;
  50. default:
  51. r = 0;
  52. break;
  53. }
  54. if (0 != r)
  55. {
  56. assert(0);
  57. break; // TODO: handle send failed
  58. }
  59. }
  60. aio_rtmp_client_destroy(s_param.rtmp);
  61. flv_reader_destroy(f);
  62. s_param.file = NULL;
  63. s_param.rtmp = NULL;
  64. return 0;
  65. }
  66. static void rtmp_client_publish_onsend(void*, size_t)
  67. {
  68. }
  69. static void rtmp_client_publish_onready(void*)
  70. {
  71. pthread_t thread;
  72. thread_create(&thread, rtmp_client_push, (void*)s_param.file);
  73. thread_detach(thread);
  74. }
  75. static void rtmp_client_publish_onerror(void* /*param*/, int code)
  76. {
  77. // exit publish thread
  78. s_param.code = code;
  79. }
  80. static void rtmp_onconnect(void*, int code, socket_t tcp, aio_socket_t aio)
  81. {
  82. assert(0 == code);
  83. struct aio_rtmp_client_handler_t handler;
  84. memset(&handler, 0, sizeof(handler));
  85. handler.onerror = rtmp_client_publish_onerror;
  86. handler.onready = rtmp_client_publish_onready;
  87. handler.onsend = rtmp_client_publish_onsend;
  88. s_param.code = 0;
  89. s_param.rtmp = aio_rtmp_client_create(aio, s_param.app, s_param.stream, s_param.tcurl, &handler, NULL);
  90. assert(0 == aio_rtmp_client_start(s_param.rtmp, 0));
  91. }
  92. // rtmp://video-center.alivecdn.com/live/hello?vhost=your.domain
  93. // rtmp_publish_aio_test("video-center.alivecdn.com", "live", "hello?vhost=your.domain", local-flv-file-name)
  94. void rtmp_publish_aio_test(const char* host, const char* app, const char* stream, const char* file)
  95. {
  96. s_param.file = file;
  97. s_param.app = app;
  98. s_param.stream = stream;
  99. snprintf(s_param.tcurl, sizeof(s_param.tcurl), "rtmp://%s/%s", host, app); // tcurl
  100. aio_socket_init(1);
  101. aio_connect(host, 1935, 3000, rtmp_onconnect, NULL);
  102. while (aio_socket_process(5000) > 0)
  103. {
  104. aio_timeout_process();
  105. }
  106. aio_socket_clean();
  107. }