obs-ffmpeg-mux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /******************************************************************************
  2. Copyright (C) 2015 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs-module.h>
  15. #include <obs-hotkey.h>
  16. #include <obs-avc.h>
  17. #include <util/dstr.h>
  18. #include <util/pipe.h>
  19. #include <util/darray.h>
  20. #include <util/platform.h>
  21. #include <util/circlebuf.h>
  22. #include <util/threading.h>
  23. #include "ffmpeg-mux/ffmpeg-mux.h"
  24. #ifdef _WIN32
  25. #include "util/windows/win-version.h"
  26. #endif
  27. #include <libavformat/avformat.h>
  28. #define do_log(level, format, ...) \
  29. blog(level, "[ffmpeg muxer: '%s'] " format, \
  30. obs_output_get_name(stream->output), ##__VA_ARGS__)
  31. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  32. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  33. struct ffmpeg_muxer {
  34. obs_output_t *output;
  35. os_process_pipe_t *pipe;
  36. int64_t stop_ts;
  37. uint64_t total_bytes;
  38. struct dstr path;
  39. bool sent_headers;
  40. volatile bool active;
  41. volatile bool stopping;
  42. volatile bool capturing;
  43. /* replay buffer */
  44. struct circlebuf packets;
  45. int64_t cur_size;
  46. int64_t cur_time;
  47. int64_t max_size;
  48. int64_t max_time;
  49. int64_t save_ts;
  50. int keyframes;
  51. obs_hotkey_id hotkey;
  52. DARRAY(struct encoder_packet) mux_packets;
  53. pthread_t mux_thread;
  54. bool mux_thread_joinable;
  55. volatile bool muxing;
  56. bool is_network;
  57. };
  58. static const char *ffmpeg_mux_getname(void *type)
  59. {
  60. UNUSED_PARAMETER(type);
  61. return obs_module_text("FFmpegMuxer");
  62. }
  63. static const char *ffmpeg_mpegts_mux_getname(void *type)
  64. {
  65. UNUSED_PARAMETER(type);
  66. return obs_module_text("FFmpegMpegtsMuxer");
  67. }
  68. static inline void replay_buffer_clear(struct ffmpeg_muxer *stream)
  69. {
  70. while (stream->packets.size > 0) {
  71. struct encoder_packet pkt;
  72. circlebuf_pop_front(&stream->packets, &pkt, sizeof(pkt));
  73. obs_encoder_packet_release(&pkt);
  74. }
  75. circlebuf_free(&stream->packets);
  76. stream->cur_size = 0;
  77. stream->cur_time = 0;
  78. stream->max_size = 0;
  79. stream->max_time = 0;
  80. stream->save_ts = 0;
  81. stream->keyframes = 0;
  82. }
  83. static void ffmpeg_mux_destroy(void *data)
  84. {
  85. struct ffmpeg_muxer *stream = data;
  86. replay_buffer_clear(stream);
  87. if (stream->mux_thread_joinable)
  88. pthread_join(stream->mux_thread, NULL);
  89. da_free(stream->mux_packets);
  90. os_process_pipe_destroy(stream->pipe);
  91. dstr_free(&stream->path);
  92. bfree(stream);
  93. }
  94. static void *ffmpeg_mux_create(obs_data_t *settings, obs_output_t *output)
  95. {
  96. struct ffmpeg_muxer *stream = bzalloc(sizeof(*stream));
  97. stream->output = output;
  98. if (obs_output_get_flags(output) & OBS_OUTPUT_SERVICE)
  99. stream->is_network = true;
  100. UNUSED_PARAMETER(settings);
  101. return stream;
  102. }
  103. #ifdef _WIN32
  104. #define FFMPEG_MUX "obs-ffmpeg-mux.exe"
  105. #else
  106. #define FFMPEG_MUX "obs-ffmpeg-mux"
  107. #endif
  108. static inline bool capturing(struct ffmpeg_muxer *stream)
  109. {
  110. return os_atomic_load_bool(&stream->capturing);
  111. }
  112. static inline bool stopping(struct ffmpeg_muxer *stream)
  113. {
  114. return os_atomic_load_bool(&stream->stopping);
  115. }
  116. static inline bool active(struct ffmpeg_muxer *stream)
  117. {
  118. return os_atomic_load_bool(&stream->active);
  119. }
  120. /* TODO: allow codecs other than h264 whenever we start using them */
  121. static void add_video_encoder_params(struct ffmpeg_muxer *stream,
  122. struct dstr *cmd, obs_encoder_t *vencoder)
  123. {
  124. obs_data_t *settings = obs_encoder_get_settings(vencoder);
  125. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  126. video_t *video = obs_get_video();
  127. const struct video_output_info *info = video_output_get_info(video);
  128. obs_data_release(settings);
  129. enum AVColorPrimaries pri = AVCOL_PRI_UNSPECIFIED;
  130. enum AVColorTransferCharacteristic trc = AVCOL_TRC_UNSPECIFIED;
  131. enum AVColorSpace spc = AVCOL_SPC_UNSPECIFIED;
  132. switch (info->colorspace) {
  133. case VIDEO_CS_601:
  134. pri = AVCOL_PRI_SMPTE170M;
  135. trc = AVCOL_TRC_SMPTE170M;
  136. spc = AVCOL_SPC_SMPTE170M;
  137. break;
  138. case VIDEO_CS_DEFAULT:
  139. case VIDEO_CS_709:
  140. pri = AVCOL_PRI_BT709;
  141. trc = AVCOL_TRC_BT709;
  142. spc = AVCOL_SPC_BT709;
  143. break;
  144. case VIDEO_CS_SRGB:
  145. pri = AVCOL_PRI_BT709;
  146. trc = AVCOL_TRC_IEC61966_2_1;
  147. spc = AVCOL_SPC_BT709;
  148. break;
  149. }
  150. const enum AVColorRange range = (info->range == VIDEO_RANGE_FULL)
  151. ? AVCOL_RANGE_JPEG
  152. : AVCOL_RANGE_MPEG;
  153. dstr_catf(cmd, "%s %d %d %d %d %d %d %d %d %d ",
  154. obs_encoder_get_codec(vencoder), bitrate,
  155. obs_output_get_width(stream->output),
  156. obs_output_get_height(stream->output), (int)pri, (int)trc,
  157. (int)spc, (int)range, (int)info->fps_num, (int)info->fps_den);
  158. }
  159. static void add_audio_encoder_params(struct dstr *cmd, obs_encoder_t *aencoder)
  160. {
  161. obs_data_t *settings = obs_encoder_get_settings(aencoder);
  162. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  163. audio_t *audio = obs_get_audio();
  164. struct dstr name = {0};
  165. obs_data_release(settings);
  166. dstr_copy(&name, obs_encoder_get_name(aencoder));
  167. dstr_replace(&name, "\"", "\"\"");
  168. dstr_catf(cmd, "\"%s\" %d %d %d ", name.array, bitrate,
  169. (int)obs_encoder_get_sample_rate(aencoder),
  170. (int)audio_output_get_channels(audio));
  171. dstr_free(&name);
  172. }
  173. static void log_muxer_params(struct ffmpeg_muxer *stream, const char *settings)
  174. {
  175. int ret;
  176. AVDictionary *dict = NULL;
  177. if ((ret = av_dict_parse_string(&dict, settings, "=", " ", 0))) {
  178. warn("Failed to parse muxer settings: %s\n%s", av_err2str(ret),
  179. settings);
  180. av_dict_free(&dict);
  181. return;
  182. }
  183. if (av_dict_count(dict) > 0) {
  184. struct dstr str = {0};
  185. AVDictionaryEntry *entry = NULL;
  186. while ((entry = av_dict_get(dict, "", entry,
  187. AV_DICT_IGNORE_SUFFIX)))
  188. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  189. info("Using muxer settings:%s", str.array);
  190. dstr_free(&str);
  191. }
  192. av_dict_free(&dict);
  193. }
  194. static void add_muxer_params(struct dstr *cmd, struct ffmpeg_muxer *stream)
  195. {
  196. obs_data_t *settings = obs_output_get_settings(stream->output);
  197. struct dstr mux = {0};
  198. dstr_copy(&mux, obs_data_get_string(settings, "muxer_settings"));
  199. log_muxer_params(stream, mux.array);
  200. dstr_replace(&mux, "\"", "\\\"");
  201. obs_data_release(settings);
  202. dstr_catf(cmd, "\"%s\" ", mux.array ? mux.array : "");
  203. dstr_free(&mux);
  204. }
  205. static void build_command_line(struct ffmpeg_muxer *stream, struct dstr *cmd,
  206. const char *path)
  207. {
  208. obs_encoder_t *vencoder = obs_output_get_video_encoder(stream->output);
  209. obs_encoder_t *aencoders[MAX_AUDIO_MIXES];
  210. int num_tracks = 0;
  211. for (;;) {
  212. obs_encoder_t *aencoder = obs_output_get_audio_encoder(
  213. stream->output, num_tracks);
  214. if (!aencoder)
  215. break;
  216. aencoders[num_tracks] = aencoder;
  217. num_tracks++;
  218. }
  219. dstr_init_move_array(cmd, os_get_executable_path_ptr(FFMPEG_MUX));
  220. dstr_insert_ch(cmd, 0, '\"');
  221. dstr_cat(cmd, "\" \"");
  222. dstr_copy(&stream->path, path);
  223. dstr_replace(&stream->path, "\"", "\"\"");
  224. dstr_cat_dstr(cmd, &stream->path);
  225. dstr_catf(cmd, "\" %d %d ", vencoder ? 1 : 0, num_tracks);
  226. if (vencoder)
  227. add_video_encoder_params(stream, cmd, vencoder);
  228. if (num_tracks) {
  229. dstr_cat(cmd, "aac ");
  230. for (int i = 0; i < num_tracks; i++) {
  231. add_audio_encoder_params(cmd, aencoders[i]);
  232. }
  233. }
  234. add_muxer_params(cmd, stream);
  235. }
  236. static inline void start_pipe(struct ffmpeg_muxer *stream, const char *path)
  237. {
  238. struct dstr cmd;
  239. build_command_line(stream, &cmd, path);
  240. stream->pipe = os_process_pipe_create(cmd.array, "w");
  241. dstr_free(&cmd);
  242. }
  243. static void set_file_not_readable_error(struct ffmpeg_muxer *stream,
  244. obs_data_t *settings, const char *path)
  245. {
  246. struct dstr error_message;
  247. dstr_init_copy(&error_message, obs_module_text("UnableToWritePath"));
  248. #ifdef _WIN32
  249. /* special warning for Windows 10 users about Defender */
  250. struct win_version_info ver;
  251. get_win_ver(&ver);
  252. if (ver.major >= 10) {
  253. dstr_cat(&error_message, "\n\n");
  254. dstr_cat(&error_message,
  255. obs_module_text("WarnWindowsDefender"));
  256. }
  257. #endif
  258. dstr_replace(&error_message, "%1", path);
  259. obs_output_set_last_error(stream->output, error_message.array);
  260. dstr_free(&error_message);
  261. obs_data_release(settings);
  262. }
  263. static bool ffmpeg_mux_start(void *data)
  264. {
  265. struct ffmpeg_muxer *stream = data;
  266. obs_data_t *settings;
  267. const char *path;
  268. if (!obs_output_can_begin_data_capture(stream->output, 0))
  269. return false;
  270. if (!obs_output_initialize_encoders(stream->output, 0))
  271. return false;
  272. settings = obs_output_get_settings(stream->output);
  273. if (stream->is_network) {
  274. obs_service_t *service;
  275. service = obs_output_get_service(stream->output);
  276. if (!service)
  277. return false;
  278. path = obs_service_get_url(service);
  279. } else {
  280. path = obs_data_get_string(settings, "path");
  281. }
  282. if (!stream->is_network) {
  283. /* ensure output path is writable to avoid generic error
  284. * message.
  285. *
  286. * TODO: remove once ffmpeg-mux is refactored to pass
  287. * errors back */
  288. FILE *test_file = os_fopen(path, "wb");
  289. if (!test_file) {
  290. set_file_not_readable_error(stream, settings, path);
  291. return false;
  292. }
  293. fclose(test_file);
  294. os_unlink(path);
  295. }
  296. start_pipe(stream, path);
  297. obs_data_release(settings);
  298. if (!stream->pipe) {
  299. obs_output_set_last_error(
  300. stream->output, obs_module_text("HelperProcessFailed"));
  301. warn("Failed to create process pipe");
  302. return false;
  303. }
  304. /* write headers and start capture */
  305. os_atomic_set_bool(&stream->active, true);
  306. os_atomic_set_bool(&stream->capturing, true);
  307. stream->total_bytes = 0;
  308. obs_output_begin_data_capture(stream->output, 0);
  309. info("Writing file '%s'...", stream->path.array);
  310. return true;
  311. }
  312. static int deactivate(struct ffmpeg_muxer *stream, int code)
  313. {
  314. int ret = -1;
  315. if (active(stream)) {
  316. ret = os_process_pipe_destroy(stream->pipe);
  317. stream->pipe = NULL;
  318. os_atomic_set_bool(&stream->active, false);
  319. os_atomic_set_bool(&stream->sent_headers, false);
  320. info("Output of file '%s' stopped", stream->path.array);
  321. }
  322. if (code) {
  323. obs_output_signal_stop(stream->output, code);
  324. } else if (stopping(stream)) {
  325. obs_output_end_data_capture(stream->output);
  326. }
  327. os_atomic_set_bool(&stream->stopping, false);
  328. return ret;
  329. }
  330. static void ffmpeg_mux_stop(void *data, uint64_t ts)
  331. {
  332. struct ffmpeg_muxer *stream = data;
  333. if (capturing(stream) || ts == 0) {
  334. stream->stop_ts = (int64_t)ts / 1000LL;
  335. os_atomic_set_bool(&stream->stopping, true);
  336. os_atomic_set_bool(&stream->capturing, false);
  337. }
  338. }
  339. static void signal_failure(struct ffmpeg_muxer *stream)
  340. {
  341. char error[1024];
  342. int ret;
  343. int code;
  344. size_t len;
  345. len = os_process_pipe_read_err(stream->pipe, (uint8_t *)error,
  346. sizeof(error) - 1);
  347. if (len > 0) {
  348. error[len] = 0;
  349. warn("ffmpeg-mux: %s", error);
  350. obs_output_set_last_error(stream->output, error);
  351. }
  352. ret = deactivate(stream, 0);
  353. switch (ret) {
  354. case FFM_UNSUPPORTED:
  355. code = OBS_OUTPUT_UNSUPPORTED;
  356. break;
  357. default:
  358. code = OBS_OUTPUT_ERROR;
  359. }
  360. obs_output_signal_stop(stream->output, code);
  361. os_atomic_set_bool(&stream->capturing, false);
  362. }
  363. static bool write_packet(struct ffmpeg_muxer *stream,
  364. struct encoder_packet *packet)
  365. {
  366. bool is_video = packet->type == OBS_ENCODER_VIDEO;
  367. size_t ret;
  368. struct ffm_packet_info info = {.pts = packet->pts,
  369. .dts = packet->dts,
  370. .size = (uint32_t)packet->size,
  371. .index = (int)packet->track_idx,
  372. .type = is_video ? FFM_PACKET_VIDEO
  373. : FFM_PACKET_AUDIO,
  374. .keyframe = packet->keyframe};
  375. ret = os_process_pipe_write(stream->pipe, (const uint8_t *)&info,
  376. sizeof(info));
  377. if (ret != sizeof(info)) {
  378. warn("os_process_pipe_write for info structure failed");
  379. signal_failure(stream);
  380. return false;
  381. }
  382. ret = os_process_pipe_write(stream->pipe, packet->data, packet->size);
  383. if (ret != packet->size) {
  384. warn("os_process_pipe_write for packet data failed");
  385. signal_failure(stream);
  386. return false;
  387. }
  388. stream->total_bytes += packet->size;
  389. return true;
  390. }
  391. static bool send_audio_headers(struct ffmpeg_muxer *stream,
  392. obs_encoder_t *aencoder, size_t idx)
  393. {
  394. struct encoder_packet packet = {
  395. .type = OBS_ENCODER_AUDIO, .timebase_den = 1, .track_idx = idx};
  396. obs_encoder_get_extra_data(aencoder, &packet.data, &packet.size);
  397. return write_packet(stream, &packet);
  398. }
  399. static bool send_video_headers(struct ffmpeg_muxer *stream)
  400. {
  401. obs_encoder_t *vencoder = obs_output_get_video_encoder(stream->output);
  402. struct encoder_packet packet = {.type = OBS_ENCODER_VIDEO,
  403. .timebase_den = 1};
  404. obs_encoder_get_extra_data(vencoder, &packet.data, &packet.size);
  405. return write_packet(stream, &packet);
  406. }
  407. static bool send_headers(struct ffmpeg_muxer *stream)
  408. {
  409. obs_encoder_t *aencoder;
  410. size_t idx = 0;
  411. if (!send_video_headers(stream))
  412. return false;
  413. do {
  414. aencoder = obs_output_get_audio_encoder(stream->output, idx);
  415. if (aencoder) {
  416. if (!send_audio_headers(stream, aencoder, idx)) {
  417. return false;
  418. }
  419. idx++;
  420. }
  421. } while (aencoder);
  422. return true;
  423. }
  424. static void ffmpeg_mux_data(void *data, struct encoder_packet *packet)
  425. {
  426. struct ffmpeg_muxer *stream = data;
  427. if (!active(stream))
  428. return;
  429. /* encoder failure */
  430. if (!packet) {
  431. deactivate(stream, OBS_OUTPUT_ENCODE_ERROR);
  432. return;
  433. }
  434. if (!stream->sent_headers) {
  435. if (!send_headers(stream))
  436. return;
  437. stream->sent_headers = true;
  438. }
  439. if (stopping(stream)) {
  440. if (packet->sys_dts_usec >= stream->stop_ts) {
  441. deactivate(stream, 0);
  442. return;
  443. }
  444. }
  445. write_packet(stream, packet);
  446. }
  447. static obs_properties_t *ffmpeg_mux_properties(void *unused)
  448. {
  449. UNUSED_PARAMETER(unused);
  450. obs_properties_t *props = obs_properties_create();
  451. obs_properties_add_text(props, "path", obs_module_text("FilePath"),
  452. OBS_TEXT_DEFAULT);
  453. return props;
  454. }
  455. static uint64_t ffmpeg_mux_total_bytes(void *data)
  456. {
  457. struct ffmpeg_muxer *stream = data;
  458. return stream->total_bytes;
  459. }
  460. struct obs_output_info ffmpeg_muxer = {
  461. .id = "ffmpeg_muxer",
  462. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_MULTI_TRACK |
  463. OBS_OUTPUT_CAN_PAUSE,
  464. .get_name = ffmpeg_mux_getname,
  465. .create = ffmpeg_mux_create,
  466. .destroy = ffmpeg_mux_destroy,
  467. .start = ffmpeg_mux_start,
  468. .stop = ffmpeg_mux_stop,
  469. .encoded_packet = ffmpeg_mux_data,
  470. .get_total_bytes = ffmpeg_mux_total_bytes,
  471. .get_properties = ffmpeg_mux_properties,
  472. };
  473. static int connect_time(struct ffmpeg_muxer *stream)
  474. {
  475. UNUSED_PARAMETER(stream);
  476. /* TODO */
  477. return 0;
  478. }
  479. static int ffmpeg_mpegts_mux_connect_time(void *data)
  480. {
  481. struct ffmpeg_muxer *stream = data;
  482. /* TODO */
  483. return connect_time(stream);
  484. }
  485. struct obs_output_info ffmpeg_mpegts_muxer = {
  486. .id = "ffmpeg_mpegts_muxer",
  487. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_MULTI_TRACK |
  488. OBS_OUTPUT_SERVICE,
  489. .encoded_video_codecs = "h264",
  490. .encoded_audio_codecs = "aac",
  491. .get_name = ffmpeg_mpegts_mux_getname,
  492. .create = ffmpeg_mux_create,
  493. .destroy = ffmpeg_mux_destroy,
  494. .start = ffmpeg_mux_start,
  495. .stop = ffmpeg_mux_stop,
  496. .encoded_packet = ffmpeg_mux_data,
  497. .get_total_bytes = ffmpeg_mux_total_bytes,
  498. .get_properties = ffmpeg_mux_properties,
  499. .get_connect_time_ms = ffmpeg_mpegts_mux_connect_time,
  500. };
  501. /* ------------------------------------------------------------------------ */
  502. static const char *replay_buffer_getname(void *type)
  503. {
  504. UNUSED_PARAMETER(type);
  505. return obs_module_text("ReplayBuffer");
  506. }
  507. static void replay_buffer_hotkey(void *data, obs_hotkey_id id,
  508. obs_hotkey_t *hotkey, bool pressed)
  509. {
  510. UNUSED_PARAMETER(id);
  511. UNUSED_PARAMETER(hotkey);
  512. UNUSED_PARAMETER(pressed);
  513. if (!pressed)
  514. return;
  515. struct ffmpeg_muxer *stream = data;
  516. if (os_atomic_load_bool(&stream->active)) {
  517. obs_encoder_t *vencoder =
  518. obs_output_get_video_encoder(stream->output);
  519. if (obs_encoder_paused(vencoder)) {
  520. info("Could not save buffer because encoders paused");
  521. return;
  522. }
  523. stream->save_ts = os_gettime_ns() / 1000LL;
  524. }
  525. }
  526. static void save_replay_proc(void *data, calldata_t *cd)
  527. {
  528. replay_buffer_hotkey(data, 0, NULL, true);
  529. UNUSED_PARAMETER(cd);
  530. }
  531. static void get_last_replay(void *data, calldata_t *cd)
  532. {
  533. struct ffmpeg_muxer *stream = data;
  534. if (!os_atomic_load_bool(&stream->muxing))
  535. calldata_set_string(cd, "path", stream->path.array);
  536. }
  537. static void *replay_buffer_create(obs_data_t *settings, obs_output_t *output)
  538. {
  539. UNUSED_PARAMETER(settings);
  540. struct ffmpeg_muxer *stream = bzalloc(sizeof(*stream));
  541. stream->output = output;
  542. stream->hotkey =
  543. obs_hotkey_register_output(output, "ReplayBuffer.Save",
  544. obs_module_text("ReplayBuffer.Save"),
  545. replay_buffer_hotkey, stream);
  546. proc_handler_t *ph = obs_output_get_proc_handler(output);
  547. proc_handler_add(ph, "void save()", save_replay_proc, stream);
  548. proc_handler_add(ph, "void get_last_replay(out string path)",
  549. get_last_replay, stream);
  550. return stream;
  551. }
  552. static void replay_buffer_destroy(void *data)
  553. {
  554. struct ffmpeg_muxer *stream = data;
  555. if (stream->hotkey)
  556. obs_hotkey_unregister(stream->hotkey);
  557. ffmpeg_mux_destroy(data);
  558. }
  559. static bool replay_buffer_start(void *data)
  560. {
  561. struct ffmpeg_muxer *stream = data;
  562. if (!obs_output_can_begin_data_capture(stream->output, 0))
  563. return false;
  564. if (!obs_output_initialize_encoders(stream->output, 0))
  565. return false;
  566. obs_data_t *s = obs_output_get_settings(stream->output);
  567. stream->max_time = obs_data_get_int(s, "max_time_sec") * 1000000LL;
  568. stream->max_size = obs_data_get_int(s, "max_size_mb") * (1024 * 1024);
  569. obs_data_release(s);
  570. os_atomic_set_bool(&stream->active, true);
  571. os_atomic_set_bool(&stream->capturing, true);
  572. stream->total_bytes = 0;
  573. obs_output_begin_data_capture(stream->output, 0);
  574. return true;
  575. }
  576. static bool purge_front(struct ffmpeg_muxer *stream)
  577. {
  578. struct encoder_packet pkt;
  579. bool keyframe;
  580. circlebuf_pop_front(&stream->packets, &pkt, sizeof(pkt));
  581. keyframe = pkt.type == OBS_ENCODER_VIDEO && pkt.keyframe;
  582. if (keyframe)
  583. stream->keyframes--;
  584. if (!stream->packets.size) {
  585. stream->cur_size = 0;
  586. stream->cur_time = 0;
  587. } else {
  588. struct encoder_packet first;
  589. circlebuf_peek_front(&stream->packets, &first, sizeof(first));
  590. stream->cur_time = first.dts_usec;
  591. stream->cur_size -= (int64_t)pkt.size;
  592. }
  593. obs_encoder_packet_release(&pkt);
  594. return keyframe;
  595. }
  596. static inline void purge(struct ffmpeg_muxer *stream)
  597. {
  598. if (purge_front(stream)) {
  599. struct encoder_packet pkt;
  600. for (;;) {
  601. circlebuf_peek_front(&stream->packets, &pkt,
  602. sizeof(pkt));
  603. if (pkt.type == OBS_ENCODER_VIDEO && pkt.keyframe)
  604. return;
  605. purge_front(stream);
  606. }
  607. }
  608. }
  609. static inline void replay_buffer_purge(struct ffmpeg_muxer *stream,
  610. struct encoder_packet *pkt)
  611. {
  612. if (stream->max_size) {
  613. if (!stream->packets.size || stream->keyframes <= 2)
  614. return;
  615. while ((stream->cur_size + (int64_t)pkt->size) >
  616. stream->max_size)
  617. purge(stream);
  618. }
  619. if (!stream->packets.size || stream->keyframes <= 2)
  620. return;
  621. while ((pkt->dts_usec - stream->cur_time) > stream->max_time)
  622. purge(stream);
  623. }
  624. static void insert_packet(struct darray *array, struct encoder_packet *packet,
  625. int64_t video_offset, int64_t *audio_offsets,
  626. int64_t video_dts_offset, int64_t *audio_dts_offsets)
  627. {
  628. struct encoder_packet pkt;
  629. DARRAY(struct encoder_packet) packets;
  630. packets.da = *array;
  631. size_t idx;
  632. obs_encoder_packet_ref(&pkt, packet);
  633. if (pkt.type == OBS_ENCODER_VIDEO) {
  634. pkt.dts_usec -= video_offset;
  635. pkt.dts -= video_dts_offset;
  636. pkt.pts -= video_dts_offset;
  637. } else {
  638. pkt.dts_usec -= audio_offsets[pkt.track_idx];
  639. pkt.dts -= audio_dts_offsets[pkt.track_idx];
  640. pkt.pts -= audio_dts_offsets[pkt.track_idx];
  641. }
  642. for (idx = packets.num; idx > 0; idx--) {
  643. struct encoder_packet *p = packets.array + (idx - 1);
  644. if (p->dts_usec < pkt.dts_usec)
  645. break;
  646. }
  647. da_insert(packets, idx, &pkt);
  648. *array = packets.da;
  649. }
  650. static void *replay_buffer_mux_thread(void *data)
  651. {
  652. struct ffmpeg_muxer *stream = data;
  653. start_pipe(stream, stream->path.array);
  654. if (!stream->pipe) {
  655. warn("Failed to create process pipe");
  656. goto error;
  657. }
  658. if (!send_headers(stream)) {
  659. warn("Could not write headers for file '%s'",
  660. stream->path.array);
  661. goto error;
  662. }
  663. for (size_t i = 0; i < stream->mux_packets.num; i++) {
  664. struct encoder_packet *pkt = &stream->mux_packets.array[i];
  665. write_packet(stream, pkt);
  666. obs_encoder_packet_release(pkt);
  667. }
  668. info("Wrote replay buffer to '%s'", stream->path.array);
  669. error:
  670. os_process_pipe_destroy(stream->pipe);
  671. stream->pipe = NULL;
  672. da_free(stream->mux_packets);
  673. os_atomic_set_bool(&stream->muxing, false);
  674. return NULL;
  675. }
  676. static void replay_buffer_save(struct ffmpeg_muxer *stream)
  677. {
  678. const size_t size = sizeof(struct encoder_packet);
  679. size_t num_packets = stream->packets.size / size;
  680. da_reserve(stream->mux_packets, num_packets);
  681. /* ---------------------------- */
  682. /* reorder packets */
  683. bool found_video = false;
  684. bool found_audio[MAX_AUDIO_MIXES] = {0};
  685. int64_t video_offset = 0;
  686. int64_t video_dts_offset = 0;
  687. int64_t audio_offsets[MAX_AUDIO_MIXES] = {0};
  688. int64_t audio_dts_offsets[MAX_AUDIO_MIXES] = {0};
  689. for (size_t i = 0; i < num_packets; i++) {
  690. struct encoder_packet *pkt;
  691. pkt = circlebuf_data(&stream->packets, i * size);
  692. if (pkt->type == OBS_ENCODER_VIDEO) {
  693. if (!found_video) {
  694. video_offset = pkt->dts_usec;
  695. video_dts_offset = pkt->dts;
  696. found_video = true;
  697. }
  698. } else {
  699. if (!found_audio[pkt->track_idx]) {
  700. found_audio[pkt->track_idx] = true;
  701. audio_offsets[pkt->track_idx] = pkt->dts_usec;
  702. audio_dts_offsets[pkt->track_idx] = pkt->dts;
  703. }
  704. }
  705. insert_packet(&stream->mux_packets.da, pkt, video_offset,
  706. audio_offsets, video_dts_offset,
  707. audio_dts_offsets);
  708. }
  709. /* ---------------------------- */
  710. /* generate filename */
  711. obs_data_t *settings = obs_output_get_settings(stream->output);
  712. const char *dir = obs_data_get_string(settings, "directory");
  713. const char *fmt = obs_data_get_string(settings, "format");
  714. const char *ext = obs_data_get_string(settings, "extension");
  715. bool space = obs_data_get_bool(settings, "allow_spaces");
  716. char *filename = os_generate_formatted_filename(ext, space, fmt);
  717. dstr_copy(&stream->path, dir);
  718. dstr_replace(&stream->path, "\\", "/");
  719. if (dstr_end(&stream->path) != '/')
  720. dstr_cat_ch(&stream->path, '/');
  721. dstr_cat(&stream->path, filename);
  722. char *slash = strrchr(stream->path.array, '/');
  723. if (slash) {
  724. *slash = 0;
  725. os_mkdirs(stream->path.array);
  726. *slash = '/';
  727. }
  728. bfree(filename);
  729. obs_data_release(settings);
  730. /* ---------------------------- */
  731. os_atomic_set_bool(&stream->muxing, true);
  732. stream->mux_thread_joinable = pthread_create(&stream->mux_thread, NULL,
  733. replay_buffer_mux_thread,
  734. stream) == 0;
  735. }
  736. static void deactivate_replay_buffer(struct ffmpeg_muxer *stream, int code)
  737. {
  738. if (code) {
  739. obs_output_signal_stop(stream->output, code);
  740. } else if (stopping(stream)) {
  741. obs_output_end_data_capture(stream->output);
  742. }
  743. os_atomic_set_bool(&stream->active, false);
  744. os_atomic_set_bool(&stream->sent_headers, false);
  745. os_atomic_set_bool(&stream->stopping, false);
  746. replay_buffer_clear(stream);
  747. }
  748. static void replay_buffer_data(void *data, struct encoder_packet *packet)
  749. {
  750. struct ffmpeg_muxer *stream = data;
  751. struct encoder_packet pkt;
  752. if (!active(stream))
  753. return;
  754. /* encoder failure */
  755. if (!packet) {
  756. deactivate_replay_buffer(stream, OBS_OUTPUT_ENCODE_ERROR);
  757. return;
  758. }
  759. if (stopping(stream)) {
  760. if (packet->sys_dts_usec >= stream->stop_ts) {
  761. deactivate_replay_buffer(stream, 0);
  762. return;
  763. }
  764. }
  765. obs_encoder_packet_ref(&pkt, packet);
  766. replay_buffer_purge(stream, &pkt);
  767. if (!stream->packets.size)
  768. stream->cur_time = pkt.dts_usec;
  769. stream->cur_size += pkt.size;
  770. circlebuf_push_back(&stream->packets, packet, sizeof(*packet));
  771. if (packet->type == OBS_ENCODER_VIDEO && packet->keyframe)
  772. stream->keyframes++;
  773. if (stream->save_ts && packet->sys_dts_usec >= stream->save_ts) {
  774. if (os_atomic_load_bool(&stream->muxing))
  775. return;
  776. if (stream->mux_thread_joinable) {
  777. pthread_join(stream->mux_thread, NULL);
  778. stream->mux_thread_joinable = false;
  779. }
  780. stream->save_ts = 0;
  781. replay_buffer_save(stream);
  782. }
  783. }
  784. static void replay_buffer_defaults(obs_data_t *s)
  785. {
  786. obs_data_set_default_int(s, "max_time_sec", 15);
  787. obs_data_set_default_int(s, "max_size_mb", 500);
  788. obs_data_set_default_string(s, "format", "%CCYY-%MM-%DD %hh-%mm-%ss");
  789. obs_data_set_default_string(s, "extension", "mp4");
  790. obs_data_set_default_bool(s, "allow_spaces", true);
  791. }
  792. struct obs_output_info replay_buffer = {
  793. .id = "replay_buffer",
  794. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_MULTI_TRACK |
  795. OBS_OUTPUT_CAN_PAUSE,
  796. .get_name = replay_buffer_getname,
  797. .create = replay_buffer_create,
  798. .destroy = replay_buffer_destroy,
  799. .start = replay_buffer_start,
  800. .stop = ffmpeg_mux_stop,
  801. .encoded_packet = replay_buffer_data,
  802. .get_total_bytes = ffmpeg_mux_total_bytes,
  803. .get_defaults = replay_buffer_defaults,
  804. };