1
0

flv-mux.c 17 KB

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