| 1 | #include "vaapi_display_drm.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #ifdef IN_LIBVA |
| 11 | #include "va/drm/va_drm.h" |
| 12 | #else |
| 13 | #include <va/va_drm.h> |
| 14 | #endif |
| 15 | |
| 16 | #include "rtc_base/logging.h" |
| 17 | |
| 18 | static bool check_h264_encoding_support(VADisplay va_display) { |
| 19 | VAProfile profile_list[] = {VAProfileH264High, VAProfileH264Main, |
| 20 | VAProfileH264ConstrainedBaseline}; |
| 21 | |
| 22 | VAProfile h264_profile = VAProfileH264ConstrainedBaseline; |
| 23 | int slice_entrypoint; |
| 24 | bool support_encode = false; |
| 25 | int selected_entrypoint = -1; |
| 26 | uint32_t i; |
| 27 | |
| 28 | if (!va_display) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | const int max_entrypoints = vaMaxNumEntrypoints(va_display); |
| 33 | if (max_entrypoints <= 0) { |
| 34 | RTC_LOG(LS_ERROR) << "VAAPI reported no entrypoints"; |
| 35 | return false; |
| 36 | } |
| 37 | std::vector<VAEntrypoint> entrypoints(max_entrypoints); |
| 38 | |
| 39 | /* use the highest profile */ |
| 40 | for (i = 0; i < sizeof(profile_list) / sizeof(profile_list[0]); i++) { |
| 41 | if ((h264_profile != ~0) && h264_profile != profile_list[i]) |
| 42 | continue; |
| 43 | |
| 44 | h264_profile = profile_list[i]; |
| 45 | int num_entrypoints = max_entrypoints; |
| 46 | vaQueryConfigEntrypoints(va_display, h264_profile, entrypoints.data(), |
| 47 | &num_entrypoints); |
| 48 | for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; |
| 49 | slice_entrypoint++) { |
| 50 | if ((entrypoints[slice_entrypoint] == VAEntrypointEncSlice) || |
| 51 | (entrypoints[slice_entrypoint] == VAEntrypointEncSliceLP)) { |
| 52 | support_encode = true; |
| 53 | selected_entrypoint = entrypoints[slice_entrypoint]; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | if (support_encode) { |
| 58 | RTC_LOG(LS_INFO) << "Using EntryPoint - " << selected_entrypoint; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (support_encode) { |
| 64 | RTC_LOG(LS_INFO) << "Supported H264 Encoder, Using EntryPoint - " |
| 65 | << selected_entrypoint; |
| 66 | } else { |
| 67 | RTC_LOG(LS_ERROR) |
| 68 | << "Can't find VAEntrypointEncSlice or VAEntrypointEncSliceLP for " |
| 69 | "H264 profiles"; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | static VADisplay va_open_display_drm(int* drm_fd) { |
| 77 | VADisplay va_dpy; |
| 78 | int i; |
| 79 | |
| 80 | static const char* drm_device_paths[] = {"/dev/dri/renderD128", |
| 81 | "/dev/dri/renderD129", NULL}; |
| 82 | for (i = 0; drm_device_paths[i]; i++) { |
| 83 | *drm_fd = open(drm_device_paths[i], O_RDWR); |
| 84 | if (*drm_fd < 0) |
| 85 | continue; |
| 86 | |
| 87 | va_dpy = vaGetDisplayDRM(*drm_fd); |
| 88 | if (!va_dpy) { |
| 89 | close(*drm_fd); |
| 90 | *drm_fd = -1; |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | vaSetErrorCallback(va_dpy, NULL, NULL); |
| 95 | vaSetInfoCallback(va_dpy, NULL, NULL); |
| 96 | |
| 97 | int major_ver = 0; |
| 98 | int minor_ver = 0; |
| 99 | VAStatus va_status = vaInitialize(va_dpy, &major_ver, &minor_ver); |
| 100 | if (va_status == VA_STATUS_SUCCESS && check_h264_encoding_support(va_dpy)) { |
| 101 | RTC_LOG(LS_INFO) << "Initialized VAAPI successfully with version " << major_ver << "." << minor_ver; |
| 102 | return va_dpy; |
| 103 | } |
| 104 | RTC_LOG(LS_ERROR) << "Failed to initialize VAAPI with status: " |
| 105 | << vaErrorStr(va_status); |
| 106 | vaTerminate(va_dpy); |
| 107 | close(*drm_fd); |
| 108 | *drm_fd = -1; |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | namespace livekit_ffi { |
| 114 | |
| 115 | VaapiDisplayDrm::~VaapiDisplayDrm() { |
| 116 | Close(); |
| 117 | } |
| 118 | |
| 119 | bool VaapiDisplayDrm::Open() { |
| 120 | Close(); |
| 121 | va_display_ = va_open_display_drm(&drm_fd_); |
| 122 | if (!va_display_) { |
| 123 | RTC_LOG(LS_ERROR) << "Failed to open VA drm display. Maybe the AMD video " |
| 124 | "driver or libva-dev/libdrm-dev is not installed?"; |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool VaapiDisplayDrm::isOpen() const { |
| 131 | return va_display_ != nullptr; |
| 132 | } |
| 133 | |
| 134 | void VaapiDisplayDrm::Close() { |
| 135 | if (va_display_) { |
| 136 | vaTerminate(va_display_); |
| 137 | va_display_ = nullptr; |
| 138 | } |
| 139 | if (drm_fd_ >= 0) { |
| 140 | close(drm_fd_); |
| 141 | drm_fd_ = -1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | } // namespace livekit_ffi |