SimpleOutput.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. #include "SimpleOutput.hpp"
  2. #include <utility/audio-encoders.hpp>
  3. #include <utility/StartMultiTrackVideoStreamingGuard.hpp>
  4. #include <widgets/OBSBasic.hpp>
  5. #include <qt-wrappers.hpp>
  6. using namespace std;
  7. static bool CreateSimpleAACEncoder(OBSEncoder &res, int bitrate, const char *name, size_t idx)
  8. {
  9. const char *id_ = GetSimpleAACEncoderForBitrate(bitrate);
  10. if (!id_) {
  11. res = nullptr;
  12. return false;
  13. }
  14. res = obs_audio_encoder_create(id_, name, nullptr, idx, nullptr);
  15. if (res) {
  16. obs_encoder_release(res);
  17. return true;
  18. }
  19. return false;
  20. }
  21. static bool CreateSimpleOpusEncoder(OBSEncoder &res, int bitrate, const char *name, size_t idx)
  22. {
  23. const char *id_ = GetSimpleOpusEncoderForBitrate(bitrate);
  24. if (!id_) {
  25. res = nullptr;
  26. return false;
  27. }
  28. res = obs_audio_encoder_create(id_, name, nullptr, idx, nullptr);
  29. if (res) {
  30. obs_encoder_release(res);
  31. return true;
  32. }
  33. return false;
  34. }
  35. extern bool EncoderAvailable(const char *encoder);
  36. void SimpleOutput::LoadRecordingPreset_Lossless()
  37. {
  38. fileOutput = obs_output_create("ffmpeg_output", "simple_ffmpeg_output", nullptr, nullptr);
  39. if (!fileOutput)
  40. throw "Failed to create recording FFmpeg output "
  41. "(simple output)";
  42. OBSDataAutoRelease settings = obs_data_create();
  43. obs_data_set_string(settings, "format_name", "avi");
  44. obs_data_set_string(settings, "video_encoder", "utvideo");
  45. obs_data_set_string(settings, "audio_encoder", "pcm_s16le");
  46. obs_output_update(fileOutput, settings);
  47. }
  48. void SimpleOutput::LoadRecordingPreset_Lossy(const char *encoderId)
  49. {
  50. videoRecording = obs_video_encoder_create(encoderId, "simple_video_recording", nullptr, nullptr);
  51. if (!videoRecording)
  52. throw "Failed to create video recording encoder (simple output)";
  53. obs_encoder_release(videoRecording);
  54. }
  55. void SimpleOutput::LoadStreamingPreset_Lossy(const char *encoderId)
  56. {
  57. videoStreaming = obs_video_encoder_create(encoderId, "simple_video_stream", nullptr, nullptr);
  58. if (!videoStreaming)
  59. throw "Failed to create video streaming encoder (simple output)";
  60. obs_encoder_release(videoStreaming);
  61. }
  62. /* mistakes have been made to lead us to this. */
  63. const char *get_simple_output_encoder(const char *encoder)
  64. {
  65. if (strcmp(encoder, SIMPLE_ENCODER_X264) == 0) {
  66. return "obs_x264";
  67. } else if (strcmp(encoder, SIMPLE_ENCODER_X264_LOWCPU) == 0) {
  68. return "obs_x264";
  69. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
  70. return "obs_qsv11_v2";
  71. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV_AV1) == 0) {
  72. return "obs_qsv11_av1";
  73. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
  74. return "h264_texture_amf";
  75. #ifdef ENABLE_HEVC
  76. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  77. return "h265_texture_amf";
  78. #endif
  79. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_AV1) == 0) {
  80. return "av1_texture_amf";
  81. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
  82. return EncoderAvailable("obs_nvenc_h264_tex") ? "obs_nvenc_h264_tex" : "ffmpeg_nvenc";
  83. #ifdef ENABLE_HEVC
  84. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  85. return EncoderAvailable("obs_nvenc_hevc_tex") ? "obs_nvenc_hevc_tex" : "ffmpeg_hevc_nvenc";
  86. #endif
  87. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  88. return "obs_nvenc_av1_tex";
  89. } else if (strcmp(encoder, SIMPLE_ENCODER_APPLE_H264) == 0) {
  90. return "com.apple.videotoolbox.videoencoder.ave.avc";
  91. #ifdef ENABLE_HEVC
  92. } else if (strcmp(encoder, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  93. return "com.apple.videotoolbox.videoencoder.ave.hevc";
  94. #endif
  95. }
  96. return "obs_x264";
  97. }
  98. void SimpleOutput::LoadRecordingPreset()
  99. {
  100. const char *quality = config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  101. const char *encoder = config_get_string(main->Config(), "SimpleOutput", "RecEncoder");
  102. const char *audio_encoder = config_get_string(main->Config(), "SimpleOutput", "RecAudioEncoder");
  103. videoEncoder = encoder;
  104. videoQuality = quality;
  105. ffmpegOutput = false;
  106. if (strcmp(quality, "Stream") == 0) {
  107. videoRecording = videoStreaming;
  108. audioRecording = audioStreaming;
  109. usingRecordingPreset = false;
  110. return;
  111. } else if (strcmp(quality, "Lossless") == 0) {
  112. LoadRecordingPreset_Lossless();
  113. usingRecordingPreset = true;
  114. ffmpegOutput = true;
  115. return;
  116. } else {
  117. lowCPUx264 = false;
  118. if (strcmp(encoder, SIMPLE_ENCODER_X264_LOWCPU) == 0)
  119. lowCPUx264 = true;
  120. LoadRecordingPreset_Lossy(get_simple_output_encoder(encoder));
  121. usingRecordingPreset = true;
  122. bool success = false;
  123. if (strcmp(audio_encoder, "opus") == 0)
  124. success = CreateSimpleOpusEncoder(audioRecording, 192, "simple_opus_recording", 0);
  125. else
  126. success = CreateSimpleAACEncoder(audioRecording, 192, "simple_aac_recording", 0);
  127. if (!success)
  128. throw "Failed to create audio recording encoder "
  129. "(simple output)";
  130. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  131. char name[23];
  132. if (strcmp(audio_encoder, "opus") == 0) {
  133. snprintf(name, sizeof name, "simple_opus_recording%d", i);
  134. success = CreateSimpleOpusEncoder(audioTrack[i], GetAudioBitrate(), name, i);
  135. } else {
  136. snprintf(name, sizeof name, "simple_aac_recording%d", i);
  137. success = CreateSimpleAACEncoder(audioTrack[i], GetAudioBitrate(), name, i);
  138. }
  139. if (!success)
  140. throw "Failed to create multi-track audio recording encoder "
  141. "(simple output)";
  142. }
  143. }
  144. }
  145. #define SIMPLE_ARCHIVE_NAME "simple_archive_audio"
  146. SimpleOutput::SimpleOutput(OBSBasic *main_) : BasicOutputHandler(main_)
  147. {
  148. const char *encoder = config_get_string(main->Config(), "SimpleOutput", "StreamEncoder");
  149. const char *audio_encoder = config_get_string(main->Config(), "SimpleOutput", "StreamAudioEncoder");
  150. LoadStreamingPreset_Lossy(get_simple_output_encoder(encoder));
  151. bool success = false;
  152. if (strcmp(audio_encoder, "opus") == 0)
  153. success = CreateSimpleOpusEncoder(audioStreaming, GetAudioBitrate(), "simple_opus", 0);
  154. else
  155. success = CreateSimpleAACEncoder(audioStreaming, GetAudioBitrate(), "simple_aac", 0);
  156. if (!success)
  157. throw "Failed to create audio streaming encoder (simple output)";
  158. if (strcmp(audio_encoder, "opus") == 0)
  159. success = CreateSimpleOpusEncoder(audioArchive, GetAudioBitrate(), SIMPLE_ARCHIVE_NAME, 1);
  160. else
  161. success = CreateSimpleAACEncoder(audioArchive, GetAudioBitrate(), SIMPLE_ARCHIVE_NAME, 1);
  162. if (!success)
  163. throw "Failed to create audio archive encoder (simple output)";
  164. LoadRecordingPreset();
  165. if (!ffmpegOutput) {
  166. bool useReplayBuffer = config_get_bool(main->Config(), "SimpleOutput", "RecRB");
  167. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  168. if (useReplayBuffer) {
  169. OBSDataAutoRelease hotkey;
  170. const char *str = config_get_string(main->Config(), "Hotkeys", "ReplayBuffer");
  171. if (str)
  172. hotkey = obs_data_create_from_json(str);
  173. else
  174. hotkey = nullptr;
  175. replayBuffer = obs_output_create("replay_buffer", Str("ReplayBuffer"), nullptr, hotkey);
  176. if (!replayBuffer)
  177. throw "Failed to create replay buffer output "
  178. "(simple output)";
  179. signal_handler_t *signal = obs_output_get_signal_handler(replayBuffer);
  180. startReplayBuffer.Connect(signal, "start", OBSStartReplayBuffer, this);
  181. stopReplayBuffer.Connect(signal, "stop", OBSStopReplayBuffer, this);
  182. replayBufferStopping.Connect(signal, "stopping", OBSReplayBufferStopping, this);
  183. replayBufferSaved.Connect(signal, "saved", OBSReplayBufferSaved, this);
  184. }
  185. const char *mux = "ffmpeg_muxer";
  186. if (strcmp(recFormat, "hybrid_mp4") == 0)
  187. mux = "mp4_output";
  188. else if (strcmp(recFormat, "hybrid_mov") == 0)
  189. mux = "mov_output";
  190. fileOutput = obs_output_create(mux, "simple_file_output", nullptr, nullptr);
  191. if (!fileOutput)
  192. throw "Failed to create recording output "
  193. "(simple output)";
  194. }
  195. startRecording.Connect(obs_output_get_signal_handler(fileOutput), "start", OBSStartRecording, this);
  196. stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop", OBSStopRecording, this);
  197. recordStopping.Connect(obs_output_get_signal_handler(fileOutput), "stopping", OBSRecordStopping, this);
  198. }
  199. int SimpleOutput::GetAudioBitrate() const
  200. {
  201. const char *audio_encoder = config_get_string(main->Config(), "SimpleOutput", "StreamAudioEncoder");
  202. int bitrate = (int)config_get_uint(main->Config(), "SimpleOutput", "ABitrate");
  203. if (strcmp(audio_encoder, "opus") == 0)
  204. return FindClosestAvailableSimpleOpusBitrate(bitrate);
  205. return FindClosestAvailableSimpleAACBitrate(bitrate);
  206. }
  207. void SimpleOutput::Update()
  208. {
  209. OBSDataAutoRelease videoSettings = obs_data_create();
  210. OBSDataAutoRelease audioSettings = obs_data_create();
  211. int videoBitrate = config_get_uint(main->Config(), "SimpleOutput", "VBitrate");
  212. int audioBitrate = GetAudioBitrate();
  213. bool advanced = config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  214. bool enforceBitrate = !config_get_bool(main->Config(), "Stream1", "IgnoreRecommended");
  215. const char *custom = config_get_string(main->Config(), "SimpleOutput", "x264Settings");
  216. const char *encoder = config_get_string(main->Config(), "SimpleOutput", "StreamEncoder");
  217. const char *encoder_id = obs_encoder_get_id(videoStreaming);
  218. const char *presetType;
  219. const char *preset;
  220. if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
  221. presetType = "QSVPreset";
  222. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV_AV1) == 0) {
  223. presetType = "QSVPreset";
  224. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
  225. presetType = "AMDPreset";
  226. #ifdef ENABLE_HEVC
  227. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  228. presetType = "AMDPreset";
  229. #endif
  230. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
  231. presetType = "NVENCPreset2";
  232. #ifdef ENABLE_HEVC
  233. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  234. presetType = "NVENCPreset2";
  235. #endif
  236. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_AV1) == 0) {
  237. presetType = "AMDAV1Preset";
  238. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  239. presetType = "NVENCPreset2";
  240. } else {
  241. presetType = "Preset";
  242. }
  243. preset = config_get_string(main->Config(), "SimpleOutput", presetType);
  244. /* Only use preset2 for legacy/FFmpeg NVENC Encoder. */
  245. if (strncmp(encoder_id, "ffmpeg_", 7) == 0 && strcmp(presetType, "NVENCPreset2") == 0) {
  246. obs_data_set_string(videoSettings, "preset2", preset);
  247. } else {
  248. obs_data_set_string(videoSettings, "preset", preset);
  249. }
  250. obs_data_set_string(videoSettings, "rate_control", "CBR");
  251. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  252. if (advanced)
  253. obs_data_set_string(videoSettings, "x264opts", custom);
  254. obs_data_set_string(audioSettings, "rate_control", "CBR");
  255. obs_data_set_int(audioSettings, "bitrate", audioBitrate);
  256. obs_service_apply_encoder_settings(main->GetService(), videoSettings, audioSettings);
  257. if (!enforceBitrate) {
  258. int maxVideoBitrate;
  259. int maxAudioBitrate;
  260. obs_service_get_max_bitrate(main->GetService(), &maxVideoBitrate, &maxAudioBitrate);
  261. std::string videoBitrateLogString = maxVideoBitrate > 0 ? std::to_string(maxVideoBitrate) : "None";
  262. std::string audioBitrateLogString = maxAudioBitrate > 0 ? std::to_string(maxAudioBitrate) : "None";
  263. blog(LOG_INFO,
  264. "User is ignoring service bitrate limits.\n"
  265. "Service Recommendations:\n"
  266. "\tvideo bitrate: %s\n"
  267. "\taudio bitrate: %s",
  268. videoBitrateLogString.c_str(), audioBitrateLogString.c_str());
  269. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  270. obs_data_set_int(audioSettings, "bitrate", audioBitrate);
  271. }
  272. video_t *video = obs_get_video();
  273. enum video_format format = video_output_get_format(video);
  274. switch (format) {
  275. case VIDEO_FORMAT_I420:
  276. case VIDEO_FORMAT_NV12:
  277. case VIDEO_FORMAT_I010:
  278. case VIDEO_FORMAT_P010:
  279. break;
  280. default:
  281. obs_encoder_set_preferred_video_format(videoStreaming, VIDEO_FORMAT_NV12);
  282. }
  283. obs_encoder_update(videoStreaming, videoSettings);
  284. obs_encoder_update(audioStreaming, audioSettings);
  285. obs_encoder_update(audioArchive, audioSettings);
  286. }
  287. void SimpleOutput::UpdateRecordingAudioSettings()
  288. {
  289. OBSDataAutoRelease settings = obs_data_create();
  290. obs_data_set_int(settings, "bitrate", 192);
  291. obs_data_set_string(settings, "rate_control", "CBR");
  292. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  293. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  294. const char *quality = config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  295. bool flv = strcmp(recFormat, "flv") == 0;
  296. if (flv || strcmp(quality, "Stream") == 0) {
  297. obs_encoder_update(audioRecording, settings);
  298. } else {
  299. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  300. if ((tracks & (1 << i)) != 0) {
  301. obs_encoder_update(audioTrack[i], settings);
  302. }
  303. }
  304. }
  305. }
  306. #define CROSS_DIST_CUTOFF 2000.0
  307. int SimpleOutput::CalcCRF(int crf)
  308. {
  309. int cx = config_get_uint(main->Config(), "Video", "OutputCX");
  310. int cy = config_get_uint(main->Config(), "Video", "OutputCY");
  311. double fCX = double(cx);
  312. double fCY = double(cy);
  313. if (lowCPUx264)
  314. crf -= 2;
  315. double crossDist = sqrt(fCX * fCX + fCY * fCY);
  316. double crfResReduction = fmin(CROSS_DIST_CUTOFF, crossDist) / CROSS_DIST_CUTOFF;
  317. crfResReduction = (1.0 - crfResReduction) * 10.0;
  318. return crf - int(crfResReduction);
  319. }
  320. void SimpleOutput::UpdateRecordingSettings_x264_crf(int crf)
  321. {
  322. OBSDataAutoRelease settings = obs_data_create();
  323. obs_data_set_int(settings, "crf", crf);
  324. obs_data_set_bool(settings, "use_bufsize", true);
  325. obs_data_set_string(settings, "rate_control", "CRF");
  326. obs_data_set_string(settings, "profile", "high");
  327. obs_data_set_string(settings, "preset", lowCPUx264 ? "ultrafast" : "veryfast");
  328. obs_encoder_update(videoRecording, settings);
  329. }
  330. static bool icq_available(obs_encoder_t *encoder)
  331. {
  332. obs_properties_t *props = obs_encoder_properties(encoder);
  333. obs_property_t *p = obs_properties_get(props, "rate_control");
  334. bool icq_found = false;
  335. size_t num = obs_property_list_item_count(p);
  336. for (size_t i = 0; i < num; i++) {
  337. const char *val = obs_property_list_item_string(p, i);
  338. if (strcmp(val, "ICQ") == 0) {
  339. icq_found = true;
  340. break;
  341. }
  342. }
  343. obs_properties_destroy(props);
  344. return icq_found;
  345. }
  346. void SimpleOutput::UpdateRecordingSettings_qsv11(int crf, bool av1)
  347. {
  348. bool icq = icq_available(videoRecording);
  349. OBSDataAutoRelease settings = obs_data_create();
  350. obs_data_set_string(settings, "profile", "high");
  351. if (icq && !av1) {
  352. obs_data_set_string(settings, "rate_control", "ICQ");
  353. obs_data_set_int(settings, "icq_quality", crf);
  354. } else {
  355. obs_data_set_string(settings, "rate_control", "CQP");
  356. obs_data_set_int(settings, "cqp", crf);
  357. }
  358. obs_encoder_update(videoRecording, settings);
  359. }
  360. void SimpleOutput::UpdateRecordingSettings_nvenc(int cqp)
  361. {
  362. OBSDataAutoRelease settings = obs_data_create();
  363. obs_data_set_string(settings, "rate_control", "CQP");
  364. obs_data_set_string(settings, "profile", "high");
  365. obs_data_set_int(settings, "cqp", cqp);
  366. obs_encoder_update(videoRecording, settings);
  367. }
  368. void SimpleOutput::UpdateRecordingSettings_nvenc_hevc_av1(int cqp)
  369. {
  370. OBSDataAutoRelease settings = obs_data_create();
  371. obs_data_set_string(settings, "rate_control", "CQP");
  372. obs_data_set_string(settings, "profile", "main");
  373. obs_data_set_int(settings, "cqp", cqp);
  374. obs_encoder_update(videoRecording, settings);
  375. }
  376. void SimpleOutput::UpdateRecordingSettings_apple(int quality)
  377. {
  378. OBSDataAutoRelease settings = obs_data_create();
  379. obs_data_set_string(settings, "rate_control", "CRF");
  380. obs_data_set_string(settings, "profile", "high");
  381. obs_data_set_int(settings, "quality", quality);
  382. obs_encoder_update(videoRecording, settings);
  383. }
  384. #ifdef ENABLE_HEVC
  385. void SimpleOutput::UpdateRecordingSettings_apple_hevc(int quality)
  386. {
  387. OBSDataAutoRelease settings = obs_data_create();
  388. obs_data_set_string(settings, "rate_control", "CRF");
  389. obs_data_set_string(settings, "profile", "main");
  390. obs_data_set_int(settings, "quality", quality);
  391. obs_encoder_update(videoRecording, settings);
  392. }
  393. #endif
  394. void SimpleOutput::UpdateRecordingSettings_amd_cqp(int cqp)
  395. {
  396. OBSDataAutoRelease settings = obs_data_create();
  397. obs_data_set_string(settings, "rate_control", "CQP");
  398. obs_data_set_string(settings, "profile", "high");
  399. obs_data_set_string(settings, "preset", "quality");
  400. obs_data_set_int(settings, "cqp", cqp);
  401. obs_encoder_update(videoRecording, settings);
  402. }
  403. void SimpleOutput::UpdateRecordingSettings()
  404. {
  405. bool ultra_hq = (videoQuality == "HQ");
  406. int crf = CalcCRF(ultra_hq ? 16 : 23);
  407. if (astrcmp_n(videoEncoder.c_str(), "x264", 4) == 0) {
  408. UpdateRecordingSettings_x264_crf(crf);
  409. } else if (videoEncoder == SIMPLE_ENCODER_QSV) {
  410. UpdateRecordingSettings_qsv11(crf, false);
  411. } else if (videoEncoder == SIMPLE_ENCODER_QSV_AV1) {
  412. UpdateRecordingSettings_qsv11(crf, true);
  413. } else if (videoEncoder == SIMPLE_ENCODER_AMD) {
  414. UpdateRecordingSettings_amd_cqp(crf);
  415. #ifdef ENABLE_HEVC
  416. } else if (videoEncoder == SIMPLE_ENCODER_AMD_HEVC) {
  417. UpdateRecordingSettings_amd_cqp(crf);
  418. #endif
  419. } else if (videoEncoder == SIMPLE_ENCODER_AMD_AV1) {
  420. UpdateRecordingSettings_amd_cqp(crf);
  421. } else if (videoEncoder == SIMPLE_ENCODER_NVENC) {
  422. UpdateRecordingSettings_nvenc(crf);
  423. #ifdef ENABLE_HEVC
  424. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_HEVC) {
  425. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  426. #endif
  427. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_AV1) {
  428. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  429. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_H264) {
  430. /* These are magic numbers. 0 - 100, more is better. */
  431. UpdateRecordingSettings_apple(ultra_hq ? 70 : 50);
  432. #ifdef ENABLE_HEVC
  433. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_HEVC) {
  434. UpdateRecordingSettings_apple_hevc(ultra_hq ? 70 : 50);
  435. #endif
  436. }
  437. UpdateRecordingAudioSettings();
  438. }
  439. inline void SimpleOutput::SetupOutputs()
  440. {
  441. SimpleOutput::Update();
  442. obs_encoder_set_video(videoStreaming, obs_get_video());
  443. obs_encoder_set_audio(audioStreaming, obs_get_audio());
  444. obs_encoder_set_audio(audioArchive, obs_get_audio());
  445. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  446. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  447. bool flv = strcmp(recFormat, "flv") == 0;
  448. if (usingRecordingPreset) {
  449. if (ffmpegOutput) {
  450. obs_output_set_media(fileOutput, obs_get_video(), obs_get_audio());
  451. } else {
  452. obs_encoder_set_video(videoRecording, obs_get_video());
  453. if (flv) {
  454. obs_encoder_set_audio(audioRecording, obs_get_audio());
  455. } else {
  456. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  457. if ((tracks & (1 << i)) != 0) {
  458. obs_encoder_set_audio(audioTrack[i], obs_get_audio());
  459. }
  460. }
  461. }
  462. }
  463. } else {
  464. obs_encoder_set_audio(audioRecording, obs_get_audio());
  465. }
  466. }
  467. std::shared_future<void> SimpleOutput::SetupStreaming(obs_service_t *service, SetupStreamingContinuation_t continuation)
  468. {
  469. if (!Active())
  470. SetupOutputs();
  471. Auth *auth = main->GetAuth();
  472. if (auth)
  473. auth->OnStreamConfig();
  474. /* --------------------- */
  475. const char *type = GetStreamOutputType(service);
  476. if (!type) {
  477. continuation(false);
  478. return StartMultitrackVideoStreamingGuard::MakeReadyFuture();
  479. }
  480. auto audio_bitrate = GetAudioBitrate();
  481. auto vod_track_mixer = IsVodTrackEnabled(service) ? std::optional{1} : std::nullopt;
  482. auto handle_multitrack_video_result = [=](std::optional<bool> multitrackVideoResult) {
  483. if (multitrackVideoResult.has_value())
  484. return multitrackVideoResult.value();
  485. /* XXX: this is messy and disgusting and should be refactored */
  486. if (outputType != type) {
  487. streamDelayStarting.Disconnect();
  488. streamStopping.Disconnect();
  489. startStreaming.Disconnect();
  490. stopStreaming.Disconnect();
  491. streamOutput = obs_output_create(type, "simple_stream", nullptr, nullptr);
  492. if (!streamOutput) {
  493. blog(LOG_WARNING,
  494. "Creation of stream output type '%s' "
  495. "failed!",
  496. type);
  497. return false;
  498. }
  499. streamDelayStarting.Connect(obs_output_get_signal_handler(streamOutput), "starting",
  500. OBSStreamStarting, this);
  501. streamStopping.Connect(obs_output_get_signal_handler(streamOutput), "stopping",
  502. OBSStreamStopping, this);
  503. startStreaming.Connect(obs_output_get_signal_handler(streamOutput), "start", OBSStartStreaming,
  504. this);
  505. stopStreaming.Connect(obs_output_get_signal_handler(streamOutput), "stop", OBSStopStreaming,
  506. this);
  507. outputType = type;
  508. }
  509. obs_output_set_video_encoder(streamOutput, videoStreaming);
  510. obs_output_set_audio_encoder(streamOutput, audioStreaming, 0);
  511. obs_output_set_service(streamOutput, service);
  512. return true;
  513. };
  514. return SetupMultitrackVideo(service, GetSimpleAACEncoderForBitrate(audio_bitrate), 0, vod_track_mixer,
  515. [=](std::optional<bool> res) {
  516. continuation(handle_multitrack_video_result(res));
  517. });
  518. }
  519. bool SimpleOutput::IsVodTrackEnabled(obs_service_t *service)
  520. {
  521. bool advanced = config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  522. bool enable = config_get_bool(main->Config(), "SimpleOutput", "VodTrackEnabled");
  523. bool enableForCustomServer = config_get_bool(App()->GetUserConfig(), "General", "EnableCustomServerVodTrack");
  524. OBSDataAutoRelease settings = obs_service_get_settings(service);
  525. const char *name = obs_data_get_string(settings, "service");
  526. const char *id = obs_service_get_id(service);
  527. if (strcmp(id, "rtmp_custom") == 0)
  528. return enableForCustomServer ? enable : false;
  529. else
  530. return advanced && enable && ServiceSupportsVodTrack(name);
  531. }
  532. void SimpleOutput::SetupVodTrack(obs_service_t *service)
  533. {
  534. if (IsVodTrackEnabled(service))
  535. obs_output_set_audio_encoder(streamOutput, audioArchive, 1);
  536. else
  537. clear_archive_encoder(streamOutput, SIMPLE_ARCHIVE_NAME);
  538. }
  539. bool SimpleOutput::StartStreaming(obs_service_t *service)
  540. {
  541. bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
  542. int retryDelay = config_get_uint(main->Config(), "Output", "RetryDelay");
  543. int maxRetries = config_get_uint(main->Config(), "Output", "MaxRetries");
  544. bool useDelay = config_get_bool(main->Config(), "Output", "DelayEnable");
  545. int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
  546. bool preserveDelay = config_get_bool(main->Config(), "Output", "DelayPreserve");
  547. const char *bindIP = config_get_string(main->Config(), "Output", "BindIP");
  548. const char *ipFamily = config_get_string(main->Config(), "Output", "IPFamily");
  549. #ifdef _WIN32
  550. bool enableNewSocketLoop = config_get_bool(main->Config(), "Output", "NewSocketLoopEnable");
  551. bool enableLowLatencyMode = config_get_bool(main->Config(), "Output", "LowLatencyEnable");
  552. #endif
  553. bool enableDynBitrate = config_get_bool(main->Config(), "Output", "DynamicBitrate");
  554. if (multitrackVideo && multitrackVideoActive &&
  555. !multitrackVideo->HandleIncompatibleSettings(main, main->Config(), service, enableDynBitrate)) {
  556. multitrackVideoActive = false;
  557. return false;
  558. }
  559. OBSDataAutoRelease settings = obs_data_create();
  560. obs_data_set_string(settings, "bind_ip", bindIP);
  561. obs_data_set_string(settings, "ip_family", ipFamily);
  562. #ifdef _WIN32
  563. obs_data_set_bool(settings, "new_socket_loop_enabled", enableNewSocketLoop);
  564. obs_data_set_bool(settings, "low_latency_mode_enabled", enableLowLatencyMode);
  565. #endif
  566. obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
  567. auto streamOutput = StreamingOutput(); // shadowing is sort of bad, but also convenient
  568. obs_output_update(streamOutput, settings);
  569. if (!reconnect)
  570. maxRetries = 0;
  571. obs_output_set_delay(streamOutput, useDelay ? delaySec : 0, preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
  572. obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
  573. if (!multitrackVideo || !multitrackVideoActive)
  574. SetupVodTrack(service);
  575. if (obs_output_start(streamOutput)) {
  576. if (multitrackVideo && multitrackVideoActive)
  577. multitrackVideo->StartedStreaming();
  578. return true;
  579. }
  580. if (multitrackVideo && multitrackVideoActive)
  581. multitrackVideoActive = false;
  582. const char *error = obs_output_get_last_error(streamOutput);
  583. bool hasLastError = error && *error;
  584. if (hasLastError)
  585. lastError = error;
  586. else
  587. lastError = string();
  588. const char *type = obs_output_get_id(streamOutput);
  589. blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type, hasLastError ? " Last Error: " : "",
  590. hasLastError ? error : "");
  591. return false;
  592. }
  593. void SimpleOutput::UpdateRecording()
  594. {
  595. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  596. bool flv = strcmp(recFormat, "flv") == 0;
  597. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  598. int idx = 0;
  599. int idx2 = 0;
  600. const char *quality = config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  601. if (replayBufferActive || recordingActive)
  602. return;
  603. if (usingRecordingPreset) {
  604. if (!ffmpegOutput)
  605. UpdateRecordingSettings();
  606. } else if (!obs_output_active(streamOutput)) {
  607. Update();
  608. }
  609. if (!Active())
  610. SetupOutputs();
  611. if (!ffmpegOutput) {
  612. obs_output_set_video_encoder(fileOutput, videoRecording);
  613. if (flv || strcmp(quality, "Stream") == 0) {
  614. obs_output_set_audio_encoder(fileOutput, audioRecording, 0);
  615. } else {
  616. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  617. if ((tracks & (1 << i)) != 0) {
  618. obs_output_set_audio_encoder(fileOutput, audioTrack[i], idx++);
  619. }
  620. }
  621. }
  622. }
  623. if (replayBuffer) {
  624. obs_output_set_video_encoder(replayBuffer, videoRecording);
  625. if (flv || strcmp(quality, "Stream") == 0) {
  626. obs_output_set_audio_encoder(replayBuffer, audioRecording, 0);
  627. } else {
  628. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  629. if ((tracks & (1 << i)) != 0) {
  630. obs_output_set_audio_encoder(replayBuffer, audioTrack[i], idx2++);
  631. }
  632. }
  633. }
  634. }
  635. recordingConfigured = true;
  636. }
  637. bool SimpleOutput::ConfigureRecording(bool updateReplayBuffer)
  638. {
  639. const char *path = config_get_string(main->Config(), "SimpleOutput", "FilePath");
  640. const char *format = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  641. const char *mux = config_get_string(main->Config(), "SimpleOutput", "MuxerCustom");
  642. bool noSpace = config_get_bool(main->Config(), "SimpleOutput", "FileNameWithoutSpace");
  643. const char *filenameFormat = config_get_string(main->Config(), "Output", "FilenameFormatting");
  644. bool overwriteIfExists = config_get_bool(main->Config(), "Output", "OverwriteIfExists");
  645. const char *rbPrefix = config_get_string(main->Config(), "SimpleOutput", "RecRBPrefix");
  646. const char *rbSuffix = config_get_string(main->Config(), "SimpleOutput", "RecRBSuffix");
  647. int rbTime = config_get_int(main->Config(), "SimpleOutput", "RecRBTime");
  648. int rbSize = config_get_int(main->Config(), "SimpleOutput", "RecRBSize");
  649. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  650. bool is_fragmented = strncmp(format, "fragmented", 10) == 0;
  651. bool is_lossless = videoQuality == "Lossless";
  652. string f;
  653. OBSDataAutoRelease settings = obs_data_create();
  654. if (updateReplayBuffer) {
  655. f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
  656. string ext = GetFormatExt(format);
  657. obs_data_set_string(settings, "directory", path);
  658. obs_data_set_string(settings, "format", f.c_str());
  659. obs_data_set_string(settings, "extension", ext.c_str());
  660. obs_data_set_bool(settings, "allow_spaces", !noSpace);
  661. obs_data_set_int(settings, "max_time_sec", rbTime);
  662. obs_data_set_int(settings, "max_size_mb", usingRecordingPreset ? rbSize : 0);
  663. } else {
  664. f = GetFormatString(filenameFormat, nullptr, nullptr);
  665. string strPath = GetRecordingFilename(path, ffmpegOutput ? "avi" : format, noSpace, overwriteIfExists,
  666. f.c_str(), ffmpegOutput);
  667. obs_data_set_string(settings, ffmpegOutput ? "url" : "path", strPath.c_str());
  668. if (ffmpegOutput)
  669. obs_output_set_mixers(fileOutput, tracks);
  670. }
  671. // Use fragmented MOV/MP4 if user has not already specified custom movflags
  672. if (is_fragmented && !is_lossless && (!mux || strstr(mux, "movflags") == NULL)) {
  673. string mux_frag = "movflags=frag_keyframe+empty_moov+delay_moov";
  674. if (mux) {
  675. mux_frag += " ";
  676. mux_frag += mux;
  677. }
  678. obs_data_set_string(settings, "muxer_settings", mux_frag.c_str());
  679. } else {
  680. if (is_fragmented && !is_lossless)
  681. blog(LOG_WARNING, "User enabled fragmented recording, "
  682. "but custom muxer settings contained movflags.");
  683. obs_data_set_string(settings, "muxer_settings", mux);
  684. }
  685. if (updateReplayBuffer)
  686. obs_output_update(replayBuffer, settings);
  687. else
  688. obs_output_update(fileOutput, settings);
  689. return true;
  690. }
  691. bool SimpleOutput::StartRecording()
  692. {
  693. UpdateRecording();
  694. if (!ConfigureRecording(false))
  695. return false;
  696. if (!obs_output_start(fileOutput)) {
  697. QString error_reason;
  698. const char *error = obs_output_get_last_error(fileOutput);
  699. if (error)
  700. error_reason = QT_UTF8(error);
  701. else
  702. error_reason = QTStr("Output.StartFailedGeneric");
  703. QMessageBox::critical(main, QTStr("Output.StartRecordingFailed"), error_reason);
  704. return false;
  705. }
  706. return true;
  707. }
  708. bool SimpleOutput::StartReplayBuffer()
  709. {
  710. UpdateRecording();
  711. if (!ConfigureRecording(true))
  712. return false;
  713. if (!obs_output_start(replayBuffer)) {
  714. QMessageBox::critical(main, QTStr("Output.StartReplayFailed"), QTStr("Output.StartFailedGeneric"));
  715. return false;
  716. }
  717. return true;
  718. }
  719. void SimpleOutput::StopStreaming(bool force)
  720. {
  721. auto output = StreamingOutput();
  722. if (force && output)
  723. obs_output_force_stop(output);
  724. else if (multitrackVideo && multitrackVideoActive)
  725. multitrackVideo->StopStreaming();
  726. else
  727. obs_output_stop(output);
  728. }
  729. void SimpleOutput::StopRecording(bool force)
  730. {
  731. if (force)
  732. obs_output_force_stop(fileOutput);
  733. else
  734. obs_output_stop(fileOutput);
  735. }
  736. void SimpleOutput::StopReplayBuffer(bool force)
  737. {
  738. if (force)
  739. obs_output_force_stop(replayBuffer);
  740. else
  741. obs_output_stop(replayBuffer);
  742. }
  743. bool SimpleOutput::StreamingActive() const
  744. {
  745. return obs_output_active(StreamingOutput());
  746. }
  747. bool SimpleOutput::RecordingActive() const
  748. {
  749. return obs_output_active(fileOutput);
  750. }
  751. bool SimpleOutput::ReplayBufferActive() const
  752. {
  753. return obs_output_active(replayBuffer);
  754. }