1
0

flv-mux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. static inline double encoder_bitrate(obs_encoder_t *encoder)
  28. {
  29. obs_data_t *settings = obs_encoder_get_settings(encoder);
  30. double bitrate = obs_data_get_double(settings, "bitrate");
  31. obs_data_release(settings);
  32. return bitrate;
  33. }
  34. #define FLV_INFO_SIZE_OFFSET 42
  35. void write_file_info(FILE *file, int64_t duration_ms, int64_t size)
  36. {
  37. char buf[64];
  38. char *enc = buf;
  39. char *end = enc + sizeof(buf);
  40. fseek(file, FLV_INFO_SIZE_OFFSET, SEEK_SET);
  41. enc_num_val(&enc, end, "duration", (double)duration_ms / 1000.0);
  42. enc_num_val(&enc, end, "fileSize", (double)size);
  43. fwrite(buf, 1, enc - buf, file);
  44. }
  45. static void build_flv_meta_data(obs_output_t *context, uint8_t **output,
  46. size_t *size)
  47. {
  48. obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
  49. obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
  50. video_t *video = obs_encoder_video(vencoder);
  51. audio_t *audio = obs_encoder_audio(aencoder);
  52. char buf[4096];
  53. char *enc = buf;
  54. char *end = enc + sizeof(buf);
  55. struct dstr encoder_name = {0};
  56. enc_str(&enc, end, "@setDataFrame");
  57. enc_str(&enc, end, "onMetaData");
  58. *enc++ = AMF_ECMA_ARRAY;
  59. enc = AMF_EncodeInt32(enc, end, 20);
  60. enc_num_val(&enc, end, "duration", 0.0);
  61. enc_num_val(&enc, end, "fileSize", 0.0);
  62. enc_num_val(&enc, end, "width",
  63. (double)obs_encoder_get_width(vencoder));
  64. enc_num_val(&enc, end, "height",
  65. (double)obs_encoder_get_height(vencoder));
  66. enc_num_val(&enc, end, "videocodecid", VIDEODATA_AVCVIDEOPACKET);
  67. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  68. enc_num_val(&enc, end, "framerate", video_output_get_frame_rate(video));
  69. enc_num_val(&enc, end, "audiocodecid", AUDIODATA_AAC);
  70. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  71. enc_num_val(&enc, end, "audiosamplerate",
  72. (double)obs_encoder_get_sample_rate(aencoder));
  73. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  74. enc_num_val(&enc, end, "audiochannels",
  75. (double)audio_output_get_channels(audio));
  76. enc_bool_val(&enc, end, "stereo",
  77. audio_output_get_channels(audio) == 2);
  78. enc_bool_val(&enc, end, "2.1", audio_output_get_channels(audio) == 3);
  79. enc_bool_val(&enc, end, "3.1", audio_output_get_channels(audio) == 4);
  80. enc_bool_val(&enc, end, "4.0", audio_output_get_channels(audio) == 4);
  81. enc_bool_val(&enc, end, "4.1", audio_output_get_channels(audio) == 5);
  82. enc_bool_val(&enc, end, "5.1", audio_output_get_channels(audio) == 6);
  83. enc_bool_val(&enc, end, "7.1", audio_output_get_channels(audio) == 8);
  84. dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME);
  85. #ifdef HAVE_OBSCONFIG_H
  86. dstr_cat(&encoder_name, OBS_VERSION);
  87. #else
  88. dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER,
  89. LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER);
  90. #endif
  91. dstr_cat(&encoder_name, ")");
  92. enc_str_val(&enc, end, "encoder", encoder_name.array);
  93. dstr_free(&encoder_name);
  94. *enc++ = 0;
  95. *enc++ = 0;
  96. *enc++ = AMF_OBJECT_END;
  97. *size = enc - buf;
  98. *output = bmemdup(buf, *size);
  99. }
  100. void flv_meta_data(obs_output_t *context, uint8_t **output, size_t *size,
  101. bool write_header)
  102. {
  103. struct array_output_data data;
  104. struct serializer s;
  105. uint8_t *meta_data = NULL;
  106. size_t meta_data_size;
  107. uint32_t start_pos;
  108. array_output_serializer_init(&s, &data);
  109. build_flv_meta_data(context, &meta_data, &meta_data_size);
  110. if (write_header) {
  111. s_write(&s, "FLV", 3);
  112. s_w8(&s, 1);
  113. s_w8(&s, 5);
  114. s_wb32(&s, 9);
  115. s_wb32(&s, 0);
  116. }
  117. start_pos = serializer_get_pos(&s);
  118. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  119. s_wb24(&s, (uint32_t)meta_data_size);
  120. s_wb32(&s, 0);
  121. s_wb24(&s, 0);
  122. s_write(&s, meta_data, meta_data_size);
  123. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos - 1);
  124. *output = data.bytes.array;
  125. *size = data.bytes.num;
  126. bfree(meta_data);
  127. }
  128. #ifdef DEBUG_TIMESTAMPS
  129. static int32_t last_time = 0;
  130. #endif
  131. static void flv_video(struct serializer *s, int32_t dts_offset,
  132. struct encoder_packet *packet, bool is_header)
  133. {
  134. int64_t offset = packet->pts - packet->dts;
  135. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  136. if (!packet->data || !packet->size)
  137. return;
  138. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  139. #ifdef DEBUG_TIMESTAMPS
  140. blog(LOG_DEBUG, "Video: %lu", time_ms);
  141. if (last_time > time_ms)
  142. blog(LOG_DEBUG, "Non-monotonic");
  143. last_time = time_ms;
  144. #endif
  145. s_wb24(s, (uint32_t)packet->size + 5);
  146. s_wb24(s, time_ms);
  147. s_w8(s, (time_ms >> 24) & 0x7F);
  148. s_wb24(s, 0);
  149. /* these are the 5 extra bytes mentioned above */
  150. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  151. s_w8(s, is_header ? 0 : 1);
  152. s_wb24(s, get_ms_time(packet, offset));
  153. s_write(s, packet->data, packet->size);
  154. /* write tag size (starting byte doesn't count) */
  155. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  156. }
  157. static void flv_audio(struct serializer *s, int32_t dts_offset,
  158. struct encoder_packet *packet, bool is_header)
  159. {
  160. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  161. if (!packet->data || !packet->size)
  162. return;
  163. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  164. #ifdef DEBUG_TIMESTAMPS
  165. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  166. if (last_time > time_ms)
  167. blog(LOG_DEBUG, "Non-monotonic");
  168. last_time = time_ms;
  169. #endif
  170. s_wb24(s, (uint32_t)packet->size + 2);
  171. s_wb24(s, time_ms);
  172. s_w8(s, (time_ms >> 24) & 0x7F);
  173. s_wb24(s, 0);
  174. /* these are the two extra bytes mentioned above */
  175. s_w8(s, 0xaf);
  176. s_w8(s, is_header ? 0 : 1);
  177. s_write(s, packet->data, packet->size);
  178. /* write tag size (starting byte doesn't count) */
  179. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  180. }
  181. void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset,
  182. uint8_t **output, size_t *size, bool is_header)
  183. {
  184. struct array_output_data data;
  185. struct serializer s;
  186. array_output_serializer_init(&s, &data);
  187. if (packet->type == OBS_ENCODER_VIDEO)
  188. flv_video(&s, dts_offset, packet, is_header);
  189. else
  190. flv_audio(&s, dts_offset, packet, is_header);
  191. *output = data.bytes.array;
  192. *size = data.bytes.num;
  193. }
  194. /* ------------------------------------------------------------------------- */
  195. /* stuff for additional media streams */
  196. #define s_amf_conststring(s, str) \
  197. do { \
  198. const size_t len = sizeof(str) - 1; \
  199. s_wb16(s, (uint16_t)len); \
  200. serialize(s, str, len); \
  201. } while (false)
  202. #define s_amf_double(s, d) \
  203. do { \
  204. double d_val = d; \
  205. uint64_t u_val = *(uint64_t *)&d_val; \
  206. s_wb64(s, u_val); \
  207. } while (false)
  208. static void flv_build_additional_meta_data(uint8_t **data, size_t *size)
  209. {
  210. struct array_output_data out;
  211. struct serializer s;
  212. array_output_serializer_init(&s, &out);
  213. s_w8(&s, AMF_STRING);
  214. s_amf_conststring(&s, "@setDataFrame");
  215. s_w8(&s, AMF_STRING);
  216. s_amf_conststring(&s, "onExpectAdditionalMedia");
  217. s_w8(&s, AMF_OBJECT);
  218. {
  219. s_amf_conststring(&s, "processingIntents");
  220. s_w8(&s, AMF_STRICT_ARRAY);
  221. s_wb32(&s, 1);
  222. {
  223. s_w8(&s, AMF_STRING);
  224. s_amf_conststring(&s, "ArchiveProgramNarrationAudio");
  225. }
  226. /* ---- */
  227. s_amf_conststring(&s, "additionalMedia");
  228. s_w8(&s, AMF_OBJECT);
  229. {
  230. s_amf_conststring(&s, "stream0");
  231. s_w8(&s, AMF_OBJECT);
  232. {
  233. s_amf_conststring(&s, "type");
  234. s_w8(&s, AMF_NUMBER);
  235. s_amf_double(&s, RTMP_PACKET_TYPE_AUDIO);
  236. /* ---- */
  237. s_amf_conststring(&s, "mediaLabels");
  238. s_w8(&s, AMF_OBJECT);
  239. {
  240. s_amf_conststring(&s, "contentType");
  241. s_w8(&s, AMF_STRING);
  242. s_amf_conststring(&s, "PNAR");
  243. }
  244. s_wb24(&s, AMF_OBJECT_END);
  245. }
  246. s_wb24(&s, AMF_OBJECT_END);
  247. }
  248. s_wb24(&s, AMF_OBJECT_END);
  249. /* ---- */
  250. s_amf_conststring(&s, "defaultMedia");
  251. s_w8(&s, AMF_OBJECT);
  252. {
  253. s_amf_conststring(&s, "audio");
  254. s_w8(&s, AMF_OBJECT);
  255. {
  256. s_amf_conststring(&s, "mediaLabels");
  257. s_w8(&s, AMF_OBJECT);
  258. {
  259. s_amf_conststring(&s, "contentType");
  260. s_w8(&s, AMF_STRING);
  261. s_amf_conststring(&s, "PRM");
  262. }
  263. s_wb24(&s, AMF_OBJECT_END);
  264. }
  265. s_wb24(&s, AMF_OBJECT_END);
  266. }
  267. s_wb24(&s, AMF_OBJECT_END);
  268. }
  269. s_wb24(&s, AMF_OBJECT_END);
  270. *data = out.bytes.array;
  271. *size = out.bytes.num;
  272. }
  273. void flv_additional_meta_data(obs_output_t *context, uint8_t **data,
  274. size_t *size)
  275. {
  276. UNUSED_PARAMETER(context);
  277. struct array_output_data out;
  278. struct serializer s;
  279. uint8_t *meta_data = NULL;
  280. size_t meta_data_size;
  281. flv_build_additional_meta_data(&meta_data, &meta_data_size);
  282. array_output_serializer_init(&s, &out);
  283. s_w8(&s, RTMP_PACKET_TYPE_INFO); //18
  284. s_wb24(&s, (uint32_t)meta_data_size);
  285. s_wb32(&s, 0);
  286. s_wb24(&s, 0);
  287. s_write(&s, meta_data, meta_data_size);
  288. bfree(meta_data);
  289. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1);
  290. *data = out.bytes.array;
  291. *size = out.bytes.num;
  292. }
  293. static inline void s_u29(struct serializer *s, uint32_t val)
  294. {
  295. if (val <= 0x7F) {
  296. s_w8(s, val);
  297. } else if (val <= 0x3FFF) {
  298. s_w8(s, 0x80 | (val >> 7));
  299. s_w8(s, val & 0x7F);
  300. } else if (val <= 0x1FFFFF) {
  301. s_w8(s, 0x80 | (val >> 14));
  302. s_w8(s, 0x80 | ((val >> 7) & 0x7F));
  303. s_w8(s, val & 0x7F);
  304. } else {
  305. s_w8(s, 0x80 | (val >> 22));
  306. s_w8(s, 0x80 | ((val >> 15) & 0x7F));
  307. s_w8(s, 0x80 | ((val >> 8) & 0x7F));
  308. s_w8(s, val & 0xFF);
  309. }
  310. }
  311. static inline void s_u29b_value(struct serializer *s, uint32_t val)
  312. {
  313. s_u29(s, 1 | ((val & 0xFFFFFFF) << 1));
  314. }
  315. static void flv_build_additional_audio(uint8_t **data, size_t *size,
  316. struct encoder_packet *packet,
  317. bool is_header, size_t index)
  318. {
  319. UNUSED_PARAMETER(index);
  320. struct array_output_data out;
  321. struct serializer s;
  322. array_output_serializer_init(&s, &out);
  323. s_w8(&s, AMF_STRING);
  324. s_amf_conststring(&s, "additionalMedia");
  325. s_w8(&s, AMF_OBJECT);
  326. {
  327. s_amf_conststring(&s, "id");
  328. s_w8(&s, AMF_STRING);
  329. s_amf_conststring(&s, "stream0");
  330. /* ----- */
  331. s_amf_conststring(&s, "media");
  332. s_w8(&s, AMF_AVMPLUS);
  333. s_w8(&s, AMF3_BYTE_ARRAY);
  334. s_u29b_value(&s, (uint32_t)packet->size + 2);
  335. s_w8(&s, 0xaf);
  336. s_w8(&s, is_header ? 0 : 1);
  337. s_write(&s, packet->data, packet->size);
  338. }
  339. s_wb24(&s, AMF_OBJECT_END);
  340. *data = out.bytes.array;
  341. *size = out.bytes.num;
  342. }
  343. static void flv_additional_audio(struct serializer *s, int32_t dts_offset,
  344. struct encoder_packet *packet, bool is_header,
  345. size_t index)
  346. {
  347. int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset;
  348. uint8_t *data;
  349. size_t size;
  350. if (!packet->data || !packet->size)
  351. return;
  352. flv_build_additional_audio(&data, &size, packet, is_header, index);
  353. s_w8(s, RTMP_PACKET_TYPE_INFO); //18
  354. #ifdef DEBUG_TIMESTAMPS
  355. blog(LOG_DEBUG, "Audio2: %lu", time_ms);
  356. if (last_time > time_ms)
  357. blog(LOG_DEBUG, "Non-monotonic");
  358. last_time = time_ms;
  359. #endif
  360. s_wb24(s, (uint32_t)size);
  361. s_wb24(s, time_ms);
  362. s_w8(s, (time_ms >> 24) & 0x7F);
  363. s_wb24(s, 0);
  364. serialize(s, data, size);
  365. bfree(data);
  366. s_wb32(s, (uint32_t)serializer_get_pos(s) - 1);
  367. }
  368. void flv_additional_packet_mux(struct encoder_packet *packet,
  369. int32_t dts_offset, uint8_t **data, size_t *size,
  370. bool is_header, size_t index)
  371. {
  372. struct array_output_data out;
  373. struct serializer s;
  374. array_output_serializer_init(&s, &out);
  375. if (packet->type == OBS_ENCODER_VIDEO) {
  376. //currently unsupported
  377. bcrash("who said you could output an additional video packet?");
  378. } else {
  379. flv_additional_audio(&s, dts_offset, packet, is_header, index);
  380. }
  381. *data = out.bytes.array;
  382. *size = out.bytes.num;
  383. }