flv-mux.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <obs.h>
  15. #include <stdio.h>
  16. #include <util/dstr.h>
  17. #include <util/array-serializer.h>
  18. #include "flv-mux.h"
  19. #include "obs-output-ver.h"
  20. #include "rtmp-helpers.h"
  21. /* TODO: FIXME: this is currently hard-coded to h264 and aac! ..not that we'll
  22. * use anything else for a long time. */
  23. //#define DEBUG_TIMESTAMPS
  24. //#define WRITE_FLV_HEADER
  25. #define VIDEO_HEADER_SIZE 5
  26. static inline double encoder_bitrate(obs_encoder_t *encoder)
  27. {
  28. obs_data_t *settings = obs_encoder_get_settings(encoder);
  29. double bitrate = obs_data_get_double(settings, "bitrate");
  30. obs_data_release(settings);
  31. return bitrate;
  32. }
  33. #define FLV_INFO_SIZE_OFFSET 42
  34. void write_file_info(FILE *file, int64_t duration_ms, int64_t size)
  35. {
  36. char buf[64];
  37. char *enc = buf;
  38. char *end = enc + sizeof(buf);
  39. fseek(file, FLV_INFO_SIZE_OFFSET, SEEK_SET);
  40. enc_num_val(&enc, end, "duration", (double)duration_ms / 1000.0);
  41. enc_num_val(&enc, end, "fileSize", (double)size);
  42. fwrite(buf, 1, enc - buf, file);
  43. }
  44. static bool build_flv_meta_data(obs_output_t *context, uint8_t **output,
  45. size_t *size, size_t a_idx)
  46. {
  47. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  48. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, a_idx);
  49. video_t *video = obs_encoder_video(vencoder);
  50. audio_t *audio = obs_encoder_audio(aencoder);
  51. char buf[4096];
  52. char *enc = buf;
  53. char *end = enc + sizeof(buf);
  54. struct dstr encoder_name = {0};
  55. if (a_idx > 0 && !aencoder)
  56. return false;
  57. enc_str(&enc, end, "onMetaData");
  58. *enc++ = AMF_ECMA_ARRAY;
  59. enc = AMF_EncodeInt32(enc, end, a_idx == 0 ? 20 : 15);
  60. enc_num_val(&enc, end, "duration", 0.0);
  61. enc_num_val(&enc, end, "fileSize", 0.0);
  62. if (a_idx == 0) {
  63. enc_num_val(&enc, end, "width",
  64. (double)obs_encoder_get_width(vencoder));
  65. enc_num_val(&enc, end, "height",
  66. (double)obs_encoder_get_height(vencoder));
  67. enc_str_val(&enc, end, "videocodecid", "avc1");
  68. enc_num_val(&enc, end, "videodatarate",
  69. encoder_bitrate(vencoder));
  70. enc_num_val(&enc, end, "framerate",
  71. video_output_get_frame_rate(video));
  72. }
  73. enc_str_val(&enc, end, "audiocodecid", "mp4a");
  74. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  75. enc_num_val(&enc, end, "audiosamplerate",
  76. (double)obs_encoder_get_sample_rate(aencoder));
  77. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  78. enc_num_val(&enc, end, "audiochannels",
  79. (double)audio_output_get_channels(audio));
  80. enc_bool_val(&enc, end, "stereo",
  81. audio_output_get_channels(audio) == 2);
  82. enc_bool_val(&enc, end, "2.1", audio_output_get_channels(audio) == 3);
  83. enc_bool_val(&enc, end, "3.1", audio_output_get_channels(audio) == 4);
  84. enc_bool_val(&enc, end, "4.0", audio_output_get_channels(audio) == 4);
  85. enc_bool_val(&enc, end, "4.1", audio_output_get_channels(audio) == 5);
  86. enc_bool_val(&enc, end, "5.1", audio_output_get_channels(audio) == 6);
  87. enc_bool_val(&enc, end, "7.1", audio_output_get_channels(audio) == 8);
  88. dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME);
  89. #ifdef HAVE_OBSCONFIG_H
  90. dstr_cat(&encoder_name, OBS_VERSION);
  91. #else
  92. dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER,
  93. LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER);
  94. #endif
  95. dstr_cat(&encoder_name, ")");
  96. enc_str_val(&enc, end, "encoder", encoder_name.array);
  97. dstr_free(&encoder_name);
  98. *enc++ = 0;
  99. *enc++ = 0;
  100. *enc++ = AMF_OBJECT_END;
  101. *size = enc - buf;
  102. *output = bmemdup(buf, *size);
  103. return true;
  104. }
  105. bool flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  106. bool write_header, size_t audio_idx)
  107. {
  108. struct array_output_data data;
  109. struct serializer s;
  110. uint8_t *meta_data = NULL;
  111. size_t meta_data_size;
  112. uint32_t start_pos;
  113. array_output_serializer_init(&s, &data);
  114. if (!build_flv_meta_data(context, &meta_data, &meta_data_size,
  115. audio_idx)) {
  116. bfree(meta_data);
  117. return false;
  118. }
  119. if (write_header) {
  120. s_write(&s, "FLV", 3);
  121. s_w8(&s, 1);
  122. s_w8(&s, 5);
  123. s_wb32(&s, 9);
  124. s_wb32(&s, 0);
  125. }
  126. start_pos = serializer_get_pos(&s);
  127. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  128. s_wb24(&s, (uint32_t)meta_data_size);
  129. s_wb32(&s, 0);
  130. s_wb24(&s, 0);
  131. s_write(&s, meta_data, meta_data_size);
  132. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1);
  133. *output = data.bytes.array;
  134. *size = data.bytes.num;
  135. bfree(meta_data);
  136. return true;
  137. }
  138. #ifdef DEBUG_TIMESTAMPS
  139. static int32_t last_time = 0;
  140. #endif
  141. static void flv_video(struct serializer *s, int32_t dts_offset,
  142. struct encoder_packet *packet, bool is_header)
  143. {
  144. int64_t offset = packet->pts - packet->dts;
  145. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  146. if (!packet->data || !packet->size)
  147. return;
  148. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  149. #ifdef DEBUG_TIMESTAMPS
  150. blog(LOG_DEBUG, "Video: %lu", time_ms);
  151. if (last_time > time_ms)
  152. blog(LOG_DEBUG, "Non-monotonic");
  153. last_time = time_ms;
  154. #endif
  155. s_wb24(s, (uint32_t)packet->size + 5);
  156. s_wb24(s, time_ms);
  157. s_w8(s, (time_ms >> 24) & 0x7F);
  158. s_wb24(s, 0);
  159. /* these are the 5 extra bytes mentioned above */
  160. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  161. s_w8(s, is_header ? 0 : 1);
  162. s_wb24(s, get_ms_time(packet, offset));
  163. s_write(s, packet->data, packet->size);
  164. /* write tag size (starting byte doesn't count) */
  165. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  166. }
  167. static void flv_audio(struct serializer *s, int32_t dts_offset,
  168. struct encoder_packet *packet, bool is_header)
  169. {
  170. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  171. if (!packet->data || !packet->size)
  172. return;
  173. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  174. #ifdef DEBUG_TIMESTAMPS
  175. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  176. if (last_time > time_ms)
  177. blog(LOG_DEBUG, "Non-monotonic");
  178. last_time = time_ms;
  179. #endif
  180. s_wb24(s, (uint32_t)packet->size + 2);
  181. s_wb24(s, time_ms);
  182. s_w8(s, (time_ms >> 24) & 0x7F);
  183. s_wb24(s, 0);
  184. /* these are the two extra bytes mentioned above */
  185. s_w8(s, 0xaf);
  186. s_w8(s, is_header ? 0 : 1);
  187. s_write(s, packet->data, packet->size);
  188. /* write tag size (starting byte doesn't count) */
  189. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  190. }
  191. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  192. uint8_t **output, size_t *size, bool is_header)
  193. {
  194. struct array_output_data data;
  195. struct serializer s;
  196. array_output_serializer_init(&s, &data);
  197. if (packet->type == OBS_ENCODER_VIDEO)
  198. flv_video(&s, dts_offset, packet, is_header);
  199. else
  200. flv_audio(&s, dts_offset, packet, is_header);
  201. *output = data.bytes.array;
  202. *size = data.bytes.num;
  203. }