flv-output.c 5.4 KB

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