Skip to content

obs-ffmpeg: Set DRI devices and their name persistently - #5336

Merged
WizardCM merged 1 commit into
obsproject:masterfrom
carlocastoldi:2936-fix
Dec 12, 2021
Merged

WizardCM merged 1 commit into
obsproject:masterfrom
carlocastoldi:2936-fix

Conversation

@carlocastoldi

@carlocastoldi carlocastoldi commented Sep 27, 2021 •

Copy link
Copy Markdown
Contributor

Description

As discussed in the issue, I retrieve the DRI devices from /dev/dri/by-path/ instead of /dev/dri/renderDXXX (e.g. /dev/dri/by-path/pci-0000:00:02.0-render). This enable us to use lspci to get the device name as well.
If /dev/dri/by-path/ does not exist, I fall back to the old implementation

immagine

This is the first time contributing here: I am not sure it is correct to add the -lpci flag where I did in CMakeLists.txt. Additionally I don't work often in C, but I hope this works out as intended since it's a small contribution.

Motivation and Context

Fixes #2936

How Has This Been Tested?

I tested it in a clean environment on my desktop with two vaapi devices. It appears to work as intended

specs:

OS: Manjaro Linux x86_64 
Kernel: 5.14.7-2-MANJARO 
Resolution: 1920x1080, 1920x1080 
DE: Plasma 5.22.5 
WM: KWin 

CPU: Intel i5-6400 (4) @ 3.300GHz 
GPU: AMD ATI Radeon R9 285/380 
GPU: Intel HD Graphics 530 
Memory: 7176MiB / 15867MiB 

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Tweak (non-breaking change to improve existing functionality)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch. (as in "it's not in my master branch)
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@WizardCM WizardCM added kind/bug Categorizes issue or PR as related to a bug. platform/linux Categorizes issue or PR as affecting Linux specifically labels Sep 27, 2021
Comment thread CMakeLists.txt Resolved
@tytan652

tytan652 commented Sep 27, 2021 •

Copy link
Copy Markdown
Collaborator

I try to make a finder that try to follow what Pat may want and use it in the right CMakeLists.

FindLibpci.cmake in cmake/Modules

# * Try to find Libpci
# Once done this will define
#
# LIBPCI_FOUND - system has Libpci
# LIBPCI_INCLUDE_DIRS - the Libpci include directory
# LIBPCI_LIBRARIES - the libraries needed to use Libpci
# LIBPCI_DEFINITIONS - Compiler switches required for using Libpci

# Use pkg-config to get the directories and then use these values in the
# find_path() and find_library() calls

find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
  pkg_check_modules(_LIBPCI libpci)
endif()

find_path(
  LIBPCI_INCLUDE_DIR
  NAMES pci.h
  HINTS ${_LIBPCI_INCLUDE_DIRS}
  PATHS /usr/include /usr/local/include /opt/local/include
  PATH_SUFFIXES pci/)

find_library(
  LIBPCI_LIB
  NAMES ${_LIBPCI_LIBRARIES} libpci
  HINTS ${_LIBPCI_LIBRARY_DIRS}
  PATHS /usr/lib /usr/local/lib /opt/local/lib)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Libpci REQUIRED_VARS LIBPCI_LIB LIBPCI_INCLUDE_DIR)
mark_as_advanced(LIBPCI_INCLUDE_DIR LIBPCI_LIB)

if(LIBPCI_FOUND)
  set(LIBPCI_INCLUDE_DIRS ${LIBPCI_INCLUDE_DIR})
  set(LIBPCI_LIBRARIES ${LIBPCI_LIB})

  if(NOT TARGET LIBPCI::LIBPCI)
    if(IS_ABSOLUTE "${LIBPCI_LIBRARIES}")
      add_library(LIBPCI::LIBPCI UNKNOWN IMPORTED)
      set_target_properties(LIBPCI::LIBPCI PROPERTIES IMPORTED_LOCATION
                                                "${LIBPCI_LIBRARIES}")
    else()
      add_library(LIBPCI::LIBPCI INTERFACE IMPORTED)
      set_target_properties(LIBPCI::LIBPCI PROPERTIES IMPORTED_LIBNAME
                                                "${LIBPCI_LIBRARIES}")
    endif()
  endif()
endif()

For plugins/obs-ffmpeg/CMakeLists.txt (doesn't follow the CMake overhaul for now)

diff --git a/plugins/obs-ffmpeg/CMakeLists.txt b/plugins/obs-ffmpeg/CMakeLists.txt
index 9df656b2d..36d8cb56f 100644
--- a/plugins/obs-ffmpeg/CMakeLists.txt
+++ b/plugins/obs-ffmpeg/CMakeLists.txt
@@ -11,6 +11,11 @@ find_package(FFmpeg REQUIRED
 	COMPONENTS avcodec avfilter avdevice avutil swscale avformat swresample)
 include_directories(${FFMPEG_INCLUDE_DIRS})
 
+if(UNIX AND NOT APPLE)
+	find_package(Libpci REQUIRED)
+	include_directories(${LIBPCI_INCLUDE_DIRS})
+endif()
+
 configure_file(
 	"${CMAKE_CURRENT_SOURCE_DIR}/obs-ffmpeg-config.h.in"
 	"${CMAKE_CURRENT_BINARY_DIR}/obs-ffmpeg-config.h")
@@ -37,7 +42,8 @@ if(UNIX AND NOT APPLE)
 	list(APPEND obs-ffmpeg_SOURCES
 		obs-ffmpeg-vaapi.c)
 	LIST(APPEND obs-ffmpeg_PLATFORM_DEPS
-		${LIBVA_LBRARIES})
+		${LIBVA_LBRARIES}
+		${LIBPCI_LIBRARIES})
 endif()
 
 if(ENABLE_FFMPEG_LOGGING)

