flv-mux.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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,
  45. uint8_t **output, 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",
  83. audio_output_get_channels(audio) == 3);
  84. enc_bool_val(&enc, end, "3.1",
  85. audio_output_get_channels(audio) == 4);
  86. enc_bool_val(&enc, end, "4.0",
  87. audio_output_get_channels(audio) == 4);
  88. enc_bool_val(&enc, end, "4.1",
  89. audio_output_get_channels(audio) == 5);
  90. enc_bool_val(&enc, end, "5.1",
  91. audio_output_get_channels(audio) == 6);
  92. enc_bool_val(&enc, end, "7.1",
  93. audio_output_get_channels(audio) == 8);
  94. dstr_printf(&encoder_name, "%s (libobs version ",
  95. MODULE_NAME);
  96. #ifdef HAVE_OBSCONFIG_H
  97. dstr_cat(&encoder_name, OBS_VERSION);
  98. #else
  99. dstr_catf(&encoder_name, "%d.%d.%d",
  100. LIBOBS_API_MAJOR_VER,
  101. LIBOBS_API_MINOR_VER,
  102. LIBOBS_API_PATCH_VER);
  103. #endif
  104. dstr_cat(&encoder_name, ")");
  105. enc_str_val(&enc, end, "encoder", encoder_name.array);
  106. dstr_free(&encoder_name);
  107. *enc++ = 0;
  108. *enc++ = 0;
  109. *enc++ = AMF_OBJECT_END;
  110. *size = enc-buf;
  111. *output = bmemdup(buf, *size);
  112. return true;
  113. }
  114. bool flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  115. bool write_header, size_t audio_idx)
  116. {
  117. struct array_output_data data;
  118. struct serializer s;
  119. uint8_t *meta_data = NULL;
  120. size_t meta_data_size;
  121. uint32_t start_pos;
  122. array_output_serializer_init(&s, &data);
  123. if (!build_flv_meta_data(context, &meta_data, &meta_data_size,
  124. audio_idx)) {
  125. bfree(meta_data);
  126. return false;
  127. }
  128. if (write_header) {
  129. s_write(&s, "FLV", 3);
  130. s_w8(&s, 1);
  131. s_w8(&s, 5);
  132. s_wb32(&s, 9);
  133. s_wb32(&s, 0);
  134. }
  135. start_pos = serializer_get_pos(&s);
  136. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  137. s_wb24(&s, (uint32_t)meta_data_size);
  138. s_wb32(&s, 0);
  139. s_wb24(&s, 0);
  140. s_write(&s, meta_data, meta_data_size);
  141. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1);
  142. *output = data.bytes.array;
  143. *size = data.bytes.num;
  144. bfree(meta_data);
  145. return true;
  146. }
  147. #ifdef DEBUG_TIMESTAMPS
  148. static int32_t last_time = 0;
  149. #endif
  150. static void flv_video(struct serializer *s, int32_t dts_offset,
  151. struct encoder_packet *packet, bool is_header)
  152. {
  153. int64_t offset = packet->pts - packet->dts;
  154. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  155. if (!packet->data || !packet->size)
  156. return;
  157. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  158. #ifdef DEBUG_TIMESTAMPS
  159. blog(LOG_DEBUG, "Video: %lu", time_ms);
  160. if (last_time > time_ms)
  161. blog(LOG_DEBUG, "Non-monotonic");
  162. last_time = time_ms;
  163. #endif
  164. s_wb24(s, (uint32_t)packet->size + 5);
  165. s_wb24(s, time_ms);
  166. s_w8(s, (time_ms >> 24) & 0x7F);
  167. s_wb24(s, 0);
  168. /* these are the 5 extra bytes mentioned above */
  169. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  170. s_w8(s, is_header ? 0 : 1);
  171. s_wb24(s, get_ms_time(packet, offset));
  172. s_write(s, packet->data, packet->size);
  173. /* write tag size (starting byte doesn't count) */
  174. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  175. }
  176. static void flv_audio(struct serializer *s, int32_t dts_offset,
  177. struct encoder_packet *packet, bool is_header)
  178. {
  179. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  180. if (!packet->data || !packet->size)
  181. return;
  182. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  183. #ifdef DEBUG_TIMESTAMPS
  184. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  185. if (last_time > time_ms)
  186. blog(LOG_DEBUG, "Non-monotonic");
  187. last_time = time_ms;
  188. #endif
  189. s_wb24(s, (uint32_t)packet->size + 2);
  190. s_wb24(s, time_ms);
  191. s_w8(s, (time_ms >> 24) & 0x7F);
  192. s_wb24(s, 0);
  193. /* these are the two extra bytes mentioned above */
  194. s_w8(s, 0xaf);
  195. s_w8(s, is_header ? 0 : 1);
  196. s_write(s, packet->data, packet->size);
  197. /* write tag size (starting byte doesn't count) */
  198. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  199. }
  200. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  201. uint8_t **output, size_t *size, bool is_header)
  202. {
  203. struct array_output_data data;
  204. struct serializer s;
  205. array_output_serializer_init(&s, &data);
  206. if (packet->type == OBS_ENCODER_VIDEO)
  207. flv_video(&s, dts_offset, packet, is_header);
  208. else
  209. flv_audio(&s, dts_offset, packet, is_header);
  210. *output = data.bytes.array;
  211. *size = data.bytes.num;
  212. }