flv-mux.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/array-serializer.h>
  17. #include "flv-mux.h"
  18. #include "obs-output-ver.h"
  19. #include "rtmp-helpers.h"
  20. /* TODO: FIXME: this is currently hard-coded to h264 and aac! ..not that we'll
  21. * use anything else for a long time. */
  22. //#define DEBUG_TIMESTAMPS
  23. //#define WRITE_FLV_HEADER
  24. #define VIDEO_HEADER_SIZE 5
  25. static inline double encoder_bitrate(obs_encoder_t encoder)
  26. {
  27. obs_data_t settings = obs_encoder_get_settings(encoder);
  28. double bitrate = obs_data_getdouble(settings, "bitrate");
  29. obs_data_release(settings);
  30. return bitrate;
  31. }
  32. #define FLV_INFO_SIZE_OFFSET 42
  33. void write_file_info(FILE *file, int64_t duration_ms, int64_t size)
  34. {
  35. char buf[64];
  36. char *enc = buf;
  37. char *end = enc + sizeof(buf);
  38. fseek(file, FLV_INFO_SIZE_OFFSET, SEEK_SET);
  39. enc_num_val(&enc, end, "duration", (double)duration_ms / 1000.0);
  40. enc_num_val(&enc, end, "fileSize", (double)size);
  41. fwrite(buf, 1, enc - buf, file);
  42. }
  43. static void build_flv_meta_data(obs_output_t context,
  44. uint8_t **output, size_t *size)
  45. {
  46. obs_encoder_t vencoder = obs_output_get_video_encoder(context);
  47. obs_encoder_t aencoder = obs_output_get_audio_encoder(context);
  48. video_t video = obs_encoder_video(vencoder);
  49. audio_t audio = obs_encoder_audio(aencoder);
  50. char buf[4096];
  51. char *enc = buf;
  52. char *end = enc+sizeof(buf);
  53. enc_str(&enc, end, "onMetaData");
  54. *enc++ = AMF_ECMA_ARRAY;
  55. enc = AMF_EncodeInt32(enc, end, 14);
  56. enc_num_val(&enc, end, "duration", 0.0);
  57. enc_num_val(&enc, end, "fileSize", 0.0);
  58. enc_num_val(&enc, end, "width", (double)video_output_width(video));
  59. enc_num_val(&enc, end, "height", (double)video_output_height(video));
  60. enc_str_val(&enc, end, "videocodecid", "avc1");
  61. enc_num_val(&enc, end, "videodatarate", encoder_bitrate(vencoder));
  62. enc_num_val(&enc, end, "framerate", video_output_framerate(video));
  63. enc_str_val(&enc, end, "audiocodecid", "mp4a");
  64. enc_num_val(&enc, end, "audiodatarate", encoder_bitrate(aencoder));
  65. enc_num_val(&enc, end, "audiosamplerate",
  66. (double)audio_output_samplerate(audio));
  67. enc_num_val(&enc, end, "audiosamplesize", 16.0);
  68. enc_num_val(&enc, end, "audiochannels",
  69. (double)audio_output_channels(audio));
  70. enc_bool_val(&enc, end, "stereo", audio_output_channels(audio) == 2);
  71. enc_str_val(&enc, end, "encoder", MODULE_NAME);
  72. *enc++ = 0;
  73. *enc++ = 0;
  74. *enc++ = AMF_OBJECT_END;
  75. *size = enc-buf;
  76. *output = bmemdup(buf, *size);
  77. }
  78. void flv_meta_data(obs_output_t context, uint8_t **output, size_t *size,
  79. bool write_header)
  80. {
  81. struct array_output_data data;
  82. struct serializer s;
  83. uint8_t *meta_data;
  84. size_t meta_data_size;
  85. uint32_t start_pos;
  86. array_output_serializer_init(&s, &data);
  87. build_flv_meta_data(context, &meta_data, &meta_data_size);
  88. if (write_header) {
  89. s_write(&s, "FLV", 3);
  90. s_w8(&s, 1);
  91. s_w8(&s, 5);
  92. s_wb32(&s, 9);
  93. s_wb32(&s, 0);
  94. }
  95. start_pos = serializer_get_pos(&s);
  96. s_w8(&s, RTMP_PACKET_TYPE_INFO);
  97. s_wb24(&s, (uint32_t)meta_data_size);
  98. s_wb32(&s, 0);
  99. s_wb24(&s, 0);
  100. s_write(&s, meta_data, meta_data_size);
  101. s_wb32(&s, (uint32_t)serializer_get_pos(&s) - start_pos + 4 - 1);
  102. *output = data.bytes.array;
  103. *size = data.bytes.num;
  104. bfree(meta_data);
  105. }
  106. #ifdef DEBUG_TIMESTAMPS
  107. static int32_t last_time = 0;
  108. #endif
  109. static void flv_video(struct serializer *s, struct encoder_packet *packet,
  110. bool is_header)
  111. {
  112. int64_t offset = packet->pts - packet->dts;
  113. int32_t time_ms = get_ms_time(packet, packet->dts);
  114. if (!packet->data || !packet->size)
  115. return;
  116. s_w8(s, RTMP_PACKET_TYPE_VIDEO);
  117. #ifdef DEBUG_TIMESTAMPS
  118. blog(LOG_DEBUG, "Video: %lu", time_ms);
  119. if (last_time > time_ms)
  120. blog(LOG_DEBUG, "Non-monotonic");
  121. last_time = time_ms;
  122. #endif
  123. s_wb24(s, (uint32_t)packet->size + 5);
  124. s_wb24(s, time_ms);
  125. s_w8(s, (time_ms >> 24) & 0x7F);
  126. s_wb24(s, 0);
  127. /* these are the 5 extra bytes mentioned above */
  128. s_w8(s, packet->keyframe ? 0x17 : 0x27);
  129. s_w8(s, is_header ? 0 : 1);
  130. s_wb24(s, get_ms_time(packet, offset));
  131. s_write(s, packet->data, packet->size);
  132. /* write tag size (starting byte doesnt count) */
  133. s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1);
  134. }
  135. static void flv_audio(struct serializer *s, struct encoder_packet *packet,
  136. bool is_header)
  137. {
  138. int32_t time_ms = get_ms_time(packet, packet->dts);
  139. if (!packet->data || !packet->size)
  140. return;
  141. s_w8(s, RTMP_PACKET_TYPE_AUDIO);
  142. #ifdef DEBUG_TIMESTAMPS
  143. blog(LOG_DEBUG, "Audio: %lu", time_ms);
  144. if (last_time > time_ms)
  145. blog(LOG_DEBUG, "Non-monotonic");
  146. last_time = time_ms;
  147. #endif
  148. s_wb24(s, (uint32_t)packet->size + 2);
  149. s_wb24(s, time_ms);
  150. s_w8(s, (time_ms >> 24) & 0x7F);
  151. s_wb24(s, 0);
  152. /* these are the two extra bytes mentioned above */
  153. s_w8(s, 0xaf);
  154. s_w8(s, is_header ? 0 : 1);
  155. s_write(s, packet->data, packet->size);
  156. /* write tag size (starting byte doesnt count) */
  157. s_wb32(s, (uint32_t)serializer_get_pos(s) + 4 - 1);
  158. }
  159. void flv_packet_mux(struct encoder_packet *packet,
  160. uint8_t **output, size_t *size, bool is_header)
  161. {
  162. struct array_output_data data;
  163. struct serializer s;
  164. array_output_serializer_init(&s, &data);
  165. if (packet->type == OBS_ENCODER_VIDEO)
  166. flv_video(&s, packet, is_header);
  167. else
  168. flv_audio(&s, packet, is_header);
  169. *output = data.bytes.array;
  170. *size = data.bytes.num;
  171. }