@carlocastoldi

Copy link
Copy Markdown
Contributor Author

@tytan652: I try to make a finder that try to follow what Pat may want and use it in the right CMakeLists.

Thank you very much for the help you gave me. You really save me some time and it was interesting seeing how cmake works on huge projects such as this one! ^^

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread cmake/Modules/FindLibpci.cmake Resolved
@kkartaltepe

Copy link
Copy Markdown
Collaborator

You will need to add libpci to the CI build packages most likely.

@tytan652 tytan652 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tabulations nitpicking.

Your editor may not use the EditorConfig file of the repo, this file allows things like defining tab spacing.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/CMakeLists.txt Resolved
@tytan652

tytan652 commented Sep 28, 2021 •

Copy link
Copy Markdown
Collaborator

The main.yml is quite long so I give you this:

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 6846e1a88..20a7dcff9 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -422,7 +422,8 @@ jobs:
            qtbase5-private-dev \
            libqt5svg5-dev \
            swig \
-           libcmocka-dev
+           libcmocka-dev \
+	    libpci-dev
       - name: 'Restore Chromium Embedded Framework from cache'
         id: cef-cache
         uses: actions/[email protected]

You may also need to add libspci-dev to CI/install-dependencies-linux.sh scripts, but this one can wait a bit.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
@carlocastoldi

Copy link
Copy Markdown
Contributor Author

@tytan652 : You may also need to add libspci-dev to CI/install-dependencies-linux.sh scripts, but this one can wait a bit.

I waited on this to first be sure I solved every review

@kkartaltepe kkartaltepe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well on my machine, thanks.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
retrieving the DRI devices from /dev/dri/by-path/ instead of
/dev/dri/renderDXXX. This enable us to use lspci to get the device
name as well.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asked for clarification about some numbers.

Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
Comment thread plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c Resolved
@carlocastoldi

Copy link
Copy Markdown
Contributor Author

Sorry, but I may have not fully get what you want me to do, @RytoEX. I answered your doubts on what the numbers meant. Do you want me to explicitly clarify it in the code with variable names as well?

@RytoEX

RytoEX commented Oct 11, 2021

Copy link
Copy Markdown
Member

Sorry, but I may have not fully get what you want me to do, @RytoEX. I answered your doubts on what the numbers meant. Do you want me to explicitly clarify it in the code with variable names as well?

I assume you mean that you explained it here:

// file_name pattern: pci-<pci_slot::12>-<type::{"card","render"}>

My point was that it's unclear at first glance, to me at least, what the numbers correspond to in those lines of code. You could use defines with descriptive names instead of raw numbers. I'll defer to @kkartaltepe on if it's really necessary though.

@WizardCM
WizardCM self-requested a review October 16, 2021 22:39
@carlocastoldi

Copy link
Copy Markdown
Contributor Author

Hi, I am sorry to bump this up but we are still on @kkartaltepe to tell us his idea on what to do with the numbers. What do you think? Should I clarify them even further? If yes, how would you prefer?

@kkartaltepe

kkartaltepe commented Nov 26, 2021 •

Copy link
Copy Markdown
Collaborator

I don't mind the numbers with the comment above. With the string format the 12 character pci address after the 4 character prefix is clear. And renumbering the render devices from 128 back to 0 is clear enough if you know that render devices start at 128 (which the loop starts at).

Last i looked this seemed fine to me.

@carlocastoldi

Copy link
Copy Markdown
Contributor Author

Alright, thank you a lot!

@RytoEX are you satisfied by this solution? Do you think we can merge this now?

@RytoEX

RytoEX commented Nov 26, 2021

Copy link
Copy Markdown
Member

Alright, thank you a lot!

@RytoEX are you satisfied by this solution? Do you think we can merge this now?

I don't make merge decisions for Linux based code as it's not my daily driver. If @kkartaltepe says it's fine, I defer to his opinion on code in this plugin. Though we'll probably leave the merge decision up to someone else.

@carlocastoldi

Copy link
Copy Markdown
Contributor Author

Alright, i get it. Thanks!
@WizardCM and @tytan652 can I ask you to please consider finishing the review on this pull request? Thank you ^^

@WizardCM

Copy link
Copy Markdown
Member

Alright, i get it. Thanks!
@WizardCM and @tytan652 can I ask you to please consider finishing the review on this pull request? Thank you ^^

I have plans on testing later today.

@WizardCM WizardCM added the Seeking Testers Build artifacts on CI label Nov 28, 2021

@WizardCM WizardCM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing stands out as broken to me, and everything works the same way as it did before for me. FindLibpci works as it should.

Unfortunately, actually encoding doesn't seem to work for me (I need to spend time debugging), but that happens without this PR too.

@WizardCM
WizardCM merged commit 4623a6b into obsproject:master Dec 12, 2021
@WizardCM WizardCM added this to the OBS Studio 27.2 milestone Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. platform/linux Categorizes issue or PR as affecting Linux specifically Seeking Testers Build artifacts on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] DRI devices are not persistent across reboots and should not be used to identify GPUs

5 participants