flv-mux.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  173. bool write_header)
  174. {
  175. struct array_output_data data;
  176. struct serializer s;
  177. uint8_t *meta_data = NULL;
  178. size_t meta_data_size;
  179. uint32_t start_pos;
  180. array_output_serializer_init(&s, &data);
  181. build_flv_meta_data(context, &meta_data, &meta_data_size);
  182. if (write_header) {
  183. s_write(&s, "FLV", 3);
  184. s_w8(&s, 1);
  185. s_w8(&s, 5);
  186. s_wb32(&s, 9);
  187. s_wb32(&s, 0);
  188. }
  189. start_pos = serializer_get_pos(&s);
  190. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  191. s_wb24(&s, (uint32_t)meta_data_size);
  192. s_wb32(&s, 0);
  193. s_wb24(&s, 0);
  194. s_write(&s, meta_data, meta_data_size);
  195. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1);
  196. *output = data.bytes.array;
  197. *size = data.bytes.num;
  198. bfree(meta_data);
  199. }
  200. #ifdef DEBUG_TIMESTAMPS
  201. static int32_t last_time = 0;
  202. #endif
  203. static void flv_video(struct serializer *s, int32_t dts_offset,
  204. struct encoder_packet *packet, bool is_header)
  205. {
  206. int64_t offset = packet->pts - packet->dts;
  207. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  208. if (!packet->data || !packet->size)
  209. return;
  210. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  211. #ifdef DEBUG_TIMESTAMPS
  212. blog(LOG_DEBUG, "Video: %lu", time_ms);
  213. if (last_time > time_ms)
  214. blog(LOG_DEBUG, "Non-monotonic");
  215. last_time = time_ms;
  216. #endif
  217. s_wb24(s, (uint32_t)packet->size + 5);
  218. s_wb24(s, (uint32_t)time_ms);
  219. s_w8(s, (time_ms >> 24) & 0x7F);
  220. s_wb24(s, 0);
  221. /* these are the 5 extra bytes mentioned above */
  222. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  223. s_w8(s, is_header ? 0 : 1);
  224. s_wb24(s, get_ms_time(packet, offset));
  225. s_write(s, packet->data, packet->size);
  226. /* write tag size (starting byte doesn't count) */
  227. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  228. }
  229. static void flv_audio(struct serializer *s, int32_t dts_offset,
  230. struct encoder_packet *packet, bool is_header)
  231. {
  232. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  233. if (!packet->data || !packet->size)
  234. return;
  235. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  236. #ifdef DEBUG_TIMESTAMPS
  237. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  238. if (last_time > time_ms)
  239. blog(LOG_DEBUG, "Non-monotonic");
  240. last_time = time_ms;
  241. #endif
  242. s_wb24(s, (uint32_t)packet->size + 2);
  243. s_wb24(s, (uint32_t)time_ms);
  244. s_w8(s, (time_ms >> 24) & 0x7F);
  245. s_wb24(s, 0);
  246. /* these are the two extra bytes mentioned above */
  247. s_w8(s, 0xaf);
  248. s_w8(s, is_header ? 0 : 1);
  249. s_write(s, packet->data, packet->size);
  250. /* write tag size (starting byte doesn't count) */
  251. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  252. }
  253. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  254. uint8_t **output, size_t *size, bool is_header)
  255. {
  256. struct array_output_data data;
  257. struct serializer s;
  258. array_output_serializer_init(&s, &data);
  259. if (packet->type == OBS_ENCODER_VIDEO)
  260. flv_video(&s, dts_offset, packet, is_header);
  261. else
  262. flv_audio(&s, dts_offset, packet, is_header);
  263. *output = data.bytes.array;
  264. *size = data.bytes.num;
  265. }
  266. // Y2023 spec
  267. void flv_packet_ex(struct encoder_packet *packet, enum video_id_t codec_id,
  268. int32_t dts_offset, uint8_t **output, size_t *size, int type)
  269. {
  270. struct array_output_data data;
  271. struct serializer s;
  272. array_output_serializer_init(&s, &data);
  273. assert(packet->type == OBS_ENCODER_VIDEO);
  274. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  275. // packet head
  276. int header_metadata_size = 5;
  277. #ifdef ENABLE_HEVC
  278. // 3 extra bytes for composition time offset
  279. if (codec_id == CODEC_HEVC && type == PACKETTYPE_FRAMES) {
  280. header_metadata_size = 8;
  281. }
  282. #endif
  283. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  284. s_wb24(&s, (uint32_t)packet->size + header_metadata_size);
  285. s_wtimestamp(&s, time_ms);
  286. s_wb24(&s, 0); // always 0
  287. // packet ext header
  288. s_w8(&s,
  289. FRAME_HEADER_EX | type | (packet->keyframe ? FT_KEY : FT_INTER));
  290. s_w4cc(&s, codec_id);
  291. #ifdef ENABLE_HEVC
  292. // hevc composition time offset
  293. if (codec_id == CODEC_HEVC && type == PACKETTYPE_FRAMES) {
  294. s_wb24(&s, get_ms_time(packet, packet->pts - packet->dts));
  295. }
  296. #endif
  297. // packet data
  298. s_write(&s, packet->data, packet->size);
  299. // packet tail
  300. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1);
  301. *output = data.bytes.array;
  302. *size = data.bytes.num;
  303. }
  304. void flv_packet_start(struct encoder_packet *packet, enum video_id_t codec,
  305. uint8_t **output, size_t *size)
  306. {
  307. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_START);
  308. }
  309. void flv_packet_frames(struct encoder_packet *packet, enum video_id_t codec,
  310. int32_t dts_offset, uint8_t **output, size_t *size)
  311. {
  312. int packet_type = PACKETTYPE_FRAMES;
  313. #ifdef ENABLE_HEVC
  314. // PACKETTYPE_FRAMESX is an optimization to avoid sending composition
  315. // time offsets of 0. See Enhanced RTMP spec.
  316. if (codec == CODEC_HEVC && packet->dts == packet->pts)
  317. packet_type = PACKETTYPE_FRAMESX;
  318. #endif
  319. flv_packet_ex(packet, codec, dts_offset, output, size, packet_type);
  320. }
  321. void flv_packet_end(struct encoder_packet *packet, enum video_id_t codec,
  322. uint8_t **output, size_t *size)
  323. {
  324. flv_packet_ex(packet, codec, 0, output, size, PACKETTYPE_SEQ_END);
  325. }
  326. void flv_packet_metadata(enum video_id_t codec_id, uint8_t **output,
  327. size_t *size, int bits_per_raw_sample,
  328. uint8_t color_primaries, int color_trc,
  329. int color_space, int min_luminance, int max_luminance)
  330. {
  331. // metadata array
  332. struct array_output_data data;
  333. struct array_output_data metadata;
  334. struct serializer s;
  335. array_output_serializer_init(&s, &data);
  336. // metadata data array
  337. {
  338. struct serializer s;
  339. array_output_serializer_init(&s, &metadata);
  340. s_w8(&s, DATA_TYPE_STRING);
  341. s_wstring(&s, "colorInfo");
  342. s_w8(&s, DATA_TYPE_OBJECT);
  343. {
  344. // colorConfig:
  345. s_wstring(&s, "colorConfig");
  346. s_w8(&s, DATA_TYPE_OBJECT);
  347. {
  348. s_wstring(&s, "bitDepth");
  349. s_w8(&s, DATA_TYPE_NUMBER);
  350. s_wbd(&s, bits_per_raw_sample);
  351. s_wstring(&s, "colorPrimaries");
  352. s_w8(&s, DATA_TYPE_NUMBER);
  353. s_wbd(&s, color_primaries);
  354. s_wstring(&s, "transferCharacteristics");
  355. s_w8(&s, DATA_TYPE_NUMBER);
  356. s_wbd(&s, color_trc);
  357. s_wstring(&s, "matrixCoefficients");
  358. s_w8(&s, DATA_TYPE_NUMBER);
  359. s_wbd(&s, color_space);
  360. }
  361. s_w8(&s, 0);
  362. s_w8(&s, 0);
  363. s_w8(&s, DATA_TYPE_OBJECT_END);
  364. if (max_luminance != 0) {
  365. // hdrMdcv
  366. s_wstring(&s, "hdrMdcv");
  367. s_w8(&s, DATA_TYPE_OBJECT);
  368. {
  369. s_wstring(&s, "maxLuminance");
  370. s_w8(&s, DATA_TYPE_NUMBER);
  371. s_wbd(&s, max_luminance);
  372. s_wstring(&s, "minLuminance");
  373. s_w8(&s, DATA_TYPE_NUMBER);
  374. s_wbd(&s, min_luminance);
  375. }
  376. s_w8(&s, 0);
  377. s_w8(&s, 0);
  378. s_w8(&s, DATA_TYPE_OBJECT_END);
  379. }
  380. }
  381. s_w8(&s, 0);
  382. s_w8(&s, 0);
  383. s_w8(&s, DATA_TYPE_OBJECT_END);
  384. }
  385. // packet head
  386. s_w8(&s, RTMP_PACKET_TYPE_VIDEO);
  387. s_wb24(&s, (uint32_t)metadata.bytes.num + 5); // 5 = (w8+w4cc)
  388. s_wtimestamp(&s, 0);
  389. s_wb24(&s, 0); // always 0
  390. // packet ext header
  391. // these are the 5 extra bytes mentioned above
  392. s_w8(&s, FRAME_HEADER_EX | PACKETTYPE_METADATA);
  393. s_w4cc(&s, codec_id);
  394. // packet data
  395. s_write(&s, metadata.bytes.array, metadata.bytes.num);
  396. array_output_serializer_free(&metadata); // must be freed
  397. // packet tail
  398. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1);
  399. *output = data.bytes.array;
  400. *size = data.bytes.num;
  401. }
  402. /* ------------------------------------------------------------------------- */
  403. /* stuff for additional media streams */
  404. #define s_amf_conststring(s, str) \
  405. do { \
  406. const size_t len = sizeof(str) - 1; \
  407. s_wb16(s, (uint16_t)len); \
  408. serialize(s, str, len); \
  409. } while (false)
  410. #define s_amf_double(s, d) \
  411. do { \
  412. double d_val = d; \
  413. uint64_t u_val = *(uint64_t *)&d_val; \
  414. s_wb64(s, u_val); \
  415. } while (false)
  416. static void flv_build_additional_meta_data(uint8_t **data, size_t *size)
  417. {
  418. struct array_output_data out;
  419. struct serializer s;
  420. array_output_serializer_init(&s, &out);
  421. s_w8(&s, AMF_STRING);
  422. s_amf_conststring(&s, "@setDataFrame");
  423. s_w8(&s, AMF_STRING);
  424. s_amf_conststring(&s, "onExpectAdditionalMedia");
  425. s_w8(&s, AMF_OBJECT);
  426. {
  427. s_amf_conststring(&s, "processingIntents");
  428. s_w8(&s, AMF_STRICT_ARRAY);
  429. s_wb32(&s, 1);
  430. {
  431. s_w8(&s, AMF_STRING);
  432. s_amf_conststring(&s, "ArchiveProgramNarrationAudio");
  433. }
  434. /* ---- */
  435. s_amf_conststring(&s, "additionalMedia");
  436. s_w8(&s, AMF_OBJECT);
  437. {
  438. s_amf_conststring(&s, "stream0");
  439. s_w8(&s, AMF_OBJECT);
  440. {
  441. s_amf_conststring(&s, "type");
  442. s_w8(&s, AMF_NUMBER);
  443. s_amf_double(&s, RTMP_PACKET_TYPE_AUDIO);
  444. /* ---- */
  445. s_amf_conststring(&s, "mediaLabels");
  446. s_w8(&s, AMF_OBJECT);
  447. {
  448. s_amf_conststring(&s, "contentType");
  449. s_w8(&s, AMF_STRING);
  450. s_amf_conststring(&s, "PNAR");
  451. }
  452. s_wb24(&s, AMF_OBJECT_END);
  453. }
  454. s_wb24(&s, AMF_OBJECT_END);
  455. }
  456. s_wb24(&s, AMF_OBJECT_END);
  457. /* ---- */
  458. s_amf_conststring(&s, "defaultMedia");
  459. s_w8(&s, AMF_OBJECT);
  460. {
  461. s_amf_conststring(&s, "audio");
  462. s_w8(&s, AMF_OBJECT);
  463. {
  464. s_amf_conststring(&s, "mediaLabels");
  465. s_w8(&s, AMF_OBJECT);
  466. {
  467. s_amf_conststring(&s, "contentType");
  468. s_w8(&s, AMF_STRING);
  469. s_amf_conststring(&s, "PRM");
  470. }
  471. s_wb24(&s, AMF_OBJECT_END);
  472. }
  473. s_wb24(&s, AMF_OBJECT_END);
  474. }
  475. s_wb24(&s, AMF_OBJECT_END);
  476. }
  477. s_wb24(&s, AMF_OBJECT_END);
  478. *data = out.bytes.array;
  479. *size = out.bytes.num;
  480. }
  481. void flv_additional_meta_data(obs_output_t *context, uint8_t **data,
  482. size_t *size)
  483. {
  484. UNUSED_PARAMETER(context);
  485. struct array_output_data out;
  486. struct serializer s;
  487. uint8_t *meta_data = NULL;
  488. size_t meta_data_size;
  489. flv_build_additional_meta_data(&meta_data, &meta_data_size);
  490. array_output_serializer_init(&s, &out);
  491. s_w8(&s, RTMP_PACKET_TYPE_INFO); //18
  492. s_wb24(&s, (uint32_t)meta_data_size);
  493. s_wb32(&s, 0);
  494. s_wb24(&s, 0);
  495. s_write(&s, meta_data, meta_data_size);
  496. bfree(meta_data);
  497. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1);
  498. *data = out.bytes.array;
  499. *size = out.bytes.num;
  500. }
  501. static inline void s_u29(struct serializer *s, uint32_t val)
  502. {
  503. if (val <= 0x7F) {
  504. s_w8(s, val);
  505. } else if (val <= 0x3FFF) {
  506. s_w8(s, 0x80 | (val >> 7));
  507. s_w8(s, val & 0x7F);
  508. } else if (val <= 0x1FFFFF) {
  509. s_w8(s, 0x80 | (val >> 14));
  510. s_w8(s, 0x80 | ((val >> 7) & 0x7F));
  511. s_w8(s, val & 0x7F);
  512. } else {
  513. s_w8(s, 0x80 | (val >> 22));
  514. s_w8(s, 0x80 | ((val >> 15) & 0x7F));
  515. s_w8(s, 0x80 | ((val >> 8) & 0x7F));
  516. s_w8(s, val & 0xFF);
  517. }
  518. }
  519. static inline void s_u29b_value(struct serializer *s, uint32_t val)
  520. {
  521. s_u29(s, 1 | ((val & 0xFFFFFFF) << 1));
  522. }
  523. static void flv_build_additional_audio(uint8_t **data, size_t *size,
  524. struct encoder_packet *packet,
  525. bool is_header, size_t index)
  526. {
  527. UNUSED_PARAMETER(index);
  528. struct array_output_data out;
  529. struct serializer s;
  530. array_output_serializer_init(&s, &out);
  531. s_w8(&s, AMF_STRING);
  532. s_amf_conststring(&s, "additionalMedia");
  533. s_w8(&s, AMF_OBJECT);
  534. {
  535. s_amf_conststring(&s, "id");
  536. s_w8(&s, AMF_STRING);
  537. s_amf_conststring(&s, "stream0");
  538. /* ----- */
  539. s_amf_conststring(&s, "media");
  540. s_w8(&s, AMF_AVMPLUS);
  541. s_w8(&s, AMF3_BYTE_ARRAY);
  542. s_u29b_value(&s, (uint32_t)packet->size + 2);
  543. s_w8(&s, 0xaf);
  544. s_w8(&s, is_header ? 0 : 1);
  545. s_write(&s, packet->data, packet->size);
  546. }
  547. s_wb24(&s, AMF_OBJECT_END);
  548. *data = out.bytes.array;
  549. *size = out.bytes.num;
  550. }
  551. static void flv_additional_audio(struct serializer *s, int32_t dts_offset,
  552. struct encoder_packet *packet, bool is_header,
  553. size_t index)
  554. {
  555. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  556. uint8_t *data;
  557. size_t size;
  558. if (!packet->data || !packet->size)
  559. return;
  560. flv_build_additional_audio(&data, &size, packet, is_header, index);
  561. s_w8(s, RTMP_PACKET_TYPE_INFO); //18
  562. #ifdef DEBUG_TIMESTAMPS
  563. blog(LOG_DEBUG, "Audio2: %lu", time_ms);
  564. if (last_time > time_ms)
  565. blog(LOG_DEBUG, "Non-monotonic");
  566. last_time = time_ms;
  567. #endif
  568. s_wb24(s, (uint32_t)size);
  569. s_wb24(s, (uint32_t)time_ms);
  570. s_w8(s, (time_ms >> 24) & 0x7F);
  571. s_wb24(s, 0);
  572. serialize(s, data, size);
  573. bfree(data);
  574. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  575. }
  576. void flv_additional_packet_mux(struct encoder_packet *packet,
  577. int32_t dts_offset, uint8_t **data, size_t *size,
  578. bool is_header, size_t index)
  579. {
  580. struct array_output_data out;
  581. struct serializer s;
  582. array_output_serializer_init(&s, &out);
  583. if (packet->type == OBS_ENCODER_VIDEO) {
  584. //currently unsupported
  585. bcrash("who said you could output an additional video packet?");
  586. } else {
  587. flv_additional_audio(&s, dts_offset, packet, is_header, index);
  588. }
  589. *data = out.bytes.array;
  590. *size = out.bytes.num;
  591. }