flv-output.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /******************************************************************************
  2. Copyright (C) 2014 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 <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. bool active;
  32. bool sent_headers;
  33. int64_t last_packet_ts;
  34. };
  35. static const char *flv_output_getname(void *unused)
  36. {
  37. UNUSED_PARAMETER(unused);
  38. return obs_module_text("FLVOutput");
  39. }
  40. static void flv_output_stop(void *data);
  41. static void flv_output_destroy(void *data)
  42. {
  43. struct flv_output *stream = data;
  44. if (stream->active)
  45. flv_output_stop(data);
  46. dstr_free(&stream->path);
  47. bfree(stream);
  48. }
  49. static void *flv_output_create(obs_data_t *settings, obs_output_t *output)
  50. {
  51. struct flv_output *stream = bzalloc(sizeof(struct flv_output));
  52. stream->output = output;
  53. UNUSED_PARAMETER(settings);
  54. return stream;
  55. }
  56. static void flv_output_stop(void *data)
  57. {
  58. struct flv_output *stream = data;
  59. if (stream->active) {
  60. if (stream->file)
  61. write_file_info(stream->file, stream->last_packet_ts,
  62. os_ftelli64(stream->file));
  63. fclose(stream->file);
  64. obs_output_end_data_capture(stream->output);
  65. stream->active = false;
  66. stream->sent_headers = false;
  67. info("FLV file output complete");
  68. }
  69. }
  70. static int write_packet(struct flv_output *stream,
  71. struct encoder_packet *packet, bool is_header)
  72. {
  73. uint8_t *data;
  74. size_t size;
  75. int ret = 0;
  76. stream->last_packet_ts = get_ms_time(packet, packet->dts);
  77. flv_packet_mux(packet, &data, &size, is_header);
  78. fwrite(data, 1, size, stream->file);
  79. bfree(data);
  80. obs_free_encoder_packet(packet);
  81. return ret;
  82. }
  83. static void write_meta_data(struct flv_output *stream)
  84. {
  85. uint8_t *meta_data;
  86. size_t meta_data_size;
  87. flv_meta_data(stream->output, &meta_data, &meta_data_size, true, 0);
  88. fwrite(meta_data, 1, meta_data_size, stream->file);
  89. bfree(meta_data);
  90. }
  91. static void write_audio_header(struct flv_output *stream)
  92. {
  93. obs_output_t *context = stream->output;
  94. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  95. uint8_t *header;
  96. struct encoder_packet packet = {
  97. .type = OBS_ENCODER_AUDIO,
  98. .timebase_den = 1
  99. };
  100. obs_encoder_get_extra_data(aencoder, &header, &packet.size);
  101. packet.data = bmemdup(header, packet.size);
  102. write_packet(stream, &packet, true);
  103. }
  104. static void write_video_header(struct flv_output *stream)
  105. {
  106. obs_output_t *context = stream->output;
  107. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  108. uint8_t *header;
  109. size_t size;
  110. struct encoder_packet packet = {
  111. .type = OBS_ENCODER_VIDEO,
  112. .timebase_den = 1,
  113. .keyframe = true
  114. };
  115. obs_encoder_get_extra_data(vencoder, &header, &size);
  116. packet.size = obs_parse_avc_header(&packet.data, header, size);
  117. write_packet(stream, &packet, true);
  118. }
  119. static void write_headers(struct flv_output *stream)
  120. {
  121. write_meta_data(stream);
  122. write_audio_header(stream);
  123. write_video_header(stream);
  124. }
  125. static bool flv_output_start(void *data)
  126. {
  127. struct flv_output *stream = data;
  128. obs_data_t *settings;
  129. const char *path;
  130. if (!obs_output_can_begin_data_capture(stream->output, 0))
  131. return false;
  132. if (!obs_output_initialize_encoders(stream->output, 0))
  133. return false;
  134. /* get path */
  135. settings = obs_output_get_settings(stream->output);
  136. path = obs_data_get_string(settings, "path");
  137. dstr_copy(&stream->path, path);
  138. obs_data_release(settings);
  139. stream->file = os_fopen(stream->path.array, "wb");
  140. if (!stream->file) {
  141. warn("Unable to open FLV file '%s'", stream->path.array);
  142. return false;
  143. }
  144. /* write headers and start capture */
  145. stream->active = true;
  146. obs_output_begin_data_capture(stream->output, 0);
  147. info("Writing FLV file '%s'...", stream->path.array);
  148. return true;
  149. }
  150. static void flv_output_data(void *data, struct encoder_packet *packet)
  151. {
  152. struct flv_output *stream = data;
  153. struct encoder_packet parsed_packet;
  154. if (!stream->sent_headers) {
  155. write_headers(stream);
  156. stream->sent_headers = true;
  157. }
  158. if (packet->type == OBS_ENCODER_VIDEO) {
  159. obs_parse_avc_packet(&parsed_packet, packet);
  160. write_packet(stream, &parsed_packet, false);
  161. obs_free_encoder_packet(&parsed_packet);
  162. } else {
  163. write_packet(stream, packet, false);
  164. }
  165. }
  166. static obs_properties_t *flv_output_properties(void *unused)
  167. {
  168. UNUSED_PARAMETER(unused);
  169. obs_properties_t *props = obs_properties_create();
  170. obs_properties_add_text(props, "path",
  171. obs_module_text("FLVOutput.FilePath"),
  172. OBS_TEXT_DEFAULT);
  173. return props;
  174. }
  175. struct obs_output_info flv_output_info = {
  176. .id = "flv_output",
  177. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED,
  178. .get_name = flv_output_getname,
  179. .create = flv_output_create,
  180. .destroy = flv_output_destroy,
  181. .start = flv_output_start,
  182. .stop = flv_output_stop,
  183. .encoded_packet = flv_output_data,
  184. .get_properties = flv_output_properties
  185. };