flv-mux.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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, size_t *size)
  160. {
  161. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  162. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  163. video_t *video = obs_encoder_video(vencoder);
  164. audio_t *audio = obs_encoder_audio(aencoder);
  165. char buf[4096];
  166. char *enc = buf;
  167. char *end = enc + sizeof(buf);
  168. struct dstr encoder_name = {0};
  169. enc_str(&enc, end, "@setDataFrame");
  170. enc_str(&enc, end, "onMetaData");
  171. *enc++ = AMF_ECMA_ARRAY;
  172. enc = AMF_EncodeInt32(enc, end, 20);
  173. enc_num_val(&enc, end, "duration", 0.0);
  174. enc_num_val(&enc, end, "fileSize", 0.0);
  175. enc_num_val(&enc, end, "width", (double)obs_encoder_get_width(vencoder));
  176. enc_num_val(&enc, end, "height", (double)obs_encoder_get_height(vencoder));
  177. enc_num_val(&enc, end, "videocodecid", encoder_video_codec(vencoder));
  178. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  179. enc_num_val(&enc, end, "framerate", video_output_get_frame_rate(video));
  180. enc_num_val(&enc, end, "audiocodecid", AUDIODATA_AAC);
  181. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  182. enc_num_val(&enc, end, "audiosamplerate", (double)obs_encoder_get_sample_rate(aencoder));
  183. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  184. enc_num_val(&enc, end, "audiochannels", (double)audio_output_get_channels(audio));
  185. enc_bool_val(&enc, end, "stereo", audio_output_get_channels(audio) == 2);
  186. enc_bool_val(&enc, end, "2.1", audio_output_get_channels(audio) == 3);
  187. enc_bool_val(&enc, end, "3.1", audio_output_get_channels(audio) == 4);
  188. enc_bool_val(&enc, end, "4.0", audio_output_get_channels(audio) == 4);
  189. enc_bool_val(&enc, end, "4.1", audio_output_get_channels(audio) == 5);
  190. enc_bool_val(&enc, end, "5.1", audio_output_get_channels(audio) == 6);
  191. enc_bool_val(&enc, end, "7.1", audio_output_get_channels(audio) == 8);
  192. dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME);
  193. #ifdef HAVE_OBSCONFIG_H
  194. dstr_cat(&encoder_name, obs_get_version_string());
  195. #else
  196. dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER);
  197. #endif
  198. dstr_cat(&encoder_name, ")");
  199. enc_str_val(&enc, end, "encoder", encoder_name.array);
  200. dstr_free(&encoder_name);
  201. *enc++ = 0;
  202. *enc++ = 0;
  203. *enc++ = AMF_OBJECT_END;
  204. *size = enc - buf;
  205. *output = bmemdup(buf, *size);
  206. }
  207. static inline void write_previous_tag_size_without_header(struct serializer *s, uint32_t header_size)
  208. {
  209. assert(serializer_get_pos(s) >= header_size);
  210. assert(serializer_get_pos(s) >= 11);
  211. /*
  212. * From FLV file format specification version 10:
  213. * Size of previous [current] tag, including its header.
  214. * For FLV version 1 this value is 11 plus the DataSize of
  215. * the previous [current] tag.
  216. */
  217. s_wb32(s, (uint32_t)serializer_get_pos(s) - header_size);
  218. }
  219. static inline void write_previous_tag_size(struct serializer *s)
  220. {
  221. write_previous_tag_size_without_header(s, 0);
  222. }
  223. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size, bool write_header)
  224. {
  225. struct array_output_data data;
  226. struct serializer s;
  227. uint8_t *meta_data = NULL;
  228. size_t meta_data_size;
  229. uint32_t start_pos;
  230. array_output_serializer_init(&s, &data);
  231. build_flv_meta_data(context, &meta_data, &meta_data_size);
  232. if (write_header) {
  233. s_write(&s, "FLV", 3);
  234. s_w8(&s, 1);
  235. s_w8(&s, 5);
  236. s_wb32(&s, 9);
  237. s_wb32(&s, 0);
  238. }
  239. start_pos = serializer_get_pos(&s);
  240. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  241. s_wb24(&s, (uint32_t)meta_data_size);
  242. s_wb32(&s, 0);
  243. s_wb24(&s, 0);
  244. s_write(&s, meta_data, meta_data_size);
  245. write_previous_tag_size_without_header(&s, start_pos);
  246. *output = data.bytes.array;
  247. *size = data.bytes.num;
  248. bfree(meta_data);
  249. }
  250. #ifdef DEBUG_TIMESTAMPS
  251. static int32_t last_time = 0;
  252. #endif
  253. static void flv_video(struct serializer *s, int32_t dts_offset, struct encoder_packet *packet, bool is_header)
  254. {
  255. int64_t offset = packet->pts - packet->dts;
  256. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  257. if (!packet->data || !packet->size)
  258. return;
  259. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  260. #ifdef DEBUG_TIMESTAMPS
  261. blog(LOG_DEBUG, "Video: %lu", time_ms);
  262. if (last_time > time_ms)
  263. blog(LOG_DEBUG, "Non-monotonic");
  264. last_time = time_ms;
  265. #endif
  266. s_wb24(s, (uint32_t)packet->size + 5);
  267. s_wb24(s, (uint32_t)time_ms);
  268. s_w8(s, (time_ms >> 24) & 0x7F);
  269. s_wb24(s, 0);
  270. /* these are the 5 extra bytes mentioned above */
  271. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  272. s_w8(s, is_header ? 0 : 1);
  273. s_wb24(s, get_ms_time(packet, offset));
  274. s_write(s, packet->data, packet->size);
  275. write_previous_tag_size(s);
  276. }
  277. static void flv_audio(struct serializer *s, int32_t dts_offset, struct encoder_packet *packet, bool is_header)
  278. {
  279. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  280. if (!packet->data || !packet->size)
  281. return;
  282. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  283. #ifdef DEBUG_TIMESTAMPS
  284. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  285. if (last_time > time_ms)
  286. blog(LOG_DEBUG, "Non-monotonic");
  287. last_time = time_ms;
  288. #endif
  289. s_wb24(s, (uint32_t)packet->size + 2);
  290. s_wb24(s, (uint32_t)time_ms);
  291. s_w8(s, (time_ms >> 24) & 0x7F);
  292. s_wb24(s, 0);
  293. /* these are the two extra bytes mentioned above */
  294. s_w8(s, 0xaf);
  295. s_w8(s, is_header ? 0 : 1);
  296. s_write(s, packet->data, packet->size);
  297. write_previous_tag_size(s);
  298. }
  299. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset, uint8_t **output, size_t *size, bool is_header)
  300. {
  301. struct array_output_data data;
  302. struct serializer s;
  303. array_output_serializer_init(&s, &data);
  304. if (packet->type == OBS_ENCODER_VIDEO)
  305. flv_video(&s, dts_offset, packet, is_header);
  306. else
  307. flv_audio(&s, dts_offset, packet, is_header);
  308. *output = data.bytes.array;
  309. *size = data.bytes.num;
  310. }
  311. void flv_packet_audio_ex(struct encoder_packet *packet, enum audio_id_t codec_id, int32_t dts_offset, uint8_t **output,
  312. size_t *size, int type, size_t idx)
  313. {
  314. struct array_output_data data;
  315. struct serializer s;
  316. array_output_serializer_init(&s, &data);
  317. assert(packet->type == OBS_ENCODER_AUDIO);
  318. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  319. bool is_multitrack = idx > 0;
  320. if (!packet->data || !packet->size)
  321. return;
  322. int header_metadata_size = 5; // w8+wa4cc
  323. if (is_multitrack)
  324. header_metadata_size += 2; // w8 + w8
  325. s_w8(&s, RTMP_PACKET_TYPE_AUDIO);
  326. #ifdef DEBUG_TIMESTAMPS
  327. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  328. if (last_time > time_ms)
  329. blog(LOG_DEBUG, "Non-monotonic");
  330. last_time = time_ms;
  331. #endif
  332. s_wb24(&s, (uint32_t)packet->size + header_metadata_size);
  333. s_wb24(&s, (uint32_t)time_ms);
  334. s_w8(&s, (time_ms >> 24) & 0x7F);
  335. s_wb24(&s, 0);
  336. s_w8(&s, AUDIO_HEADER_EX | (is_multitrack ? AUDIO_PACKETTYPE_MULTITRACK : type));
  337. if (is_multitrack) {
  338. s_w8(&s, MULTITRACKTYPE_ONE_TRACK | type);
  339. s_wa4cc(&s, codec_id);
  340. s_w8(&s, (uint8_t)idx);
  341. } else {
  342. s_wa4cc(&s, codec_id);
  343. }
  344. s_write(&s, packet->data, packet->size);
  345. write_previous_tag_size(&s);
  346. *output = data.bytes.array;
  347. *size = data.bytes.num;
  348. }
  349. // Y2023 spec
  350. void flv_packet_ex(struct encoder_packet *packet, enum video_id_t codec_id, int32_t dts_offset, uint8_t **output,
  351. size_t *size, int type, size_t idx)
  352. {
  353. struct array_output_data data;
  354. struct serializer s;
  355. array_output_serializer_init(&s, &data);
  356. assert(packet->type == OBS_ENCODER_VIDEO);
  357. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  358. bool is_multitrack = idx > 0;
  359. // packet head
  360. int header_metadata_size = 5; // w8+w4cc
  361. // 3 extra bytes for composition time offset
  362. if ((codec_id == CODEC_H264 || codec_id == CODEC_HEVC) && type == PACKETTYPE_FRAMES) {
  363. header_metadata_size += 3; // w24
  364. }
  365. if (is_multitrack)
  366. header_metadata_size += 2; // w8+w8
  367. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  368. s_wb24(&s, (uint32_t)packet->size + header_metadata_size);
  369. s_wtimestamp(&s, time_ms);
  370. s_wb24(&s, 0); // always 0
  371. uint8_t frame_type = packet->keyframe ? FT_KEY : FT_INTER;
  372. /*
  373. * We only explicitly emit trackIds iff idx > 0.
  374. * The default trackId is 0.
  375. */
  376. if (is_multitrack) {
  377. s_w8(&s, FRAME_HEADER_EX | PACKETTYPE_MULTITRACK | frame_type);
  378. s_w8(&s, MULTITRACKTYPE_ONE_TRACK | type);
  379. s_w4cc(&s, codec_id);
  380. // trackId
  381. s_w8(&s, (uint8_t)idx);
  382. } else {
  383. s_w8(&s, FRAME_HEADER_EX | type | frame_type);
  384. s_w4cc(&s, codec_id);
  385. }
  386. // H.264/HEVC composition time offset
  387. if ((codec_id == CODEC_H264 || codec_id == CODEC_HEVC) && type == PACKETTYPE_FRAMES) {
  388. s_wb24(&s, get_ms_time(packet, packet->pts - packet->dts));
  389. }
  390. // packet data
  391. s_write(&s, packet->data, packet->size);
  392. // packet tail
  393. write_previous_tag_size(&s);
  394. *output = data.bytes.array;
  395. *size = data.bytes.num;
  396. }
  397. void flv_packet_start(struct encoder_packet *packet, enum video_id_t codec, uint8_t **output, size_t *size, size_t idx)
  398. {
  399. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_START, idx);
  400. }
  401. void flv_packet_frames(struct encoder_packet *packet, enum video_id_t codec, int32_t dts_offset, uint8_t **output,
  402. size_t *size, size_t idx)
  403. {
  404. int packet_type = PACKETTYPE_FRAMES;
  405. // PACKETTYPE_FRAMESX is an optimization to avoid sending composition
  406. // time offsets of 0. See Enhanced RTMP spec.
  407. if ((codec == CODEC_H264 || codec == CODEC_HEVC) && packet->dts == packet->pts)
  408. packet_type = PACKETTYPE_FRAMESX;
  409. flv_packet_ex(packet, codec, dts_offset, output, size, packet_type, idx);
  410. }
  411. void flv_packet_end(struct encoder_packet *packet, enum video_id_t codec, uint8_t **output, size_t *size, size_t idx)
  412. {
  413. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_END, idx);
  414. }
  415. void flv_packet_audio_start(struct encoder_packet *packet, enum audio_id_t codec, uint8_t **output, size_t *size,
  416. size_t idx)
  417. {
  418. flv_packet_audio_ex(packet, codec, 0, output, size, AUDIO_PACKETTYPE_SEQ_START, idx);
  419. }
  420. void flv_packet_audio_frames(struct encoder_packet *packet, enum audio_id_t codec, int32_t dts_offset, uint8_t **output,
  421. size_t *size, size_t idx)
  422. {
  423. flv_packet_audio_ex(packet, codec, dts_offset, output, size, AUDIO_PACKETTYPE_FRAMES, idx);
  424. }
  425. void flv_packet_metadata(enum video_id_t codec_id, uint8_t **output, size_t *size, int bits_per_raw_sample,
  426. uint8_t color_primaries, int color_trc, int color_space, int min_luminance, int max_luminance,
  427. size_t idx)
  428. {
  429. // metadata array
  430. struct array_output_data data;
  431. struct array_output_data metadata;
  432. struct serializer s;
  433. array_output_serializer_init(&s, &data);
  434. // metadata data array
  435. {
  436. struct serializer s;
  437. array_output_serializer_init(&s, &metadata);
  438. s_w8(&s, DATA_TYPE_STRING);
  439. s_wstring(&s, "colorInfo");
  440. s_w8(&s, DATA_TYPE_OBJECT);
  441. {
  442. // colorConfig:
  443. s_wstring(&s, "colorConfig");
  444. s_w8(&s, DATA_TYPE_OBJECT);
  445. {
  446. s_wstring(&s, "bitDepth");
  447. s_w8(&s, DATA_TYPE_NUMBER);
  448. s_wbd(&s, bits_per_raw_sample);
  449. s_wstring(&s, "colorPrimaries");
  450. s_w8(&s, DATA_TYPE_NUMBER);
  451. s_wbd(&s, color_primaries);
  452. s_wstring(&s, "transferCharacteristics");
  453. s_w8(&s, DATA_TYPE_NUMBER);
  454. s_wbd(&s, color_trc);
  455. s_wstring(&s, "matrixCoefficients");
  456. s_w8(&s, DATA_TYPE_NUMBER);
  457. s_wbd(&s, color_space);
  458. }
  459. s_w8(&s, 0);
  460. s_w8(&s, 0);
  461. s_w8(&s, DATA_TYPE_OBJECT_END);
  462. if (max_luminance != 0) {
  463. // hdrMdcv
  464. s_wstring(&s, "hdrMdcv");
  465. s_w8(&s, DATA_TYPE_OBJECT);
  466. {
  467. s_wstring(&s, "maxLuminance");
  468. s_w8(&s, DATA_TYPE_NUMBER);
  469. s_wbd(&s, max_luminance);
  470. s_wstring(&s, "minLuminance");
  471. s_w8(&s, DATA_TYPE_NUMBER);
  472. s_wbd(&s, min_luminance);
  473. }
  474. s_w8(&s, 0);
  475. s_w8(&s, 0);
  476. s_w8(&s, DATA_TYPE_OBJECT_END);
  477. }
  478. }
  479. s_w8(&s, 0);
  480. s_w8(&s, 0);
  481. s_w8(&s, DATA_TYPE_OBJECT_END);
  482. }
  483. bool is_multitrack = idx > 0;
  484. // packet head
  485. // w8+w4cc
  486. int header_metadata_size = 5;
  487. if (is_multitrack) {
  488. // w8+w8
  489. header_metadata_size += 2;
  490. }
  491. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  492. s_wb24(&s, (uint32_t)metadata.bytes.num + header_metadata_size);
  493. s_wtimestamp(&s, 0);
  494. s_wb24(&s, 0); // always 0
  495. // packet ext header
  496. // these are the 5 extra bytes mentioned above
  497. s_w8(&s, FRAME_HEADER_EX | (is_multitrack ? PACKETTYPE_MULTITRACK : PACKETTYPE_METADATA));
  498. /*
  499. * We only add explicitly emit trackIds iff idx > 0.
  500. * The default trackId is 0.
  501. */
  502. if (is_multitrack) {
  503. s_w8(&s, (uint8_t)MULTITRACKTYPE_ONE_TRACK | (uint8_t)PACKETTYPE_METADATA);
  504. s_w4cc(&s, codec_id);
  505. // trackId
  506. s_w8(&s, (uint8_t)idx);
  507. } else {
  508. s_w4cc(&s, codec_id);
  509. }
  510. // packet data
  511. s_write(&s, metadata.bytes.array, metadata.bytes.num);
  512. array_output_serializer_free(&metadata); // must be freed
  513. // packet tail
  514. write_previous_tag_size(&s);
  515. *output = data.bytes.array;
  516. *size = data.bytes.num;
  517. }