obs-ffmpeg-hls-mux.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "obs-ffmpeg-mux.h"
  2. #define do_log(level, format, ...) \
  3. blog(level, "[ffmpeg hls muxer: '%s'] " format, \
  4. obs_output_get_name(stream->output), ##__VA_ARGS__)
  5. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  6. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  7. const char *ffmpeg_hls_mux_getname(void *type)
  8. {
  9. UNUSED_PARAMETER(type);
  10. return obs_module_text("FFmpegHlsMuxer");
  11. }
  12. int hls_stream_dropped_frames(void *data)
  13. {
  14. struct ffmpeg_muxer *stream = data;
  15. return stream->dropped_frames;
  16. }
  17. void ffmpeg_hls_mux_destroy(void *data)
  18. {
  19. struct ffmpeg_muxer *stream = data;
  20. if (stream) {
  21. deactivate(stream, 0);
  22. pthread_mutex_destroy(&stream->write_mutex);
  23. os_sem_destroy(stream->write_sem);
  24. os_event_destroy(stream->stop_event);
  25. da_free(stream->mux_packets);
  26. circlebuf_free(&stream->packets);
  27. os_process_pipe_destroy(stream->pipe);
  28. dstr_free(&stream->path);
  29. dstr_free(&stream->printable_path);
  30. dstr_free(&stream->stream_key);
  31. dstr_free(&stream->muxer_settings);
  32. bfree(data);
  33. }
  34. }
  35. void *ffmpeg_hls_mux_create(obs_data_t *settings, obs_output_t *output)
  36. {
  37. struct ffmpeg_muxer *stream = bzalloc(sizeof(*stream));
  38. pthread_mutex_init_value(&stream->write_mutex);
  39. stream->output = output;
  40. /* init mutex, semaphore and event */
  41. if (pthread_mutex_init(&stream->write_mutex, NULL) != 0)
  42. goto fail;
  43. if (os_event_init(&stream->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  44. goto fail;
  45. if (os_sem_init(&stream->write_sem, 0) != 0)
  46. goto fail;
  47. UNUSED_PARAMETER(settings);
  48. return stream;
  49. fail:
  50. ffmpeg_hls_mux_destroy(stream);
  51. return NULL;
  52. }
  53. static bool process_packet(struct ffmpeg_muxer *stream)
  54. {
  55. struct encoder_packet packet;
  56. bool has_packet = false;
  57. bool ret = true;
  58. pthread_mutex_lock(&stream->write_mutex);
  59. if (stream->packets.size) {
  60. circlebuf_pop_front(&stream->packets, &packet, sizeof(packet));
  61. has_packet = true;
  62. }
  63. pthread_mutex_unlock(&stream->write_mutex);
  64. if (has_packet) {
  65. ret = write_packet(stream, &packet);
  66. obs_encoder_packet_release(&packet);
  67. }
  68. return ret;
  69. }
  70. static void *write_thread(void *data)
  71. {
  72. struct ffmpeg_muxer *stream = data;
  73. while (os_sem_wait(stream->write_sem) == 0) {
  74. if (os_event_try(stream->stop_event) == 0)
  75. return NULL;
  76. if (!process_packet(stream))
  77. break;
  78. }
  79. obs_output_signal_stop(stream->output, OBS_OUTPUT_ERROR);
  80. deactivate(stream, 0);
  81. return NULL;
  82. }
  83. bool ffmpeg_hls_mux_start(void *data)
  84. {
  85. struct ffmpeg_muxer *stream = data;
  86. obs_service_t *service;
  87. const char *path_str;
  88. const char *stream_key;
  89. struct dstr path = {0};
  90. obs_encoder_t *vencoder;
  91. obs_data_t *settings;
  92. int keyint_sec;
  93. if (!obs_output_can_begin_data_capture(stream->output, 0))
  94. return false;
  95. if (!obs_output_initialize_encoders(stream->output, 0))
  96. return false;
  97. service = obs_output_get_service(stream->output);
  98. if (!service)
  99. return false;
  100. path_str = obs_service_get_url(service);
  101. stream_key = obs_service_get_key(service);
  102. dstr_copy(&stream->stream_key, stream_key);
  103. dstr_copy(&path, path_str);
  104. dstr_replace(&path, "{stream_key}", stream_key);
  105. dstr_copy(&stream->muxer_settings,
  106. "method=PUT http_persistent=1 ignore_io_errors=1 ");
  107. dstr_catf(&stream->muxer_settings, "http_user_agent=libobs/%s",
  108. OBS_VERSION);
  109. vencoder = obs_output_get_video_encoder(stream->output);
  110. settings = obs_encoder_get_settings(vencoder);
  111. keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
  112. if (keyint_sec) {
  113. dstr_catf(&stream->muxer_settings, " hls_time=%d", keyint_sec);
  114. stream->keyint_sec = keyint_sec;
  115. }
  116. obs_data_release(settings);
  117. start_pipe(stream, path.array);
  118. dstr_free(&path);
  119. if (!stream->pipe) {
  120. obs_output_set_last_error(
  121. stream->output, obs_module_text("HelperProcessFailed"));
  122. warn("Failed to create process pipe");
  123. return false;
  124. }
  125. stream->mux_thread_joinable = pthread_create(&stream->mux_thread, NULL,
  126. write_thread, stream) == 0;
  127. if (!stream->mux_thread_joinable)
  128. return false;
  129. /* write headers and start capture */
  130. os_atomic_set_bool(&stream->active, true);
  131. os_atomic_set_bool(&stream->capturing, true);
  132. stream->is_hls = true;
  133. stream->total_bytes = 0;
  134. stream->dropped_frames = 0;
  135. stream->min_priority = 0;
  136. obs_output_begin_data_capture(stream->output, 0);
  137. dstr_copy(&stream->printable_path, path_str);
  138. info("Writing to path '%s'...", stream->printable_path.array);
  139. return true;
  140. }
  141. static bool write_packet_to_buf(struct ffmpeg_muxer *stream,
  142. struct encoder_packet *packet)
  143. {
  144. circlebuf_push_back(&stream->packets, packet,
  145. sizeof(struct encoder_packet));
  146. return true;
  147. }
  148. static void drop_frames(struct ffmpeg_muxer *stream, int highest_priority)
  149. {
  150. struct circlebuf new_buf = {0};
  151. int num_frames_dropped = 0;
  152. circlebuf_reserve(&new_buf, sizeof(struct encoder_packet) * 8);
  153. while (stream->packets.size) {
  154. struct encoder_packet packet;
  155. circlebuf_pop_front(&stream->packets, &packet, sizeof(packet));
  156. /* do not drop audio data or video keyframes */
  157. if (packet.type == OBS_ENCODER_AUDIO ||
  158. packet.drop_priority >= highest_priority) {
  159. circlebuf_push_back(&new_buf, &packet, sizeof(packet));
  160. } else {
  161. num_frames_dropped++;
  162. obs_encoder_packet_release(&packet);
  163. }
  164. }
  165. circlebuf_free(&stream->packets);
  166. stream->packets = new_buf;
  167. if (stream->min_priority < highest_priority)
  168. stream->min_priority = highest_priority;
  169. stream->dropped_frames += num_frames_dropped;
  170. }
  171. static bool find_first_video_packet(struct ffmpeg_muxer *stream,
  172. struct encoder_packet *first)
  173. {
  174. size_t count = stream->packets.size / sizeof(*first);
  175. for (size_t i = 0; i < count; i++) {
  176. struct encoder_packet *cur =
  177. circlebuf_data(&stream->packets, i * sizeof(*first));
  178. if (cur->type == OBS_ENCODER_VIDEO && !cur->keyframe) {
  179. *first = *cur;
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. void check_to_drop_frames(struct ffmpeg_muxer *stream, bool pframes)
  186. {
  187. struct encoder_packet first;
  188. int64_t buffer_duration_usec;
  189. int priority = pframes ? OBS_NAL_PRIORITY_HIGHEST
  190. : OBS_NAL_PRIORITY_HIGH;
  191. int keyint_sec = stream->keyint_sec;
  192. int64_t drop_threshold_sec = keyint_sec ? 2 * keyint_sec : 10;
  193. if (!find_first_video_packet(stream, &first))
  194. return;
  195. buffer_duration_usec = stream->last_dts_usec - first.dts_usec;
  196. if (buffer_duration_usec > drop_threshold_sec * 1000000)
  197. drop_frames(stream, priority);
  198. }
  199. static bool add_video_packet(struct ffmpeg_muxer *stream,
  200. struct encoder_packet *packet)
  201. {
  202. check_to_drop_frames(stream, false);
  203. check_to_drop_frames(stream, true);
  204. /* if currently dropping frames, drop packets until it reaches the
  205. * desired priority */
  206. if (packet->drop_priority < stream->min_priority) {
  207. stream->dropped_frames++;
  208. return false;
  209. } else {
  210. stream->min_priority = 0;
  211. }
  212. stream->last_dts_usec = packet->dts_usec;
  213. return write_packet_to_buf(stream, packet);
  214. }
  215. void ffmpeg_hls_mux_data(void *data, struct encoder_packet *packet)
  216. {
  217. struct ffmpeg_muxer *stream = data;
  218. struct encoder_packet new_packet;
  219. struct encoder_packet tmp_packet;
  220. bool added_packet = false;
  221. if (!active(stream))
  222. return;
  223. /* encoder failure */
  224. if (!packet) {
  225. deactivate(stream, OBS_OUTPUT_ENCODE_ERROR);
  226. return;
  227. }
  228. if (!stream->sent_headers) {
  229. if (!send_headers(stream))
  230. return;
  231. stream->sent_headers = true;
  232. }
  233. if (stopping(stream)) {
  234. if (packet->sys_dts_usec >= stream->stop_ts) {
  235. deactivate(stream, 0);
  236. return;
  237. }
  238. }
  239. if (packet->type == OBS_ENCODER_VIDEO) {
  240. obs_parse_avc_packet(&tmp_packet, packet);
  241. packet->drop_priority = tmp_packet.priority;
  242. obs_encoder_packet_release(&tmp_packet);
  243. }
  244. obs_encoder_packet_ref(&new_packet, packet);
  245. pthread_mutex_lock(&stream->write_mutex);
  246. if (active(stream)) {
  247. added_packet =
  248. (packet->type == OBS_ENCODER_VIDEO)
  249. ? add_video_packet(stream, &new_packet)
  250. : write_packet_to_buf(stream, &new_packet);
  251. }
  252. pthread_mutex_unlock(&stream->write_mutex);
  253. if (added_packet)
  254. os_sem_post(stream->write_sem);
  255. else
  256. obs_encoder_packet_release(&new_packet);
  257. }
  258. struct obs_output_info ffmpeg_hls_muxer = {
  259. .id = "ffmpeg_hls_muxer",
  260. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_MULTI_TRACK |
  261. OBS_OUTPUT_SERVICE,
  262. .encoded_video_codecs = "h264",
  263. .encoded_audio_codecs = "aac",
  264. .get_name = ffmpeg_hls_mux_getname,
  265. .create = ffmpeg_hls_mux_create,
  266. .destroy = ffmpeg_hls_mux_destroy,
  267. .start = ffmpeg_hls_mux_start,
  268. .stop = ffmpeg_mux_stop,
  269. .encoded_packet = ffmpeg_hls_mux_data,
  270. .get_total_bytes = ffmpeg_mux_total_bytes,
  271. .get_dropped_frames = hls_stream_dropped_frames,
  272. };