1
0

flv-mux.c 18 KB

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