ffmpeg-encoded-output.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /******************************************************************************
  2. Copyright (C) 2019 Haivision Systems Inc.
  3. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include <obs-module.h>
  16. #include <obs-avc.h>
  17. #include <util/platform.h>
  18. #include <util/circlebuf.h>
  19. #include <util/dstr.h>
  20. #include <util/threading.h>
  21. #include <inttypes.h>
  22. #include "obs-ffmpeg-output.h"
  23. #include "obs-ffmpeg-formats.h"
  24. #define do_log(level, format, ...) \
  25. blog(level, "[ffmpeg-encoded-output: '%s'] " format, \
  26. obs_output_get_name(stream->output), ##__VA_ARGS__)
  27. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  28. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  29. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  30. #define OPT_DROP_THRESHOLD "drop_threshold_ms"
  31. #define OPT_PFRAME_DROP_THRESHOLD "pframe_drop_threshold_ms"
  32. #define OPT_MAX_SHUTDOWN_TIME_SEC "max_shutdown_time_sec"
  33. #define OPT_LOWLATENCY_ENABLED "low_latency_mode_enabled"
  34. //#define TEST_FRAMEDROPS
  35. #ifdef TEST_FRAMEDROPS
  36. #define DROPTEST_MAX_KBPS 3000
  37. #define DROPTEST_MAX_BYTES (DROPTEST_MAX_KBPS * 1000 / 8)
  38. struct droptest_info {
  39. uint64_t ts;
  40. size_t size;
  41. };
  42. #endif
  43. struct ffmpeg_encoded_output {
  44. obs_output_t *output;
  45. pthread_mutex_t packets_mutex;
  46. struct circlebuf packets;
  47. bool sent_sps_pps;
  48. bool got_first_video;
  49. int64_t start_dts_offset;
  50. volatile bool connecting;
  51. pthread_t connect_thread;
  52. volatile bool active;
  53. volatile bool disconnected;
  54. pthread_t send_thread;
  55. int max_shutdown_time_sec;
  56. os_sem_t *send_sem;
  57. os_event_t *stop_event;
  58. uint64_t stop_ts;
  59. uint64_t shutdown_timeout_ts;
  60. struct dstr path, key;
  61. struct dstr username, password;
  62. struct dstr encoder_name;
  63. /* frame drop variables */
  64. int64_t drop_threshold_usec;
  65. int64_t pframe_drop_threshold_usec;
  66. int min_priority;
  67. float congestion;
  68. int64_t last_dts_usec;
  69. uint64_t total_bytes_sent;
  70. int dropped_frames;
  71. #ifdef TEST_FRAMEDROPS
  72. struct circlebuf droptest_info;
  73. size_t droptest_size;
  74. #endif
  75. uint8_t *write_buf;
  76. size_t write_buf_len;
  77. size_t write_buf_size;
  78. pthread_mutex_t write_buf_mutex;
  79. os_event_t *buffer_space_available_event;
  80. os_event_t *buffer_has_data_event;
  81. os_event_t *send_thread_signaled_exit;
  82. struct ffmpeg_data ff_data;
  83. };