rtmp-stream.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <obs-module.h>
  2. #include <util/platform.h>
  3. #include <util/deque.h>
  4. #include <util/dstr.h>
  5. #include <util/threading.h>
  6. #include <inttypes.h>
  7. #include "librtmp/rtmp.h"
  8. #include "librtmp/log.h"
  9. #include "flv-mux.h"
  10. #include "net-if.h"
  11. #ifdef _WIN32
  12. #include <Iphlpapi.h>
  13. #else
  14. #include <sys/ioctl.h>
  15. #endif
  16. #define do_log(level, format, ...) \
  17. blog(level, "[rtmp stream: '%s'] " format, \
  18. obs_output_get_name(stream->output), ##__VA_ARGS__)
  19. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  20. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  21. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  22. #define OPT_DYN_BITRATE "dyn_bitrate"
  23. #define OPT_DROP_THRESHOLD "drop_threshold_ms"
  24. #define OPT_PFRAME_DROP_THRESHOLD "pframe_drop_threshold_ms"
  25. #define OPT_MAX_SHUTDOWN_TIME_SEC "max_shutdown_time_sec"
  26. #define OPT_BIND_IP "bind_ip"
  27. #define OPT_IP_FAMILY "ip_family"
  28. #define OPT_NEWSOCKETLOOP_ENABLED "new_socket_loop_enabled"
  29. #define OPT_LOWLATENCY_ENABLED "low_latency_mode_enabled"
  30. #define OPT_METADATA_MULTITRACK "metadata_multitrack"
  31. //#define TEST_FRAMEDROPS
  32. //#define TEST_FRAMEDROPS_WITH_BITRATE_SHORTCUTS
  33. #ifdef TEST_FRAMEDROPS
  34. #define DROPTEST_MAX_KBPS 3000
  35. #define DROPTEST_MAX_BYTES (DROPTEST_MAX_KBPS * 1000 / 8)
  36. struct droptest_info {
  37. uint64_t ts;
  38. size_t size;
  39. };
  40. #endif
  41. struct dbr_frame {
  42. uint64_t send_beg;
  43. uint64_t send_end;
  44. size_t size;
  45. };
  46. struct rtmp_stream {
  47. obs_output_t *output;
  48. pthread_mutex_t packets_mutex;
  49. struct deque packets;
  50. bool sent_headers;
  51. bool got_first_video;
  52. int64_t start_dts_offset;
  53. volatile bool connecting;
  54. pthread_t connect_thread;
  55. volatile bool active;
  56. volatile bool disconnected;
  57. volatile bool encode_error;
  58. pthread_t send_thread;
  59. int max_shutdown_time_sec;
  60. os_sem_t *send_sem;
  61. os_event_t *stop_event;
  62. uint64_t stop_ts;
  63. uint64_t shutdown_timeout_ts;
  64. struct dstr path, key;
  65. struct dstr username, password;
  66. struct dstr encoder_name;
  67. struct dstr bind_ip;
  68. socklen_t addrlen_hint; /* hint IPv4 vs IPv6 */
  69. /* frame drop variables */
  70. int64_t drop_threshold_usec;
  71. int64_t pframe_drop_threshold_usec;
  72. int min_priority;
  73. float congestion;
  74. int64_t last_dts_usec;
  75. uint64_t total_bytes_sent;
  76. int dropped_frames;
  77. #ifdef TEST_FRAMEDROPS
  78. struct deque droptest_info;
  79. uint64_t droptest_last_key_check;
  80. size_t droptest_max;
  81. size_t droptest_size;
  82. #endif
  83. pthread_mutex_t dbr_mutex;
  84. struct deque dbr_frames;
  85. size_t dbr_data_size;
  86. uint64_t dbr_inc_timeout;
  87. long audio_bitrate;
  88. long dbr_est_bitrate;
  89. long dbr_orig_bitrate;
  90. long dbr_prev_bitrate;
  91. long dbr_cur_bitrate;
  92. long dbr_inc_bitrate;
  93. bool dbr_enabled;
  94. enum video_id_t video_codec;
  95. RTMP rtmp;
  96. bool new_socket_loop;
  97. bool low_latency_mode;
  98. bool disable_send_window_optimization;
  99. bool socket_thread_active;
  100. pthread_t socket_thread;
  101. uint8_t *write_buf;
  102. size_t write_buf_len;
  103. size_t write_buf_size;
  104. pthread_mutex_t write_buf_mutex;
  105. os_event_t *buffer_space_available_event;
  106. os_event_t *buffer_has_data_event;
  107. os_event_t *socket_available_event;
  108. os_event_t *send_thread_signaled_exit;
  109. };
  110. #ifdef _WIN32
  111. void *socket_thread_windows(void *data);
  112. #endif
  113. /* Adapted from FFmpeg's libavutil/pixfmt.h
  114. *
  115. * Renamed to make it apparent that these are not imported as this module does
  116. * not use or link against FFmpeg.
  117. */
  118. /**
  119. * Chromaticity coordinates of the source primaries.
  120. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.1 and ITU-T H.273.
  121. */
  122. enum OBSColorPrimaries {
  123. OBSCOL_PRI_RESERVED0 = 0,
  124. OBSCOL_PRI_BT709 =
  125. 1, ///< also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
  126. OBSCOL_PRI_UNSPECIFIED = 2,
  127. OBSCOL_PRI_RESERVED = 3,
  128. OBSCOL_PRI_BT470M =
  129. 4, ///< also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  130. OBSCOL_PRI_BT470BG =
  131. 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
  132. OBSCOL_PRI_SMPTE170M =
  133. 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
  134. OBSCOL_PRI_SMPTE240M =
  135. 7, ///< identical to above, also called "SMPTE C" even though it uses D65
  136. OBSCOL_PRI_FILM = 8, ///< colour filters using Illuminant C
  137. OBSCOL_PRI_BT2020 = 9, ///< ITU-R BT2020
  138. OBSCOL_PRI_SMPTE428 = 10, ///< SMPTE ST 428-1 (CIE 1931 XYZ)
  139. OBSCOL_PRI_SMPTEST428_1 = OBSCOL_PRI_SMPTE428,
  140. OBSCOL_PRI_SMPTE431 = 11, ///< SMPTE ST 431-2 (2011) / DCI P3
  141. OBSCOL_PRI_SMPTE432 =
  142. 12, ///< SMPTE ST 432-1 (2010) / P3 D65 / Display P3
  143. OBSCOL_PRI_EBU3213 =
  144. 22, ///< EBU Tech. 3213-E (nothing there) / one of JEDEC P22 group phosphors
  145. OBSCOL_PRI_JEDEC_P22 = OBSCOL_PRI_EBU3213,
  146. OBSCOL_PRI_NB ///< Not part of ABI
  147. };
  148. /**
  149. * Color Transfer Characteristic.
  150. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.2.
  151. */
  152. enum OBSColorTransferCharacteristic {
  153. OBSCOL_TRC_RESERVED0 = 0,
  154. OBSCOL_TRC_BT709 = 1, ///< also ITU-R BT1361
  155. OBSCOL_TRC_UNSPECIFIED = 2,
  156. OBSCOL_TRC_RESERVED = 3,
  157. OBSCOL_TRC_GAMMA22 =
  158. 4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
  159. OBSCOL_TRC_GAMMA28 = 5, ///< also ITU-R BT470BG
  160. OBSCOL_TRC_SMPTE170M =
  161. 6, ///< also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
  162. OBSCOL_TRC_SMPTE240M = 7,
  163. OBSCOL_TRC_LINEAR = 8, ///< "Linear transfer characteristics"
  164. OBSCOL_TRC_LOG =
  165. 9, ///< "Logarithmic transfer characteristic (100:1 range)"
  166. OBSCOL_TRC_LOG_SQRT =
  167. 10, ///< "Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
  168. OBSCOL_TRC_IEC61966_2_4 = 11, ///< IEC 61966-2-4
  169. OBSCOL_TRC_BT1361_ECG = 12, ///< ITU-R BT1361 Extended Colour Gamut
  170. OBSCOL_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC)
  171. OBSCOL_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10-bit system
  172. OBSCOL_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12-bit system
  173. OBSCOL_TRC_SMPTE2084 =
  174. 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
  175. OBSCOL_TRC_SMPTEST2084 = OBSCOL_TRC_SMPTE2084,
  176. OBSCOL_TRC_SMPTE428 = 17, ///< SMPTE ST 428-1
  177. OBSCOL_TRC_SMPTEST428_1 = OBSCOL_TRC_SMPTE428,
  178. OBSCOL_TRC_ARIB_STD_B67 =
  179. 18, ///< ARIB STD-B67, known as "Hybrid log-gamma"
  180. OBSCOL_TRC_NB ///< Not part of ABI
  181. };
  182. /**
  183. * YUV colorspace type.
  184. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.3.
  185. */
  186. enum OBSColorSpace {
  187. OBSCOL_SPC_RGB =
  188. 0, ///< order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
  189. OBSCOL_SPC_BT709 =
  190. 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
  191. OBSCOL_SPC_UNSPECIFIED = 2,
  192. OBSCOL_SPC_RESERVED =
  193. 3, ///< reserved for future use by ITU-T and ISO/IEC just like 15-255 are
  194. OBSCOL_SPC_FCC =
  195. 4, ///< FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  196. OBSCOL_SPC_BT470BG =
  197. 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
  198. OBSCOL_SPC_SMPTE170M =
  199. 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
  200. OBSCOL_SPC_SMPTE240M =
  201. 7, ///< derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
  202. OBSCOL_SPC_YCGCO =
  203. 8, ///< used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
  204. OBSCOL_SPC_YCOCG = OBSCOL_SPC_YCGCO,
  205. OBSCOL_SPC_BT2020_NCL =
  206. 9, ///< ITU-R BT2020 non-constant luminance system
  207. OBSCOL_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system
  208. OBSCOL_SPC_SMPTE2085 = 11, ///< SMPTE 2085, Y'D'zD'x
  209. OBSCOL_SPC_CHROMA_DERIVED_NCL =
  210. 12, ///< Chromaticity-derived non-constant luminance system
  211. OBSCOL_SPC_CHROMA_DERIVED_CL =
  212. 13, ///< Chromaticity-derived constant luminance system
  213. OBSCOL_SPC_ICTCP = 14, ///< ITU-R BT.2100-0, ICtCp
  214. OBSCOL_SPC_NB ///< Not part of ABI
  215. };