obs-ffmpeg-output.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #include <libavutil/opt.h>
  3. #include <libavutil/pixdesc.h>
  4. #include <libavcodec/avcodec.h>
  5. #include <libavformat/avformat.h>
  6. #include <libswscale/swscale.h>
  7. #ifdef NEW_MPEGTS_OUTPUT
  8. #include "obs-ffmpeg-url.h"
  9. #endif
  10. struct ffmpeg_cfg {
  11. const char *url;
  12. const char *format_name;
  13. const char *format_mime_type;
  14. const char *muxer_settings;
  15. const char *protocol_settings; // not used yet for SRT nor RIST
  16. int gop_size;
  17. int video_bitrate;
  18. int audio_bitrate;
  19. const char *video_encoder;
  20. int video_encoder_id;
  21. const char *audio_encoder;
  22. int audio_encoder_id;
  23. int audio_bitrates[MAX_AUDIO_MIXES]; // multi-track
  24. const char *video_settings;
  25. const char *audio_settings;
  26. int audio_mix_count;
  27. int audio_tracks;
  28. const char *audio_stream_names[MAX_AUDIO_MIXES];
  29. enum AVPixelFormat format;
  30. enum AVColorRange color_range;
  31. enum AVColorPrimaries color_primaries;
  32. enum AVColorTransferCharacteristic color_trc;
  33. enum AVColorSpace colorspace;
  34. int max_luminance;
  35. int scale_width;
  36. int scale_height;
  37. int width;
  38. int height;
  39. int frame_size; // audio frame size
  40. const char *username;
  41. const char *password;
  42. const char *stream_id;
  43. const char *encrypt_passphrase;
  44. bool is_srt;
  45. bool is_rist;
  46. int srt_pkt_size;
  47. };
  48. struct ffmpeg_audio_info {
  49. AVStream *stream;
  50. AVCodecContext *ctx;
  51. };
  52. struct ffmpeg_data {
  53. AVStream *video;
  54. AVCodecContext *video_ctx;
  55. struct ffmpeg_audio_info *audio_infos;
  56. const AVCodec *acodec;
  57. const AVCodec *vcodec;
  58. AVFormatContext *output;
  59. struct SwsContext *swscale;
  60. int64_t total_frames;
  61. AVFrame *vframe;
  62. int frame_size;
  63. uint64_t start_timestamp;
  64. int64_t total_samples[MAX_AUDIO_MIXES];
  65. uint32_t audio_samplerate;
  66. enum audio_format audio_format;
  67. size_t audio_planes;
  68. size_t audio_size;
  69. int num_audio_streams;
  70. /* audio_tracks is a bitmask storing the indices of the mixes */
  71. int audio_tracks;
  72. struct deque excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
  73. uint8_t *samples[MAX_AUDIO_MIXES][MAX_AV_PLANES];
  74. AVFrame *aframe[MAX_AUDIO_MIXES];
  75. struct ffmpeg_cfg config;
  76. bool initialized;
  77. char *last_error;
  78. };
  79. struct ffmpeg_output {
  80. obs_output_t *output;
  81. volatile bool active;
  82. struct ffmpeg_data ff_data;
  83. bool connecting;
  84. pthread_t start_thread;
  85. uint64_t total_bytes;
  86. uint64_t audio_start_ts;
  87. uint64_t video_start_ts;
  88. uint64_t stop_ts;
  89. volatile bool stopping;
  90. bool write_thread_active;
  91. pthread_mutex_t write_mutex;
  92. pthread_t write_thread;
  93. os_sem_t *write_sem;
  94. os_event_t *stop_event;
  95. DARRAY(AVPacket *) packets;
  96. #ifdef NEW_MPEGTS_OUTPUT
  97. /* used for SRT & RIST */
  98. URLContext *h;
  99. AVIOContext *s;
  100. bool got_headers;
  101. volatile bool running;
  102. pthread_t start_stop_thread;
  103. pthread_mutex_t start_stop_mutex;
  104. volatile bool start_stop_thread_active;
  105. bool has_connected;
  106. #endif
  107. };
  108. #ifdef NEW_MPEGTS_OUTPUT
  109. enum mpegts_cmd_type { MPEGTS_CMD_START, MPEGTS_CMD_STOP };
  110. struct mpegts_cmd {
  111. enum mpegts_cmd_type type;
  112. bool signal_stop;
  113. struct ffmpeg_output *stream;
  114. uint64_t ts;
  115. };
  116. #endif
  117. bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config);
  118. void ffmpeg_data_free(struct ffmpeg_data *data);