vlc-video-plugin.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #endif
  4. #include <util/platform.h>
  5. #include "vlc-video-plugin.h"
  6. OBS_DECLARE_MODULE()
  7. OBS_MODULE_USE_DEFAULT_LOCALE("vlc-video", "en-US")
  8. /* libvlc core */
  9. LIBVLC_NEW libvlc_new_;
  10. LIBVLC_RELEASE libvlc_release_;
  11. LIBVLC_CLOCK libvlc_clock_;
  12. LIBVLC_EVENT_ATTACH libvlc_event_attach_;
  13. /* libvlc media */
  14. LIBVLC_MEDIA_NEW_PATH libvlc_media_new_path_;
  15. LIBVLC_MEDIA_NEW_LOCATION libvlc_media_new_location_;
  16. LIBVLC_MEDIA_ADD_OPTION libvlc_media_add_option_;
  17. LIBVLC_MEDIA_RELEASE libvlc_media_release_;
  18. LIBVLC_MEDIA_RELEASE libvlc_media_retain_;
  19. /* libvlc media player */
  20. LIBVLC_MEDIA_PLAYER_NEW libvlc_media_player_new_;
  21. LIBVLC_MEDIA_PLAYER_NEW_FROM_MEDIA libvlc_media_player_new_from_media_;
  22. LIBVLC_MEDIA_PLAYER_RELEASE libvlc_media_player_release_;
  23. LIBVLC_VIDEO_SET_CALLBACKS libvlc_video_set_callbacks_;
  24. LIBVLC_VIDEO_SET_FORMAT_CALLBACKS libvlc_video_set_format_callbacks_;
  25. LIBVLC_AUDIO_SET_CALLBACKS libvlc_audio_set_callbacks_;
  26. LIBVLC_AUDIO_SET_FORMAT_CALLBACKS libvlc_audio_set_format_callbacks_;
  27. LIBVLC_MEDIA_PLAYER_PLAY libvlc_media_player_play_;
  28. LIBVLC_MEDIA_PLAYER_STOP libvlc_media_player_stop_;
  29. LIBVLC_MEDIA_PLAYER_GET_TIME libvlc_media_player_get_time_;
  30. LIBVLC_VIDEO_GET_SIZE libvlc_video_get_size_;
  31. LIBVLC_MEDIA_PLAYER_EVENT_MANAGER libvlc_media_player_event_manager_;
  32. /* libvlc media list */
  33. LIBVLC_MEDIA_LIST_NEW libvlc_media_list_new_;
  34. LIBVLC_MEDIA_LIST_RELEASE libvlc_media_list_release_;
  35. LIBVLC_MEDIA_LIST_ADD_MEDIA libvlc_media_list_add_media_;
  36. LIBVLC_MEDIA_LIST_LOCK libvlc_media_list_lock_;
  37. LIBVLC_MEDIA_LIST_UNLOCK libvlc_media_list_unlock_;
  38. LIBVLC_MEDIA_LIST_EVENT_MANAGER libvlc_media_list_event_manager_;
  39. /* libvlc media list player */
  40. LIBVLC_MEDIA_LIST_PLAYER_NEW libvlc_media_list_player_new_;
  41. LIBVLC_MEDIA_LIST_PLAYER_RELEASE libvlc_media_list_player_release_;
  42. LIBVLC_MEDIA_LIST_PLAYER_PLAY libvlc_media_list_player_play_;
  43. LIBVLC_MEDIA_LIST_PLAYER_PAUSE libvlc_media_list_player_pause_;
  44. LIBVLC_MEDIA_LIST_PLAYER_STOP libvlc_media_list_player_stop_;
  45. LIBVLC_MEDIA_LIST_PLAYER_SET_MEDIA_PLAYER libvlc_media_list_player_set_media_player_;
  46. LIBVLC_MEDIA_LIST_PLAYER_SET_MEDIA_LIST libvlc_media_list_player_set_media_list_;
  47. LIBVLC_MEDIA_LIST_PLAYER_EVENT_MANAGER libvlc_media_list_player_event_manager_;
  48. LIBVLC_MEDIA_LIST_PLAYER_SET_PLAYBACK_MODE libvlc_media_list_player_set_playback_mode_;
  49. LIBVLC_MEDIA_LIST_PLAYER_NEXT libvlc_media_list_player_next_;
  50. LIBVLC_MEDIA_LIST_PLAYER_PREVIOUS libvlc_media_list_player_previous_;
  51. void *libvlc_module = NULL;
  52. #ifdef __APPLE__
  53. void *libvlc_core_module = NULL;
  54. #endif
  55. libvlc_instance_t *libvlc = NULL;
  56. uint64_t time_start = 0;
  57. static bool load_vlc_funcs(void)
  58. {
  59. #define LOAD_VLC_FUNC(func) \
  60. do { \
  61. func ## _ = os_dlsym(libvlc_module, #func); \
  62. if (!func ## _) { \
  63. blog(LOG_WARNING, "Could not func VLC function %s, " \
  64. "VLC loading failed", #func); \
  65. return false; \
  66. } \
  67. } while (false)
  68. /* libvlc core */
  69. LOAD_VLC_FUNC(libvlc_new);
  70. LOAD_VLC_FUNC(libvlc_release);
  71. LOAD_VLC_FUNC(libvlc_clock);
  72. LOAD_VLC_FUNC(libvlc_event_attach);
  73. /* libvlc media */
  74. LOAD_VLC_FUNC(libvlc_media_new_path);
  75. LOAD_VLC_FUNC(libvlc_media_new_location);
  76. LOAD_VLC_FUNC(libvlc_media_add_option);
  77. LOAD_VLC_FUNC(libvlc_media_release);
  78. LOAD_VLC_FUNC(libvlc_media_retain);
  79. /* libvlc media player */
  80. LOAD_VLC_FUNC(libvlc_media_player_new);
  81. LOAD_VLC_FUNC(libvlc_media_player_new_from_media);
  82. LOAD_VLC_FUNC(libvlc_media_player_release);
  83. LOAD_VLC_FUNC(libvlc_video_set_callbacks);
  84. LOAD_VLC_FUNC(libvlc_video_set_format_callbacks);
  85. LOAD_VLC_FUNC(libvlc_audio_set_callbacks);
  86. LOAD_VLC_FUNC(libvlc_audio_set_format_callbacks);
  87. LOAD_VLC_FUNC(libvlc_media_player_play);
  88. LOAD_VLC_FUNC(libvlc_media_player_stop);
  89. LOAD_VLC_FUNC(libvlc_media_player_get_time);
  90. LOAD_VLC_FUNC(libvlc_video_get_size);
  91. LOAD_VLC_FUNC(libvlc_media_player_event_manager);
  92. /* libvlc media list */
  93. LOAD_VLC_FUNC(libvlc_media_list_new);
  94. LOAD_VLC_FUNC(libvlc_media_list_release);
  95. LOAD_VLC_FUNC(libvlc_media_list_add_media);
  96. LOAD_VLC_FUNC(libvlc_media_list_lock);
  97. LOAD_VLC_FUNC(libvlc_media_list_unlock);
  98. LOAD_VLC_FUNC(libvlc_media_list_event_manager);
  99. /* libvlc media list player */
  100. LOAD_VLC_FUNC(libvlc_media_list_player_new);
  101. LOAD_VLC_FUNC(libvlc_media_list_player_release);
  102. LOAD_VLC_FUNC(libvlc_media_list_player_play);
  103. LOAD_VLC_FUNC(libvlc_media_list_player_pause);
  104. LOAD_VLC_FUNC(libvlc_media_list_player_stop);
  105. LOAD_VLC_FUNC(libvlc_media_list_player_set_media_player);
  106. LOAD_VLC_FUNC(libvlc_media_list_player_set_media_list);
  107. LOAD_VLC_FUNC(libvlc_media_list_player_event_manager);
  108. LOAD_VLC_FUNC(libvlc_media_list_player_set_playback_mode);
  109. LOAD_VLC_FUNC(libvlc_media_list_player_next);
  110. LOAD_VLC_FUNC(libvlc_media_list_player_previous);
  111. return true;
  112. }
  113. static bool load_libvlc_module(void)
  114. {
  115. #ifdef _WIN32
  116. char *path_utf8 = NULL;
  117. wchar_t path[1024];
  118. LSTATUS status;
  119. DWORD size;
  120. HKEY key;
  121. memset(path, 0, 1024 * sizeof(wchar_t));
  122. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  123. L"SOFTWARE\\VideoLAN\\VLC",
  124. &key);
  125. if (status != ERROR_SUCCESS)
  126. return false;
  127. size = 1024;
  128. status = RegQueryValueExW(key, L"InstallDir", NULL, NULL,
  129. (LPBYTE)path, &size);
  130. if (status == ERROR_SUCCESS) {
  131. wcscat(path, L"\\libvlc.dll");
  132. os_wcs_to_utf8_ptr(path, 0, &path_utf8);
  133. libvlc_module = os_dlopen(path_utf8);
  134. bfree(path_utf8);
  135. }
  136. RegCloseKey(key);
  137. #else
  138. #ifdef __APPLE__
  139. #define LIBVLC_DIR "/Applications/VLC.app/Contents/MacOS/"
  140. /* According to otoolo -L, this is what libvlc.dylib wants. */
  141. #define LIBVLC_CORE_FILE LIBVLC_DIR "lib/libvlccore.dylib"
  142. #define LIBVLC_FILE LIBVLC_DIR "lib/libvlc.5.dylib"
  143. setenv("VLC_PLUGIN_PATH", LIBVLC_DIR "plugins", false);
  144. libvlc_core_module = os_dlopen(LIBVLC_CORE_FILE);
  145. if (!libvlc_core_module)
  146. return false;
  147. #else
  148. #define LIBVLC_FILE "libvlc.so.5"
  149. #endif
  150. libvlc_module = os_dlopen(LIBVLC_FILE);
  151. #endif
  152. return libvlc_module != NULL;
  153. }
  154. extern struct obs_source_info vlc_source_info;
  155. bool load_libvlc(void)
  156. {
  157. if (libvlc)
  158. return true;
  159. libvlc = libvlc_new_(0, 0);
  160. if (!libvlc) {
  161. blog(LOG_INFO, "Couldn't create libvlc instance");
  162. return false;
  163. }
  164. time_start = (uint64_t)libvlc_clock_() * 1000ULL;
  165. return true;
  166. }
  167. bool obs_module_load(void)
  168. {
  169. if (!load_libvlc_module()) {
  170. blog(LOG_INFO, "Couldn't find VLC installation, VLC video "
  171. "source disabled");
  172. return true;
  173. }
  174. if (!load_vlc_funcs())
  175. return true;
  176. blog(LOG_INFO, "VLC found, VLC video source enabled");
  177. obs_register_source(&vlc_source_info);
  178. return true;
  179. }
  180. void obs_module_unload(void)
  181. {
  182. if (libvlc)
  183. libvlc_release_(libvlc);
  184. #ifdef __APPLE__
  185. if (libvlc_core_module)
  186. os_dlclose(libvlc_core_module);
  187. #endif
  188. if (libvlc_module)
  189. os_dlclose(libvlc_module);
  190. }