flv-mux.c 20 KB

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