flv-mux.c 5.8 KB

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