flv-mux.c 17 KB

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