rtmp-stream.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <obs-module.h>
  2. #include <obs-avc.h>
  3. #include <util/platform.h>
  4. #include <util/circlebuf.h>
  5. #include <util/dstr.h>
  6. #include <util/threading.h>
  7. #include <inttypes.h>
  8. #include "librtmp/rtmp.h"
  9. #include "librtmp/log.h"
  10. #include "flv-mux.h"
  11. #include "net-if.h"
  12. #ifdef _WIN32
  13. #include <Iphlpapi.h>
  14. #else
  15. #include <sys/ioctl.h>
  16. #endif
  17. #define do_log(level, format, ...) \
  18. blog(level, "[rtmp stream: '%s'] " format, \
  19. obs_output_get_name(stream->output), ##__VA_ARGS__)
  20. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  21. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  22. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  23. #define OPT_DYN_BITRATE "dyn_bitrate"
  24. #define OPT_DROP_THRESHOLD "drop_threshold_ms"
  25. #define OPT_PFRAME_DROP_THRESHOLD "pframe_drop_threshold_ms"
  26. #define OPT_MAX_SHUTDOWN_TIME_SEC "max_shutdown_time_sec"
  27. #define OPT_BIND_IP "bind_ip"
  28. #define OPT_NEWSOCKETLOOP_ENABLED "new_socket_loop_enabled"
  29. #define OPT_LOWLATENCY_ENABLED "low_latency_mode_enabled"
  30. //#define TEST_FRAMEDROPS
  31. //#define TEST_FRAMEDROPS_WITH_BITRATE_SHORTCUTS
  32. #ifdef TEST_FRAMEDROPS
  33. #define DROPTEST_MAX_KBPS 3000
  34. #define DROPTEST_MAX_BYTES (DROPTEST_MAX_KBPS * 1000 / 8)
  35. struct droptest_info {
  36. uint64_t ts;
  37. size_t size;
  38. };
  39. #endif
  40. struct dbr_frame {
  41. uint64_t send_beg;
  42. uint64_t send_end;
  43. size_t size;
  44. };
  45. struct rtmp_stream {
  46. obs_output_t *output;
  47. pthread_mutex_t packets_mutex;
  48. struct circlebuf packets;
  49. bool sent_headers;
  50. bool got_first_video;
  51. int64_t start_dts_offset;
  52. volatile bool connecting;
  53. pthread_t connect_thread;
  54. volatile bool active;
  55. volatile bool disconnected;
  56. volatile bool encode_error;
  57. pthread_t send_thread;
  58. int max_shutdown_time_sec;
  59. os_sem_t *send_sem;
  60. os_event_t *stop_event;
  61. uint64_t stop_ts;
  62. uint64_t shutdown_timeout_ts;
  63. struct dstr path, key;
  64. struct dstr username, password;
  65. struct dstr encoder_name;
  66. struct dstr bind_ip;
  67. /* frame drop variables */
  68. int64_t drop_threshold_usec;
  69. int64_t pframe_drop_threshold_usec;
  70. int min_priority;
  71. float congestion;
  72. int64_t last_dts_usec;
  73. uint64_t total_bytes_sent;
  74. int dropped_frames;
  75. #ifdef TEST_FRAMEDROPS
  76. struct circlebuf droptest_info;
  77. uint64_t droptest_last_key_check;
  78. size_t droptest_max;
  79. size_t droptest_size;
  80. #endif
  81. pthread_mutex_t dbr_mutex;
  82. struct circlebuf dbr_frames;
  83. size_t dbr_data_size;
  84. uint64_t dbr_inc_timeout;
  85. long audio_bitrate;
  86. long dbr_est_bitrate;
  87. long dbr_orig_bitrate;
  88. long dbr_prev_bitrate;
  89. long dbr_cur_bitrate;
  90. long dbr_inc_bitrate;
  91. bool dbr_enabled;
  92. RTMP rtmp;
  93. bool new_socket_loop;
  94. bool low_latency_mode;
  95. bool disable_send_window_optimization;
  96. bool socket_thread_active;
  97. pthread_t socket_thread;
  98. uint8_t *write_buf;
  99. size_t write_buf_len;
  100. size_t write_buf_size;
  101. pthread_mutex_t write_buf_mutex;
  102. os_event_t *buffer_space_available_event;
  103. os_event_t *buffer_has_data_event;
  104. os_event_t *socket_available_event;
  105. os_event_t *send_thread_signaled_exit;
  106. };
  107. #ifdef _WIN32
  108. void *socket_thread_windows(void *data);
  109. #endif