WHIPSimulcastEncoders.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /******************************************************************************
  2. Copyright (C) 2025 by Sean DuBois <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. struct WHIPSimulcastEncoders {
  16. public:
  17. void Create(const char *encoderId, int rescaleFilter, int whipSimulcastTotalLayers, uint32_t outputWidth,
  18. uint32_t outputHeight)
  19. {
  20. if (rescaleFilter == OBS_SCALE_DISABLE) {
  21. rescaleFilter = OBS_SCALE_BICUBIC;
  22. }
  23. if (whipSimulcastTotalLayers <= 1) {
  24. return;
  25. }
  26. auto widthStep = outputWidth / whipSimulcastTotalLayers;
  27. auto heightStep = outputHeight / whipSimulcastTotalLayers;
  28. std::string encoder_name = "whip_simulcast_0";
  29. for (auto i = whipSimulcastTotalLayers - 1; i > 0; i--) {
  30. uint32_t width = widthStep * i;
  31. width -= width % 2;
  32. uint32_t height = heightStep * i;
  33. height -= height % 2;
  34. encoder_name[encoder_name.size() - 1] = std::to_string(i).at(0);
  35. auto whip_simulcast_encoder =
  36. obs_video_encoder_create(encoderId, encoder_name.c_str(), nullptr, nullptr);
  37. if (whip_simulcast_encoder) {
  38. obs_encoder_set_video(whip_simulcast_encoder, obs_get_video());
  39. obs_encoder_set_scaled_size(whip_simulcast_encoder, width, height);
  40. obs_encoder_set_gpu_scale_type(whip_simulcast_encoder, (obs_scale_type)rescaleFilter);
  41. whipSimulcastEncoders.push_back(whip_simulcast_encoder);
  42. obs_encoder_release(whip_simulcast_encoder);
  43. } else {
  44. blog(LOG_WARNING,
  45. "Failed to create video streaming WHIP Simulcast encoders (BasicOutputHandler)");
  46. }
  47. }
  48. }
  49. void Update(obs_data_t *videoSettings, int videoBitrate)
  50. {
  51. auto bitrateStep = videoBitrate / static_cast<int>(whipSimulcastEncoders.size() + 1);
  52. for (auto &whipSimulcastEncoder : whipSimulcastEncoders) {
  53. videoBitrate -= bitrateStep;
  54. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  55. obs_encoder_update(whipSimulcastEncoder, videoSettings);
  56. }
  57. }
  58. void SetVideoFormat(enum video_format format)
  59. {
  60. for (auto enc : whipSimulcastEncoders)
  61. obs_encoder_set_preferred_video_format(enc, format);
  62. }
  63. void SetStreamOutput(obs_output_t *streamOutput)
  64. {
  65. for (size_t i = 0; i < whipSimulcastEncoders.size(); i++)
  66. obs_output_set_video_encoder2(streamOutput, whipSimulcastEncoders[i], i + 1);
  67. }
  68. private:
  69. std::vector<OBSEncoder> whipSimulcastEncoders;
  70. };