obs-ffmpeg-output.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. };
  45. struct ffmpeg_audio_info {
  46. AVStream *stream;
  47. AVCodecContext *ctx;
  48. };
  49. struct ffmpeg_data {
  50. AVStream *video;
  51. AVCodecContext *video_ctx;
  52. struct ffmpeg_audio_info *audio_infos;
  53. const AVCodec *acodec;
  54. const AVCodec *vcodec;
  55. AVFormatContext *output;
  56. struct SwsContext *swscale;
  57. int64_t total_frames;
  58. AVFrame *vframe;
  59. int frame_size;
  60. uint64_t start_timestamp;
  61. int64_t total_samples[MAX_AUDIO_MIXES];
  62. uint32_t audio_samplerate;
  63. enum audio_format audio_format;
  64. size_t audio_planes;
  65. size_t audio_size;
  66. int num_audio_streams;
  67. /* audio_tracks is a bitmask storing the indices of the mixes */
  68. int audio_tracks;
  69. struct deque excess_frames[MAX_AUDIO_MIXES][MAX_AV_PLANES];
  70. uint8_t *samples[MAX_AUDIO_MIXES][MAX_AV_PLANES];
  71. AVFrame *aframe[MAX_AUDIO_MIXES];
  72. struct ffmpeg_cfg config;
  73. bool initialized;
  74. char *last_error;
  75. };
  76. struct ffmpeg_output {
  77. obs_output_t *output;
  78. volatile bool active;
  79. struct ffmpeg_data ff_data;
  80. bool connecting;
  81. pthread_t start_thread;
  82. uint64_t total_bytes;
  83. uint64_t audio_start_ts;
  84. uint64_t video_start_ts;
  85. uint64_t stop_ts;
  86. volatile bool stopping;
  87. bool write_thread_active;
  88. pthread_mutex_t write_mutex;
  89. pthread_t write_thread;
  90. os_sem_t *write_sem;
  91. os_event_t *stop_event;
  92. DARRAY(AVPacket *) packets;
  93. #ifdef NEW_MPEGTS_OUTPUT
  94. /* used for SRT & RIST */
  95. URLContext *h;
  96. AVIOContext *s;
  97. bool got_headers;
  98. #endif
  99. };
  100. bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config);
  101. void ffmpeg_data_free(struct ffmpeg_data *data);