obs-ffmpeg.c 3.9 KB

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