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.

67 lines
2.1KB

  1. #include "sockutil.h"
  2. #include "rtmp-client.h"
  3. #include "flv-writer.h"
  4. #include "flv-proto.h"
  5. #include <assert.h>
  6. #include <stdio.h>
  7. static void* s_flv;
  8. static int rtmp_client_send(void* param, const void* header, size_t len, const void* data, size_t bytes)
  9. {
  10. socket_t* socket = (socket_t*)param;
  11. socket_bufvec_t vec[2];
  12. socket_setbufvec(vec, 0, (void*)header, len);
  13. socket_setbufvec(vec, 1, (void*)data, bytes);
  14. return socket_send_v_all_by_time(*socket, vec, bytes ? 2 : 1, 0, 2000);
  15. }
  16. static int rtmp_client_onaudio(void* /*param*/, const void* data, size_t bytes, uint32_t timestamp)
  17. {
  18. return flv_writer_input(s_flv, FLV_TYPE_AUDIO, data, bytes, timestamp);
  19. }
  20. static int rtmp_client_onvideo(void* /*param*/, const void* data, size_t bytes, uint32_t timestamp)
  21. {
  22. return flv_writer_input(s_flv, FLV_TYPE_VIDEO, data, bytes, timestamp);
  23. }
  24. static int rtmp_client_onscript(void* /*param*/, const void* data, size_t bytes, uint32_t timestamp)
  25. {
  26. return flv_writer_input(s_flv, FLV_TYPE_SCRIPT, data, bytes, timestamp);
  27. }
  28. // rtmp://live.alivecdn.com/live/hello?key=xxxxxx
  29. // rtmp_publish_aio_test("live.alivecdn.com", "live", "hello?key=xxxxxx", save-to-local-flv-file-name)
  30. void rtmp_play_test(const char* host, const char* app, const char* stream, const char* flv)
  31. {
  32. static char packet[8 * 1024 * 1024];
  33. snprintf(packet, sizeof(packet), "rtmp://%s/%s", host, app); // tcurl
  34. socket_init();
  35. socket_t socket = socket_connect_host(host, 1935, 2000);
  36. socket_setnonblock(socket, 0);
  37. struct rtmp_client_handler_t handler;
  38. memset(&handler, 0, sizeof(handler));
  39. handler.send = rtmp_client_send;
  40. handler.onaudio = rtmp_client_onaudio;
  41. handler.onvideo = rtmp_client_onvideo;
  42. handler.onscript = rtmp_client_onscript;
  43. rtmp_client_t* rtmp = rtmp_client_create(app, stream, packet/*tcurl*/, &socket, &handler);
  44. s_flv = flv_writer_create(flv);
  45. int r = rtmp_client_start(rtmp, 1);
  46. while ((r = socket_recv(socket, packet, sizeof(packet), 0)) > 0)
  47. {
  48. assert(0 == rtmp_client_input(rtmp, packet, r));
  49. }
  50. rtmp_client_stop(rtmp);
  51. flv_writer_destroy(s_flv);
  52. rtmp_client_destroy(rtmp);
  53. socket_close(socket);
  54. socket_cleanup();
  55. }