flv-mux.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 void build_flv_meta_data(obs_output_t *context,
  45. uint8_t **output, size_t *size)
  46. {
  47. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  48. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context);
  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. enc_str(&enc, end, "onMetaData");
  56. *enc++ = AMF_ECMA_ARRAY;
  57. enc = AMF_EncodeInt32(enc, end, 14);
  58. enc_num_val(&enc, end, "duration", 0.0);
  59. enc_num_val(&enc, end, "fileSize", 0.0);
  60. enc_num_val(&enc, end, "width",
  61. (double)obs_encoder_get_width(vencoder));
  62. enc_num_val(&enc, end, "height",
  63. (double)obs_encoder_get_height(vencoder));
  64. enc_str_val(&enc, end, "videocodecid", "avc1");
  65. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  66. enc_num_val(&enc, end, "framerate", video_output_get_frame_rate(video));
  67. enc_str_val(&enc, end, "audiocodecid", "mp4a");
  68. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  69. enc_num_val(&enc, end, "audiosamplerate",
  70. (double)audio_output_get_sample_rate(audio));
  71. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  72. enc_num_val(&enc, end, "audiochannels",
  73. (double)audio_output_get_channels(audio));
  74. enc_bool_val(&enc, end, "stereo",
  75. audio_output_get_channels(audio) == 2);
  76. dstr_printf(&encoder_name, "%s (libobs version ",
  77. MODULE_NAME);
  78. #ifdef HAVE_OBSCONFIG_H
  79. dstr_cat(&encoder_name, OBS_VERSION);
  80. #else
  81. dstr_catf(&encoder_name, "%d.%d.%d",
  82. LIBOBS_API_MAJOR_VER,
  83. LIBOBS_API_MINOR_VER,
  84. LIBOBS_API_PATCH_VER);
  85. #endif
  86. dstr_cat(&encoder_name, ")");
  87. enc_str_val(&enc, end, "encoder", encoder_name.array);
  88. dstr_free(&encoder_name);
  89. *enc++ = 0;
  90. *enc++ = 0;
  91. *enc++ = AMF_OBJECT_END;
  92. *size = enc-buf;
  93. *output = bmemdup(buf, *size);
  94. }
  95. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  96. bool write_header)
  97. {
  98. struct array_output_data data;
  99. struct serializer s;
  100. uint8_t *meta_data;
  101. size_t meta_data_size;
  102. uint32_t start_pos;
  103. array_output_serializer_init(&s, &data);
  104. build_flv_meta_data(context, &meta_data, &meta_data_size);
  105. if (write_header) {
  106. s_write(&s, "FLV", 3);
  107. s_w8(&s, 1);
  108. s_w8(&s, 5);
  109. s_wb32(&s, 9);
  110. s_wb32(&s, 0);
  111. }
  112. start_pos = serializer_get_pos(&s);
  113. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  114. s_wb24(&s, (uint32_t)meta_data_size);
  115. s_wb32(&s, 0);
  116. s_wb24(&s, 0);
  117. s_write(&s, meta_data, meta_data_size);
  118. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos + 4 - 1);
  119. *output = data.bytes.array;
  120. *size = data.bytes.num;
  121. bfree(meta_data);
  122. }
  123. #ifdef DEBUG_TIMESTAMPS
  124. static int32_t last_time = 0;
  125. #endif
  126. static void flv_video(struct serializer *s, struct encoder_packet *packet,
  127. bool is_header)
  128. {
  129. int64_t offset = packet->pts - packet->dts;
  130. int32_t time_ms = get_ms_time(packet, packet->dts);
  131. if (!packet->data || !packet->size)
  132. return;
  133. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  134. #ifdef DEBUG_TIMESTAMPS
  135. blog(LOG_DEBUG, "Video: %lu", time_ms);
  136. if (last_time > time_ms)
  137. blog(LOG_DEBUG, "Non-monotonic");
  138. last_time = time_ms;
  139. #endif
  140. s_wb24(s, (uint32_t)packet->size + 5);
  141. s_wb24(s, time_ms);
  142. s_w8(s, (time_ms >> 24) & 0x7F);
  143. s_wb24(s, 0);
  144. /* these are the 5 extra bytes mentioned above */
  145. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  146. s_w8(s, is_header ? 0 : 1);
  147. s_wb24(s, get_ms_time(packet, offset));
  148. s_write(s, packet->data, packet->size);
  149. /* write tag size (starting byte doesnt count) */
  150. s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1);
  151. }
  152. static void flv_audio(struct serializer *s, struct encoder_packet *packet,
  153. bool is_header)
  154. {
  155. int32_t time_ms = get_ms_time(packet, packet->dts);
  156. if (!packet->data || !packet->size)
  157. return;
  158. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  159. #ifdef DEBUG_TIMESTAMPS
  160. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  161. if (last_time > time_ms)
  162. blog(LOG_DEBUG, "Non-monotonic");
  163. last_time = time_ms;
  164. #endif
  165. s_wb24(s, (uint32_t)packet->size + 2);
  166. s_wb24(s, time_ms);
  167. s_w8(s, (time_ms >> 24) & 0x7F);
  168. s_wb24(s, 0);
  169. /* these are the two extra bytes mentioned above */
  170. s_w8(s, 0xaf);
  171. s_w8(s, is_header ? 0 : 1);
  172. s_write(s, packet->data, packet->size);
  173. /* write tag size (starting byte doesnt count) */
  174. s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1);
  175. }
  176. void flv_packet_mux(struct encoder_packet *packet,
  177. uint8_t **output, size_t *size, bool is_header)
  178. {
  179. struct array_output_data data;
  180. struct serializer s;
  181. array_output_serializer_init(&s, &data);
  182. if (packet->type == OBS_ENCODER_VIDEO)
  183. flv_video(&s, packet, is_header);
  184. else
  185. flv_audio(&s, packet, is_header);
  186. *output = data.bytes.array;
  187. *size = data.bytes.num;
  188. }