obs-ffmpeg.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <obs-module.h>
  2. #include <util/darray.h>
  3. #include <util/platform.h>
  4. #include <libavutil/log.h>
  5. #include <libavutil/avutil.h>
  6. #include <libavcodec/avcodec.h>
  7. #include <libavformat/avformat.h>
  8. #include <pthread.h>
  9. OBS_DECLARE_MODULE()
  10. OBS_MODULE_USE_DEFAULT_LOCALE("obs-ffmpeg", "en-US")
  11. MODULE_EXPORT const char *obs_module_description(void)
  12. {
  13. return "FFmpeg based sources/outputs/encoders";
  14. }
  15. extern struct obs_source_info ffmpeg_source;
  16. extern struct obs_output_info ffmpeg_output;
  17. extern struct obs_output_info ffmpeg_muxer;
  18. extern struct obs_output_info replay_buffer;
  19. extern struct obs_encoder_info aac_encoder_info;
  20. extern struct obs_encoder_info opus_encoder_info;
  21. extern struct obs_encoder_info nvenc_encoder_info;
  22. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 27, 100)
  23. #define LIBAVUTIL_VAAPI_AVAILABLE
  24. #endif
  25. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  26. extern struct obs_encoder_info vaapi_encoder_info;
  27. #endif
  28. static DARRAY(struct log_context {
  29. void *context;
  30. char str[4096];
  31. int print_prefix;
  32. } *) active_log_contexts;
  33. static DARRAY(struct log_context *) cached_log_contexts;
  34. pthread_mutex_t log_contexts_mutex = PTHREAD_MUTEX_INITIALIZER;
  35. static struct log_context *create_or_fetch_log_context(void *context)
  36. {
  37. pthread_mutex_lock(&log_contexts_mutex);
  38. for (size_t i = 0; i < active_log_contexts.num; i++) {
  39. if (context == active_log_contexts.array[i]->context) {
  40. pthread_mutex_unlock(&log_contexts_mutex);
  41. return active_log_contexts.array[i];
  42. }
  43. }
  44. struct log_context *new_log_context = NULL;
  45. size_t cnt = cached_log_contexts.num;
  46. if (!!cnt) {
  47. new_log_context = cached_log_contexts.array[cnt - 1];
  48. da_pop_back(cached_log_contexts);
  49. }
  50. if (!new_log_context)
  51. new_log_context = bzalloc(sizeof(struct log_context));
  52. new_log_context->context = context;
  53. new_log_context->str[0] = '\0';
  54. new_log_context->print_prefix = 1;
  55. da_push_back(active_log_contexts, &new_log_context);
  56. pthread_mutex_unlock(&log_contexts_mutex);
  57. return new_log_context;
  58. }
  59. static void destroy_log_context(struct log_context *log_context)
  60. {
  61. pthread_mutex_lock(&log_contexts_mutex);
  62. da_erase_item(active_log_contexts, &log_context);
  63. da_push_back(cached_log_contexts, &log_context);
  64. pthread_mutex_unlock(&log_contexts_mutex);
  65. }
  66. static void ffmpeg_log_callback(void* context, int level, const char* format,
  67. va_list args)
  68. {
  69. if (format == NULL)
  70. return;
  71. struct log_context *log_context = create_or_fetch_log_context(context);
  72. char *str = log_context->str;
  73. av_log_format_line(context, level, format, args, str + strlen(str),
  74. (int)(sizeof(log_context->str) - strlen(str)),
  75. &log_context->print_prefix);
  76. int obs_level;
  77. switch (level) {
  78. case AV_LOG_PANIC:
  79. case AV_LOG_FATAL:
  80. obs_level = LOG_ERROR;
  81. break;
  82. case AV_LOG_ERROR:
  83. case AV_LOG_WARNING:
  84. obs_level = LOG_WARNING;
  85. break;
  86. case AV_LOG_INFO:
  87. case AV_LOG_VERBOSE:
  88. obs_level = LOG_INFO;
  89. break;
  90. case AV_LOG_DEBUG:
  91. default:
  92. obs_level = LOG_DEBUG;
  93. }
  94. if (!log_context->print_prefix)
  95. return;
  96. char *str_end = str + strlen(str) - 1;
  97. while(str < str_end) {
  98. if (*str_end != '\n')
  99. break;
  100. *str_end-- = '\0';
  101. }
  102. if (str_end <= str)
  103. goto cleanup;
  104. blog(obs_level, "[ffmpeg] %s", str);
  105. cleanup:
  106. destroy_log_context(log_context);
  107. }
  108. #ifndef __APPLE__
  109. static const char *nvenc_check_name = "nvenc_check";
  110. static bool nvenc_supported(void)
  111. {
  112. av_register_all();
  113. profile_start(nvenc_check_name);
  114. AVCodec *nvenc = avcodec_find_encoder_by_name("nvenc_h264");
  115. void *lib = NULL;
  116. bool success = false;
  117. if (!nvenc) {
  118. goto cleanup;
  119. }
  120. #if defined(_WIN32)
  121. if (sizeof(void*) == 8) {
  122. lib = os_dlopen("nvEncodeAPI64.dll");
  123. } else {
  124. lib = os_dlopen("nvEncodeAPI.dll");
  125. }
  126. #else
  127. lib = os_dlopen("libnvidia-encode.so.1");
  128. #endif
  129. /* ------------------------------------------- */
  130. success = !!lib;
  131. cleanup:
  132. if (lib)
  133. os_dlclose(lib);
  134. profile_end(nvenc_check_name);
  135. return success;
  136. }
  137. #endif
  138. #ifdef LIBAVUTIL_VAAPI_AVAILABLE
  139. static bool vaapi_supported(void)
  140. {
  141. AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
  142. return !!vaenc;
  143. }
  144. #endif
  145. bool obs_module_load(void)
  146. {
  147. da_init(active_log_contexts);
  148. da_init(cached_log_contexts);
  149. //av_log_set_callback(ffmpeg_log_callback);
  150. obs_register_source(&ffmpeg_source);
  151. obs_register_output(&ffmpeg_output);
  152. obs_register_output(&ffmpeg_muxer);
  153. obs_register_output(&replay_buffer);
  154. obs_register_encoder(&aac_encoder_info);
  155. obs_register_encoder(&opus_encoder_info);
  156. #ifndef __APPLE__
  157. if (nvenc_supported()) {
  158. blog(LOG_INFO, "NVENC supported");
  159. obs_register_encoder(&nvenc_encoder_info);
  160. }
  161. #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
  162. if (vaapi_supported()) {
  163. blog(LOG_INFO, "FFMPEG VAAPI supported");
  164. obs_register_encoder(&vaapi_encoder_info);
  165. }
  166. #endif
  167. #endif
  168. return true;
  169. }
  170. void obs_module_unload(void)
  171. {
  172. av_log_set_callback(av_log_default_callback);
  173. #ifdef _WIN32
  174. pthread_mutex_destroy(&log_contexts_mutex);
  175. #endif
  176. for (size_t i = 0; i < active_log_contexts.num; i++) {
  177. bfree(active_log_contexts.array[i]);
  178. }
  179. for (size_t i = 0; i < cached_log_contexts.num; i++) {
  180. bfree(cached_log_contexts.array[i]);
  181. }
  182. da_free(active_log_contexts);
  183. da_free(cached_log_contexts);
  184. }