obs-ffmpeg-logging.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, va_list args)
  44. {
  45. if (format == NULL)
  46. return;
  47. struct log_context *log_context = create_or_fetch_log_context(context);
  48. char *str = log_context->str;
  49. av_log_format_line(context, level, format, args, str + strlen(str),
  50. (int)(sizeof(log_context->str) - strlen(str)), &log_context->print_prefix);
  51. int obs_level;
  52. switch (level) {
  53. case AV_LOG_PANIC:
  54. case AV_LOG_FATAL:
  55. obs_level = LOG_ERROR;
  56. break;
  57. case AV_LOG_ERROR:
  58. case AV_LOG_WARNING:
  59. obs_level = LOG_WARNING;
  60. break;
  61. case AV_LOG_INFO:
  62. case AV_LOG_VERBOSE:
  63. obs_level = LOG_INFO;
  64. break;
  65. case AV_LOG_DEBUG:
  66. default:
  67. obs_level = LOG_DEBUG;
  68. }
  69. if (!log_context->print_prefix)
  70. return;
  71. char *str_end = str + strlen(str) - 1;
  72. while (str < str_end) {
  73. if (*str_end != '\n')
  74. break;
  75. *str_end-- = '\0';
  76. }
  77. if (str_end <= str)
  78. goto cleanup;
  79. blog(obs_level, "[ffmpeg] %s", str);
  80. cleanup:
  81. destroy_log_context(log_context);
  82. }
  83. static bool logging_initialized = false;
  84. void obs_ffmpeg_load_logging(void)
  85. {
  86. da_init(active_log_contexts);
  87. da_init(cached_log_contexts);
  88. if (pthread_mutex_init(&log_contexts_mutex, NULL) == 0) {
  89. av_log_set_callback(ffmpeg_log_callback);
  90. logging_initialized = true;
  91. }
  92. }
  93. void obs_ffmpeg_unload_logging(void)
  94. {
  95. if (!logging_initialized)
  96. return;
  97. logging_initialized = false;
  98. av_log_set_callback(av_log_default_callback);
  99. pthread_mutex_destroy(&log_contexts_mutex);
  100. for (size_t i = 0; i < active_log_contexts.num; i++) {
  101. bfree(active_log_contexts.array[i]);
  102. }
  103. for (size_t i = 0; i < cached_log_contexts.num; i++) {
  104. bfree(cached_log_contexts.array[i]);
  105. }
  106. da_free(active_log_contexts);
  107. da_free(cached_log_contexts);
  108. }