obs-ffmpeg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <obs-module.h>
  2. #include <util/darray.h>
  3. #include <libavutil/log.h>
  4. #include <pthread.h>
  5. OBS_DECLARE_MODULE()
  6. OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
  7. extern struct obs_source_info ffmpeg_source;
  8. extern struct obs_output_info ffmpeg_output;
  9. extern struct obs_output_info ffmpeg_muxer;
  10. extern struct obs_encoder_info aac_encoder_info;
  11. static DARRAY(struct log_context {
  12. void *context;
  13. char str[4096];
  14. int print_prefix;
  15. } *) active_log_contexts;
  16. static DARRAY(struct log_context *) cached_log_contexts;
  17. pthread_mutex_t log_contexts_mutex = PTHREAD_MUTEX_INITIALIZER;
  18. static struct log_context *create_or_fetch_log_context(void *context)
  19. {
  20. pthread_mutex_lock(&log_contexts_mutex);
  21. for (size_t i = 0; i < active_log_contexts.num; i++) {
  22. if (context == active_log_contexts.array[i]->context) {
  23. pthread_mutex_unlock(&log_contexts_mutex);
  24. return active_log_contexts.array[i];
  25. }
  26. }
  27. struct log_context *new_log_context = NULL;
  28. size_t cnt = cached_log_contexts.num;
  29. if (!!cnt) {
  30. new_log_context = cached_log_contexts.array[cnt - 1];
  31. da_pop_back(cached_log_contexts);
  32. }
  33. if (!new_log_context)
  34. new_log_context = bzalloc(sizeof(struct log_context));
  35. new_log_context->context = context;
  36. new_log_context->str[0] = '\0';
  37. new_log_context->print_prefix = 1;
  38. da_push_back(active_log_contexts, &new_log_context);
  39. pthread_mutex_unlock(&log_contexts_mutex);
  40. return new_log_context;
  41. }
  42. static void destroy_log_context(struct log_context *log_context)
  43. {
  44. pthread_mutex_lock(&log_contexts_mutex);
  45. da_erase_item(active_log_contexts, &log_context);
  46. da_push_back(cached_log_contexts, &log_context);
  47. pthread_mutex_unlock(&log_contexts_mutex);
  48. }
  49. static void ffmpeg_log_callback(void* context, int level, const char* format,
  50. va_list args)
  51. {
  52. if (format == NULL)
  53. return;
  54. struct log_context *log_context = create_or_fetch_log_context(context);
  55. char *str = log_context->str;
  56. av_log_format_line(context, level, format, args, str + strlen(str),
  57. (int)(sizeof(log_context->str) - strlen(str)),
  58. &log_context->print_prefix);
  59. int obs_level;
  60. switch (level) {
  61. case AV_LOG_PANIC:
  62. case AV_LOG_FATAL:
  63. obs_level = LOG_ERROR;
  64. break;
  65. case AV_LOG_ERROR:
  66. case AV_LOG_WARNING:
  67. obs_level = LOG_WARNING;
  68. break;
  69. case AV_LOG_INFO:
  70. case AV_LOG_VERBOSE:
  71. obs_level = LOG_INFO;
  72. break;
  73. case AV_LOG_DEBUG:
  74. default:
  75. obs_level = LOG_DEBUG;
  76. }
  77. if (!log_context->print_prefix)
  78. return;
  79. char *str_end = str + strlen(str) - 1;
  80. while(str < str_end) {
  81. if (*str_end != '\n')
  82. break;
  83. *str_end-- = '\0';
  84. }
  85. if (str_end <= str)
  86. goto cleanup;
  87. blog(obs_level, "[ffmpeg] %s", str);
  88. cleanup:
  89. destroy_log_context(log_context);
  90. }
  91. bool obs_module_load(void)
  92. {
  93. da_init(active_log_contexts);
  94. da_init(cached_log_contexts);
  95. //av_log_set_callback(ffmpeg_log_callback);
  96. obs_register_source(&ffmpeg_source);
  97. obs_register_output(&ffmpeg_output);
  98. obs_register_output(&ffmpeg_muxer);
  99. obs_register_encoder(&aac_encoder_info);
  100. return true;
  101. }
  102. void obs_module_unload(void)
  103. {
  104. av_log_set_callback(av_log_default_callback);
  105. #ifdef _WIN32
  106. pthread_mutex_destroy(&log_contexts_mutex);
  107. #endif
  108. for (size_t i = 0; i < active_log_contexts.num; i++) {
  109. bfree(active_log_contexts.array[i]);
  110. }
  111. for (size_t i = 0; i < cached_log_contexts.num; i++) {
  112. bfree(cached_log_contexts.array[i]);
  113. }
  114. da_free(active_log_contexts);
  115. da_free(cached_log_contexts);
  116. }