flv-mux.c 17 KB

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