rtmp-stream.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <obs-module.h>
  2. #include <util/platform.h>
  3. #include <util/circlebuf.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_NEWSOCKETLOOP_ENABLED "new_socket_loop_enabled"
  28. #define OPT_LOWLATENCY_ENABLED "low_latency_mode_enabled"
  29. #define OPT_METADATA_MULTITRACK "metadata_multitrack"
  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. enum video_id_t video_codec;
  93. RTMP rtmp;
  94. bool new_socket_loop;
  95. bool low_latency_mode;
  96. bool disable_send_window_optimization;
  97. bool socket_thread_active;
  98. pthread_t socket_thread;
  99. uint8_t *write_buf;
  100. size_t write_buf_len;
  101. size_t write_buf_size;
  102. pthread_mutex_t write_buf_mutex;
  103. os_event_t *buffer_space_available_event;
  104. os_event_t *buffer_has_data_event;
  105. os_event_t *socket_available_event;
  106. os_event_t *send_thread_signaled_exit;
  107. };
  108. #ifdef _WIN32
  109. void *socket_thread_windows(void *data);
  110. #endif
  111. /* Adapted from FFmpeg's libavutil/pixfmt.h
  112. *
  113. * Renamed to make it apparent that these are not imported as this module does
  114. * not use or link against FFmpeg.
  115. */
  116. /**
  117. * Chromaticity coordinates of the source primaries.
  118. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.1 and ITU-T H.273.
  119. */
  120. enum OBSColorPrimaries {
  121. OBSCOL_PRI_RESERVED0 = 0,
  122. OBSCOL_PRI_BT709 =
  123. 1, ///< also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
  124. OBSCOL_PRI_UNSPECIFIED = 2,
  125. OBSCOL_PRI_RESERVED = 3,
  126. OBSCOL_PRI_BT470M =
  127. 4, ///< also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  128. OBSCOL_PRI_BT470BG =
  129. 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
  130. OBSCOL_PRI_SMPTE170M =
  131. 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
  132. OBSCOL_PRI_SMPTE240M =
  133. 7, ///< identical to above, also called "SMPTE C" even though it uses D65
  134. OBSCOL_PRI_FILM = 8, ///< colour filters using Illuminant C
  135. OBSCOL_PRI_BT2020 = 9, ///< ITU-R BT2020
  136. OBSCOL_PRI_SMPTE428 = 10, ///< SMPTE ST 428-1 (CIE 1931 XYZ)
  137. OBSCOL_PRI_SMPTEST428_1 = OBSCOL_PRI_SMPTE428,
  138. OBSCOL_PRI_SMPTE431 = 11, ///< SMPTE ST 431-2 (2011) / DCI P3
  139. OBSCOL_PRI_SMPTE432 =
  140. 12, ///< SMPTE ST 432-1 (2010) / P3 D65 / Display P3
  141. OBSCOL_PRI_EBU3213 =
  142. 22, ///< EBU Tech. 3213-E (nothing there) / one of JEDEC P22 group phosphors
  143. OBSCOL_PRI_JEDEC_P22 = OBSCOL_PRI_EBU3213,
  144. OBSCOL_PRI_NB ///< Not part of ABI
  145. };
  146. /**
  147. * Color Transfer Characteristic.
  148. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.2.
  149. */
  150. enum OBSColorTransferCharacteristic {
  151. OBSCOL_TRC_RESERVED0 = 0,
  152. OBSCOL_TRC_BT709 = 1, ///< also ITU-R BT1361
  153. OBSCOL_TRC_UNSPECIFIED = 2,
  154. OBSCOL_TRC_RESERVED = 3,
  155. OBSCOL_TRC_GAMMA22 =
  156. 4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
  157. OBSCOL_TRC_GAMMA28 = 5, ///< also ITU-R BT470BG
  158. OBSCOL_TRC_SMPTE170M =
  159. 6, ///< also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
  160. OBSCOL_TRC_SMPTE240M = 7,
  161. OBSCOL_TRC_LINEAR = 8, ///< "Linear transfer characteristics"
  162. OBSCOL_TRC_LOG =
  163. 9, ///< "Logarithmic transfer characteristic (100:1 range)"
  164. OBSCOL_TRC_LOG_SQRT =
  165. 10, ///< "Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
  166. OBSCOL_TRC_IEC61966_2_4 = 11, ///< IEC 61966-2-4
  167. OBSCOL_TRC_BT1361_ECG = 12, ///< ITU-R BT1361 Extended Colour Gamut
  168. OBSCOL_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC)
  169. OBSCOL_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10-bit system
  170. OBSCOL_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12-bit system
  171. OBSCOL_TRC_SMPTE2084 =
  172. 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
  173. OBSCOL_TRC_SMPTEST2084 = OBSCOL_TRC_SMPTE2084,
  174. OBSCOL_TRC_SMPTE428 = 17, ///< SMPTE ST 428-1
  175. OBSCOL_TRC_SMPTEST428_1 = OBSCOL_TRC_SMPTE428,
  176. OBSCOL_TRC_ARIB_STD_B67 =
  177. 18, ///< ARIB STD-B67, known as "Hybrid log-gamma"
  178. OBSCOL_TRC_NB ///< Not part of ABI
  179. };
  180. /**
  181. * YUV colorspace type.
  182. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.3.
  183. */
  184. enum OBSColorSpace {
  185. OBSCOL_SPC_RGB =
  186. 0, ///< order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
  187. OBSCOL_SPC_BT709 =
  188. 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
  189. OBSCOL_SPC_UNSPECIFIED = 2,
  190. OBSCOL_SPC_RESERVED =
  191. 3, ///< reserved for future use by ITU-T and ISO/IEC just like 15-255 are
  192. OBSCOL_SPC_FCC =
  193. 4, ///< FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  194. OBSCOL_SPC_BT470BG =
  195. 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
  196. OBSCOL_SPC_SMPTE170M =
  197. 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
  198. OBSCOL_SPC_SMPTE240M =
  199. 7, ///< derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
  200. OBSCOL_SPC_YCGCO =
  201. 8, ///< used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
  202. OBSCOL_SPC_YCOCG = OBSCOL_SPC_YCGCO,
  203. OBSCOL_SPC_BT2020_NCL =
  204. 9, ///< ITU-R BT2020 non-constant luminance system
  205. OBSCOL_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system
  206. OBSCOL_SPC_SMPTE2085 = 11, ///< SMPTE 2085, Y'D'zD'x
  207. OBSCOL_SPC_CHROMA_DERIVED_NCL =
  208. 12, ///< Chromaticity-derived non-constant luminance system
  209. OBSCOL_SPC_CHROMA_DERIVED_CL =
  210. 13, ///< Chromaticity-derived constant luminance system
  211. OBSCOL_SPC_ICTCP = 14, ///< ITU-R BT.2100-0, ICtCp
  212. OBSCOL_SPC_NB ///< Not part of ABI
  213. };