flv-mux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #define VIDEODATA_AVCVIDEOPACKET 7.0
  27. #define AUDIODATA_AAC 10.0
  28. static inline double encoder_bitrate(obs_encoder_t *encoder)
  29. {
  30. obs_data_t *settings = obs_encoder_get_settings(encoder);
  31. double bitrate = obs_data_get_double(settings, "bitrate");
  32. obs_data_release(settings);
  33. return bitrate;
  34. }
  35. #define FLV_INFO_SIZE_OFFSET 42
  36. void write_file_info(FILE *file, int64_t duration_ms, int64_t size)
  37. {
  38. char buf[64];
  39. char *enc = buf;
  40. char *end = enc + sizeof(buf);
  41. fseek(file, FLV_INFO_SIZE_OFFSET, SEEK_SET);
  42. enc_num_val(&enc, end, "duration", (double)duration_ms / 1000.0);
  43. enc_num_val(&enc, end, "fileSize", (double)size);
  44. fwrite(buf, 1, enc - buf, file);
  45. }
  46. static void build_flv_meta_data(obs_output_t *context, uint8_t **output,
  47. size_t *size)
  48. {
  49. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  50. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  51. video_t *video = obs_encoder_video(vencoder);
  52. audio_t *audio = obs_encoder_audio(aencoder);
  53. char buf[4096];
  54. char *enc = buf;
  55. char *end = enc + sizeof(buf);
  56. struct dstr encoder_name = {0};
  57. enc_str(&enc, end, "@setDataFrame");
  58. enc_str(&enc, end, "onMetaData");
  59. *enc++ = AMF_ECMA_ARRAY;
  60. enc = AMF_EncodeInt32(enc, end, 20);
  61. enc_num_val(&enc, end, "duration", 0.0);
  62. enc_num_val(&enc, end, "fileSize", 0.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_num_val(&enc, end, "videocodecid", VIDEODATA_AVCVIDEOPACKET);
  68. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  69. enc_num_val(&enc, end, "framerate", video_output_get_frame_rate(video));
  70. enc_num_val(&enc, end, "audiocodecid", AUDIODATA_AAC);
  71. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  72. enc_num_val(&enc, end, "audiosamplerate",
  73. (double)obs_encoder_get_sample_rate(aencoder));
  74. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  75. enc_num_val(&enc, end, "audiochannels",
  76. (double)audio_output_get_channels(audio));
  77. enc_bool_val(&enc, end, "stereo",
  78. audio_output_get_channels(audio) == 2);
  79. enc_bool_val(&enc, end, "2.1", audio_output_get_channels(audio) == 3);
  80. enc_bool_val(&enc, end, "3.1", audio_output_get_channels(audio) == 4);
  81. enc_bool_val(&enc, end, "4.0", audio_output_get_channels(audio) == 4);
  82. enc_bool_val(&enc, end, "4.1", audio_output_get_channels(audio) == 5);
  83. enc_bool_val(&enc, end, "5.1", audio_output_get_channels(audio) == 6);
  84. enc_bool_val(&enc, end, "7.1", audio_output_get_channels(audio) == 8);
  85. dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME);
  86. #ifdef HAVE_OBSCONFIG_H
  87. dstr_cat(&encoder_name, OBS_VERSION);
  88. #else
  89. dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER,
  90. LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER);
  91. #endif
  92. dstr_cat(&encoder_name, ")");
  93. enc_str_val(&enc, end, "encoder", encoder_name.array);
  94. dstr_free(&encoder_name);
  95. *enc++ = 0;
  96. *enc++ = 0;
  97. *enc++ = AMF_OBJECT_END;
  98. *size = enc - buf;
  99. *output = bmemdup(buf, *size);
  100. }
  101. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  102. bool write_header)
  103. {
  104. struct array_output_data data;
  105. struct serializer s;
  106. uint8_t *meta_data = NULL;
  107. size_t meta_data_size;
  108. uint32_t start_pos;
  109. array_output_serializer_init(&s, &data);
  110. build_flv_meta_data(context, &meta_data, &meta_data_size);
  111. if (write_header) {
  112. s_write(&s, "FLV", 3);
  113. s_w8(&s, 1);
  114. s_w8(&s, 5);
  115. s_wb32(&s, 9);
  116. s_wb32(&s, 0);
  117. }
  118. start_pos = serializer_get_pos(&s);
  119. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  120. s_wb24(&s, (uint32_t)meta_data_size);
  121. s_wb32(&s, 0);
  122. s_wb24(&s, 0);
  123. s_write(&s, meta_data, meta_data_size);
  124. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1);
  125. *output = data.bytes.array;
  126. *size = data.bytes.num;
  127. bfree(meta_data);
  128. }
  129. #ifdef DEBUG_TIMESTAMPS
  130. static int32_t last_time = 0;
  131. #endif
  132. static void flv_video(struct serializer *s, int32_t dts_offset,
  133. struct encoder_packet *packet, bool is_header)
  134. {
  135. int64_t offset = packet->pts - packet->dts;
  136. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  137. if (!packet->data || !packet->size)
  138. return;
  139. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  140. #ifdef DEBUG_TIMESTAMPS
  141. blog(LOG_DEBUG, "Video: %lu", time_ms);
  142. if (last_time > time_ms)
  143. blog(LOG_DEBUG, "Non-monotonic");
  144. last_time = time_ms;
  145. #endif
  146. s_wb24(s, (uint32_t)packet->size + 5);
  147. s_wb24(s, time_ms);
  148. s_w8(s, (time_ms >> 24) & 0x7F);
  149. s_wb24(s, 0);
  150. /* these are the 5 extra bytes mentioned above */
  151. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  152. s_w8(s, is_header ? 0 : 1);
  153. s_wb24(s, get_ms_time(packet, offset));
  154. s_write(s, packet->data, packet->size);
  155. /* write tag size (starting byte doesn't count) */
  156. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  157. }
  158. static void flv_audio(struct serializer *s, int32_t dts_offset,
  159. struct encoder_packet *packet, bool is_header)
  160. {
  161. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  162. if (!packet->data || !packet->size)
  163. return;
  164. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  165. #ifdef DEBUG_TIMESTAMPS
  166. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  167. if (last_time > time_ms)
  168. blog(LOG_DEBUG, "Non-monotonic");
  169. last_time = time_ms;
  170. #endif
  171. s_wb24(s, (uint32_t)packet->size + 2);
  172. s_wb24(s, time_ms);
  173. s_w8(s, (time_ms >> 24) & 0x7F);
  174. s_wb24(s, 0);
  175. /* these are the two extra bytes mentioned above */
  176. s_w8(s, 0xaf);
  177. s_w8(s, is_header ? 0 : 1);
  178. s_write(s, packet->data, packet->size);
  179. /* write tag size (starting byte doesn't count) */
  180. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  181. }
  182. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  183. uint8_t **output, size_t *size, bool is_header)
  184. {
  185. struct array_output_data data;
  186. struct serializer s;
  187. array_output_serializer_init(&s, &data);
  188. if (packet->type == OBS_ENCODER_VIDEO)
  189. flv_video(&s, dts_offset, packet, is_header);
  190. else
  191. flv_audio(&s, dts_offset, packet, is_header);
  192. *output = data.bytes.array;
  193. *size = data.bytes.num;
  194. }
  195. /* ------------------------------------------------------------------------- */
  196. /* stuff for additional media streams */
  197. #define s_amf_conststring(s, str) \
  198. do { \
  199. const size_t len = sizeof(str) - 1; \
  200. s_wb16(s, (uint16_t)len); \
  201. serialize(s, str, len); \
  202. } while (false)
  203. #define s_amf_double(s, d) \
  204. do { \
  205. double d_val = d; \
  206. uint64_t u_val = *(uint64_t *)&d_val; \
  207. s_wb64(s, u_val); \
  208. } while (false)
  209. static void flv_build_additional_meta_data(uint8_t **data, size_t *size)
  210. {
  211. struct array_output_data out;
  212. struct serializer s;
  213. array_output_serializer_init(&s, &out);
  214. s_w8(&s, AMF_STRING);
  215. s_amf_conststring(&s, "@setDataFrame");
  216. s_w8(&s, AMF_STRING);
  217. s_amf_conststring(&s, "onExpectAdditionalMedia");
  218. s_w8(&s, AMF_OBJECT);
  219. {
  220. s_amf_conststring(&s, "processingIntents");
  221. s_w8(&s, AMF_STRICT_ARRAY);
  222. s_wb32(&s, 1);
  223. {
  224. s_w8(&s, AMF_STRING);
  225. s_amf_conststring(&s, "ArchiveProgramNarrationAudio");
  226. }
  227. /* ---- */
  228. s_amf_conststring(&s, "additionalMedia");
  229. s_w8(&s, AMF_OBJECT);
  230. {
  231. s_amf_conststring(&s, "stream0");
  232. s_w8(&s, AMF_OBJECT);
  233. {
  234. s_amf_conststring(&s, "type");
  235. s_w8(&s, AMF_NUMBER);
  236. s_amf_double(&s, RTMP_PACKET_TYPE_AUDIO);
  237. /* ---- */
  238. s_amf_conststring(&s, "mediaLabels");
  239. s_w8(&s, AMF_OBJECT);
  240. {
  241. s_amf_conststring(&s, "contentType");
  242. s_w8(&s, AMF_STRING);
  243. s_amf_conststring(&s, "PNAR");
  244. }
  245. s_wb24(&s, AMF_OBJECT_END);
  246. }
  247. s_wb24(&s, AMF_OBJECT_END);
  248. }
  249. s_wb24(&s, AMF_OBJECT_END);
  250. /* ---- */
  251. s_amf_conststring(&s, "defaultMedia");
  252. s_w8(&s, AMF_OBJECT);
  253. {
  254. s_amf_conststring(&s, "audio");
  255. s_w8(&s, AMF_OBJECT);
  256. {
  257. s_amf_conststring(&s, "mediaLabels");
  258. s_w8(&s, AMF_OBJECT);
  259. {
  260. s_amf_conststring(&s, "contentType");
  261. s_w8(&s, AMF_STRING);
  262. s_amf_conststring(&s, "PRM");
  263. }
  264. s_wb24(&s, AMF_OBJECT_END);
  265. }
  266. s_wb24(&s, AMF_OBJECT_END);
  267. }
  268. s_wb24(&s, AMF_OBJECT_END);
  269. }
  270. s_wb24(&s, AMF_OBJECT_END);
  271. *data = out.bytes.array;
  272. *size = out.bytes.num;
  273. }
  274. void flv_additional_meta_data(obs_output_t *context, uint8_t **data,
  275. size_t *size)
  276. {
  277. UNUSED_PARAMETER(context);
  278. struct array_output_data out;
  279. struct serializer s;
  280. uint8_t *meta_data = NULL;
  281. size_t meta_data_size;
  282. flv_build_additional_meta_data(&meta_data, &meta_data_size);
  283. array_output_serializer_init(&s, &out);
  284. s_w8(&s, RTMP_PACKET_TYPE_INFO); //18
  285. s_wb24(&s, (uint32_t)meta_data_size);
  286. s_wb32(&s, 0);
  287. s_wb24(&s, 0);
  288. s_write(&s, meta_data, meta_data_size);
  289. bfree(meta_data);
  290. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1);
  291. *data = out.bytes.array;
  292. *size = out.bytes.num;
  293. }
  294. static inline void s_u29(struct serializer *s, uint32_t val)
  295. {
  296. if (val <= 0x7F) {
  297. s_w8(s, val);
  298. } else if (val <= 0x3FFF) {
  299. s_w8(s, 0x80 | (val >> 7));
  300. s_w8(s, val & 0x7F);
  301. } else if (val <= 0x1FFFFF) {
  302. s_w8(s, 0x80 | (val >> 14));
  303. s_w8(s, 0x80 | ((val >> 7) & 0x7F));
  304. s_w8(s, val & 0x7F);
  305. } else {
  306. s_w8(s, 0x80 | (val >> 22));
  307. s_w8(s, 0x80 | ((val >> 15) & 0x7F));
  308. s_w8(s, 0x80 | ((val >> 8) & 0x7F));
  309. s_w8(s, val & 0xFF);
  310. }
  311. }
  312. static inline void s_u29b_value(struct serializer *s, uint32_t val)
  313. {
  314. s_u29(s, 1 | ((val & 0xFFFFFFF) << 1));
  315. }
  316. static void flv_build_additional_audio(uint8_t **data, size_t *size,
  317. struct encoder_packet *packet,
  318. bool is_header, size_t index)
  319. {
  320. UNUSED_PARAMETER(index);
  321. struct array_output_data out;
  322. struct serializer s;
  323. array_output_serializer_init(&s, &out);
  324. s_w8(&s, AMF_STRING);
  325. s_amf_conststring(&s, "additionalMedia");
  326. s_w8(&s, AMF_OBJECT);
  327. {
  328. s_amf_conststring(&s, "id");
  329. s_w8(&s, AMF_STRING);
  330. s_amf_conststring(&s, "stream0");
  331. /* ----- */
  332. s_amf_conststring(&s, "media");
  333. s_w8(&s, AMF_AVMPLUS);
  334. s_w8(&s, AMF3_BYTE_ARRAY);
  335. s_u29b_value(&s, (uint32_t)packet->size + 2);
  336. s_w8(&s, 0xaf);
  337. s_w8(&s, is_header ? 0 : 1);
  338. s_write(&s, packet->data, packet->size);
  339. }
  340. s_wb24(&s, AMF_OBJECT_END);
  341. *data = out.bytes.array;
  342. *size = out.bytes.num;
  343. }
  344. static void flv_additional_audio(struct serializer *s, int32_t dts_offset,
  345. struct encoder_packet *packet, bool is_header,
  346. size_t index)
  347. {
  348. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  349. uint8_t *data;
  350. size_t size;
  351. if (!packet->data || !packet->size)
  352. return;
  353. flv_build_additional_audio(&data, &size, packet, is_header, index);
  354. s_w8(s, RTMP_PACKET_TYPE_INFO); //18
  355. #ifdef DEBUG_TIMESTAMPS
  356. blog(LOG_DEBUG, "Audio2: %lu", time_ms);
  357. if (last_time > time_ms)
  358. blog(LOG_DEBUG, "Non-monotonic");
  359. last_time = time_ms;
  360. #endif
  361. s_wb24(s, (uint32_t)size);
  362. s_wb24(s, time_ms);
  363. s_w8(s, (time_ms >> 24) & 0x7F);
  364. s_wb24(s, 0);
  365. serialize(s, data, size);
  366. bfree(data);
  367. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  368. }
  369. void flv_additional_packet_mux(struct encoder_packet *packet,
  370. int32_t dts_offset, uint8_t **data, size_t *size,
  371. bool is_header, size_t index)
  372. {
  373. struct array_output_data out;
  374. struct serializer s;
  375. array_output_serializer_init(&s, &out);
  376. if (packet->type == OBS_ENCODER_VIDEO) {
  377. //currently unsupported
  378. bcrash("who said you could output an additional video packet?");
  379. } else {
  380. flv_additional_audio(&s, dts_offset, packet, is_header, index);
  381. }
  382. *data = out.bytes.array;
  383. *size = out.bytes.num;
  384. }