flv-mux.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 AUDIODATA_AAC 10.0
  26. #define VIDEO_FRAMETYPE_OFFSET 4
  27. enum video_frametype_t {
  28. FT_KEY = 1 << VIDEO_FRAMETYPE_OFFSET,
  29. FT_INTER = 2 << VIDEO_FRAMETYPE_OFFSET,
  30. };
  31. // Y2023 spec
  32. const uint8_t FRAME_HEADER_EX = 8 << VIDEO_FRAMETYPE_OFFSET;
  33. enum packet_type_t {
  34. PACKETTYPE_SEQ_START = 0,
  35. PACKETTYPE_FRAMES = 1,
  36. PACKETTYPE_SEQ_END = 2,
  37. #ifdef ENABLE_HEVC
  38. PACKETTYPE_FRAMESX = 3,
  39. #endif
  40. PACKETTYPE_METADATA = 4
  41. };
  42. enum datatype_t {
  43. DATA_TYPE_NUMBER = 0,
  44. DATA_TYPE_STRING = 2,
  45. DATA_TYPE_OBJECT = 3,
  46. DATA_TYPE_OBJECT_END = 9,
  47. };
  48. static void s_w4cc(struct serializer *s, enum video_id_t id)
  49. {
  50. switch (id) {
  51. case CODEC_AV1:
  52. s_w8(s, 'a');
  53. s_w8(s, 'v');
  54. s_w8(s, '0');
  55. s_w8(s, '1');
  56. break;
  57. #ifdef ENABLE_HEVC
  58. case CODEC_HEVC:
  59. s_w8(s, 'h');
  60. s_w8(s, 'v');
  61. s_w8(s, 'c');
  62. s_w8(s, '1');
  63. break;
  64. #endif
  65. case CODEC_H264:
  66. assert(0);
  67. }
  68. }
  69. static void s_wstring(struct serializer *s, const char *str)
  70. {
  71. size_t len = strlen(str);
  72. s_wb16(s, (uint16_t)len);
  73. s_write(s, str, len);
  74. }
  75. static inline void s_wtimestamp(struct serializer *s, int32_t i32)
  76. {
  77. s_wb24(s, (uint32_t)(i32 & 0xFFFFFF));
  78. s_w8(s, (uint32_t)(i32 >> 24) & 0x7F);
  79. }
  80. static inline double encoder_bitrate(obs_encoder_t *encoder)
  81. {
  82. obs_data_t *settings = obs_encoder_get_settings(encoder);
  83. double bitrate = obs_data_get_double(settings, "bitrate");
  84. obs_data_release(settings);
  85. return bitrate;
  86. }
  87. static const double VIDEODATA_AVCVIDEOPACKET = 7.0;
  88. // Additional FLV onMetaData values for Enhanced RTMP/FLV
  89. static const double VIDEODATA_AV1VIDEOPACKET = 1635135537.0; // FourCC "av01"
  90. #ifdef ENABLE_HEVC
  91. static const double VIDEODATA_HEVCVIDEOPACKET = 1752589105.0; // FourCC "hvc1"
  92. #endif
  93. static inline double encoder_video_codec(obs_encoder_t *encoder)
  94. {
  95. const char *codec = obs_encoder_get_codec(encoder);
  96. if (strcmp(codec, "h264") == 0)
  97. return VIDEODATA_AVCVIDEOPACKET;
  98. if (strcmp(codec, "av1") == 0)
  99. return VIDEODATA_AV1VIDEOPACKET;
  100. #ifdef ENABLE_HEVC
  101. if (strcmp(codec, "hevc") == 0)
  102. return VIDEODATA_HEVCVIDEOPACKET;
  103. #endif
  104. return 0.0;
  105. }
  106. #define FLV_INFO_SIZE_OFFSET 42
  107. void write_file_info(FILE *file, int64_t duration_ms, int64_t size)
  108. {
  109. char buf[64];
  110. char *enc = buf;
  111. char *end = enc + sizeof(buf);
  112. fseek(file, FLV_INFO_SIZE_OFFSET, SEEK_SET);
  113. enc_num_val(&enc, end, "duration", (double)duration_ms / 1000.0);
  114. enc_num_val(&enc, end, "fileSize", (double)size);
  115. fwrite(buf, 1, enc - buf, file);
  116. }
  117. static void build_flv_meta_data(obs_output_t *context, uint8_t **output,
  118. size_t *size)
  119. {
  120. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  121. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  122. video_t *video = obs_encoder_video(vencoder);
  123. audio_t *audio = obs_encoder_audio(aencoder);
  124. char buf[4096];
  125. char *enc = buf;
  126. char *end = enc + sizeof(buf);
  127. struct dstr encoder_name = {0};
  128. enc_str(&enc, end, "@setDataFrame");
  129. enc_str(&enc, end, "onMetaData");
  130. *enc++ = AMF_ECMA_ARRAY;
  131. enc = AMF_EncodeInt32(enc, end, 20);
  132. enc_num_val(&enc, end, "duration", 0.0);
  133. enc_num_val(&enc, end, "fileSize", 0.0);
  134. enc_num_val(&enc, end, "width",
  135. (double)obs_encoder_get_width(vencoder));
  136. enc_num_val(&enc, end, "height",
  137. (double)obs_encoder_get_height(vencoder));
  138. enc_num_val(&enc, end, "videocodecid", encoder_video_codec(vencoder));
  139. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  140. enc_num_val(&enc, end, "framerate", video_output_get_frame_rate(video));
  141. enc_num_val(&enc, end, "audiocodecid", AUDIODATA_AAC);
  142. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  143. enc_num_val(&enc, end, "audiosamplerate",
  144. (double)obs_encoder_get_sample_rate(aencoder));
  145. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  146. enc_num_val(&enc, end, "audiochannels",
  147. (double)audio_output_get_channels(audio));
  148. enc_bool_val(&enc, end, "stereo",
  149. audio_output_get_channels(audio) == 2);
  150. enc_bool_val(&enc, end, "2.1", audio_output_get_channels(audio) == 3);
  151. enc_bool_val(&enc, end, "3.1", audio_output_get_channels(audio) == 4);
  152. enc_bool_val(&enc, end, "4.0", audio_output_get_channels(audio) == 4);
  153. enc_bool_val(&enc, end, "4.1", audio_output_get_channels(audio) == 5);
  154. enc_bool_val(&enc, end, "5.1", audio_output_get_channels(audio) == 6);
  155. enc_bool_val(&enc, end, "7.1", audio_output_get_channels(audio) == 8);
  156. dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME);
  157. #ifdef HAVE_OBSCONFIG_H
  158. dstr_cat(&encoder_name, obs_get_version_string());
  159. #else
  160. dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER,
  161. LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER);
  162. #endif
  163. dstr_cat(&encoder_name, ")");
  164. enc_str_val(&enc, end, "encoder", encoder_name.array);
  165. dstr_free(&encoder_name);
  166. *enc++ = 0;
  167. *enc++ = 0;
  168. *enc++ = AMF_OBJECT_END;
  169. *size = enc - buf;
  170. *output = bmemdup(buf, *size);
  171. }
  172. static inline void write_previous_tag_size_without_header(struct serializer *s,
  173. uint32_t header_size)
  174. {
  175. assert(serializer_get_pos(s) >= header_size);
  176. assert(serializer_get_pos(s) >= 11);
  177. /*
  178. * From FLV file format specification version 10:
  179. * Size of previous [current] tag, including its header.
  180. * For FLV version 1 this value is 11 plus the DataSize of
  181. * the previous [current] tag.
  182. */
  183. s_wb32(s, (uint32_t)serializer_get_pos(s) - header_size);
  184. }
  185. static inline void write_previous_tag_size(struct serializer *s)
  186. {
  187. write_previous_tag_size_without_header(s, 0);
  188. }
  189. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  190. bool write_header)
  191. {
  192. struct array_output_data data;
  193. struct serializer s;
  194. uint8_t *meta_data = NULL;
  195. size_t meta_data_size;
  196. uint32_t start_pos;
  197. array_output_serializer_init(&s, &data);
  198. build_flv_meta_data(context, &meta_data, &meta_data_size);
  199. if (write_header) {
  200. s_write(&s, "FLV", 3);
  201. s_w8(&s, 1);
  202. s_w8(&s, 5);
  203. s_wb32(&s, 9);
  204. s_wb32(&s, 0);
  205. }
  206. start_pos = serializer_get_pos(&s);
  207. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  208. s_wb24(&s, (uint32_t)meta_data_size);
  209. s_wb32(&s, 0);
  210. s_wb24(&s, 0);
  211. s_write(&s, meta_data, meta_data_size);
  212. write_previous_tag_size_without_header(&s, start_pos);
  213. *output = data.bytes.array;
  214. *size = data.bytes.num;
  215. bfree(meta_data);
  216. }
  217. #ifdef DEBUG_TIMESTAMPS
  218. static int32_t last_time = 0;
  219. #endif
  220. static void flv_video(struct serializer *s, int32_t dts_offset,
  221. struct encoder_packet *packet, bool is_header)
  222. {
  223. int64_t offset = packet->pts - packet->dts;
  224. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  225. if (!packet->data || !packet->size)
  226. return;
  227. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  228. #ifdef DEBUG_TIMESTAMPS
  229. blog(LOG_DEBUG, "Video: %lu", time_ms);
  230. if (last_time > time_ms)
  231. blog(LOG_DEBUG, "Non-monotonic");
  232. last_time = time_ms;
  233. #endif
  234. s_wb24(s, (uint32_t)packet->size + 5);
  235. s_wb24(s, (uint32_t)time_ms);
  236. s_w8(s, (time_ms >> 24) & 0x7F);
  237. s_wb24(s, 0);
  238. /* these are the 5 extra bytes mentioned above */
  239. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  240. s_w8(s, is_header ? 0 : 1);
  241. s_wb24(s, get_ms_time(packet, offset));
  242. s_write(s, packet->data, packet->size);
  243. write_previous_tag_size(s);
  244. }
  245. static void flv_audio(struct serializer *s, int32_t dts_offset,
  246. struct encoder_packet *packet, bool is_header)
  247. {
  248. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  249. if (!packet->data || !packet->size)
  250. return;
  251. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  252. #ifdef DEBUG_TIMESTAMPS
  253. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  254. if (last_time > time_ms)
  255. blog(LOG_DEBUG, "Non-monotonic");
  256. last_time = time_ms;
  257. #endif
  258. s_wb24(s, (uint32_t)packet->size + 2);
  259. s_wb24(s, (uint32_t)time_ms);
  260. s_w8(s, (time_ms >> 24) & 0x7F);
  261. s_wb24(s, 0);
  262. /* these are the two extra bytes mentioned above */
  263. s_w8(s, 0xaf);
  264. s_w8(s, is_header ? 0 : 1);
  265. s_write(s, packet->data, packet->size);
  266. write_previous_tag_size(s);
  267. }
  268. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  269. uint8_t **output, size_t *size, bool is_header)
  270. {
  271. struct array_output_data data;
  272. struct serializer s;
  273. array_output_serializer_init(&s, &data);
  274. if (packet->type == OBS_ENCODER_VIDEO)
  275. flv_video(&s, dts_offset, packet, is_header);
  276. else
  277. flv_audio(&s, dts_offset, packet, is_header);
  278. *output = data.bytes.array;
  279. *size = data.bytes.num;
  280. }
  281. // Y2023 spec
  282. void flv_packet_ex(struct encoder_packet *packet, enum video_id_t codec_id,
  283. int32_t dts_offset, uint8_t **output, size_t *size, int type)
  284. {
  285. struct array_output_data data;
  286. struct serializer s;
  287. array_output_serializer_init(&s, &data);
  288. assert(packet->type == OBS_ENCODER_VIDEO);
  289. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  290. // packet head
  291. int header_metadata_size = 5;
  292. #ifdef ENABLE_HEVC
  293. // 3 extra bytes for composition time offset
  294. if (codec_id == CODEC_HEVC && type == PACKETTYPE_FRAMES) {
  295. header_metadata_size = 8;
  296. }
  297. #endif
  298. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  299. s_wb24(&s, (uint32_t)packet->size + header_metadata_size);
  300. s_wtimestamp(&s, time_ms);
  301. s_wb24(&s, 0); // always 0
  302. // packet ext header
  303. s_w8(&s,
  304. FRAME_HEADER_EX | type | (packet->keyframe ? FT_KEY : FT_INTER));
  305. s_w4cc(&s, codec_id);
  306. #ifdef ENABLE_HEVC
  307. // hevc composition time offset
  308. if (codec_id == CODEC_HEVC && type == PACKETTYPE_FRAMES) {
  309. s_wb24(&s, get_ms_time(packet, packet->pts - packet->dts));
  310. }
  311. #endif
  312. // packet data
  313. s_write(&s, packet->data, packet->size);
  314. // packet tail
  315. write_previous_tag_size(&s);
  316. *output = data.bytes.array;
  317. *size = data.bytes.num;
  318. }
  319. void flv_packet_start(struct encoder_packet *packet, enum video_id_t codec,
  320. uint8_t **output, size_t *size)
  321. {
  322. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_START);
  323. }
  324. void flv_packet_frames(struct encoder_packet *packet, enum video_id_t codec,
  325. int32_t dts_offset, uint8_t **output, size_t *size)
  326. {
  327. int packet_type = PACKETTYPE_FRAMES;
  328. #ifdef ENABLE_HEVC
  329. // PACKETTYPE_FRAMESX is an optimization to avoid sending composition
  330. // time offsets of 0. See Enhanced RTMP spec.
  331. if (codec == CODEC_HEVC && packet->dts == packet->pts)
  332. packet_type = PACKETTYPE_FRAMESX;
  333. #endif
  334. flv_packet_ex(packet, codec, dts_offset, output, size, packet_type);
  335. }
  336. void flv_packet_end(struct encoder_packet *packet, enum video_id_t codec,
  337. uint8_t **output, size_t *size)
  338. {
  339. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_END);
  340. }
  341. void flv_packet_metadata(enum video_id_t codec_id, uint8_t **output,
  342. size_t *size, int bits_per_raw_sample,
  343. uint8_t color_primaries, int color_trc,
  344. int color_space, int min_luminance, int max_luminance)
  345. {
  346. // metadata array
  347. struct array_output_data data;
  348. struct array_output_data metadata;
  349. struct serializer s;
  350. array_output_serializer_init(&s, &data);
  351. // metadata data array
  352. {
  353. struct serializer s;
  354. array_output_serializer_init(&s, &metadata);
  355. s_w8(&s, DATA_TYPE_STRING);
  356. s_wstring(&s, "colorInfo");
  357. s_w8(&s, DATA_TYPE_OBJECT);
  358. {
  359. // colorConfig:
  360. s_wstring(&s, "colorConfig");
  361. s_w8(&s, DATA_TYPE_OBJECT);
  362. {
  363. s_wstring(&s, "bitDepth");
  364. s_w8(&s, DATA_TYPE_NUMBER);
  365. s_wbd(&s, bits_per_raw_sample);
  366. s_wstring(&s, "colorPrimaries");
  367. s_w8(&s, DATA_TYPE_NUMBER);
  368. s_wbd(&s, color_primaries);
  369. s_wstring(&s, "transferCharacteristics");
  370. s_w8(&s, DATA_TYPE_NUMBER);
  371. s_wbd(&s, color_trc);
  372. s_wstring(&s, "matrixCoefficients");
  373. s_w8(&s, DATA_TYPE_NUMBER);
  374. s_wbd(&s, color_space);
  375. }
  376. s_w8(&s, 0);
  377. s_w8(&s, 0);
  378. s_w8(&s, DATA_TYPE_OBJECT_END);
  379. if (max_luminance != 0) {
  380. // hdrMdcv
  381. s_wstring(&s, "hdrMdcv");
  382. s_w8(&s, DATA_TYPE_OBJECT);
  383. {
  384. s_wstring(&s, "maxLuminance");
  385. s_w8(&s, DATA_TYPE_NUMBER);
  386. s_wbd(&s, max_luminance);
  387. s_wstring(&s, "minLuminance");
  388. s_w8(&s, DATA_TYPE_NUMBER);
  389. s_wbd(&s, min_luminance);
  390. }
  391. s_w8(&s, 0);
  392. s_w8(&s, 0);
  393. s_w8(&s, DATA_TYPE_OBJECT_END);
  394. }
  395. }
  396. s_w8(&s, 0);
  397. s_w8(&s, 0);
  398. s_w8(&s, DATA_TYPE_OBJECT_END);
  399. }
  400. // packet head
  401. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  402. s_wb24(&s, (uint32_t)metadata.bytes.num + 5); // 5 = (w8+w4cc)
  403. s_wtimestamp(&s, 0);
  404. s_wb24(&s, 0); // always 0
  405. // packet ext header
  406. // these are the 5 extra bytes mentioned above
  407. s_w8(&s, FRAME_HEADER_EX | PACKETTYPE_METADATA);
  408. s_w4cc(&s, codec_id);
  409. // packet data
  410. s_write(&s, metadata.bytes.array, metadata.bytes.num);
  411. array_output_serializer_free(&metadata); // must be freed
  412. // packet tail
  413. write_previous_tag_size(&s);
  414. *output = data.bytes.array;
  415. *size = data.bytes.num;
  416. }
  417. /* ------------------------------------------------------------------------- */
  418. /* stuff for additional media streams */
  419. #define s_amf_conststring(s, str) \
  420. do { \
  421. const size_t len = sizeof(str) - 1; \
  422. s_wb16(s, (uint16_t)len); \
  423. serialize(s, str, len); \
  424. } while (false)
  425. #define s_amf_double(s, d) \
  426. do { \
  427. double d_val = d; \
  428. uint64_t u_val = *(uint64_t *)&d_val; \
  429. s_wb64(s, u_val); \
  430. } while (false)
  431. static void flv_build_additional_meta_data(uint8_t **data, size_t *size)
  432. {
  433. struct array_output_data out;
  434. struct serializer s;
  435. array_output_serializer_init(&s, &out);
  436. s_w8(&s, AMF_STRING);
  437. s_amf_conststring(&s, "@setDataFrame");
  438. s_w8(&s, AMF_STRING);
  439. s_amf_conststring(&s, "onExpectAdditionalMedia");
  440. s_w8(&s, AMF_OBJECT);
  441. {
  442. s_amf_conststring(&s, "processingIntents");
  443. s_w8(&s, AMF_STRICT_ARRAY);
  444. s_wb32(&s, 1);
  445. {
  446. s_w8(&s, AMF_STRING);
  447. s_amf_conststring(&s, "ArchiveProgramNarrationAudio");
  448. }
  449. /* ---- */
  450. s_amf_conststring(&s, "additionalMedia");
  451. s_w8(&s, AMF_OBJECT);
  452. {
  453. s_amf_conststring(&s, "stream0");
  454. s_w8(&s, AMF_OBJECT);
  455. {
  456. s_amf_conststring(&s, "type");
  457. s_w8(&s, AMF_NUMBER);
  458. s_amf_double(&s, RTMP_PACKET_TYPE_AUDIO);
  459. /* ---- */
  460. s_amf_conststring(&s, "mediaLabels");
  461. s_w8(&s, AMF_OBJECT);
  462. {
  463. s_amf_conststring(&s, "contentType");
  464. s_w8(&s, AMF_STRING);
  465. s_amf_conststring(&s, "PNAR");
  466. }
  467. s_wb24(&s, AMF_OBJECT_END);
  468. }
  469. s_wb24(&s, AMF_OBJECT_END);
  470. }
  471. s_wb24(&s, AMF_OBJECT_END);
  472. /* ---- */
  473. s_amf_conststring(&s, "defaultMedia");
  474. s_w8(&s, AMF_OBJECT);
  475. {
  476. s_amf_conststring(&s, "audio");
  477. s_w8(&s, AMF_OBJECT);
  478. {
  479. s_amf_conststring(&s, "mediaLabels");
  480. s_w8(&s, AMF_OBJECT);
  481. {
  482. s_amf_conststring(&s, "contentType");
  483. s_w8(&s, AMF_STRING);
  484. s_amf_conststring(&s, "PRM");
  485. }
  486. s_wb24(&s, AMF_OBJECT_END);
  487. }
  488. s_wb24(&s, AMF_OBJECT_END);
  489. }
  490. s_wb24(&s, AMF_OBJECT_END);
  491. }
  492. s_wb24(&s, AMF_OBJECT_END);
  493. *data = out.bytes.array;
  494. *size = out.bytes.num;
  495. }
  496. void flv_additional_meta_data(obs_output_t *context, uint8_t **data,
  497. size_t *size)
  498. {
  499. UNUSED_PARAMETER(context);
  500. struct array_output_data out;
  501. struct serializer s;
  502. uint8_t *meta_data = NULL;
  503. size_t meta_data_size;
  504. flv_build_additional_meta_data(&meta_data, &meta_data_size);
  505. array_output_serializer_init(&s, &out);
  506. s_w8(&s, RTMP_PACKET_TYPE_INFO); //18
  507. s_wb24(&s, (uint32_t)meta_data_size);
  508. s_wb32(&s, 0);
  509. s_wb24(&s, 0);
  510. s_write(&s, meta_data, meta_data_size);
  511. bfree(meta_data);
  512. write_previous_tag_size(&s);
  513. *data = out.bytes.array;
  514. *size = out.bytes.num;
  515. }
  516. static inline void s_u29(struct serializer *s, uint32_t val)
  517. {
  518. if (val <= 0x7F) {
  519. s_w8(s, val);
  520. } else if (val <= 0x3FFF) {
  521. s_w8(s, 0x80 | (val >> 7));
  522. s_w8(s, val & 0x7F);
  523. } else if (val <= 0x1FFFFF) {
  524. s_w8(s, 0x80 | (val >> 14));
  525. s_w8(s, 0x80 | ((val >> 7) & 0x7F));
  526. s_w8(s, val & 0x7F);
  527. } else {
  528. s_w8(s, 0x80 | (val >> 22));
  529. s_w8(s, 0x80 | ((val >> 15) & 0x7F));
  530. s_w8(s, 0x80 | ((val >> 8) & 0x7F));
  531. s_w8(s, val & 0xFF);
  532. }
  533. }
  534. static inline void s_u29b_value(struct serializer *s, uint32_t val)
  535. {
  536. s_u29(s, 1 | ((val & 0xFFFFFFF) << 1));
  537. }
  538. static void flv_build_additional_audio(uint8_t **data, size_t *size,
  539. struct encoder_packet *packet,
  540. bool is_header, size_t index)
  541. {
  542. UNUSED_PARAMETER(index);
  543. struct array_output_data out;
  544. struct serializer s;
  545. array_output_serializer_init(&s, &out);
  546. s_w8(&s, AMF_STRING);
  547. s_amf_conststring(&s, "additionalMedia");
  548. s_w8(&s, AMF_OBJECT);
  549. {
  550. s_amf_conststring(&s, "id");
  551. s_w8(&s, AMF_STRING);
  552. s_amf_conststring(&s, "stream0");
  553. /* ----- */
  554. s_amf_conststring(&s, "media");
  555. s_w8(&s, AMF_AVMPLUS);
  556. s_w8(&s, AMF3_BYTE_ARRAY);
  557. s_u29b_value(&s, (uint32_t)packet->size + 2);
  558. s_w8(&s, 0xaf);
  559. s_w8(&s, is_header ? 0 : 1);
  560. s_write(&s, packet->data, packet->size);
  561. }
  562. s_wb24(&s, AMF_OBJECT_END);
  563. *data = out.bytes.array;
  564. *size = out.bytes.num;
  565. }
  566. static void flv_additional_audio(struct serializer *s, int32_t dts_offset,
  567. struct encoder_packet *packet, bool is_header,
  568. size_t index)
  569. {
  570. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  571. uint8_t *data;
  572. size_t size;
  573. if (!packet->data || !packet->size)
  574. return;
  575. flv_build_additional_audio(&data, &size, packet, is_header, index);
  576. s_w8(s, RTMP_PACKET_TYPE_INFO); //18
  577. #ifdef DEBUG_TIMESTAMPS
  578. blog(LOG_DEBUG, "Audio2: %lu", time_ms);
  579. if (last_time > time_ms)
  580. blog(LOG_DEBUG, "Non-monotonic");
  581. last_time = time_ms;
  582. #endif
  583. s_wb24(s, (uint32_t)size);
  584. s_wb24(s, (uint32_t)time_ms);
  585. s_w8(s, (time_ms >> 24) & 0x7F);
  586. s_wb24(s, 0);
  587. serialize(s, data, size);
  588. bfree(data);
  589. write_previous_tag_size(s);
  590. }
  591. void flv_additional_packet_mux(struct encoder_packet *packet,
  592. int32_t dts_offset, uint8_t **data, size_t *size,
  593. bool is_header, size_t index)
  594. {
  595. struct array_output_data out;
  596. struct serializer s;
  597. array_output_serializer_init(&s, &out);
  598. if (packet->type == OBS_ENCODER_VIDEO) {
  599. //currently unsupported
  600. bcrash("who said you could output an additional video packet?");
  601. } else {
  602. flv_additional_audio(&s, dts_offset, packet, is_header, index);
  603. }
  604. *data = out.bytes.array;
  605. *size = out.bytes.num;
  606. }