1
0

window-basic-main-outputs.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. #include <string>
  2. #include <algorithm>
  3. #include <QMessageBox>
  4. #include "qt-wrappers.hpp"
  5. #include "audio-encoders.hpp"
  6. #include "window-basic-main.hpp"
  7. #include "window-basic-main-outputs.hpp"
  8. #include "window-basic-vcam-config.hpp"
  9. using namespace std;
  10. extern bool EncoderAvailable(const char *encoder);
  11. volatile bool streaming_active = false;
  12. volatile bool recording_active = false;
  13. volatile bool recording_paused = false;
  14. volatile bool replaybuf_active = false;
  15. volatile bool virtualcam_active = false;
  16. #define FTL_PROTOCOL "ftl"
  17. #define RTMP_PROTOCOL "rtmp"
  18. static void OBSStreamStarting(void *data, calldata_t *params)
  19. {
  20. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  21. obs_output_t *obj = (obs_output_t *)calldata_ptr(params, "output");
  22. int sec = (int)obs_output_get_active_delay(obj);
  23. if (sec == 0)
  24. return;
  25. output->delayActive = true;
  26. QMetaObject::invokeMethod(output->main, "StreamDelayStarting",
  27. Q_ARG(int, sec));
  28. }
  29. static void OBSStreamStopping(void *data, calldata_t *params)
  30. {
  31. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  32. obs_output_t *obj = (obs_output_t *)calldata_ptr(params, "output");
  33. int sec = (int)obs_output_get_active_delay(obj);
  34. if (sec == 0)
  35. QMetaObject::invokeMethod(output->main, "StreamStopping");
  36. else
  37. QMetaObject::invokeMethod(output->main, "StreamDelayStopping",
  38. Q_ARG(int, sec));
  39. }
  40. static void OBSStartStreaming(void *data, calldata_t *params)
  41. {
  42. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  43. output->streamingActive = true;
  44. os_atomic_set_bool(&streaming_active, true);
  45. QMetaObject::invokeMethod(output->main, "StreamingStart");
  46. UNUSED_PARAMETER(params);
  47. }
  48. static void OBSStopStreaming(void *data, calldata_t *params)
  49. {
  50. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  51. int code = (int)calldata_int(params, "code");
  52. const char *last_error = calldata_string(params, "last_error");
  53. QString arg_last_error = QString::fromUtf8(last_error);
  54. output->streamingActive = false;
  55. output->delayActive = false;
  56. os_atomic_set_bool(&streaming_active, false);
  57. QMetaObject::invokeMethod(output->main, "StreamingStop",
  58. Q_ARG(int, code),
  59. Q_ARG(QString, arg_last_error));
  60. }
  61. static void OBSStartRecording(void *data, calldata_t *params)
  62. {
  63. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  64. output->recordingActive = true;
  65. os_atomic_set_bool(&recording_active, true);
  66. QMetaObject::invokeMethod(output->main, "RecordingStart");
  67. UNUSED_PARAMETER(params);
  68. }
  69. static void OBSStopRecording(void *data, calldata_t *params)
  70. {
  71. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  72. int code = (int)calldata_int(params, "code");
  73. const char *last_error = calldata_string(params, "last_error");
  74. QString arg_last_error = QString::fromUtf8(last_error);
  75. output->recordingActive = false;
  76. os_atomic_set_bool(&recording_active, false);
  77. os_atomic_set_bool(&recording_paused, false);
  78. QMetaObject::invokeMethod(output->main, "RecordingStop",
  79. Q_ARG(int, code),
  80. Q_ARG(QString, arg_last_error));
  81. }
  82. static void OBSRecordStopping(void *data, calldata_t *params)
  83. {
  84. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  85. QMetaObject::invokeMethod(output->main, "RecordStopping");
  86. UNUSED_PARAMETER(params);
  87. }
  88. static void OBSRecordFileChanged(void *data, calldata_t *params)
  89. {
  90. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  91. const char *next_file = calldata_string(params, "next_file");
  92. QString arg_last_file =
  93. QString::fromUtf8(output->lastRecordingPath.c_str());
  94. QMetaObject::invokeMethod(output->main, "RecordingFileChanged",
  95. Q_ARG(QString, arg_last_file));
  96. output->lastRecordingPath = next_file;
  97. }
  98. static void OBSStartReplayBuffer(void *data, calldata_t *params)
  99. {
  100. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  101. output->replayBufferActive = true;
  102. os_atomic_set_bool(&replaybuf_active, true);
  103. QMetaObject::invokeMethod(output->main, "ReplayBufferStart");
  104. UNUSED_PARAMETER(params);
  105. }
  106. static void OBSStopReplayBuffer(void *data, calldata_t *params)
  107. {
  108. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  109. int code = (int)calldata_int(params, "code");
  110. output->replayBufferActive = false;
  111. os_atomic_set_bool(&replaybuf_active, false);
  112. QMetaObject::invokeMethod(output->main, "ReplayBufferStop",
  113. Q_ARG(int, code));
  114. }
  115. static void OBSReplayBufferStopping(void *data, calldata_t *params)
  116. {
  117. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  118. QMetaObject::invokeMethod(output->main, "ReplayBufferStopping");
  119. UNUSED_PARAMETER(params);
  120. }
  121. static void OBSReplayBufferSaved(void *data, calldata_t *)
  122. {
  123. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  124. QMetaObject::invokeMethod(output->main, "ReplayBufferSaved",
  125. Qt::QueuedConnection);
  126. }
  127. static void OBSStartVirtualCam(void *data, calldata_t *params)
  128. {
  129. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  130. output->virtualCamActive = true;
  131. os_atomic_set_bool(&virtualcam_active, true);
  132. QMetaObject::invokeMethod(output->main, "OnVirtualCamStart");
  133. UNUSED_PARAMETER(params);
  134. }
  135. static void OBSStopVirtualCam(void *data, calldata_t *params)
  136. {
  137. BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
  138. int code = (int)calldata_int(params, "code");
  139. output->virtualCamActive = false;
  140. os_atomic_set_bool(&virtualcam_active, false);
  141. QMetaObject::invokeMethod(output->main, "OnVirtualCamStop",
  142. Q_ARG(int, code));
  143. obs_output_set_media(output->virtualCam, nullptr, nullptr);
  144. output->DestroyVirtualCamView();
  145. }
  146. /* ------------------------------------------------------------------------ */
  147. static bool CreateAACEncoder(OBSEncoder &res, string &id, int bitrate,
  148. const char *name, size_t idx)
  149. {
  150. const char *id_ = GetAACEncoderForBitrate(bitrate);
  151. if (!id_) {
  152. id.clear();
  153. res = nullptr;
  154. return false;
  155. }
  156. if (id == id_)
  157. return true;
  158. id = id_;
  159. res = obs_audio_encoder_create(id_, name, nullptr, idx, nullptr);
  160. if (res) {
  161. obs_encoder_release(res);
  162. return true;
  163. }
  164. return false;
  165. }
  166. /* ------------------------------------------------------------------------ */
  167. inline BasicOutputHandler::BasicOutputHandler(OBSBasic *main_) : main(main_)
  168. {
  169. if (main->vcamEnabled) {
  170. virtualCam = obs_output_create("virtualcam_output",
  171. "virtualcam_output", nullptr,
  172. nullptr);
  173. signal_handler_t *signal =
  174. obs_output_get_signal_handler(virtualCam);
  175. startVirtualCam.Connect(signal, "start", OBSStartVirtualCam,
  176. this);
  177. stopVirtualCam.Connect(signal, "stop", OBSStopVirtualCam, this);
  178. }
  179. }
  180. bool BasicOutputHandler::StartVirtualCam()
  181. {
  182. if (!main->vcamEnabled)
  183. return false;
  184. if (!virtualCamView)
  185. virtualCamView = obs_view_create();
  186. UpdateVirtualCamOutputSource();
  187. if (!virtualCamVideo) {
  188. virtualCamVideo = obs_view_add(virtualCamView);
  189. if (!virtualCamVideo)
  190. return false;
  191. }
  192. obs_output_set_media(virtualCam, virtualCamVideo, obs_get_audio());
  193. if (!Active())
  194. SetupOutputs();
  195. bool success = obs_output_start(virtualCam);
  196. if (!success)
  197. DestroyVirtualCamView();
  198. return success;
  199. }
  200. void BasicOutputHandler::StopVirtualCam()
  201. {
  202. if (main->vcamEnabled) {
  203. obs_output_stop(virtualCam);
  204. }
  205. }
  206. bool BasicOutputHandler::VirtualCamActive() const
  207. {
  208. if (main->vcamEnabled) {
  209. return obs_output_active(virtualCam);
  210. }
  211. return false;
  212. }
  213. void BasicOutputHandler::UpdateVirtualCamOutputSource()
  214. {
  215. if (!main->vcamEnabled || !virtualCamView)
  216. return;
  217. OBSSourceAutoRelease source;
  218. switch (main->vcamConfig.type) {
  219. case VCamOutputType::InternalOutput:
  220. switch (main->vcamConfig.internal) {
  221. case VCamInternalType::Default:
  222. source = obs_get_output_source(0);
  223. break;
  224. case VCamInternalType::Preview:
  225. OBSSource s = main->GetCurrentSceneSource();
  226. obs_source_get_ref(s);
  227. source = s.Get();
  228. break;
  229. }
  230. break;
  231. case VCamOutputType::SceneOutput:
  232. source = obs_get_source_by_name(main->vcamConfig.scene.c_str());
  233. break;
  234. case VCamOutputType::SourceOutput:
  235. OBSSource s =
  236. obs_get_source_by_name(main->vcamConfig.source.c_str());
  237. if (!vCamSourceScene)
  238. vCamSourceScene =
  239. obs_scene_create_private("vcam_source");
  240. source = obs_source_get_ref(
  241. obs_scene_get_source(vCamSourceScene));
  242. if (vCamSourceSceneItem &&
  243. (obs_sceneitem_get_source(vCamSourceSceneItem) != s)) {
  244. obs_sceneitem_remove(vCamSourceSceneItem);
  245. vCamSourceSceneItem = nullptr;
  246. }
  247. if (!vCamSourceSceneItem) {
  248. vCamSourceSceneItem = obs_scene_add(vCamSourceScene, s);
  249. obs_source_release(s);
  250. obs_sceneitem_set_bounds_type(vCamSourceSceneItem,
  251. OBS_BOUNDS_SCALE_INNER);
  252. obs_sceneitem_set_bounds_alignment(vCamSourceSceneItem,
  253. OBS_ALIGN_CENTER);
  254. const struct vec2 size = {
  255. (float)obs_source_get_width(source),
  256. (float)obs_source_get_height(source),
  257. };
  258. obs_sceneitem_set_bounds(vCamSourceSceneItem, &size);
  259. }
  260. break;
  261. }
  262. OBSSourceAutoRelease current = obs_view_get_source(virtualCamView, 0);
  263. if (source != current)
  264. obs_view_set_source(virtualCamView, 0, source);
  265. }
  266. void BasicOutputHandler::DestroyVirtualCamView()
  267. {
  268. obs_view_remove(virtualCamView);
  269. obs_view_set_source(virtualCamView, 0, nullptr);
  270. virtualCamVideo = nullptr;
  271. obs_view_destroy(virtualCamView);
  272. virtualCamView = nullptr;
  273. if (!vCamSourceScene)
  274. return;
  275. obs_scene_release(vCamSourceScene);
  276. vCamSourceScene = nullptr;
  277. vCamSourceSceneItem = nullptr;
  278. }
  279. /* ------------------------------------------------------------------------ */
  280. struct SimpleOutput : BasicOutputHandler {
  281. OBSEncoder aacStreaming;
  282. OBSEncoder videoStreaming;
  283. OBSEncoder aacRecording;
  284. OBSEncoder aacArchive;
  285. OBSEncoder videoRecording;
  286. string aacRecEncID;
  287. string aacStreamEncID;
  288. string aacArchiveEncID;
  289. string videoEncoder;
  290. string videoQuality;
  291. bool usingRecordingPreset = false;
  292. bool recordingConfigured = false;
  293. bool ffmpegOutput = false;
  294. bool lowCPUx264 = false;
  295. SimpleOutput(OBSBasic *main_);
  296. int CalcCRF(int crf);
  297. void UpdateRecordingSettings_x264_crf(int crf);
  298. void UpdateRecordingSettings_qsv11(int crf, bool av1);
  299. void UpdateRecordingSettings_nvenc(int cqp);
  300. void UpdateRecordingSettings_nvenc_hevc_av1(int cqp);
  301. void UpdateRecordingSettings_amd_cqp(int cqp);
  302. void UpdateRecordingSettings_apple(int quality);
  303. #ifdef ENABLE_HEVC
  304. void UpdateRecordingSettings_apple_hevc(int quality);
  305. #endif
  306. void UpdateRecordingSettings();
  307. void UpdateRecordingAudioSettings();
  308. virtual void Update() override;
  309. void SetupOutputs() override;
  310. int GetAudioBitrate() const;
  311. void LoadRecordingPreset_Lossy(const char *encoder);
  312. void LoadRecordingPreset_Lossless();
  313. void LoadRecordingPreset();
  314. void LoadStreamingPreset_Lossy(const char *encoder);
  315. void UpdateRecording();
  316. bool ConfigureRecording(bool useReplayBuffer);
  317. void SetupVodTrack(obs_service_t *service);
  318. virtual bool SetupStreaming(obs_service_t *service) override;
  319. virtual bool StartStreaming(obs_service_t *service) override;
  320. virtual bool StartRecording() override;
  321. virtual bool StartReplayBuffer() override;
  322. virtual void StopStreaming(bool force) override;
  323. virtual void StopRecording(bool force) override;
  324. virtual void StopReplayBuffer(bool force) override;
  325. virtual bool StreamingActive() const override;
  326. virtual bool RecordingActive() const override;
  327. virtual bool ReplayBufferActive() const override;
  328. };
  329. void SimpleOutput::LoadRecordingPreset_Lossless()
  330. {
  331. fileOutput = obs_output_create("ffmpeg_output", "simple_ffmpeg_output",
  332. nullptr, nullptr);
  333. if (!fileOutput)
  334. throw "Failed to create recording FFmpeg output "
  335. "(simple output)";
  336. OBSDataAutoRelease settings = obs_data_create();
  337. obs_data_set_string(settings, "format_name", "avi");
  338. obs_data_set_string(settings, "video_encoder", "utvideo");
  339. obs_data_set_string(settings, "audio_encoder", "pcm_s16le");
  340. int aMixes = 1;
  341. obs_output_set_mixers(fileOutput, aMixes);
  342. obs_output_update(fileOutput, settings);
  343. }
  344. void SimpleOutput::LoadRecordingPreset_Lossy(const char *encoderId)
  345. {
  346. videoRecording = obs_video_encoder_create(
  347. encoderId, "simple_video_recording", nullptr, nullptr);
  348. if (!videoRecording)
  349. throw "Failed to create video recording encoder (simple output)";
  350. obs_encoder_release(videoRecording);
  351. }
  352. void SimpleOutput::LoadStreamingPreset_Lossy(const char *encoderId)
  353. {
  354. videoStreaming = obs_video_encoder_create(
  355. encoderId, "simple_video_stream", nullptr, nullptr);
  356. if (!videoStreaming)
  357. throw "Failed to create video streaming encoder (simple output)";
  358. obs_encoder_release(videoStreaming);
  359. }
  360. /* mistakes have been made to lead us to this. */
  361. const char *get_simple_output_encoder(const char *encoder)
  362. {
  363. if (strcmp(encoder, SIMPLE_ENCODER_X264) == 0) {
  364. return "obs_x264";
  365. } else if (strcmp(encoder, SIMPLE_ENCODER_X264_LOWCPU) == 0) {
  366. return "obs_x264";
  367. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
  368. return "obs_qsv11_v2";
  369. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV_AV1) == 0) {
  370. return "obs_qsv11_av1";
  371. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
  372. return "h264_texture_amf";
  373. #ifdef ENABLE_HEVC
  374. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  375. return "h265_texture_amf";
  376. #endif
  377. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_AV1) == 0) {
  378. return "av1_texture_amf";
  379. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
  380. return EncoderAvailable("jim_nvenc") ? "jim_nvenc"
  381. : "ffmpeg_nvenc";
  382. #ifdef ENABLE_HEVC
  383. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  384. return EncoderAvailable("jim_hevc_nvenc") ? "jim_hevc_nvenc"
  385. : "ffmpeg_hevc_nvenc";
  386. #endif
  387. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  388. return "jim_av1_nvenc";
  389. } else if (strcmp(encoder, SIMPLE_ENCODER_APPLE_H264) == 0) {
  390. return "com.apple.videotoolbox.videoencoder.ave.avc";
  391. #ifdef ENABLE_HEVC
  392. } else if (strcmp(encoder, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  393. return "com.apple.videotoolbox.videoencoder.ave.hevc";
  394. #endif
  395. }
  396. return "obs_x264";
  397. }
  398. void SimpleOutput::LoadRecordingPreset()
  399. {
  400. const char *quality =
  401. config_get_string(main->Config(), "SimpleOutput", "RecQuality");
  402. const char *encoder =
  403. config_get_string(main->Config(), "SimpleOutput", "RecEncoder");
  404. videoEncoder = encoder;
  405. videoQuality = quality;
  406. ffmpegOutput = false;
  407. if (strcmp(quality, "Stream") == 0) {
  408. videoRecording = videoStreaming;
  409. aacRecording = aacStreaming;
  410. usingRecordingPreset = false;
  411. return;
  412. } else if (strcmp(quality, "Lossless") == 0) {
  413. LoadRecordingPreset_Lossless();
  414. usingRecordingPreset = true;
  415. ffmpegOutput = true;
  416. return;
  417. } else {
  418. lowCPUx264 = false;
  419. if (strcmp(encoder, SIMPLE_ENCODER_X264_LOWCPU) == 0)
  420. lowCPUx264 = true;
  421. LoadRecordingPreset_Lossy(get_simple_output_encoder(encoder));
  422. usingRecordingPreset = true;
  423. if (!CreateAACEncoder(aacRecording, aacRecEncID, 192,
  424. "simple_aac_recording", 0))
  425. throw "Failed to create aac recording encoder "
  426. "(simple output)";
  427. }
  428. }
  429. #define SIMPLE_ARCHIVE_NAME "simple_archive_aac"
  430. SimpleOutput::SimpleOutput(OBSBasic *main_) : BasicOutputHandler(main_)
  431. {
  432. const char *encoder = config_get_string(main->Config(), "SimpleOutput",
  433. "StreamEncoder");
  434. LoadStreamingPreset_Lossy(get_simple_output_encoder(encoder));
  435. if (!CreateAACEncoder(aacStreaming, aacStreamEncID, GetAudioBitrate(),
  436. "simple_aac", 0))
  437. throw "Failed to create aac streaming encoder (simple output)";
  438. if (!CreateAACEncoder(aacArchive, aacArchiveEncID, GetAudioBitrate(),
  439. SIMPLE_ARCHIVE_NAME, 1))
  440. throw "Failed to create aac arhive encoder (simple output)";
  441. LoadRecordingPreset();
  442. if (!ffmpegOutput) {
  443. bool useReplayBuffer = config_get_bool(main->Config(),
  444. "SimpleOutput", "RecRB");
  445. if (useReplayBuffer) {
  446. OBSDataAutoRelease hotkey;
  447. const char *str = config_get_string(
  448. main->Config(), "Hotkeys", "ReplayBuffer");
  449. if (str)
  450. hotkey = obs_data_create_from_json(str);
  451. else
  452. hotkey = nullptr;
  453. replayBuffer = obs_output_create("replay_buffer",
  454. Str("ReplayBuffer"),
  455. nullptr, hotkey);
  456. if (!replayBuffer)
  457. throw "Failed to create replay buffer output "
  458. "(simple output)";
  459. signal_handler_t *signal =
  460. obs_output_get_signal_handler(replayBuffer);
  461. startReplayBuffer.Connect(signal, "start",
  462. OBSStartReplayBuffer, this);
  463. stopReplayBuffer.Connect(signal, "stop",
  464. OBSStopReplayBuffer, this);
  465. replayBufferStopping.Connect(signal, "stopping",
  466. OBSReplayBufferStopping,
  467. this);
  468. replayBufferSaved.Connect(signal, "saved",
  469. OBSReplayBufferSaved, this);
  470. }
  471. fileOutput = obs_output_create(
  472. "ffmpeg_muxer", "simple_file_output", nullptr, nullptr);
  473. if (!fileOutput)
  474. throw "Failed to create recording output "
  475. "(simple output)";
  476. }
  477. startRecording.Connect(obs_output_get_signal_handler(fileOutput),
  478. "start", OBSStartRecording, this);
  479. stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop",
  480. OBSStopRecording, this);
  481. recordStopping.Connect(obs_output_get_signal_handler(fileOutput),
  482. "stopping", OBSRecordStopping, this);
  483. }
  484. int SimpleOutput::GetAudioBitrate() const
  485. {
  486. int bitrate = (int)config_get_uint(main->Config(), "SimpleOutput",
  487. "ABitrate");
  488. return FindClosestAvailableAACBitrate(bitrate);
  489. }
  490. void SimpleOutput::Update()
  491. {
  492. OBSDataAutoRelease videoSettings = obs_data_create();
  493. OBSDataAutoRelease aacSettings = obs_data_create();
  494. int videoBitrate =
  495. config_get_uint(main->Config(), "SimpleOutput", "VBitrate");
  496. int audioBitrate = GetAudioBitrate();
  497. bool advanced =
  498. config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  499. bool enforceBitrate = !config_get_bool(main->Config(), "Stream1",
  500. "IgnoreRecommended");
  501. const char *custom = config_get_string(main->Config(), "SimpleOutput",
  502. "x264Settings");
  503. const char *encoder = config_get_string(main->Config(), "SimpleOutput",
  504. "StreamEncoder");
  505. const char *presetType;
  506. const char *preset;
  507. if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
  508. presetType = "QSVPreset";
  509. } else if (strcmp(encoder, SIMPLE_ENCODER_QSV_AV1) == 0) {
  510. presetType = "QSVPreset";
  511. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
  512. presetType = "AMDPreset";
  513. #ifdef ENABLE_HEVC
  514. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  515. presetType = "AMDPreset";
  516. #endif
  517. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
  518. presetType = "NVENCPreset2";
  519. #ifdef ENABLE_HEVC
  520. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  521. presetType = "NVENCPreset2";
  522. #endif
  523. } else if (strcmp(encoder, SIMPLE_ENCODER_AMD_AV1) == 0) {
  524. presetType = "AMDAV1Preset";
  525. } else if (strcmp(encoder, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  526. presetType = "NVENCPreset2";
  527. } else {
  528. presetType = "Preset";
  529. }
  530. preset = config_get_string(main->Config(), "SimpleOutput", presetType);
  531. obs_data_set_string(videoSettings,
  532. (strcmp(presetType, "NVENCPreset2") == 0)
  533. ? "preset2"
  534. : "preset",
  535. preset);
  536. obs_data_set_string(videoSettings, "rate_control", "CBR");
  537. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  538. if (advanced)
  539. obs_data_set_string(videoSettings, "x264opts", custom);
  540. obs_data_set_string(aacSettings, "rate_control", "CBR");
  541. obs_data_set_int(aacSettings, "bitrate", audioBitrate);
  542. obs_service_apply_encoder_settings(main->GetService(), videoSettings,
  543. aacSettings);
  544. if (!enforceBitrate) {
  545. obs_data_set_int(videoSettings, "bitrate", videoBitrate);
  546. obs_data_set_int(aacSettings, "bitrate", audioBitrate);
  547. }
  548. video_t *video = obs_get_video();
  549. enum video_format format = video_output_get_format(video);
  550. switch (format) {
  551. case VIDEO_FORMAT_I420:
  552. case VIDEO_FORMAT_NV12:
  553. case VIDEO_FORMAT_I010:
  554. case VIDEO_FORMAT_P010:
  555. break;
  556. default:
  557. obs_encoder_set_preferred_video_format(videoStreaming,
  558. VIDEO_FORMAT_NV12);
  559. }
  560. obs_encoder_update(videoStreaming, videoSettings);
  561. obs_encoder_update(aacStreaming, aacSettings);
  562. obs_encoder_update(aacArchive, aacSettings);
  563. }
  564. void SimpleOutput::UpdateRecordingAudioSettings()
  565. {
  566. OBSDataAutoRelease settings = obs_data_create();
  567. obs_data_set_int(settings, "bitrate", 192);
  568. obs_data_set_string(settings, "rate_control", "CBR");
  569. obs_encoder_update(aacRecording, settings);
  570. }
  571. #define CROSS_DIST_CUTOFF 2000.0
  572. int SimpleOutput::CalcCRF(int crf)
  573. {
  574. int cx = config_get_uint(main->Config(), "Video", "OutputCX");
  575. int cy = config_get_uint(main->Config(), "Video", "OutputCY");
  576. double fCX = double(cx);
  577. double fCY = double(cy);
  578. if (lowCPUx264)
  579. crf -= 2;
  580. double crossDist = sqrt(fCX * fCX + fCY * fCY);
  581. double crfResReduction =
  582. fmin(CROSS_DIST_CUTOFF, crossDist) / CROSS_DIST_CUTOFF;
  583. crfResReduction = (1.0 - crfResReduction) * 10.0;
  584. return crf - int(crfResReduction);
  585. }
  586. void SimpleOutput::UpdateRecordingSettings_x264_crf(int crf)
  587. {
  588. OBSDataAutoRelease settings = obs_data_create();
  589. obs_data_set_int(settings, "crf", crf);
  590. obs_data_set_bool(settings, "use_bufsize", true);
  591. obs_data_set_string(settings, "rate_control", "CRF");
  592. obs_data_set_string(settings, "profile", "high");
  593. obs_data_set_string(settings, "preset",
  594. lowCPUx264 ? "ultrafast" : "veryfast");
  595. obs_encoder_update(videoRecording, settings);
  596. }
  597. static bool icq_available(obs_encoder_t *encoder)
  598. {
  599. obs_properties_t *props = obs_encoder_properties(encoder);
  600. obs_property_t *p = obs_properties_get(props, "rate_control");
  601. bool icq_found = false;
  602. size_t num = obs_property_list_item_count(p);
  603. for (size_t i = 0; i < num; i++) {
  604. const char *val = obs_property_list_item_string(p, i);
  605. if (strcmp(val, "ICQ") == 0) {
  606. icq_found = true;
  607. break;
  608. }
  609. }
  610. obs_properties_destroy(props);
  611. return icq_found;
  612. }
  613. void SimpleOutput::UpdateRecordingSettings_qsv11(int crf, bool av1)
  614. {
  615. bool icq = icq_available(videoRecording);
  616. OBSDataAutoRelease settings = obs_data_create();
  617. obs_data_set_string(settings, "profile", "high");
  618. if (icq && !av1) {
  619. obs_data_set_string(settings, "rate_control", "ICQ");
  620. obs_data_set_int(settings, "icq_quality", crf);
  621. } else {
  622. obs_data_set_string(settings, "rate_control", "CQP");
  623. obs_data_set_int(settings, "cqp", crf);
  624. }
  625. obs_encoder_update(videoRecording, settings);
  626. }
  627. void SimpleOutput::UpdateRecordingSettings_nvenc(int cqp)
  628. {
  629. OBSDataAutoRelease settings = obs_data_create();
  630. obs_data_set_string(settings, "rate_control", "CQP");
  631. obs_data_set_string(settings, "profile", "high");
  632. obs_data_set_int(settings, "cqp", cqp);
  633. obs_encoder_update(videoRecording, settings);
  634. }
  635. void SimpleOutput::UpdateRecordingSettings_nvenc_hevc_av1(int cqp)
  636. {
  637. OBSDataAutoRelease settings = obs_data_create();
  638. obs_data_set_string(settings, "rate_control", "CQP");
  639. obs_data_set_string(settings, "profile", "main");
  640. obs_data_set_int(settings, "cqp", cqp);
  641. obs_encoder_update(videoRecording, settings);
  642. }
  643. void SimpleOutput::UpdateRecordingSettings_apple(int quality)
  644. {
  645. OBSDataAutoRelease settings = obs_data_create();
  646. obs_data_set_string(settings, "rate_control", "CRF");
  647. obs_data_set_string(settings, "profile", "high");
  648. obs_data_set_int(settings, "quality", quality);
  649. obs_encoder_update(videoRecording, settings);
  650. }
  651. #ifdef ENABLE_HEVC
  652. void SimpleOutput::UpdateRecordingSettings_apple_hevc(int quality)
  653. {
  654. OBSDataAutoRelease settings = obs_data_create();
  655. obs_data_set_string(settings, "rate_control", "CRF");
  656. obs_data_set_string(settings, "profile", "main");
  657. obs_data_set_int(settings, "quality", quality);
  658. obs_encoder_update(videoRecording, settings);
  659. }
  660. #endif
  661. void SimpleOutput::UpdateRecordingSettings_amd_cqp(int cqp)
  662. {
  663. OBSDataAutoRelease settings = obs_data_create();
  664. obs_data_set_string(settings, "rate_control", "CQP");
  665. obs_data_set_string(settings, "profile", "high");
  666. obs_data_set_string(settings, "preset", "quality");
  667. obs_data_set_int(settings, "cqp", cqp);
  668. obs_encoder_update(videoRecording, settings);
  669. }
  670. void SimpleOutput::UpdateRecordingSettings()
  671. {
  672. bool ultra_hq = (videoQuality == "HQ");
  673. int crf = CalcCRF(ultra_hq ? 16 : 23);
  674. if (astrcmp_n(videoEncoder.c_str(), "x264", 4) == 0) {
  675. UpdateRecordingSettings_x264_crf(crf);
  676. } else if (videoEncoder == SIMPLE_ENCODER_QSV) {
  677. UpdateRecordingSettings_qsv11(crf, false);
  678. } else if (videoEncoder == SIMPLE_ENCODER_QSV_AV1) {
  679. UpdateRecordingSettings_qsv11(crf, true);
  680. } else if (videoEncoder == SIMPLE_ENCODER_AMD) {
  681. UpdateRecordingSettings_amd_cqp(crf);
  682. #ifdef ENABLE_HEVC
  683. } else if (videoEncoder == SIMPLE_ENCODER_AMD_HEVC) {
  684. UpdateRecordingSettings_amd_cqp(crf);
  685. #endif
  686. } else if (videoEncoder == SIMPLE_ENCODER_NVENC) {
  687. UpdateRecordingSettings_nvenc(crf);
  688. #ifdef ENABLE_HEVC
  689. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_HEVC) {
  690. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  691. #endif
  692. } else if (videoEncoder == SIMPLE_ENCODER_NVENC_AV1) {
  693. UpdateRecordingSettings_nvenc_hevc_av1(crf);
  694. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_H264) {
  695. /* These are magic numbers. 0 - 100, more is better. */
  696. UpdateRecordingSettings_apple(ultra_hq ? 70 : 50);
  697. #ifdef ENABLE_HEVC
  698. } else if (videoEncoder == SIMPLE_ENCODER_APPLE_HEVC) {
  699. UpdateRecordingSettings_apple_hevc(ultra_hq ? 70 : 50);
  700. #endif
  701. }
  702. UpdateRecordingAudioSettings();
  703. }
  704. inline void SimpleOutput::SetupOutputs()
  705. {
  706. SimpleOutput::Update();
  707. obs_encoder_set_video(videoStreaming, obs_get_video());
  708. obs_encoder_set_audio(aacStreaming, obs_get_audio());
  709. obs_encoder_set_audio(aacArchive, obs_get_audio());
  710. if (usingRecordingPreset) {
  711. if (ffmpegOutput) {
  712. obs_output_set_media(fileOutput, obs_get_video(),
  713. obs_get_audio());
  714. } else {
  715. obs_encoder_set_video(videoRecording, obs_get_video());
  716. obs_encoder_set_audio(aacRecording, obs_get_audio());
  717. }
  718. }
  719. }
  720. const char *FindAudioEncoderFromCodec(const char *type)
  721. {
  722. const char *alt_enc_id = nullptr;
  723. size_t i = 0;
  724. while (obs_enum_encoder_types(i++, &alt_enc_id)) {
  725. const char *codec = obs_get_encoder_codec(alt_enc_id);
  726. if (strcmp(type, codec) == 0) {
  727. return alt_enc_id;
  728. }
  729. }
  730. return nullptr;
  731. }
  732. bool SimpleOutput::SetupStreaming(obs_service_t *service)
  733. {
  734. if (!Active())
  735. SetupOutputs();
  736. Auth *auth = main->GetAuth();
  737. if (auth)
  738. auth->OnStreamConfig();
  739. /* --------------------- */
  740. const char *type = obs_service_get_output_type(service);
  741. if (!type) {
  742. type = "rtmp_output";
  743. const char *url = obs_service_get_url(service);
  744. if (url != NULL &&
  745. strncmp(url, FTL_PROTOCOL, strlen(FTL_PROTOCOL)) == 0) {
  746. type = "ftl_output";
  747. } else if (url != NULL && strncmp(url, RTMP_PROTOCOL,
  748. strlen(RTMP_PROTOCOL)) != 0) {
  749. type = "ffmpeg_mpegts_muxer";
  750. }
  751. }
  752. /* XXX: this is messy and disgusting and should be refactored */
  753. if (outputType != type) {
  754. streamDelayStarting.Disconnect();
  755. streamStopping.Disconnect();
  756. startStreaming.Disconnect();
  757. stopStreaming.Disconnect();
  758. streamOutput = obs_output_create(type, "simple_stream", nullptr,
  759. nullptr);
  760. if (!streamOutput) {
  761. blog(LOG_WARNING,
  762. "Creation of stream output type '%s' "
  763. "failed!",
  764. type);
  765. return false;
  766. }
  767. streamDelayStarting.Connect(
  768. obs_output_get_signal_handler(streamOutput), "starting",
  769. OBSStreamStarting, this);
  770. streamStopping.Connect(
  771. obs_output_get_signal_handler(streamOutput), "stopping",
  772. OBSStreamStopping, this);
  773. startStreaming.Connect(
  774. obs_output_get_signal_handler(streamOutput), "start",
  775. OBSStartStreaming, this);
  776. stopStreaming.Connect(
  777. obs_output_get_signal_handler(streamOutput), "stop",
  778. OBSStopStreaming, this);
  779. bool isEncoded = obs_output_get_flags(streamOutput) &
  780. OBS_OUTPUT_ENCODED;
  781. if (isEncoded) {
  782. const char *codec =
  783. obs_output_get_supported_audio_codecs(
  784. streamOutput);
  785. if (!codec) {
  786. blog(LOG_WARNING, "Failed to load audio codec");
  787. return false;
  788. }
  789. if (strcmp(codec, "aac") != 0) {
  790. const char *id =
  791. FindAudioEncoderFromCodec(codec);
  792. int audioBitrate = GetAudioBitrate();
  793. OBSDataAutoRelease settings = obs_data_create();
  794. obs_data_set_int(settings, "bitrate",
  795. audioBitrate);
  796. aacStreaming = obs_audio_encoder_create(
  797. id, "alt_audio_enc", nullptr, 0,
  798. nullptr);
  799. obs_encoder_release(aacStreaming);
  800. if (!aacStreaming)
  801. return false;
  802. obs_encoder_update(aacStreaming, settings);
  803. obs_encoder_set_audio(aacStreaming,
  804. obs_get_audio());
  805. }
  806. }
  807. outputType = type;
  808. }
  809. obs_output_set_video_encoder(streamOutput, videoStreaming);
  810. obs_output_set_audio_encoder(streamOutput, aacStreaming, 0);
  811. obs_output_set_service(streamOutput, service);
  812. return true;
  813. }
  814. static inline bool ServiceSupportsVodTrack(const char *service);
  815. static void clear_archive_encoder(obs_output_t *output,
  816. const char *expected_name)
  817. {
  818. obs_encoder_t *last = obs_output_get_audio_encoder(output, 1);
  819. bool clear = false;
  820. /* ensures that we don't remove twitch's soundtrack encoder */
  821. if (last) {
  822. const char *name = obs_encoder_get_name(last);
  823. clear = name && strcmp(name, expected_name) == 0;
  824. obs_encoder_release(last);
  825. }
  826. if (clear)
  827. obs_output_set_audio_encoder(output, nullptr, 1);
  828. }
  829. void SimpleOutput::SetupVodTrack(obs_service_t *service)
  830. {
  831. bool advanced =
  832. config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
  833. bool enable = config_get_bool(main->Config(), "SimpleOutput",
  834. "VodTrackEnabled");
  835. bool enableForCustomServer = config_get_bool(
  836. GetGlobalConfig(), "General", "EnableCustomServerVodTrack");
  837. OBSDataAutoRelease settings = obs_service_get_settings(service);
  838. const char *name = obs_data_get_string(settings, "service");
  839. const char *id = obs_service_get_id(service);
  840. if (strcmp(id, "rtmp_custom") == 0)
  841. enable = enableForCustomServer ? enable : false;
  842. else
  843. enable = advanced && enable && ServiceSupportsVodTrack(name);
  844. if (enable)
  845. obs_output_set_audio_encoder(streamOutput, aacArchive, 1);
  846. else
  847. clear_archive_encoder(streamOutput, SIMPLE_ARCHIVE_NAME);
  848. }
  849. bool SimpleOutput::StartStreaming(obs_service_t *service)
  850. {
  851. bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
  852. int retryDelay =
  853. config_get_uint(main->Config(), "Output", "RetryDelay");
  854. int maxRetries =
  855. config_get_uint(main->Config(), "Output", "MaxRetries");
  856. bool useDelay =
  857. config_get_bool(main->Config(), "Output", "DelayEnable");
  858. int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
  859. bool preserveDelay =
  860. config_get_bool(main->Config(), "Output", "DelayPreserve");
  861. const char *bindIP =
  862. config_get_string(main->Config(), "Output", "BindIP");
  863. bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
  864. "NewSocketLoopEnable");
  865. bool enableLowLatencyMode =
  866. config_get_bool(main->Config(), "Output", "LowLatencyEnable");
  867. bool enableDynBitrate =
  868. config_get_bool(main->Config(), "Output", "DynamicBitrate");
  869. OBSDataAutoRelease settings = obs_data_create();
  870. obs_data_set_string(settings, "bind_ip", bindIP);
  871. obs_data_set_bool(settings, "new_socket_loop_enabled",
  872. enableNewSocketLoop);
  873. obs_data_set_bool(settings, "low_latency_mode_enabled",
  874. enableLowLatencyMode);
  875. obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
  876. obs_output_update(streamOutput, settings);
  877. if (!reconnect)
  878. maxRetries = 0;
  879. obs_output_set_delay(streamOutput, useDelay ? delaySec : 0,
  880. preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
  881. obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
  882. SetupVodTrack(service);
  883. if (obs_output_start(streamOutput)) {
  884. return true;
  885. }
  886. const char *error = obs_output_get_last_error(streamOutput);
  887. bool hasLastError = error && *error;
  888. if (hasLastError)
  889. lastError = error;
  890. else
  891. lastError = string();
  892. const char *type = obs_output_get_id(streamOutput);
  893. blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type,
  894. hasLastError ? " Last Error: " : "", hasLastError ? error : "");
  895. return false;
  896. }
  897. void SimpleOutput::UpdateRecording()
  898. {
  899. if (replayBufferActive || recordingActive)
  900. return;
  901. if (usingRecordingPreset) {
  902. if (!ffmpegOutput)
  903. UpdateRecordingSettings();
  904. } else if (!obs_output_active(streamOutput)) {
  905. Update();
  906. }
  907. if (!Active())
  908. SetupOutputs();
  909. if (!ffmpegOutput) {
  910. obs_output_set_video_encoder(fileOutput, videoRecording);
  911. obs_output_set_audio_encoder(fileOutput, aacRecording, 0);
  912. }
  913. if (replayBuffer) {
  914. obs_output_set_video_encoder(replayBuffer, videoRecording);
  915. obs_output_set_audio_encoder(replayBuffer, aacRecording, 0);
  916. }
  917. recordingConfigured = true;
  918. }
  919. bool SimpleOutput::ConfigureRecording(bool updateReplayBuffer)
  920. {
  921. const char *path =
  922. config_get_string(main->Config(), "SimpleOutput", "FilePath");
  923. const char *format =
  924. config_get_string(main->Config(), "SimpleOutput", "RecFormat");
  925. const char *mux = config_get_string(main->Config(), "SimpleOutput",
  926. "MuxerCustom");
  927. bool noSpace = config_get_bool(main->Config(), "SimpleOutput",
  928. "FileNameWithoutSpace");
  929. const char *filenameFormat = config_get_string(main->Config(), "Output",
  930. "FilenameFormatting");
  931. bool overwriteIfExists =
  932. config_get_bool(main->Config(), "Output", "OverwriteIfExists");
  933. const char *rbPrefix = config_get_string(main->Config(), "SimpleOutput",
  934. "RecRBPrefix");
  935. const char *rbSuffix = config_get_string(main->Config(), "SimpleOutput",
  936. "RecRBSuffix");
  937. int rbTime =
  938. config_get_int(main->Config(), "SimpleOutput", "RecRBTime");
  939. int rbSize =
  940. config_get_int(main->Config(), "SimpleOutput", "RecRBSize");
  941. string f;
  942. string strPath;
  943. OBSDataAutoRelease settings = obs_data_create();
  944. if (updateReplayBuffer) {
  945. f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
  946. strPath = GetOutputFilename(path, ffmpegOutput ? "avi" : format,
  947. noSpace, overwriteIfExists,
  948. f.c_str());
  949. obs_data_set_string(settings, "directory", path);
  950. obs_data_set_string(settings, "format", f.c_str());
  951. obs_data_set_string(settings, "extension", format);
  952. obs_data_set_bool(settings, "allow_spaces", !noSpace);
  953. obs_data_set_int(settings, "max_time_sec", rbTime);
  954. obs_data_set_int(settings, "max_size_mb",
  955. usingRecordingPreset ? rbSize : 0);
  956. } else {
  957. f = GetFormatString(filenameFormat, nullptr, nullptr);
  958. strPath = GetRecordingFilename(path,
  959. ffmpegOutput ? "avi" : format,
  960. noSpace, overwriteIfExists,
  961. f.c_str(), ffmpegOutput);
  962. obs_data_set_string(settings, ffmpegOutput ? "url" : "path",
  963. strPath.c_str());
  964. }
  965. obs_data_set_string(settings, "muxer_settings", mux);
  966. if (updateReplayBuffer)
  967. obs_output_update(replayBuffer, settings);
  968. else
  969. obs_output_update(fileOutput, settings);
  970. return true;
  971. }
  972. bool SimpleOutput::StartRecording()
  973. {
  974. UpdateRecording();
  975. if (!ConfigureRecording(false))
  976. return false;
  977. if (!obs_output_start(fileOutput)) {
  978. QString error_reason;
  979. const char *error = obs_output_get_last_error(fileOutput);
  980. if (error)
  981. error_reason = QT_UTF8(error);
  982. else
  983. error_reason = QTStr("Output.StartFailedGeneric");
  984. QMessageBox::critical(main,
  985. QTStr("Output.StartRecordingFailed"),
  986. error_reason);
  987. return false;
  988. }
  989. return true;
  990. }
  991. bool SimpleOutput::StartReplayBuffer()
  992. {
  993. UpdateRecording();
  994. if (!ConfigureRecording(true))
  995. return false;
  996. if (!obs_output_start(replayBuffer)) {
  997. QMessageBox::critical(main, QTStr("Output.StartReplayFailed"),
  998. QTStr("Output.StartFailedGeneric"));
  999. return false;
  1000. }
  1001. return true;
  1002. }
  1003. void SimpleOutput::StopStreaming(bool force)
  1004. {
  1005. if (force)
  1006. obs_output_force_stop(streamOutput);
  1007. else
  1008. obs_output_stop(streamOutput);
  1009. }
  1010. void SimpleOutput::StopRecording(bool force)
  1011. {
  1012. if (force)
  1013. obs_output_force_stop(fileOutput);
  1014. else
  1015. obs_output_stop(fileOutput);
  1016. }
  1017. void SimpleOutput::StopReplayBuffer(bool force)
  1018. {
  1019. if (force)
  1020. obs_output_force_stop(replayBuffer);
  1021. else
  1022. obs_output_stop(replayBuffer);
  1023. }
  1024. bool SimpleOutput::StreamingActive() const
  1025. {
  1026. return obs_output_active(streamOutput);
  1027. }
  1028. bool SimpleOutput::RecordingActive() const
  1029. {
  1030. return obs_output_active(fileOutput);
  1031. }
  1032. bool SimpleOutput::ReplayBufferActive() const
  1033. {
  1034. return obs_output_active(replayBuffer);
  1035. }
  1036. /* ------------------------------------------------------------------------ */
  1037. struct AdvancedOutput : BasicOutputHandler {
  1038. OBSEncoder streamAudioEnc;
  1039. OBSEncoder streamArchiveEnc;
  1040. OBSEncoder aacTrack[MAX_AUDIO_MIXES];
  1041. OBSEncoder videoStreaming;
  1042. OBSEncoder videoRecording;
  1043. bool ffmpegOutput;
  1044. bool ffmpegRecording;
  1045. bool useStreamEncoder;
  1046. bool usesBitrate = false;
  1047. string aacEncoderID[MAX_AUDIO_MIXES];
  1048. AdvancedOutput(OBSBasic *main_);
  1049. inline void UpdateStreamSettings();
  1050. inline void UpdateRecordingSettings();
  1051. inline void UpdateAudioSettings();
  1052. virtual void Update() override;
  1053. inline void SetupVodTrack(obs_service_t *service);
  1054. inline void SetupStreaming();
  1055. inline void SetupRecording();
  1056. inline void SetupFFmpeg();
  1057. void SetupOutputs() override;
  1058. int GetAudioBitrate(size_t i) const;
  1059. virtual bool SetupStreaming(obs_service_t *service) override;
  1060. virtual bool StartStreaming(obs_service_t *service) override;
  1061. virtual bool StartRecording() override;
  1062. virtual bool StartReplayBuffer() override;
  1063. virtual void StopStreaming(bool force) override;
  1064. virtual void StopRecording(bool force) override;
  1065. virtual void StopReplayBuffer(bool force) override;
  1066. virtual bool StreamingActive() const override;
  1067. virtual bool RecordingActive() const override;
  1068. virtual bool ReplayBufferActive() const override;
  1069. };
  1070. static OBSData GetDataFromJsonFile(const char *jsonFile)
  1071. {
  1072. char fullPath[512];
  1073. OBSDataAutoRelease data = nullptr;
  1074. int ret = GetProfilePath(fullPath, sizeof(fullPath), jsonFile);
  1075. if (ret > 0) {
  1076. BPtr<char> jsonData = os_quick_read_utf8_file(fullPath);
  1077. if (!!jsonData) {
  1078. data = obs_data_create_from_json(jsonData);
  1079. }
  1080. }
  1081. if (!data)
  1082. data = obs_data_create();
  1083. return data.Get();
  1084. }
  1085. static void ApplyEncoderDefaults(OBSData &settings,
  1086. const obs_encoder_t *encoder)
  1087. {
  1088. OBSData dataRet = obs_encoder_get_defaults(encoder);
  1089. obs_data_release(dataRet);
  1090. if (!!settings)
  1091. obs_data_apply(dataRet, settings);
  1092. settings = std::move(dataRet);
  1093. }
  1094. #define ADV_ARCHIVE_NAME "adv_archive_aac"
  1095. #ifdef __APPLE__
  1096. static void translate_macvth264_encoder(const char *&encoder)
  1097. {
  1098. if (strcmp(encoder, "vt_h264_hw") == 0) {
  1099. encoder = "com.apple.videotoolbox.videoencoder.h264.gva";
  1100. } else if (strcmp(encoder, "vt_h264_sw") == 0) {
  1101. encoder = "com.apple.videotoolbox.videoencoder.h264";
  1102. }
  1103. }
  1104. #endif
  1105. AdvancedOutput::AdvancedOutput(OBSBasic *main_) : BasicOutputHandler(main_)
  1106. {
  1107. const char *recType =
  1108. config_get_string(main->Config(), "AdvOut", "RecType");
  1109. const char *streamEncoder =
  1110. config_get_string(main->Config(), "AdvOut", "Encoder");
  1111. const char *recordEncoder =
  1112. config_get_string(main->Config(), "AdvOut", "RecEncoder");
  1113. #ifdef __APPLE__
  1114. translate_macvth264_encoder(streamEncoder);
  1115. translate_macvth264_encoder(recordEncoder);
  1116. #endif
  1117. ffmpegOutput = astrcmpi(recType, "FFmpeg") == 0;
  1118. ffmpegRecording =
  1119. ffmpegOutput &&
  1120. config_get_bool(main->Config(), "AdvOut", "FFOutputToFile");
  1121. useStreamEncoder = astrcmpi(recordEncoder, "none") == 0;
  1122. OBSData streamEncSettings = GetDataFromJsonFile("streamEncoder.json");
  1123. OBSData recordEncSettings = GetDataFromJsonFile("recordEncoder.json");
  1124. if (ffmpegOutput) {
  1125. fileOutput = obs_output_create(
  1126. "ffmpeg_output", "adv_ffmpeg_output", nullptr, nullptr);
  1127. if (!fileOutput)
  1128. throw "Failed to create recording FFmpeg output "
  1129. "(advanced output)";
  1130. } else {
  1131. bool useReplayBuffer =
  1132. config_get_bool(main->Config(), "AdvOut", "RecRB");
  1133. if (useReplayBuffer) {
  1134. OBSDataAutoRelease hotkey;
  1135. const char *str = config_get_string(
  1136. main->Config(), "Hotkeys", "ReplayBuffer");
  1137. if (str)
  1138. hotkey = obs_data_create_from_json(str);
  1139. else
  1140. hotkey = nullptr;
  1141. replayBuffer = obs_output_create("replay_buffer",
  1142. Str("ReplayBuffer"),
  1143. nullptr, hotkey);
  1144. if (!replayBuffer)
  1145. throw "Failed to create replay buffer output "
  1146. "(simple output)";
  1147. signal_handler_t *signal =
  1148. obs_output_get_signal_handler(replayBuffer);
  1149. startReplayBuffer.Connect(signal, "start",
  1150. OBSStartReplayBuffer, this);
  1151. stopReplayBuffer.Connect(signal, "stop",
  1152. OBSStopReplayBuffer, this);
  1153. replayBufferStopping.Connect(signal, "stopping",
  1154. OBSReplayBufferStopping,
  1155. this);
  1156. replayBufferSaved.Connect(signal, "saved",
  1157. OBSReplayBufferSaved, this);
  1158. }
  1159. fileOutput = obs_output_create(
  1160. "ffmpeg_muxer", "adv_file_output", nullptr, nullptr);
  1161. if (!fileOutput)
  1162. throw "Failed to create recording output "
  1163. "(advanced output)";
  1164. if (!useStreamEncoder) {
  1165. videoRecording = obs_video_encoder_create(
  1166. recordEncoder, "advanced_video_recording",
  1167. recordEncSettings, nullptr);
  1168. if (!videoRecording)
  1169. throw "Failed to create recording video "
  1170. "encoder (advanced output)";
  1171. obs_encoder_release(videoRecording);
  1172. }
  1173. }
  1174. videoStreaming = obs_video_encoder_create(streamEncoder,
  1175. "advanced_video_stream",
  1176. streamEncSettings, nullptr);
  1177. if (!videoStreaming)
  1178. throw "Failed to create streaming video encoder "
  1179. "(advanced output)";
  1180. obs_encoder_release(videoStreaming);
  1181. const char *rate_control = obs_data_get_string(
  1182. useStreamEncoder ? streamEncSettings : recordEncSettings,
  1183. "rate_control");
  1184. if (!rate_control)
  1185. rate_control = "";
  1186. usesBitrate = astrcmpi(rate_control, "CBR") == 0 ||
  1187. astrcmpi(rate_control, "VBR") == 0 ||
  1188. astrcmpi(rate_control, "ABR") == 0;
  1189. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  1190. char name[9];
  1191. snprintf(name, sizeof(name), "adv_aac%d", i);
  1192. if (!CreateAACEncoder(aacTrack[i], aacEncoderID[i],
  1193. GetAudioBitrate(i), name, i))
  1194. throw "Failed to create audio encoder "
  1195. "(advanced output)";
  1196. }
  1197. std::string id;
  1198. int streamTrack =
  1199. config_get_int(main->Config(), "AdvOut", "TrackIndex") - 1;
  1200. if (!CreateAACEncoder(streamAudioEnc, id, GetAudioBitrate(streamTrack),
  1201. "adv_stream_aac", streamTrack))
  1202. throw "Failed to create streaming audio encoder "
  1203. "(advanced output)";
  1204. id = "";
  1205. int vodTrack =
  1206. config_get_int(main->Config(), "AdvOut", "VodTrackIndex") - 1;
  1207. if (!CreateAACEncoder(streamArchiveEnc, id, GetAudioBitrate(vodTrack),
  1208. ADV_ARCHIVE_NAME, vodTrack))
  1209. throw "Failed to create archive audio encoder "
  1210. "(advanced output)";
  1211. startRecording.Connect(obs_output_get_signal_handler(fileOutput),
  1212. "start", OBSStartRecording, this);
  1213. stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop",
  1214. OBSStopRecording, this);
  1215. recordStopping.Connect(obs_output_get_signal_handler(fileOutput),
  1216. "stopping", OBSRecordStopping, this);
  1217. recordFileChanged.Connect(obs_output_get_signal_handler(fileOutput),
  1218. "file_changed", OBSRecordFileChanged, this);
  1219. }
  1220. void AdvancedOutput::UpdateStreamSettings()
  1221. {
  1222. bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
  1223. "ApplyServiceSettings");
  1224. bool enforceBitrate = !config_get_bool(main->Config(), "Stream1",
  1225. "IgnoreRecommended");
  1226. bool dynBitrate =
  1227. config_get_bool(main->Config(), "Output", "DynamicBitrate");
  1228. const char *streamEncoder =
  1229. config_get_string(main->Config(), "AdvOut", "Encoder");
  1230. OBSData settings = GetDataFromJsonFile("streamEncoder.json");
  1231. ApplyEncoderDefaults(settings, videoStreaming);
  1232. if (applyServiceSettings) {
  1233. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  1234. int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  1235. obs_service_apply_encoder_settings(main->GetService(), settings,
  1236. nullptr);
  1237. if (!enforceBitrate)
  1238. obs_data_set_int(settings, "bitrate", bitrate);
  1239. int enforced_keyint_sec =
  1240. (int)obs_data_get_int(settings, "keyint_sec");
  1241. if (keyint_sec != 0 && keyint_sec < enforced_keyint_sec)
  1242. obs_data_set_int(settings, "keyint_sec", keyint_sec);
  1243. }
  1244. if (dynBitrate && astrcmpi(streamEncoder, "jim_nvenc") == 0)
  1245. obs_data_set_bool(settings, "lookahead", false);
  1246. video_t *video = obs_get_video();
  1247. enum video_format format = video_output_get_format(video);
  1248. switch (format) {
  1249. case VIDEO_FORMAT_I420:
  1250. case VIDEO_FORMAT_NV12:
  1251. case VIDEO_FORMAT_I010:
  1252. case VIDEO_FORMAT_P010:
  1253. break;
  1254. default:
  1255. obs_encoder_set_preferred_video_format(videoStreaming,
  1256. VIDEO_FORMAT_NV12);
  1257. }
  1258. obs_encoder_update(videoStreaming, settings);
  1259. }
  1260. inline void AdvancedOutput::UpdateRecordingSettings()
  1261. {
  1262. OBSData settings = GetDataFromJsonFile("recordEncoder.json");
  1263. obs_encoder_update(videoRecording, settings);
  1264. }
  1265. void AdvancedOutput::Update()
  1266. {
  1267. UpdateStreamSettings();
  1268. if (!useStreamEncoder && !ffmpegOutput)
  1269. UpdateRecordingSettings();
  1270. UpdateAudioSettings();
  1271. }
  1272. static inline bool ServiceSupportsVodTrack(const char *service)
  1273. {
  1274. static const char *vodTrackServices[] = {"Twitch"};
  1275. for (const char *vodTrackService : vodTrackServices) {
  1276. if (astrcmpi(vodTrackService, service) == 0)
  1277. return true;
  1278. }
  1279. return false;
  1280. }
  1281. inline void AdvancedOutput::SetupStreaming()
  1282. {
  1283. bool rescale = config_get_bool(main->Config(), "AdvOut", "Rescale");
  1284. const char *rescaleRes =
  1285. config_get_string(main->Config(), "AdvOut", "RescaleRes");
  1286. unsigned int cx = 0;
  1287. unsigned int cy = 0;
  1288. if (rescale && rescaleRes && *rescaleRes) {
  1289. if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
  1290. cx = 0;
  1291. cy = 0;
  1292. }
  1293. }
  1294. obs_output_set_audio_encoder(streamOutput, streamAudioEnc, 0);
  1295. obs_encoder_set_scaled_size(videoStreaming, cx, cy);
  1296. const char *id = obs_service_get_id(main->GetService());
  1297. if (strcmp(id, "rtmp_custom") == 0) {
  1298. OBSDataAutoRelease settings = obs_data_create();
  1299. obs_service_apply_encoder_settings(main->GetService(), settings,
  1300. nullptr);
  1301. obs_encoder_update(videoStreaming, settings);
  1302. }
  1303. }
  1304. inline void AdvancedOutput::SetupRecording()
  1305. {
  1306. const char *path =
  1307. config_get_string(main->Config(), "AdvOut", "RecFilePath");
  1308. const char *mux =
  1309. config_get_string(main->Config(), "AdvOut", "RecMuxerCustom");
  1310. bool rescale = config_get_bool(main->Config(), "AdvOut", "RecRescale");
  1311. const char *rescaleRes =
  1312. config_get_string(main->Config(), "AdvOut", "RecRescaleRes");
  1313. int tracks;
  1314. const char *recFormat =
  1315. config_get_string(main->Config(), "AdvOut", "RecFormat");
  1316. bool flv = strcmp(recFormat, "flv") == 0;
  1317. if (flv)
  1318. tracks = config_get_int(main->Config(), "AdvOut", "FLVTrack");
  1319. else
  1320. tracks = config_get_int(main->Config(), "AdvOut", "RecTracks");
  1321. OBSDataAutoRelease settings = obs_data_create();
  1322. unsigned int cx = 0;
  1323. unsigned int cy = 0;
  1324. int idx = 0;
  1325. if (tracks == 0)
  1326. tracks = config_get_int(main->Config(), "AdvOut", "TrackIndex");
  1327. if (useStreamEncoder) {
  1328. obs_output_set_video_encoder(fileOutput, videoStreaming);
  1329. if (replayBuffer)
  1330. obs_output_set_video_encoder(replayBuffer,
  1331. videoStreaming);
  1332. } else {
  1333. if (rescale && rescaleRes && *rescaleRes) {
  1334. if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
  1335. cx = 0;
  1336. cy = 0;
  1337. }
  1338. }
  1339. obs_encoder_set_scaled_size(videoRecording, cx, cy);
  1340. obs_output_set_video_encoder(fileOutput, videoRecording);
  1341. if (replayBuffer)
  1342. obs_output_set_video_encoder(replayBuffer,
  1343. videoRecording);
  1344. }
  1345. if (!flv) {
  1346. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  1347. if ((tracks & (1 << i)) != 0) {
  1348. obs_output_set_audio_encoder(fileOutput,
  1349. aacTrack[i], idx);
  1350. if (replayBuffer)
  1351. obs_output_set_audio_encoder(
  1352. replayBuffer, aacTrack[i], idx);
  1353. idx++;
  1354. }
  1355. }
  1356. } else if (flv && tracks != 0) {
  1357. obs_output_set_audio_encoder(fileOutput, aacTrack[tracks - 1],
  1358. idx);
  1359. if (replayBuffer)
  1360. obs_output_set_audio_encoder(replayBuffer,
  1361. aacTrack[tracks - 1], idx);
  1362. }
  1363. obs_data_set_string(settings, "path", path);
  1364. obs_data_set_string(settings, "muxer_settings", mux);
  1365. obs_output_update(fileOutput, settings);
  1366. if (replayBuffer)
  1367. obs_output_update(replayBuffer, settings);
  1368. }
  1369. inline void AdvancedOutput::SetupFFmpeg()
  1370. {
  1371. const char *url = config_get_string(main->Config(), "AdvOut", "FFURL");
  1372. int vBitrate = config_get_int(main->Config(), "AdvOut", "FFVBitrate");
  1373. int gopSize = config_get_int(main->Config(), "AdvOut", "FFVGOPSize");
  1374. bool rescale = config_get_bool(main->Config(), "AdvOut", "FFRescale");
  1375. const char *rescaleRes =
  1376. config_get_string(main->Config(), "AdvOut", "FFRescaleRes");
  1377. const char *formatName =
  1378. config_get_string(main->Config(), "AdvOut", "FFFormat");
  1379. const char *mimeType =
  1380. config_get_string(main->Config(), "AdvOut", "FFFormatMimeType");
  1381. const char *muxCustom =
  1382. config_get_string(main->Config(), "AdvOut", "FFMCustom");
  1383. const char *vEncoder =
  1384. config_get_string(main->Config(), "AdvOut", "FFVEncoder");
  1385. int vEncoderId =
  1386. config_get_int(main->Config(), "AdvOut", "FFVEncoderId");
  1387. const char *vEncCustom =
  1388. config_get_string(main->Config(), "AdvOut", "FFVCustom");
  1389. int aBitrate = config_get_int(main->Config(), "AdvOut", "FFABitrate");
  1390. int aMixes = config_get_int(main->Config(), "AdvOut", "FFAudioMixes");
  1391. const char *aEncoder =
  1392. config_get_string(main->Config(), "AdvOut", "FFAEncoder");
  1393. int aEncoderId =
  1394. config_get_int(main->Config(), "AdvOut", "FFAEncoderId");
  1395. const char *aEncCustom =
  1396. config_get_string(main->Config(), "AdvOut", "FFACustom");
  1397. OBSDataAutoRelease settings = obs_data_create();
  1398. obs_data_set_string(settings, "url", url);
  1399. obs_data_set_string(settings, "format_name", formatName);
  1400. obs_data_set_string(settings, "format_mime_type", mimeType);
  1401. obs_data_set_string(settings, "muxer_settings", muxCustom);
  1402. obs_data_set_int(settings, "gop_size", gopSize);
  1403. obs_data_set_int(settings, "video_bitrate", vBitrate);
  1404. obs_data_set_string(settings, "video_encoder", vEncoder);
  1405. obs_data_set_int(settings, "video_encoder_id", vEncoderId);
  1406. obs_data_set_string(settings, "video_settings", vEncCustom);
  1407. obs_data_set_int(settings, "audio_bitrate", aBitrate);
  1408. obs_data_set_string(settings, "audio_encoder", aEncoder);
  1409. obs_data_set_int(settings, "audio_encoder_id", aEncoderId);
  1410. obs_data_set_string(settings, "audio_settings", aEncCustom);
  1411. if (rescale && rescaleRes && *rescaleRes) {
  1412. int width;
  1413. int height;
  1414. int val = sscanf(rescaleRes, "%dx%d", &width, &height);
  1415. if (val == 2 && width && height) {
  1416. obs_data_set_int(settings, "scale_width", width);
  1417. obs_data_set_int(settings, "scale_height", height);
  1418. }
  1419. }
  1420. obs_output_set_mixers(fileOutput, aMixes);
  1421. obs_output_set_media(fileOutput, obs_get_video(), obs_get_audio());
  1422. obs_output_update(fileOutput, settings);
  1423. }
  1424. static inline void SetEncoderName(obs_encoder_t *encoder, const char *name,
  1425. const char *defaultName)
  1426. {
  1427. obs_encoder_set_name(encoder, (name && *name) ? name : defaultName);
  1428. }
  1429. inline void AdvancedOutput::UpdateAudioSettings()
  1430. {
  1431. bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
  1432. "ApplyServiceSettings");
  1433. bool enforceBitrate = !config_get_bool(main->Config(), "Stream1",
  1434. "IgnoreRecommended");
  1435. int streamTrackIndex =
  1436. config_get_int(main->Config(), "AdvOut", "TrackIndex");
  1437. int vodTrackIndex =
  1438. config_get_int(main->Config(), "AdvOut", "VodTrackIndex");
  1439. OBSDataAutoRelease settings[MAX_AUDIO_MIXES];
  1440. for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
  1441. settings[i] = obs_data_create();
  1442. obs_data_set_int(settings[i], "bitrate", GetAudioBitrate(i));
  1443. }
  1444. for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
  1445. string cfg_name = "Track";
  1446. cfg_name += to_string((int)i + 1);
  1447. cfg_name += "Name";
  1448. const char *name = config_get_string(main->Config(), "AdvOut",
  1449. cfg_name.c_str());
  1450. string def_name = "Track";
  1451. def_name += to_string((int)i + 1);
  1452. SetEncoderName(aacTrack[i], name, def_name.c_str());
  1453. }
  1454. for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
  1455. int track = (int)(i + 1);
  1456. obs_encoder_update(aacTrack[i], settings[i]);
  1457. if (track == streamTrackIndex || track == vodTrackIndex) {
  1458. if (applyServiceSettings) {
  1459. int bitrate = (int)obs_data_get_int(settings[i],
  1460. "bitrate");
  1461. obs_service_apply_encoder_settings(
  1462. main->GetService(), nullptr,
  1463. settings[i]);
  1464. if (!enforceBitrate)
  1465. obs_data_set_int(settings[i], "bitrate",
  1466. bitrate);
  1467. }
  1468. }
  1469. if (track == streamTrackIndex)
  1470. obs_encoder_update(streamAudioEnc, settings[i]);
  1471. if (track == vodTrackIndex)
  1472. obs_encoder_update(streamArchiveEnc, settings[i]);
  1473. }
  1474. }
  1475. void AdvancedOutput::SetupOutputs()
  1476. {
  1477. obs_encoder_set_video(videoStreaming, obs_get_video());
  1478. if (videoRecording)
  1479. obs_encoder_set_video(videoRecording, obs_get_video());
  1480. for (size_t i = 0; i < MAX_AUDIO_MIXES; i++)
  1481. obs_encoder_set_audio(aacTrack[i], obs_get_audio());
  1482. obs_encoder_set_audio(streamAudioEnc, obs_get_audio());
  1483. obs_encoder_set_audio(streamArchiveEnc, obs_get_audio());
  1484. SetupStreaming();
  1485. if (ffmpegOutput)
  1486. SetupFFmpeg();
  1487. else
  1488. SetupRecording();
  1489. }
  1490. int AdvancedOutput::GetAudioBitrate(size_t i) const
  1491. {
  1492. static const char *names[] = {
  1493. "Track1Bitrate", "Track2Bitrate", "Track3Bitrate",
  1494. "Track4Bitrate", "Track5Bitrate", "Track6Bitrate",
  1495. };
  1496. int bitrate = (int)config_get_uint(main->Config(), "AdvOut", names[i]);
  1497. return FindClosestAvailableAACBitrate(bitrate);
  1498. }
  1499. inline void AdvancedOutput::SetupVodTrack(obs_service_t *service)
  1500. {
  1501. int streamTrack =
  1502. config_get_int(main->Config(), "AdvOut", "TrackIndex");
  1503. bool vodTrackEnabled =
  1504. config_get_bool(main->Config(), "AdvOut", "VodTrackEnabled");
  1505. int vodTrackIndex =
  1506. config_get_int(main->Config(), "AdvOut", "VodTrackIndex");
  1507. bool enableForCustomServer = config_get_bool(
  1508. GetGlobalConfig(), "General", "EnableCustomServerVodTrack");
  1509. const char *id = obs_service_get_id(service);
  1510. if (strcmp(id, "rtmp_custom") == 0) {
  1511. vodTrackEnabled = enableForCustomServer ? vodTrackEnabled
  1512. : false;
  1513. } else {
  1514. OBSDataAutoRelease settings = obs_service_get_settings(service);
  1515. const char *service = obs_data_get_string(settings, "service");
  1516. if (!ServiceSupportsVodTrack(service))
  1517. vodTrackEnabled = false;
  1518. }
  1519. if (vodTrackEnabled && streamTrack != vodTrackIndex)
  1520. obs_output_set_audio_encoder(streamOutput, streamArchiveEnc, 1);
  1521. else
  1522. clear_archive_encoder(streamOutput, ADV_ARCHIVE_NAME);
  1523. }
  1524. bool AdvancedOutput::SetupStreaming(obs_service_t *service)
  1525. {
  1526. int streamTrack =
  1527. config_get_int(main->Config(), "AdvOut", "TrackIndex");
  1528. if (!useStreamEncoder ||
  1529. (!ffmpegOutput && !obs_output_active(fileOutput))) {
  1530. UpdateStreamSettings();
  1531. }
  1532. UpdateAudioSettings();
  1533. if (!Active())
  1534. SetupOutputs();
  1535. Auth *auth = main->GetAuth();
  1536. if (auth)
  1537. auth->OnStreamConfig();
  1538. /* --------------------- */
  1539. const char *type = obs_service_get_output_type(service);
  1540. if (!type) {
  1541. type = "rtmp_output";
  1542. const char *url = obs_service_get_url(service);
  1543. if (url != NULL &&
  1544. strncmp(url, FTL_PROTOCOL, strlen(FTL_PROTOCOL)) == 0) {
  1545. type = "ftl_output";
  1546. } else if (url != NULL && strncmp(url, RTMP_PROTOCOL,
  1547. strlen(RTMP_PROTOCOL)) != 0) {
  1548. type = "ffmpeg_mpegts_muxer";
  1549. }
  1550. }
  1551. /* XXX: this is messy and disgusting and should be refactored */
  1552. if (outputType != type) {
  1553. streamDelayStarting.Disconnect();
  1554. streamStopping.Disconnect();
  1555. startStreaming.Disconnect();
  1556. stopStreaming.Disconnect();
  1557. streamOutput =
  1558. obs_output_create(type, "adv_stream", nullptr, nullptr);
  1559. if (!streamOutput) {
  1560. blog(LOG_WARNING,
  1561. "Creation of stream output type '%s' "
  1562. "failed!",
  1563. type);
  1564. return false;
  1565. }
  1566. streamDelayStarting.Connect(
  1567. obs_output_get_signal_handler(streamOutput), "starting",
  1568. OBSStreamStarting, this);
  1569. streamStopping.Connect(
  1570. obs_output_get_signal_handler(streamOutput), "stopping",
  1571. OBSStreamStopping, this);
  1572. startStreaming.Connect(
  1573. obs_output_get_signal_handler(streamOutput), "start",
  1574. OBSStartStreaming, this);
  1575. stopStreaming.Connect(
  1576. obs_output_get_signal_handler(streamOutput), "stop",
  1577. OBSStopStreaming, this);
  1578. bool isEncoded = obs_output_get_flags(streamOutput) &
  1579. OBS_OUTPUT_ENCODED;
  1580. if (isEncoded) {
  1581. const char *codec =
  1582. obs_output_get_supported_audio_codecs(
  1583. streamOutput);
  1584. if (!codec) {
  1585. blog(LOG_WARNING, "Failed to load audio codec");
  1586. return false;
  1587. }
  1588. if (strcmp(codec, "aac") != 0) {
  1589. OBSDataAutoRelease settings =
  1590. obs_encoder_get_settings(
  1591. streamAudioEnc);
  1592. const char *id =
  1593. FindAudioEncoderFromCodec(codec);
  1594. streamAudioEnc = obs_audio_encoder_create(
  1595. id, "alt_audio_enc", nullptr,
  1596. streamTrack - 1, nullptr);
  1597. if (!streamAudioEnc)
  1598. return false;
  1599. obs_encoder_release(streamAudioEnc);
  1600. obs_encoder_update(streamAudioEnc, settings);
  1601. obs_encoder_set_audio(streamAudioEnc,
  1602. obs_get_audio());
  1603. }
  1604. }
  1605. outputType = type;
  1606. }
  1607. obs_output_set_video_encoder(streamOutput, videoStreaming);
  1608. obs_output_set_audio_encoder(streamOutput, streamAudioEnc, 0);
  1609. return true;
  1610. }
  1611. bool AdvancedOutput::StartStreaming(obs_service_t *service)
  1612. {
  1613. obs_output_set_service(streamOutput, service);
  1614. bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
  1615. int retryDelay = config_get_int(main->Config(), "Output", "RetryDelay");
  1616. int maxRetries = config_get_int(main->Config(), "Output", "MaxRetries");
  1617. bool useDelay =
  1618. config_get_bool(main->Config(), "Output", "DelayEnable");
  1619. int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
  1620. bool preserveDelay =
  1621. config_get_bool(main->Config(), "Output", "DelayPreserve");
  1622. const char *bindIP =
  1623. config_get_string(main->Config(), "Output", "BindIP");
  1624. bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
  1625. "NewSocketLoopEnable");
  1626. bool enableLowLatencyMode =
  1627. config_get_bool(main->Config(), "Output", "LowLatencyEnable");
  1628. bool enableDynBitrate =
  1629. config_get_bool(main->Config(), "Output", "DynamicBitrate");
  1630. OBSDataAutoRelease settings = obs_data_create();
  1631. obs_data_set_string(settings, "bind_ip", bindIP);
  1632. obs_data_set_bool(settings, "new_socket_loop_enabled",
  1633. enableNewSocketLoop);
  1634. obs_data_set_bool(settings, "low_latency_mode_enabled",
  1635. enableLowLatencyMode);
  1636. obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
  1637. obs_output_update(streamOutput, settings);
  1638. if (!reconnect)
  1639. maxRetries = 0;
  1640. obs_output_set_delay(streamOutput, useDelay ? delaySec : 0,
  1641. preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
  1642. obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
  1643. SetupVodTrack(service);
  1644. if (obs_output_start(streamOutput)) {
  1645. return true;
  1646. }
  1647. const char *error = obs_output_get_last_error(streamOutput);
  1648. bool hasLastError = error && *error;
  1649. if (hasLastError)
  1650. lastError = error;
  1651. else
  1652. lastError = string();
  1653. const char *type = obs_output_get_id(streamOutput);
  1654. blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type,
  1655. hasLastError ? " Last Error: " : "", hasLastError ? error : "");
  1656. return false;
  1657. }
  1658. bool AdvancedOutput::StartRecording()
  1659. {
  1660. const char *path;
  1661. const char *recFormat;
  1662. const char *filenameFormat;
  1663. bool noSpace = false;
  1664. bool overwriteIfExists = false;
  1665. bool splitFile;
  1666. const char *splitFileType;
  1667. int splitFileTime;
  1668. int splitFileSize;
  1669. if (!useStreamEncoder) {
  1670. if (!ffmpegOutput) {
  1671. UpdateRecordingSettings();
  1672. }
  1673. } else if (!obs_output_active(streamOutput)) {
  1674. UpdateStreamSettings();
  1675. }
  1676. UpdateAudioSettings();
  1677. if (!Active())
  1678. SetupOutputs();
  1679. if (!ffmpegOutput || ffmpegRecording) {
  1680. path = config_get_string(main->Config(), "AdvOut",
  1681. ffmpegRecording ? "FFFilePath"
  1682. : "RecFilePath");
  1683. recFormat = config_get_string(main->Config(), "AdvOut",
  1684. ffmpegRecording ? "FFExtension"
  1685. : "RecFormat");
  1686. filenameFormat = config_get_string(main->Config(), "Output",
  1687. "FilenameFormatting");
  1688. overwriteIfExists = config_get_bool(main->Config(), "Output",
  1689. "OverwriteIfExists");
  1690. noSpace = config_get_bool(main->Config(), "AdvOut",
  1691. ffmpegRecording
  1692. ? "FFFileNameWithoutSpace"
  1693. : "RecFileNameWithoutSpace");
  1694. splitFile = config_get_bool(main->Config(), "AdvOut",
  1695. "RecSplitFile");
  1696. string strPath = GetRecordingFilename(path, recFormat, noSpace,
  1697. overwriteIfExists,
  1698. filenameFormat,
  1699. ffmpegRecording);
  1700. OBSDataAutoRelease settings = obs_data_create();
  1701. obs_data_set_string(settings, ffmpegRecording ? "url" : "path",
  1702. strPath.c_str());
  1703. if (splitFile) {
  1704. splitFileType = config_get_string(
  1705. main->Config(), "AdvOut", "RecSplitFileType");
  1706. splitFileTime =
  1707. (astrcmpi(splitFileType, "Time") == 0)
  1708. ? config_get_int(main->Config(),
  1709. "AdvOut",
  1710. "RecSplitFileTime")
  1711. : 0;
  1712. splitFileSize =
  1713. (astrcmpi(splitFileType, "Size") == 0)
  1714. ? config_get_int(main->Config(),
  1715. "AdvOut",
  1716. "RecSplitFileSize")
  1717. : 0;
  1718. obs_data_set_string(settings, "directory", path);
  1719. obs_data_set_string(settings, "format", filenameFormat);
  1720. obs_data_set_string(settings, "extension", recFormat);
  1721. obs_data_set_bool(settings, "allow_spaces", !noSpace);
  1722. obs_data_set_bool(settings, "allow_overwrite",
  1723. overwriteIfExists);
  1724. obs_data_set_bool(settings, "split_file", true);
  1725. obs_data_set_int(settings, "max_time_sec",
  1726. splitFileTime * 60);
  1727. obs_data_set_int(settings, "max_size_mb",
  1728. splitFileSize);
  1729. }
  1730. obs_output_update(fileOutput, settings);
  1731. }
  1732. if (!obs_output_start(fileOutput)) {
  1733. QString error_reason;
  1734. const char *error = obs_output_get_last_error(fileOutput);
  1735. if (error)
  1736. error_reason = QT_UTF8(error);
  1737. else
  1738. error_reason = QTStr("Output.StartFailedGeneric");
  1739. QMessageBox::critical(main,
  1740. QTStr("Output.StartRecordingFailed"),
  1741. error_reason);
  1742. return false;
  1743. }
  1744. return true;
  1745. }
  1746. bool AdvancedOutput::StartReplayBuffer()
  1747. {
  1748. const char *path;
  1749. const char *recFormat;
  1750. const char *filenameFormat;
  1751. bool noSpace = false;
  1752. bool overwriteIfExists = false;
  1753. const char *rbPrefix;
  1754. const char *rbSuffix;
  1755. int rbTime;
  1756. int rbSize;
  1757. if (!useStreamEncoder) {
  1758. if (!ffmpegOutput)
  1759. UpdateRecordingSettings();
  1760. } else if (!obs_output_active(streamOutput)) {
  1761. UpdateStreamSettings();
  1762. }
  1763. UpdateAudioSettings();
  1764. if (!Active())
  1765. SetupOutputs();
  1766. if (!ffmpegOutput || ffmpegRecording) {
  1767. path = config_get_string(main->Config(), "AdvOut",
  1768. ffmpegRecording ? "FFFilePath"
  1769. : "RecFilePath");
  1770. recFormat = config_get_string(main->Config(), "AdvOut",
  1771. ffmpegRecording ? "FFExtension"
  1772. : "RecFormat");
  1773. filenameFormat = config_get_string(main->Config(), "Output",
  1774. "FilenameFormatting");
  1775. overwriteIfExists = config_get_bool(main->Config(), "Output",
  1776. "OverwriteIfExists");
  1777. noSpace = config_get_bool(main->Config(), "AdvOut",
  1778. ffmpegRecording
  1779. ? "FFFileNameWithoutSpace"
  1780. : "RecFileNameWithoutSpace");
  1781. rbPrefix = config_get_string(main->Config(), "SimpleOutput",
  1782. "RecRBPrefix");
  1783. rbSuffix = config_get_string(main->Config(), "SimpleOutput",
  1784. "RecRBSuffix");
  1785. rbTime = config_get_int(main->Config(), "AdvOut", "RecRBTime");
  1786. rbSize = config_get_int(main->Config(), "AdvOut", "RecRBSize");
  1787. string f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
  1788. string strPath = GetOutputFilename(
  1789. path, recFormat, noSpace, overwriteIfExists, f.c_str());
  1790. OBSDataAutoRelease settings = obs_data_create();
  1791. obs_data_set_string(settings, "directory", path);
  1792. obs_data_set_string(settings, "format", f.c_str());
  1793. obs_data_set_string(settings, "extension", recFormat);
  1794. obs_data_set_bool(settings, "allow_spaces", !noSpace);
  1795. obs_data_set_int(settings, "max_time_sec", rbTime);
  1796. obs_data_set_int(settings, "max_size_mb",
  1797. usesBitrate ? 0 : rbSize);
  1798. obs_output_update(replayBuffer, settings);
  1799. }
  1800. if (!obs_output_start(replayBuffer)) {
  1801. QString error_reason;
  1802. const char *error = obs_output_get_last_error(replayBuffer);
  1803. if (error)
  1804. error_reason = QT_UTF8(error);
  1805. else
  1806. error_reason = QTStr("Output.StartFailedGeneric");
  1807. QMessageBox::critical(main, QTStr("Output.StartReplayFailed"),
  1808. error_reason);
  1809. return false;
  1810. }
  1811. return true;
  1812. }
  1813. void AdvancedOutput::StopStreaming(bool force)
  1814. {
  1815. if (force)
  1816. obs_output_force_stop(streamOutput);
  1817. else
  1818. obs_output_stop(streamOutput);
  1819. }
  1820. void AdvancedOutput::StopRecording(bool force)
  1821. {
  1822. if (force)
  1823. obs_output_force_stop(fileOutput);
  1824. else
  1825. obs_output_stop(fileOutput);
  1826. }
  1827. void AdvancedOutput::StopReplayBuffer(bool force)
  1828. {
  1829. if (force)
  1830. obs_output_force_stop(replayBuffer);
  1831. else
  1832. obs_output_stop(replayBuffer);
  1833. }
  1834. bool AdvancedOutput::StreamingActive() const
  1835. {
  1836. return obs_output_active(streamOutput);
  1837. }
  1838. bool AdvancedOutput::RecordingActive() const
  1839. {
  1840. return obs_output_active(fileOutput);
  1841. }
  1842. bool AdvancedOutput::ReplayBufferActive() const
  1843. {
  1844. return obs_output_active(replayBuffer);
  1845. }
  1846. /* ------------------------------------------------------------------------ */
  1847. void BasicOutputHandler::SetupAutoRemux(const char *&ext)
  1848. {
  1849. bool autoRemux = config_get_bool(main->Config(), "Video", "AutoRemux");
  1850. if (autoRemux && strcmp(ext, "mp4") == 0)
  1851. ext = "mkv";
  1852. }
  1853. std::string
  1854. BasicOutputHandler::GetRecordingFilename(const char *path, const char *ext,
  1855. bool noSpace, bool overwrite,
  1856. const char *format, bool ffmpeg)
  1857. {
  1858. if (!ffmpeg)
  1859. SetupAutoRemux(ext);
  1860. string dst = GetOutputFilename(path, ext, noSpace, overwrite, format);
  1861. lastRecordingPath = dst;
  1862. return dst;
  1863. }
  1864. BasicOutputHandler *CreateSimpleOutputHandler(OBSBasic *main)
  1865. {
  1866. return new SimpleOutput(main);
  1867. }
  1868. BasicOutputHandler *CreateAdvancedOutputHandler(OBSBasic *main)
  1869. {
  1870. return new AdvancedOutput(main);
  1871. }