1
0

obs-ffmpeg-mux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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-avc.h>
  16. #include <util/dstr.h>
  17. #include <util/pipe.h>
  18. #include <util/threading.h>
  19. #include "ffmpeg-mux/ffmpeg-mux.h"
  20. #include <libavformat/avformat.h>
  21. #define do_log(level, format, ...) \
  22. blog(level, "[ffmpeg muxer: '%s'] " format, \
  23. obs_output_get_name(stream->output), ##__VA_ARGS__)
  24. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  25. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  26. struct ffmpeg_muxer {
  27. obs_output_t *output;
  28. os_process_pipe_t *pipe;
  29. int64_t stop_ts;
  30. struct dstr path;
  31. bool sent_headers;
  32. volatile bool active;
  33. volatile bool stopping;
  34. volatile bool capturing;
  35. };
  36. static const char *ffmpeg_mux_getname(void *unused)
  37. {
  38. UNUSED_PARAMETER(unused);
  39. return obs_module_text("FFmpegMuxer");
  40. }
  41. static void ffmpeg_mux_destroy(void *data)
  42. {
  43. struct ffmpeg_muxer *stream = data;
  44. os_process_pipe_destroy(stream->pipe);
  45. dstr_free(&stream->path);
  46. bfree(stream);
  47. }
  48. static void *ffmpeg_mux_create(obs_data_t *settings, obs_output_t *output)
  49. {
  50. struct ffmpeg_muxer *stream = bzalloc(sizeof(*stream));
  51. stream->output = output;
  52. UNUSED_PARAMETER(settings);
  53. return stream;
  54. }
  55. #ifdef _WIN32
  56. #ifdef _WIN64
  57. #define FFMPEG_MUX "ffmpeg-mux64.exe"
  58. #else
  59. #define FFMPEG_MUX "ffmpeg-mux32.exe"
  60. #endif
  61. #else
  62. #define FFMPEG_MUX "ffmpeg-mux"
  63. #endif
  64. static inline bool capturing(struct ffmpeg_muxer *stream)
  65. {
  66. return os_atomic_load_bool(&stream->capturing);
  67. }
  68. static inline bool stopping(struct ffmpeg_muxer *stream)
  69. {
  70. return os_atomic_load_bool(&stream->stopping);
  71. }
  72. static inline bool active(struct ffmpeg_muxer *stream)
  73. {
  74. return os_atomic_load_bool(&stream->active);
  75. }
  76. /* TODO: allow codecs other than h264 whenever we start using them */
  77. static void add_video_encoder_params(struct ffmpeg_muxer *stream,
  78. struct dstr *cmd, obs_encoder_t *vencoder)
  79. {
  80. obs_data_t *settings = obs_encoder_get_settings(vencoder);
  81. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  82. video_t *video = obs_get_video();
  83. const struct video_output_info *info = video_output_get_info(video);
  84. obs_data_release(settings);
  85. dstr_catf(cmd, "%s %d %d %d %d %d ",
  86. "h264",
  87. bitrate,
  88. obs_output_get_width(stream->output),
  89. obs_output_get_height(stream->output),
  90. (int)info->fps_num,
  91. (int)info->fps_den);
  92. }
  93. static void add_audio_encoder_params(struct dstr *cmd, obs_encoder_t *aencoder)
  94. {
  95. obs_data_t *settings = obs_encoder_get_settings(aencoder);
  96. int bitrate = (int)obs_data_get_int(settings, "bitrate");
  97. audio_t *audio = obs_get_audio();
  98. struct dstr name = {0};
  99. obs_data_release(settings);
  100. dstr_copy(&name, obs_encoder_get_name(aencoder));
  101. dstr_replace(&name, "\"", "\"\"");
  102. dstr_catf(cmd, "\"%s\" %d %d %d ",
  103. name.array,
  104. bitrate,
  105. (int)obs_encoder_get_sample_rate(aencoder),
  106. (int)audio_output_get_channels(audio));
  107. dstr_free(&name);
  108. }
  109. static void log_muxer_params(struct ffmpeg_muxer *stream, const char *settings)
  110. {
  111. int ret;
  112. AVDictionary *dict = NULL;
  113. if ((ret = av_dict_parse_string(&dict, settings, "=", " ", 0))) {
  114. warn("Failed to parse muxer settings: %s\n%s",
  115. av_err2str(ret), settings);
  116. av_dict_free(&dict);
  117. return;
  118. }
  119. if (av_dict_count(dict) > 0) {
  120. struct dstr str = {0};
  121. AVDictionaryEntry *entry = NULL;
  122. while ((entry = av_dict_get(dict, "", entry,
  123. AV_DICT_IGNORE_SUFFIX)))
  124. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  125. info("Using muxer settings:%s", str.array);
  126. dstr_free(&str);
  127. }
  128. av_dict_free(&dict);
  129. }
  130. static void add_muxer_params(struct dstr *cmd, struct ffmpeg_muxer *stream)
  131. {
  132. obs_data_t *settings = obs_output_get_settings(stream->output);
  133. struct dstr mux = {0};
  134. dstr_copy(&mux, obs_data_get_string(settings, "muxer_settings"));
  135. log_muxer_params(stream, mux.array);
  136. dstr_replace(&mux, "\"", "\\\"");
  137. obs_data_release(settings);
  138. dstr_catf(cmd, "\"%s\" ", mux.array ? mux.array : "");
  139. dstr_free(&mux);
  140. }
  141. static void build_command_line(struct ffmpeg_muxer *stream, struct dstr *cmd)
  142. {
  143. obs_encoder_t *vencoder = obs_output_get_video_encoder(stream->output);
  144. obs_encoder_t *aencoders[MAX_AUDIO_MIXES];
  145. int num_tracks = 0;
  146. for (;;) {
  147. obs_encoder_t *aencoder = obs_output_get_audio_encoder(
  148. stream->output, num_tracks);
  149. if (!aencoder)
  150. break;
  151. aencoders[num_tracks] = aencoder;
  152. num_tracks++;
  153. }
  154. dstr_init_move_array(cmd, obs_module_file(FFMPEG_MUX));
  155. dstr_insert_ch(cmd, 0, '\"');
  156. dstr_cat(cmd, "\" \"");
  157. dstr_cat_dstr(cmd, &stream->path);
  158. dstr_catf(cmd, "\" %d %d ", vencoder ? 1 : 0, num_tracks);
  159. if (vencoder)
  160. add_video_encoder_params(stream, cmd, vencoder);
  161. if (num_tracks) {
  162. dstr_cat(cmd, "aac ");
  163. for (int i = 0; i < num_tracks; i++) {
  164. add_audio_encoder_params(cmd, aencoders[i]);
  165. }
  166. }
  167. add_muxer_params(cmd, stream);
  168. }
  169. static bool ffmpeg_mux_start(void *data)
  170. {
  171. struct ffmpeg_muxer *stream = data;
  172. obs_data_t *settings;
  173. struct dstr cmd;
  174. const char *path;
  175. if (!obs_output_can_begin_data_capture(stream->output, 0))
  176. return false;
  177. if (!obs_output_initialize_encoders(stream->output, 0))
  178. return false;
  179. settings = obs_output_get_settings(stream->output);
  180. path = obs_data_get_string(settings, "path");
  181. dstr_copy(&stream->path, path);
  182. dstr_replace(&stream->path, "\"", "\"\"");
  183. obs_data_release(settings);
  184. build_command_line(stream, &cmd);
  185. stream->pipe = os_process_pipe_create(cmd.array, "w");
  186. dstr_free(&cmd);
  187. if (!stream->pipe) {
  188. warn("Failed to create process pipe");
  189. return false;
  190. }
  191. /* write headers and start capture */
  192. os_atomic_set_bool(&stream->active, true);
  193. os_atomic_set_bool(&stream->capturing, true);
  194. obs_output_begin_data_capture(stream->output, 0);
  195. info("Writing file '%s'...", stream->path.array);
  196. return true;
  197. }
  198. static int deactivate(struct ffmpeg_muxer *stream)
  199. {
  200. int ret = -1;
  201. if (active(stream)) {
  202. ret = os_process_pipe_destroy(stream->pipe);
  203. stream->pipe = NULL;
  204. os_atomic_set_bool(&stream->active, false);
  205. os_atomic_set_bool(&stream->sent_headers, false);
  206. info("Output of file '%s' stopped", stream->path.array);
  207. }
  208. if (stopping(stream))
  209. obs_output_end_data_capture(stream->output);
  210. os_atomic_set_bool(&stream->stopping, false);
  211. return ret;
  212. }
  213. static void ffmpeg_mux_stop(void *data, uint64_t ts)
  214. {
  215. struct ffmpeg_muxer *stream = data;
  216. if (capturing(stream) || ts == 0) {
  217. stream->stop_ts = (int64_t)ts / 1000LL;
  218. os_atomic_set_bool(&stream->stopping, true);
  219. os_atomic_set_bool(&stream->capturing, false);
  220. }
  221. }
  222. static void signal_failure(struct ffmpeg_muxer *stream)
  223. {
  224. int ret = deactivate(stream);
  225. int code;
  226. switch (ret) {
  227. case FFM_UNSUPPORTED: code = OBS_OUTPUT_UNSUPPORTED; break;
  228. default: code = OBS_OUTPUT_ERROR;
  229. }
  230. obs_output_signal_stop(stream->output, code);
  231. os_atomic_set_bool(&stream->capturing, false);
  232. }
  233. static bool write_packet(struct ffmpeg_muxer *stream,
  234. struct encoder_packet *packet)
  235. {
  236. bool is_video = packet->type == OBS_ENCODER_VIDEO;
  237. size_t ret;
  238. struct ffm_packet_info info = {
  239. .pts = packet->pts,
  240. .dts = packet->dts,
  241. .size = (uint32_t)packet->size,
  242. .index = (int)packet->track_idx,
  243. .type = is_video ? FFM_PACKET_VIDEO : FFM_PACKET_AUDIO,
  244. .keyframe = packet->keyframe
  245. };
  246. ret = os_process_pipe_write(stream->pipe, (const uint8_t*)&info,
  247. sizeof(info));
  248. if (ret != sizeof(info)) {
  249. warn("os_process_pipe_write for info structure failed");
  250. signal_failure(stream);
  251. return false;
  252. }
  253. ret = os_process_pipe_write(stream->pipe, packet->data, packet->size);
  254. if (ret != packet->size) {
  255. warn("os_process_pipe_write for packet data failed");
  256. signal_failure(stream);
  257. return false;
  258. }
  259. return true;
  260. }
  261. static bool send_audio_headers(struct ffmpeg_muxer *stream,
  262. obs_encoder_t *aencoder, size_t idx)
  263. {
  264. struct encoder_packet packet = {
  265. .type = OBS_ENCODER_AUDIO,
  266. .timebase_den = 1,
  267. .track_idx = idx
  268. };
  269. obs_encoder_get_extra_data(aencoder, &packet.data, &packet.size);
  270. return write_packet(stream, &packet);
  271. }
  272. static bool send_video_headers(struct ffmpeg_muxer *stream)
  273. {
  274. obs_encoder_t *vencoder = obs_output_get_video_encoder(stream->output);
  275. struct encoder_packet packet = {
  276. .type = OBS_ENCODER_VIDEO,
  277. .timebase_den = 1
  278. };
  279. obs_encoder_get_extra_data(vencoder, &packet.data, &packet.size);
  280. return write_packet(stream, &packet);
  281. }
  282. static bool send_headers(struct ffmpeg_muxer *stream)
  283. {
  284. obs_encoder_t *aencoder;
  285. size_t idx = 0;
  286. if (!send_video_headers(stream))
  287. return false;
  288. do {
  289. aencoder = obs_output_get_audio_encoder(stream->output, idx);
  290. if (aencoder) {
  291. if (!send_audio_headers(stream, aencoder, idx)) {
  292. return false;
  293. }
  294. idx++;
  295. }
  296. } while (aencoder);
  297. return true;
  298. }
  299. static void ffmpeg_mux_data(void *data, struct encoder_packet *packet)
  300. {
  301. struct ffmpeg_muxer *stream = data;
  302. if (!active(stream))
  303. return;
  304. if (!stream->sent_headers) {
  305. if (!send_headers(stream))
  306. return;
  307. stream->sent_headers = true;
  308. }
  309. if (stopping(stream)) {
  310. if (packet->sys_dts_usec >= stream->stop_ts) {
  311. deactivate(stream);
  312. return;
  313. }
  314. }
  315. write_packet(stream, packet);
  316. }
  317. static obs_properties_t *ffmpeg_mux_properties(void *unused)
  318. {
  319. UNUSED_PARAMETER(unused);
  320. obs_properties_t *props = obs_properties_create();
  321. obs_properties_add_text(props, "path",
  322. obs_module_text("FilePath"),
  323. OBS_TEXT_DEFAULT);
  324. return props;
  325. }
  326. struct obs_output_info ffmpeg_muxer = {
  327. .id = "ffmpeg_muxer",
  328. .flags = OBS_OUTPUT_AV |
  329. OBS_OUTPUT_ENCODED |
  330. OBS_OUTPUT_MULTI_TRACK,
  331. .get_name = ffmpeg_mux_getname,
  332. .create = ffmpeg_mux_create,
  333. .destroy = ffmpeg_mux_destroy,
  334. .start = ffmpeg_mux_start,
  335. .stop = ffmpeg_mux_stop,
  336. .encoded_packet = ffmpeg_mux_data,
  337. .get_properties = ffmpeg_mux_properties
  338. };