obs-ffmpeg-logging.c 3.1 KB

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