SimpleOutput.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. bool use_native = strcmp(recFormat, "hybrid_mp4") == 0;
  186. fileOutput = obs_output_create(use_native ? "mp4_output" : "ffmpeg_muxer", "simple_file_output",
  187. nullptr, nullptr);
  188. if (!fileOutput)
  189. throw "Failed to create recording output "
  190. "(simple output)";
  191. }
  192. startRecording.Connect(obs_output_get_signal_handler(fileOutput), "start", OBSStartRecording, this);
  193. stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop", OBSStopRecording, this);
  194. recordStopping.Connect(obs_output_get_signal_handler(fileOutput), "stopping", OBSRecordStopping, this);
  195. }
  196. int SimpleOutput::GetAudioBitrate() const
  197. {
  198. const char *audio_encoder = config_get_string(main->Config(), "SimpleOutput", "StreamAudioEncoder");
  199. int bitrate = (int)config_get_uint(main->Config(), "SimpleOutput", "ABitrate");
  200. if (strcmp(audio_encoder, "opus") == 0)
  201. return FindClosestAvailableSimpleOpusBitrate(bitrate);
  202. return FindClosestAvailableSimpleAACBitrate(bitrate);
  203. }
  204. void SimpleOutput::Update()
  205. {
  206. OBSDataAutoRelease videoSettings = obs_data_create();
  207. OBSDataAutoRelease audioSettings = obs_data_create();
  208. int videoBitrate = config_get_uint(main->Config(), "SimpleOutput", "VBitrate");
  209. int audioBitrate = GetAudioBitrate();
  210. bool advanced = config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  211. bool enforceBitrate = !config_get_bool(main->Config(), "Stream1", "IgnoreRecommended");
  212. const char *custom = config_get_string(main->Config(), "SimpleOutput", "x264Settings");
  213. const char *encoder = config_get_string(main->Config(), "SimpleOutput", "StreamEncoder");
  214. const char *encoder_id = obs_encoder_get_id(videoStreaming);
  215. const char *presetType;
  216. const char *preset;
  217. if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
  218. presetType = "QSVPreset";
  219. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV_AV1) == 0) {
  220. presetType = "QSVPreset";
  221. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
  222. presetType = "AMDPreset";
  223. #ifdef ENABLE_HEVC
  224. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  225. presetType = "AMDPreset";
  226. #endif
  227. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
  228. presetType = "NVENCPreset2";
  229. #ifdef ENABLE_HEVC
  230. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  231. presetType = "NVENCPreset2";
  232. #endif
  233. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_AV1) == 0) {
  234. presetType = "AMDAV1Preset";
  235. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  236. presetType = "NVENCPreset2";
  237. } else {
  238. presetType = "Preset";
  239. }
  240. preset = config_get_string(main->Config(), "SimpleOutput", presetType);
  241. /* Only use preset2 for legacy/FFmpeg NVENC Encoder. */
  242. if (strncmp(encoder_id, "ffmpeg_", 7) == 0 && strcmp(presetType, "NVENCPreset2") == 0) {
  243. obs_data_set_string(videoSettings, "preset2", preset);
  244. } else {
  245. obs_data_set_string(videoSettings, "preset", preset);
  246. }
  247. obs_data_set_string(videoSettings, "rate_control", "CBR");
  248. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  249. if (advanced)
  250. obs_data_set_string(videoSettings, "x264opts", custom);
  251. obs_data_set_string(audioSettings, "rate_control", "CBR");
  252. obs_data_set_int(audioSettings, "bitrate", audioBitrate);
  253. obs_service_apply_encoder_settings(main->GetService(), videoSettings, audioSettings);
  254. if (!enforceBitrate) {
  255. int maxVideoBitrate;
  256. int maxAudioBitrate;
  257. obs_service_get_max_bitrate(main->GetService(), &maxVideoBitrate, &maxAudioBitrate);
  258. std::string videoBitrateLogString = maxVideoBitrate > 0 ? std::to_string(maxVideoBitrate) : "None";
  259. std::string audioBitrateLogString = maxAudioBitrate > 0 ? std::to_string(maxAudioBitrate) : "None";
  260. blog(LOG_INFO,
  261. "User is ignoring service bitrate limits.\n"
  262. "Service Recommendations:\n"
  263. "\tvideo bitrate: %s\n"
  264. "\taudio bitrate: %s",
  265. videoBitrateLogString.c_str(), audioBitrateLogString.c_str());
  266. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  267. obs_data_set_int(audioSettings, "bitrate", audioBitrate);
  268. }
  269. video_t *video = obs_get_video();
  270. enum video_format format = video_output_get_format(video);
  271. switch (format) {
  272. case VIDEO_FORMAT_I420:
  273. case VIDEO_FORMAT_NV12:
  274. case VIDEO_FORMAT_I010:
  275. case VIDEO_FORMAT_P010:
  276. break;
  277. default:
  278. obs_encoder_set_preferred_video_format(videoStreaming, VIDEO_FORMAT_NV12);
  279. }
  280. obs_encoder_update(videoStreaming, videoSettings);
  281. obs_encoder_update(audioStreaming, audioSettings);
  282. obs_encoder_update(audioArchive, audioSettings);
  283. }
  284. void SimpleOutput::UpdateRecordingAudioSettings()
  285. {
  286. OBSDataAutoRelease settings = obs_data_create();
  287. obs_data_set_int(settings, "bitrate", 192);
  288. obs_data_set_string(settings, "rate_control", "CBR");
  289. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  290. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  291. const char *quality = config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  292. bool flv = strcmp(recFormat, "flv") == 0;
  293. if (flv || strcmp(quality, "Stream") == 0) {
  294. obs_encoder_update(audioRecording, settings);
  295. } else {
  296. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  297. if ((tracks & (1 << i)) != 0) {
  298. obs_encoder_update(audioTrack[i], settings);
  299. }
  300. }
  301. }
  302. }
  303. #define CROSS_DIST_CUTOFF 2000.0
  304. int SimpleOutput::CalcCRF(int crf)
  305. {
  306. int cx = config_get_uint(main->Config(), "Video", "OutputCX");
  307. int cy = config_get_uint(main->Config(), "Video", "OutputCY");
  308. double fCX = double(cx);
  309. double fCY = double(cy);
  310. if (lowCPUx264)
  311. crf -= 2;
  312. double crossDist = sqrt(fCX * fCX + fCY * fCY);
  313. double crfResReduction = fmin(CROSS_DIST_CUTOFF, crossDist) / CROSS_DIST_CUTOFF;
  314. crfResReduction = (1.0 - crfResReduction) * 10.0;
  315. return crf - int(crfResReduction);
  316. }
  317. void SimpleOutput::UpdateRecordingSettings_x264_crf(int crf)
  318. {
  319. OBSDataAutoRelease settings = obs_data_create();
  320. obs_data_set_int(settings, "crf", crf);
  321. obs_data_set_bool(settings, "use_bufsize", true);
  322. obs_data_set_string(settings, "rate_control", "CRF");
  323. obs_data_set_string(settings, "profile", "high");
  324. obs_data_set_string(settings, "preset", lowCPUx264 ? "ultrafast" : "veryfast");
  325. obs_encoder_update(videoRecording, settings);
  326. }
  327. static bool icq_available(obs_encoder_t *encoder)
  328. {
  329. obs_properties_t *props = obs_encoder_properties(encoder);
  330. obs_property_t *p = obs_properties_get(props, "rate_control");
  331. bool icq_found = false;
  332. size_t num = obs_property_list_item_count(p);
  333. for (size_t i = 0; i < num; i++) {
  334. const char *val = obs_property_list_item_string(p, i);
  335. if (strcmp(val, "ICQ") == 0) {
  336. icq_found = true;
  337. break;
  338. }
  339. }
  340. obs_properties_destroy(props);
  341. return icq_found;
  342. }
  343. void SimpleOutput::UpdateRecordingSettings_qsv11(int crf, bool av1)
  344. {
  345. bool icq = icq_available(videoRecording);
  346. OBSDataAutoRelease settings = obs_data_create();
  347. obs_data_set_string(settings, "profile", "high");
  348. if (icq && !av1) {
  349. obs_data_set_string(settings, "rate_control", "ICQ");
  350. obs_data_set_int(settings, "icq_quality", crf);
  351. } else {
  352. obs_data_set_string(settings, "rate_control", "CQP");
  353. obs_data_set_int(settings, "cqp", crf);
  354. }
  355. obs_encoder_update(videoRecording, settings);
  356. }
  357. void SimpleOutput::UpdateRecordingSettings_nvenc(int cqp)
  358. {
  359. OBSDataAutoRelease settings = obs_data_create();
  360. obs_data_set_string(settings, "rate_control", "CQP");
  361. obs_data_set_string(settings, "profile", "high");
  362. obs_data_set_int(settings, "cqp", cqp);
  363. obs_encoder_update(videoRecording, settings);
  364. }
  365. void SimpleOutput::UpdateRecordingSettings_nvenc_hevc_av1(int cqp)
  366. {
  367. OBSDataAutoRelease settings = obs_data_create();
  368. obs_data_set_string(settings, "rate_control", "CQP");
  369. obs_data_set_string(settings, "profile", "main");
  370. obs_data_set_int(settings, "cqp", cqp);
  371. obs_encoder_update(videoRecording, settings);
  372. }
  373. void SimpleOutput::UpdateRecordingSettings_apple(int quality)
  374. {
  375. OBSDataAutoRelease settings = obs_data_create();
  376. obs_data_set_string(settings, "rate_control", "CRF");
  377. obs_data_set_string(settings, "profile", "high");
  378. obs_data_set_int(settings, "quality", quality);
  379. obs_encoder_update(videoRecording, settings);
  380. }
  381. #ifdef ENABLE_HEVC
  382. void SimpleOutput::UpdateRecordingSettings_apple_hevc(int quality)
  383. {
  384. OBSDataAutoRelease settings = obs_data_create();
  385. obs_data_set_string(settings, "rate_control", "CRF");
  386. obs_data_set_string(settings, "profile", "main");
  387. obs_data_set_int(settings, "quality", quality);
  388. obs_encoder_update(videoRecording, settings);
  389. }
  390. #endif
  391. void SimpleOutput::UpdateRecordingSettings_amd_cqp(int cqp)
  392. {
  393. OBSDataAutoRelease settings = obs_data_create();
  394. obs_data_set_string(settings, "rate_control", "CQP");
  395. obs_data_set_string(settings, "profile", "high");
  396. obs_data_set_string(settings, "preset", "quality");
  397. obs_data_set_int(settings, "cqp", cqp);
  398. obs_encoder_update(videoRecording, settings);
  399. }
  400. void SimpleOutput::UpdateRecordingSettings()
  401. {
  402. bool ultra_hq = (videoQuality == "HQ");
  403. int crf = CalcCRF(ultra_hq ? 16 : 23);
  404. if (astrcmp_n(videoEncoder.c_str(), "x264", 4) == 0) {
  405. UpdateRecordingSettings_x264_crf(crf);
  406. } else if (videoEncoder == SIMPLE_ENCODER_QSV) {
  407. UpdateRecordingSettings_qsv11(crf, false);
  408. } else if (videoEncoder == SIMPLE_ENCODER_QSV_AV1) {
  409. UpdateRecordingSettings_qsv11(crf, true);
  410. } else if (videoEncoder == SIMPLE_ENCODER_AMD) {
  411. UpdateRecordingSettings_amd_cqp(crf);
  412. #ifdef ENABLE_HEVC
  413. } else if (videoEncoder == SIMPLE_ENCODER_AMD_HEVC) {
  414. UpdateRecordingSettings_amd_cqp(crf);
  415. #endif
  416. } else if (videoEncoder == SIMPLE_ENCODER_AMD_AV1) {
  417. UpdateRecordingSettings_amd_cqp(crf);
  418. } else if (videoEncoder == SIMPLE_ENCODER_NVENC) {
  419. UpdateRecordingSettings_nvenc(crf);
  420. #ifdef ENABLE_HEVC
  421. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_HEVC) {
  422. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  423. #endif
  424. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_AV1) {
  425. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  426. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_H264) {
  427. /* These are magic numbers. 0 - 100, more is better. */
  428. UpdateRecordingSettings_apple(ultra_hq ? 70 : 50);
  429. #ifdef ENABLE_HEVC
  430. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_HEVC) {
  431. UpdateRecordingSettings_apple_hevc(ultra_hq ? 70 : 50);
  432. #endif
  433. }
  434. UpdateRecordingAudioSettings();
  435. }
  436. inline void SimpleOutput::SetupOutputs()
  437. {
  438. SimpleOutput::Update();
  439. obs_encoder_set_video(videoStreaming, obs_get_video());
  440. obs_encoder_set_audio(audioStreaming, obs_get_audio());
  441. obs_encoder_set_audio(audioArchive, obs_get_audio());
  442. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  443. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  444. bool flv = strcmp(recFormat, "flv") == 0;
  445. if (usingRecordingPreset) {
  446. if (ffmpegOutput) {
  447. obs_output_set_media(fileOutput, obs_get_video(), obs_get_audio());
  448. } else {
  449. obs_encoder_set_video(videoRecording, obs_get_video());
  450. if (flv) {
  451. obs_encoder_set_audio(audioRecording, obs_get_audio());
  452. } else {
  453. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  454. if ((tracks & (1 << i)) != 0) {
  455. obs_encoder_set_audio(audioTrack[i], obs_get_audio());
  456. }
  457. }
  458. }
  459. }
  460. } else {
  461. obs_encoder_set_audio(audioRecording, obs_get_audio());
  462. }
  463. }
  464. std::shared_future<void> SimpleOutput::SetupStreaming(obs_service_t *service, SetupStreamingContinuation_t continuation)
  465. {
  466. if (!Active())
  467. SetupOutputs();
  468. Auth *auth = main->GetAuth();
  469. if (auth)
  470. auth->OnStreamConfig();
  471. /* --------------------- */
  472. const char *type = GetStreamOutputType(service);
  473. if (!type) {
  474. continuation(false);
  475. return StartMultitrackVideoStreamingGuard::MakeReadyFuture();
  476. }
  477. auto audio_bitrate = GetAudioBitrate();
  478. auto vod_track_mixer = IsVodTrackEnabled(service) ? std::optional{1} : std::nullopt;
  479. auto handle_multitrack_video_result = [=](std::optional<bool> multitrackVideoResult) {
  480. if (multitrackVideoResult.has_value())
  481. return multitrackVideoResult.value();
  482. /* XXX: this is messy and disgusting and should be refactored */
  483. if (outputType != type) {
  484. streamDelayStarting.Disconnect();
  485. streamStopping.Disconnect();
  486. startStreaming.Disconnect();
  487. stopStreaming.Disconnect();
  488. streamOutput = obs_output_create(type, "simple_stream", nullptr, nullptr);
  489. if (!streamOutput) {
  490. blog(LOG_WARNING,
  491. "Creation of stream output type '%s' "
  492. "failed!",
  493. type);
  494. return false;
  495. }
  496. streamDelayStarting.Connect(obs_output_get_signal_handler(streamOutput), "starting",
  497. OBSStreamStarting, this);
  498. streamStopping.Connect(obs_output_get_signal_handler(streamOutput), "stopping",
  499. OBSStreamStopping, this);
  500. startStreaming.Connect(obs_output_get_signal_handler(streamOutput), "start", OBSStartStreaming,
  501. this);
  502. stopStreaming.Connect(obs_output_get_signal_handler(streamOutput), "stop", OBSStopStreaming,
  503. this);
  504. outputType = type;
  505. }
  506. obs_output_set_video_encoder(streamOutput, videoStreaming);
  507. obs_output_set_audio_encoder(streamOutput, audioStreaming, 0);
  508. obs_output_set_service(streamOutput, service);
  509. return true;
  510. };
  511. return SetupMultitrackVideo(service, GetSimpleAACEncoderForBitrate(audio_bitrate), 0, vod_track_mixer,
  512. [=](std::optional<bool> res) {
  513. continuation(handle_multitrack_video_result(res));
  514. });
  515. }
  516. bool SimpleOutput::IsVodTrackEnabled(obs_service_t *service)
  517. {
  518. bool advanced = config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  519. bool enable = config_get_bool(main->Config(), "SimpleOutput", "VodTrackEnabled");
  520. bool enableForCustomServer = config_get_bool(App()->GetUserConfig(), "General", "EnableCustomServerVodTrack");
  521. OBSDataAutoRelease settings = obs_service_get_settings(service);
  522. const char *name = obs_data_get_string(settings, "service");
  523. const char *id = obs_service_get_id(service);
  524. if (strcmp(id, "rtmp_custom") == 0)
  525. return enableForCustomServer ? enable : false;
  526. else
  527. return advanced && enable && ServiceSupportsVodTrack(name);
  528. }
  529. void SimpleOutput::SetupVodTrack(obs_service_t *service)
  530. {
  531. if (IsVodTrackEnabled(service))
  532. obs_output_set_audio_encoder(streamOutput, audioArchive, 1);
  533. else
  534. clear_archive_encoder(streamOutput, SIMPLE_ARCHIVE_NAME);
  535. }
  536. bool SimpleOutput::StartStreaming(obs_service_t *service)
  537. {
  538. bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
  539. int retryDelay = config_get_uint(main->Config(), "Output", "RetryDelay");
  540. int maxRetries = config_get_uint(main->Config(), "Output", "MaxRetries");
  541. bool useDelay = config_get_bool(main->Config(), "Output", "DelayEnable");
  542. int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
  543. bool preserveDelay = config_get_bool(main->Config(), "Output", "DelayPreserve");
  544. const char *bindIP = config_get_string(main->Config(), "Output", "BindIP");
  545. const char *ipFamily = config_get_string(main->Config(), "Output", "IPFamily");
  546. #ifdef _WIN32
  547. bool enableNewSocketLoop = config_get_bool(main->Config(), "Output", "NewSocketLoopEnable");
  548. bool enableLowLatencyMode = config_get_bool(main->Config(), "Output", "LowLatencyEnable");
  549. #endif
  550. bool enableDynBitrate = config_get_bool(main->Config(), "Output", "DynamicBitrate");
  551. if (multitrackVideo && multitrackVideoActive &&
  552. !multitrackVideo->HandleIncompatibleSettings(main, main->Config(), service, enableDynBitrate)) {
  553. multitrackVideoActive = false;
  554. return false;
  555. }
  556. OBSDataAutoRelease settings = obs_data_create();
  557. obs_data_set_string(settings, "bind_ip", bindIP);
  558. obs_data_set_string(settings, "ip_family", ipFamily);
  559. #ifdef _WIN32
  560. obs_data_set_bool(settings, "new_socket_loop_enabled", enableNewSocketLoop);
  561. obs_data_set_bool(settings, "low_latency_mode_enabled", enableLowLatencyMode);
  562. #endif
  563. obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
  564. auto streamOutput = StreamingOutput(); // shadowing is sort of bad, but also convenient
  565. obs_output_update(streamOutput, settings);
  566. if (!reconnect)
  567. maxRetries = 0;
  568. obs_output_set_delay(streamOutput, useDelay ? delaySec : 0, preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
  569. obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
  570. if (!multitrackVideo || !multitrackVideoActive)
  571. SetupVodTrack(service);
  572. if (obs_output_start(streamOutput)) {
  573. if (multitrackVideo && multitrackVideoActive)
  574. multitrackVideo->StartedStreaming();
  575. return true;
  576. }
  577. if (multitrackVideo && multitrackVideoActive)
  578. multitrackVideoActive = false;
  579. const char *error = obs_output_get_last_error(streamOutput);
  580. bool hasLastError = error && *error;
  581. if (hasLastError)
  582. lastError = error;
  583. else
  584. lastError = string();
  585. const char *type = obs_output_get_id(streamOutput);
  586. blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type, hasLastError ? " Last Error: " : "",
  587. hasLastError ? error : "");
  588. return false;
  589. }
  590. void SimpleOutput::UpdateRecording()
  591. {
  592. const char *recFormat = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  593. bool flv = strcmp(recFormat, "flv") == 0;
  594. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  595. int idx = 0;
  596. int idx2 = 0;
  597. const char *quality = config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  598. if (replayBufferActive || recordingActive)
  599. return;
  600. if (usingRecordingPreset) {
  601. if (!ffmpegOutput)
  602. UpdateRecordingSettings();
  603. } else if (!obs_output_active(streamOutput)) {
  604. Update();
  605. }
  606. if (!Active())
  607. SetupOutputs();
  608. if (!ffmpegOutput) {
  609. obs_output_set_video_encoder(fileOutput, videoRecording);
  610. if (flv || strcmp(quality, "Stream") == 0) {
  611. obs_output_set_audio_encoder(fileOutput, audioRecording, 0);
  612. } else {
  613. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  614. if ((tracks & (1 << i)) != 0) {
  615. obs_output_set_audio_encoder(fileOutput, audioTrack[i], idx++);
  616. }
  617. }
  618. }
  619. }
  620. if (replayBuffer) {
  621. obs_output_set_video_encoder(replayBuffer, videoRecording);
  622. if (flv || strcmp(quality, "Stream") == 0) {
  623. obs_output_set_audio_encoder(replayBuffer, audioRecording, 0);
  624. } else {
  625. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  626. if ((tracks & (1 << i)) != 0) {
  627. obs_output_set_audio_encoder(replayBuffer, audioTrack[i], idx2++);
  628. }
  629. }
  630. }
  631. }
  632. recordingConfigured = true;
  633. }
  634. bool SimpleOutput::ConfigureRecording(bool updateReplayBuffer)
  635. {
  636. const char *path = config_get_string(main->Config(), "SimpleOutput", "FilePath");
  637. const char *format = config_get_string(main->Config(), "SimpleOutput", "RecFormat2");
  638. const char *mux = config_get_string(main->Config(), "SimpleOutput", "MuxerCustom");
  639. bool noSpace = config_get_bool(main->Config(), "SimpleOutput", "FileNameWithoutSpace");
  640. const char *filenameFormat = config_get_string(main->Config(), "Output", "FilenameFormatting");
  641. bool overwriteIfExists = config_get_bool(main->Config(), "Output", "OverwriteIfExists");
  642. const char *rbPrefix = config_get_string(main->Config(), "SimpleOutput", "RecRBPrefix");
  643. const char *rbSuffix = config_get_string(main->Config(), "SimpleOutput", "RecRBSuffix");
  644. int rbTime = config_get_int(main->Config(), "SimpleOutput", "RecRBTime");
  645. int rbSize = config_get_int(main->Config(), "SimpleOutput", "RecRBSize");
  646. int tracks = config_get_int(main->Config(), "SimpleOutput", "RecTracks");
  647. bool is_fragmented = strncmp(format, "fragmented", 10) == 0;
  648. bool is_lossless = videoQuality == "Lossless";
  649. string f;
  650. OBSDataAutoRelease settings = obs_data_create();
  651. if (updateReplayBuffer) {
  652. f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
  653. string ext = GetFormatExt(format);
  654. obs_data_set_string(settings, "directory", path);
  655. obs_data_set_string(settings, "format", f.c_str());
  656. obs_data_set_string(settings, "extension", ext.c_str());
  657. obs_data_set_bool(settings, "allow_spaces", !noSpace);
  658. obs_data_set_int(settings, "max_time_sec", rbTime);
  659. obs_data_set_int(settings, "max_size_mb", usingRecordingPreset ? rbSize : 0);
  660. } else {
  661. f = GetFormatString(filenameFormat, nullptr, nullptr);
  662. string strPath = GetRecordingFilename(path, ffmpegOutput ? "avi" : format, noSpace, overwriteIfExists,
  663. f.c_str(), ffmpegOutput);
  664. obs_data_set_string(settings, ffmpegOutput ? "url" : "path", strPath.c_str());
  665. if (ffmpegOutput)
  666. obs_output_set_mixers(fileOutput, tracks);
  667. }
  668. // Use fragmented MOV/MP4 if user has not already specified custom movflags
  669. if (is_fragmented && !is_lossless && (!mux || strstr(mux, "movflags") == NULL)) {
  670. string mux_frag = "movflags=frag_keyframe+empty_moov+delay_moov";
  671. if (mux) {
  672. mux_frag += " ";
  673. mux_frag += mux;
  674. }
  675. obs_data_set_string(settings, "muxer_settings", mux_frag.c_str());
  676. } else {
  677. if (is_fragmented && !is_lossless)
  678. blog(LOG_WARNING, "User enabled fragmented recording, "
  679. "but custom muxer settings contained movflags.");
  680. obs_data_set_string(settings, "muxer_settings", mux);
  681. }
  682. if (updateReplayBuffer)
  683. obs_output_update(replayBuffer, settings);
  684. else
  685. obs_output_update(fileOutput, settings);
  686. return true;
  687. }
  688. bool SimpleOutput::StartRecording()
  689. {
  690. UpdateRecording();
  691. if (!ConfigureRecording(false))
  692. return false;
  693. if (!obs_output_start(fileOutput)) {
  694. QString error_reason;
  695. const char *error = obs_output_get_last_error(fileOutput);
  696. if (error)
  697. error_reason = QT_UTF8(error);
  698. else
  699. error_reason = QTStr("Output.StartFailedGeneric");
  700. QMessageBox::critical(main, QTStr("Output.StartRecordingFailed"), error_reason);
  701. return false;
  702. }
  703. return true;
  704. }
  705. bool SimpleOutput::StartReplayBuffer()
  706. {
  707. UpdateRecording();
  708. if (!ConfigureRecording(true))
  709. return false;
  710. if (!obs_output_start(replayBuffer)) {
  711. QMessageBox::critical(main, QTStr("Output.StartReplayFailed"), QTStr("Output.StartFailedGeneric"));
  712. return false;
  713. }
  714. return true;
  715. }
  716. void SimpleOutput::StopStreaming(bool force)
  717. {
  718. auto output = StreamingOutput();
  719. if (force && output)
  720. obs_output_force_stop(output);
  721. else if (multitrackVideo && multitrackVideoActive)
  722. multitrackVideo->StopStreaming();
  723. else
  724. obs_output_stop(output);
  725. }
  726. void SimpleOutput::StopRecording(bool force)
  727. {
  728. if (force)
  729. obs_output_force_stop(fileOutput);
  730. else
  731. obs_output_stop(fileOutput);
  732. }
  733. void SimpleOutput::StopReplayBuffer(bool force)
  734. {
  735. if (force)
  736. obs_output_force_stop(replayBuffer);
  737. else
  738. obs_output_stop(replayBuffer);
  739. }
  740. bool SimpleOutput::StreamingActive() const
  741. {
  742. return obs_output_active(StreamingOutput());
  743. }
  744. bool SimpleOutput::RecordingActive() const
  745. {
  746. return obs_output_active(fileOutput);
  747. }
  748. bool SimpleOutput::ReplayBufferActive() const
  749. {
  750. return obs_output_active(replayBuffer);
  751. }