flv-output.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 <stdio.h>
  15. #include <obs-module.h>
  16. #include <obs-avc.h>
  17. #include <util/platform.h>
  18. #include <util/dstr.h>
  19. #include <util/threading.h>
  20. #include <inttypes.h>
  21. #include "flv-mux.h"
  22. #define do_log(level, format, ...) \
  23. blog(level, "[flv output: '%s'] " format, \
  24. obs_output_get_name(stream->output), ##__VA_ARGS__)
  25. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  26. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  27. struct flv_output {
  28. obs_output_t *output;
  29. struct dstr path;
  30. FILE *file;
  31. volatile bool active;
  32. volatile bool stopping;
  33. uint64_t stop_ts;
  34. bool sent_headers;
  35. int64_t last_packet_ts;
  36. pthread_mutex_t mutex;
  37. bool got_first_video;
  38. int32_t start_dts_offset;
  39. };
  40. static inline bool stopping(struct flv_output *stream)
  41. {
  42. return os_atomic_load_bool(&stream->stopping);
  43. }
  44. static inline bool active(struct flv_output *stream)
  45. {
  46. return os_atomic_load_bool(&stream->active);
  47. }
  48. static const char *flv_output_getname(void *unused)
  49. {
  50. UNUSED_PARAMETER(unused);
  51. return obs_module_text("FLVOutput");
  52. }
  53. static void flv_output_stop(void *data, uint64_t ts);
  54. static void flv_output_destroy(void *data)
  55. {
  56. struct flv_output *stream = data;
  57. pthread_mutex_destroy(&stream->mutex);
  58. dstr_free(&stream->path);
  59. bfree(stream);
  60. }
  61. static void *flv_output_create(obs_data_t *settings, obs_output_t *output)
  62. {
  63. struct flv_output *stream = bzalloc(sizeof(struct flv_output));
  64. stream->output = output;
  65. pthread_mutex_init(&stream->mutex, NULL);
  66. UNUSED_PARAMETER(settings);
  67. return stream;
  68. }
  69. static int write_packet(struct flv_output *stream,
  70. struct encoder_packet *packet, bool is_header)
  71. {
  72. uint8_t *data;
  73. size_t size;
  74. int ret = 0;
  75. stream->last_packet_ts = get_ms_time(packet, packet->dts);
  76. flv_packet_mux(packet, is_header ? 0 : stream->start_dts_offset, &data,
  77. &size, is_header);
  78. fwrite(data, 1, size, stream->file);
  79. bfree(data);
  80. return ret;
  81. }
  82. static void write_meta_data(struct flv_output *stream)
  83. {
  84. uint8_t *meta_data;
  85. size_t meta_data_size;
  86. flv_meta_data(stream->output, &meta_data, &meta_data_size, true);
  87. fwrite(meta_data, 1, meta_data_size, stream->file);
  88. bfree(meta_data);
  89. }
  90. static void write_audio_header(struct flv_output *stream)
  91. {
  92. obs_output_t *context = stream->output;
  93. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  94. struct encoder_packet packet = {.type = OBS_ENCODER_AUDIO,
  95. .timebase_den = 1};
  96. if (!obs_encoder_get_extra_data(aencoder, &packet.data, &packet.size))
  97. return;
  98. write_packet(stream, &packet, true);
  99. }
  100. static void write_video_header(struct flv_output *stream)
  101. {
  102. obs_output_t *context = stream->output;
  103. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  104. uint8_t *header;
  105. size_t size;
  106. struct encoder_packet packet = {.type = OBS_ENCODER_VIDEO,
  107. .timebase_den = 1,
  108. .keyframe = true};
  109. if (!obs_encoder_get_extra_data(vencoder, &header, &size))
  110. return;
  111. packet.size = obs_parse_avc_header(&packet.data, header, size);
  112. write_packet(stream, &packet, true);
  113. bfree(packet.data);
  114. }
  115. static void write_headers(struct flv_output *stream)
  116. {
  117. write_meta_data(stream);
  118. write_video_header(stream);
  119. write_audio_header(stream);
  120. }
  121. static bool flv_output_start(void *data)
  122. {
  123. struct flv_output *stream = data;
  124. obs_data_t *settings;
  125. const char *path;
  126. if (!obs_output_can_begin_data_capture(stream->output, 0))
  127. return false;
  128. if (!obs_output_initialize_encoders(stream->output, 0))
  129. return false;
  130. stream->got_first_video = false;
  131. stream->sent_headers = false;
  132. os_atomic_set_bool(&stream->stopping, false);
  133. /* get path */
  134. settings = obs_output_get_settings(stream->output);
  135. path = obs_data_get_string(settings, "path");
  136. dstr_copy(&stream->path, path);
  137. obs_data_release(settings);
  138. stream->file = os_fopen(stream->path.array, "wb");
  139. if (!stream->file) {
  140. warn("Unable to open FLV file '%s'", stream->path.array);
  141. return false;
  142. }
  143. /* write headers and start capture */
  144. os_atomic_set_bool(&stream->active, true);
  145. obs_output_begin_data_capture(stream->output, 0);
  146. info("Writing FLV file '%s'...", stream->path.array);
  147. return true;
  148. }
  149. static void flv_output_stop(void *data, uint64_t ts)
  150. {
  151. struct flv_output *stream = data;
  152. stream->stop_ts = ts / 1000;
  153. os_atomic_set_bool(&stream->stopping, true);
  154. }
  155. static void flv_output_actual_stop(struct flv_output *stream, int code)
  156. {
  157. os_atomic_set_bool(&stream->active, false);
  158. if (stream->file) {
  159. write_file_info(stream->file, stream->last_packet_ts,
  160. os_ftelli64(stream->file));
  161. fclose(stream->file);
  162. }
  163. if (code) {
  164. obs_output_signal_stop(stream->output, code);
  165. } else {
  166. obs_output_end_data_capture(stream->output);
  167. }
  168. info("FLV file output complete");
  169. }
  170. static void flv_output_data(void *data, struct encoder_packet *packet)
  171. {
  172. struct flv_output *stream = data;
  173. struct encoder_packet parsed_packet;
  174. pthread_mutex_lock(&stream->mutex);
  175. if (!active(stream))
  176. goto unlock;
  177. if (!packet) {
  178. flv_output_actual_stop(stream, OBS_OUTPUT_ENCODE_ERROR);
  179. goto unlock;
  180. }
  181. if (stopping(stream)) {
  182. if (packet->sys_dts_usec >= (int64_t)stream->stop_ts) {
  183. flv_output_actual_stop(stream, 0);
  184. goto unlock;
  185. }
  186. }
  187. if (!stream->sent_headers) {
  188. write_headers(stream);
  189. stream->sent_headers = true;
  190. }
  191. if (packet->type == OBS_ENCODER_VIDEO) {
  192. if (!stream->got_first_video) {
  193. stream->start_dts_offset =
  194. get_ms_time(packet, packet->dts);
  195. stream->got_first_video = true;
  196. }
  197. obs_parse_avc_packet(&parsed_packet, packet);
  198. write_packet(stream, &parsed_packet, false);
  199. obs_encoder_packet_release(&parsed_packet);
  200. } else {
  201. write_packet(stream, packet, false);
  202. }
  203. unlock:
  204. pthread_mutex_unlock(&stream->mutex);
  205. }
  206. static obs_properties_t *flv_output_properties(void *unused)
  207. {
  208. UNUSED_PARAMETER(unused);
  209. obs_properties_t *props = obs_properties_create();
  210. obs_properties_add_text(props, "path",
  211. obs_module_text("FLVOutput.FilePath"),
  212. OBS_TEXT_DEFAULT);
  213. return props;
  214. }
  215. struct obs_output_info flv_output_info = {
  216. .id = "flv_output",
  217. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED,
  218. .encoded_video_codecs = "h264",
  219. .encoded_audio_codecs = "aac",
  220. .get_name = flv_output_getname,
  221. .create = flv_output_create,
  222. .destroy = flv_output_destroy,
  223. .start = flv_output_start,
  224. .stop = flv_output_stop,
  225. .encoded_packet = flv_output_data,
  226. .get_properties = flv_output_properties,
  227. };