rtmp-stream.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, obs_output_get_name(stream->output), ##__VA_ARGS__)
  18. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  19. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  20. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  21. #define OPT_DYN_BITRATE "dyn_bitrate"
  22. #define OPT_DROP_THRESHOLD "drop_threshold_ms"
  23. #define OPT_PFRAME_DROP_THRESHOLD "pframe_drop_threshold_ms"
  24. #define OPT_MAX_SHUTDOWN_TIME_SEC "max_shutdown_time_sec"
  25. #define OPT_BIND_IP "bind_ip"
  26. #define OPT_IP_FAMILY "ip_family"
  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 deque packets;
  49. bool sent_headers;
  50. bool got_first_packet;
  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. socklen_t addrlen_hint; /* hint IPv4 vs IPv6 */
  68. /* frame drop variables */
  69. int64_t drop_threshold_usec;
  70. int64_t pframe_drop_threshold_usec;
  71. int min_priority;
  72. float congestion;
  73. int64_t last_dts_usec;
  74. uint64_t total_bytes_sent;
  75. int dropped_frames;
  76. #ifdef TEST_FRAMEDROPS
  77. struct deque droptest_info;
  78. uint64_t droptest_last_key_check;
  79. size_t droptest_max;
  80. size_t droptest_size;
  81. #endif
  82. pthread_mutex_t dbr_mutex;
  83. struct deque dbr_frames;
  84. size_t dbr_data_size;
  85. uint64_t dbr_inc_timeout;
  86. long audio_bitrate;
  87. long dbr_est_bitrate;
  88. long dbr_orig_bitrate;
  89. long dbr_prev_bitrate;
  90. long dbr_cur_bitrate;
  91. long dbr_inc_bitrate;
  92. bool dbr_enabled;
  93. enum audio_id_t audio_codec[MAX_OUTPUT_AUDIO_ENCODERS];
  94. enum video_id_t video_codec[MAX_OUTPUT_VIDEO_ENCODERS];
  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 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
  125. OBSCOL_PRI_UNSPECIFIED = 2,
  126. OBSCOL_PRI_RESERVED = 3,
  127. OBSCOL_PRI_BT470M = 4, ///< also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  128. OBSCOL_PRI_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
  129. OBSCOL_PRI_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
  130. OBSCOL_PRI_SMPTE240M = 7, ///< identical to above, also called "SMPTE C" even though it uses D65
  131. OBSCOL_PRI_FILM = 8, ///< colour filters using Illuminant C
  132. OBSCOL_PRI_BT2020 = 9, ///< ITU-R BT2020
  133. OBSCOL_PRI_SMPTE428 = 10, ///< SMPTE ST 428-1 (CIE 1931 XYZ)
  134. OBSCOL_PRI_SMPTEST428_1 = OBSCOL_PRI_SMPTE428,
  135. OBSCOL_PRI_SMPTE431 = 11, ///< SMPTE ST 431-2 (2011) / DCI P3
  136. OBSCOL_PRI_SMPTE432 = 12, ///< SMPTE ST 432-1 (2010) / P3 D65 / Display P3
  137. OBSCOL_PRI_EBU3213 = 22, ///< EBU Tech. 3213-E (nothing there) / one of JEDEC P22 group phosphors
  138. OBSCOL_PRI_JEDEC_P22 = OBSCOL_PRI_EBU3213,
  139. OBSCOL_PRI_NB ///< Not part of ABI
  140. };
  141. /**
  142. * Color Transfer Characteristic.
  143. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.2.
  144. */
  145. enum OBSColorTransferCharacteristic {
  146. OBSCOL_TRC_RESERVED0 = 0,
  147. OBSCOL_TRC_BT709 = 1, ///< also ITU-R BT1361
  148. OBSCOL_TRC_UNSPECIFIED = 2,
  149. OBSCOL_TRC_RESERVED = 3,
  150. OBSCOL_TRC_GAMMA22 = 4, ///< also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
  151. OBSCOL_TRC_GAMMA28 = 5, ///< also ITU-R BT470BG
  152. OBSCOL_TRC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
  153. OBSCOL_TRC_SMPTE240M = 7,
  154. OBSCOL_TRC_LINEAR = 8, ///< "Linear transfer characteristics"
  155. OBSCOL_TRC_LOG = 9, ///< "Logarithmic transfer characteristic (100:1 range)"
  156. OBSCOL_TRC_LOG_SQRT = 10, ///< "Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
  157. OBSCOL_TRC_IEC61966_2_4 = 11, ///< IEC 61966-2-4
  158. OBSCOL_TRC_BT1361_ECG = 12, ///< ITU-R BT1361 Extended Colour Gamut
  159. OBSCOL_TRC_IEC61966_2_1 = 13, ///< IEC 61966-2-1 (sRGB or sYCC)
  160. OBSCOL_TRC_BT2020_10 = 14, ///< ITU-R BT2020 for 10-bit system
  161. OBSCOL_TRC_BT2020_12 = 15, ///< ITU-R BT2020 for 12-bit system
  162. OBSCOL_TRC_SMPTE2084 = 16, ///< SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
  163. OBSCOL_TRC_SMPTEST2084 = OBSCOL_TRC_SMPTE2084,
  164. OBSCOL_TRC_SMPTE428 = 17, ///< SMPTE ST 428-1
  165. OBSCOL_TRC_SMPTEST428_1 = OBSCOL_TRC_SMPTE428,
  166. OBSCOL_TRC_ARIB_STD_B67 = 18, ///< ARIB STD-B67, known as "Hybrid log-gamma"
  167. OBSCOL_TRC_NB ///< Not part of ABI
  168. };
  169. /**
  170. * YUV colorspace type.
  171. * These values match the ones defined by ISO/IEC 23091-2_2019 subclause 8.3.
  172. */
  173. enum OBSColorSpace {
  174. OBSCOL_SPC_RGB = 0, ///< order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
  175. OBSCOL_SPC_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
  176. OBSCOL_SPC_UNSPECIFIED = 2,
  177. OBSCOL_SPC_RESERVED = 3, ///< reserved for future use by ITU-T and ISO/IEC just like 15-255 are
  178. OBSCOL_SPC_FCC = 4, ///< FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
  179. OBSCOL_SPC_BT470BG =
  180. 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
  181. OBSCOL_SPC_SMPTE170M =
  182. 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
  183. OBSCOL_SPC_SMPTE240M =
  184. 7, ///< derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
  185. OBSCOL_SPC_YCGCO = 8, ///< used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
  186. OBSCOL_SPC_YCOCG = OBSCOL_SPC_YCGCO,
  187. OBSCOL_SPC_BT2020_NCL = 9, ///< ITU-R BT2020 non-constant luminance system
  188. OBSCOL_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system
  189. OBSCOL_SPC_SMPTE2085 = 11, ///< SMPTE 2085, Y'D'zD'x
  190. OBSCOL_SPC_CHROMA_DERIVED_NCL = 12, ///< Chromaticity-derived non-constant luminance system
  191. OBSCOL_SPC_CHROMA_DERIVED_CL = 13, ///< Chromaticity-derived constant luminance system
  192. OBSCOL_SPC_ICTCP = 14, ///< ITU-R BT.2100-0, ICtCp
  193. OBSCOL_SPC_NB ///< Not part of ABI
  194. };