Media Device Allocator API to allows multiple drivers share a media device. This API solves a very common use-case for media devices where one physical device (an USB stick) provides both audio and video. When such media device exposes a standard USB Audio class, a proprietary Video class, two or more independent drivers will share a single physical USB bridge. In such cases, it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct device as the key. Once the media device is allocated by a driver, other drivers can get a reference to it. The media device is released when all the references are released.
au0828 and ALSA (snd_usb_audio) drivers are changed to use Media Device Allocator API to allocate/get reference to media device with the parent usb struct device as the key. Using this common media device, DVB, V4L2, and snd_usb_audio drivers can share media resources on an AU0828 media device.
The resource sharing varies depending on which component is in active use. When DVB is streaming, it holds the tuner in exclusive access mode. When s-video and composite are in use, the decoder will be held in exclusive access mode. Video and audio can share the tuner and decoder. au0828 acts as the master driver that decides the media topology and enforces the rules for sharing among various drivers.
- Tuner to decoder link is in sharable state when Video or audio enable it. Audio and Video share tuner (source) and decoder (sink).
- DVB holds tuner in exclusive mode.
- S-Video and Composite hold the resource in exclusive mode.
- VBI can’t share the link with Video/Audio. This is a au0828 driver bug. VBI should be able to share with video/audio. Some drivers expect that video starts streaming before vbi starts streaming. In some cases this is a hardware limitation, in other cases, it is a driver bug. Shuah will look into au0828 driver VBI related oddities. It will require updating enable/disable after the VBI bug is fixed to allow VBI sharing video/audio. Currently, starting VBI application changes input and active video/VBI captures stop.
Media sharing is tested using audio, video, and DVB applications. The following applications are used to test the exclusion and sharing rules.
- Kaffeine
- vlc digital mode
- vlc analog mode
- v4l2-ctl -f 700
- v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0
- qv4l2 -V /dev/vbi0
- qv4l2 -d /dev/video0
- xawtv -c /dev/video0
- tvtime -c 3 -d /dev/video0
- arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav
The following is a summary of test cases and the results for testing done on the patch series on Linux 5.0-rc7. Patch series is featured in an LWN Article and in on Linux Media kernel patches - Patchwork
DVB exclusive access test
When a DVB application is streaming while holding the tuner resource, audio/video/vbi applications should find the resource busy, DVB stream should not be disrupted, and the resource should be released when DVB application exits.
Application | Expected Result | Result |
---|---|---|
Start kaffeine and start digital TV. | Streaming with tuner held. | Streaming active with tuner held. |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Should exit with resource busy (EBUSY) error. | Exits with EBUSY. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start vlc, try TV digital capture and stop vlc. | Capture should fail with resource busy error. Only one DVB instance can open the device in write mode. | Capture fails with resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start vlc, try TV analog capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start qv4l2 -d /dev/video0 and try capture | Capture should fail with resource busy error | Capture fails with resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start qv4l2 -V /dev/vbi0 and try capture | Capture should fail with resource busy error | Capture fails with resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start v4l2-ctl -f 700 | Should exit with resource busy | Exits with EBUSY. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0 | Should exit with resource busy | Exits with EBUSY. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start xawtv -c /dev/video0 | Should detect resource busy | Detects resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Start tvtime -c 3 -d /dev/video0 | Should detect resource busy | Detects resource busy. No disruption to the DVB stream. Verified that DVB holds the tuner. Passed |
Stop kaffeine | Should release tuner. | Tuner is released. Passed |
VBI exclusive access test
When a VBI capture is in progress is while holding the input resource, DVB/audio/video applications should find the resource busy, VBI capture should not be disrupted, and the resource should be released when VBI application exits. When another VBI application tries to do capture, it should be able to share without disruption. However, au0828’s s_input changes input and active capture stops. This is separate problem to look into.
Application | Expected Result | Result |
---|---|---|
Start qv4l2 -V /dev/vbi0 and start capture | Capture starts with resource held. | Capture starts with resource held. |
Start kaffeine and start digital TV. | Should find resource busy. | Fails with resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Should exit with resource busy (EBUSY) error. | Exits with EBUSY. No disruption to capture. Verified that VBI holds the resource. Passed |
Start vlc, try TV digital capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Start vlc, try TV analog capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Start qv4l2 -d /dev/video0 and try capture | Capture should fail with resource busy error | Capture fails with resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Start qv4l2 -V /dev/vbi0 and start capture | Should share resource and capture? | Shares the resource, capture doesn’t start and active capture stops when s_input is changed. This is a au0828 driver s_input problem to look into separately. Unrelated problem. |
Start v4l2-ctl -f 700 | Should exit with resource busy | Exits with EBUSY. No disruption to capture. Verified that VBI holds the resource. Passed |
Start v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0 | Should exit with resource busy | Exits with EBUSY. No disruption to capture. Verified that VBI holds the resource. Passed |
Start xawtv -c /dev/video0 | Should detect resource busy | Detects resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Start tvtime -c 3 -d /dev/video0 | Should detect resource busy | Detects resource busy. No disruption to capture. Verified that VBI holds the resource. Passed |
Stop qv4l2 -V /dev/vbi0 | Should release resource | Resource is released. Passed |
Video active tests
When a video application is holding the tuner, audio and other video applications can share the tuner. Video driver allows more than one application to open the device and sharing logic allows more than video application to share the tuner. DVB and VBI applications should find the resource busy while video is actively using it.
Video tests with qv4l2 -d /dev/video0 active
Application | Expected Result | Result |
---|---|---|
Start qv4l2 -d /dev/video0 and start capture | Capture starts with resource held. | Capture starts with resource held. |
Start kaffeine and start digital TV. | Should find resource busy. | Fails with resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav and stop | Should be allowed to share the resource and when it exits, resource should be in locked state. | Shares tuner. Video stream continues. When arecord stooped, video continues to hold the resource. Passed |
Start vlc, try TV digital capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start vlc, try TV analog capture and stop vlc. | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Fails with vidioc_s_fmt_vid_cap queue busy. This check is done before, it looks to hold the resource. |
Start qv4l2 -d /dev/video0 and try capture | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Fails with vidioc_s_fmt_vid_cap queue busy. This check is done before, it looks to hold the resource. |
Start qv4l2 -V /dev/vbi0 and start capture | Should exit with resource busy | Exits with VIDIOC_STREAMON: Device or resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start v4l2-ctl -f 700 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Shares tuner link and updates frequency and exits. Video holds the resource. I expected the video driver to not update frequency. This behavior is unrelated to the patch series. |
Start v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Exits with VIDIOC_REQBUFS: failed: Device or resource busy. This check is done before, it looks to hold the resource. No disruption to the Video capture. Verified that Video holds the resource. |
Start xawtv -c /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Finds the audio device and then fails with vidioc_s_fmt_vid_cap queue busy. No disruption to the Video capture. Verified that Video holds the resource. Passed |
Start tvtime -c 3 -d /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Detects resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Stop qv4l2 -d /dev/video0 | Should release resource | Resource is released. Passed |
Video tests with v4l2-ctl –stream-mmap –stream-count=10000 -d /dev/video0 active
Application | Expected Result | Result |
---|---|---|
Start v4l2-ctl –stream-mmap –stream-count=10000 -d /dev/video0 | Capture starts with resource held. | Capture starts with resource held. |
Start kaffeine and start digital TV. | Should find resource busy. | Fails with resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav and stop | Should be allowed to share the resource and when it exits, resource should be in locked state. | Shares tuner. Video stream continues. When arecord stooped, video continues to hold the resource. Passed |
Start vlc, try TV digital capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start vlc, try TV analog capture and stop vlc. | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Fails with vidioc_s_fmt_vid_cap queue busy. This check is done before, it looks to hold the resource. |
Start qv4l2 -d /dev/video0 and try capture | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Fails with vidioc_s_fmt_vid_cap queue busy. This check is done before, it looks to hold the resource. |
Start qv4l2 -V /dev/vbi0 and start capture | Should exit with resource busy | Exits with VIDIOC_STREAMON: Device or resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Start v4l2-ctl -f 700 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Shares tuner link and updates frequency and exits. Video holds the resource. I expected the video driver to not update frequency. This behavior is unrelated to the patch series. |
Start v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource. | Exits with VIDIOC_REQBUFS: failed: Device or resource busy. This check is done before, it looks to hold the resource. No disruption to the Video capture. Verified that Video holds the resource. |
Start xawtv -c /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Finds the audio device and then fails with vidioc_s_fmt_vid_cap queue busy. No disruption to the Video capture. Verified that Video holds the resource. Passed |
Start tvtime -c 3 -d /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Detects resource busy. No disruption to capture. Verified that Video holds the resource. Passed |
Stop v4l2-ctl –stream-mmap –stream-count=10000 -d /dev/video0 | Should release resource | Resource is released. Passed |
Audio active tests with arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav
When an audio application is holding the tuner, video applications can share the tuner. Video driver allows more than one application to open the device and sharing logic allows more than video application to share the tuner. Audi driver on the other hand doesn’t. DVB and VBI applications should find the resource busy while video is actively using it.
Application | Expected Result | Result |
---|---|---|
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Capture starts with resource held. | Capture starts with resource held. |
Start kaffeine and start digital TV. | Should find resource busy. | Fails with resource busy. No disruption to capture. Verified that Audio holds the resource. Passed |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Should exit with device/resource busy. | Exits with device/resource busy. Note that this check happens before resource hold attempt. Verified audio holds the resource. Passed |
Start vlc, try TV digital capture and stop vlc. | Capture should fail with resource busy error. | Capture fails with resource busy. No disruption to capture. Verified that audio holds the resource. Passed |
Start vlc, try TV analog capture and stop vlc. | Expect video and audio link sharing to work. Audio device open should fail at open stage. | No errors from Video and audio device open fails. Verified audio holds the resource. Passed |
Start qv4l2 -d /dev/video0 and try capture. Stop. | Expect sharing video/audio sharing to work. | Capture works. Audio holds the release when video app exits. Passed |
Start qv4l2 -V /dev/vbi0 and start capture | Should exit with resource busy | Exits with VIDIOC_STREAMON: Device or resource busy. No disruption to capture. Verified that audio holds the resource. Passed |
Start v4l2-ctl -f 700 | Expect sharing audio/video sharing to work. | Shares tuner link and updates frequency and exits. audio holds the resource. Passed |
Start v4l2-ctl –stream-mmap –stream-count=100 -d /dev/video0 | Expect sharing audio/video sharing to work. | Capture works. Audio holds the release when video app exits. Passed |
Start xawtv -c /dev/video0 | Expect audio/video sharing to work. Audio device open should fail at open stage. | No errors from Video and audio/video sharing worked. Audio device open fails. Verified audio holds the resource. Passed |
Start tvtime -c 3 -d /dev/video0 | Expect sharing to work. Audio device open should fail at open stage. | No errors from Video and audio/video sharing worked. Audio device open fails. Verified audio holds the resource. Passed |
Stop arecord | Should release resource | Resource is released. Passed |
Sharing audio start - video start - audio stop - video stop testing
This is a test to make sure audio/video sharing works correctly and as audio and video applications get started and stopped, the resource continues to be held as long as one application is active and gets released correctly when the last application exits.
Application | Expected Result | Result |
---|---|---|
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Capture starts with resource held. | Capture starts with resource held. |
Start v4l2-ctl -f 700 | Expect sharing audio/video sharing to work. | Shares tuner link and updates frequency and exits. audio holds the resource. Passed |
Start v4l2-ctl –stream-mmap –stream-count=1000 -d /dev/video0 | Expect sharing audio/video sharing to work. | Capture works. Audio/video sharing works. Passed |
Stop arecord | Expect resource to stay locked with video using it. | Video capture continues. Resource is held. Passed |
Start arecord -M -D plughw:2,0 -c2 -f S16_LE -t wav foo.wav | Expect sharing audio/video sharing to work. | Capture works. Audio/video sharing works. Passed |
Stop v4l2-ctl –stream-mmap –stream-count=1000 -d /dev/video0 | Expect resource to stay locked with audio using it. | Audio capture continues. Resource is held. Passed |
Stop arecord | Should release resource | Resource is released. Passed |
Multiple video sessions testing
This is a test to make sure video sharing works correctly and as video applications get started and stopped, the resource continues to be held as long as one application is active and gets released correctly when the last application exits.
Application | Expected Result | Result |
---|---|---|
Start v4l2-ctl –stream-mmap –stream-count=1000 -d /dev/video0 | Expect sharing video sharing to work. | Capture works. |
Start v4l2-ctl -f 700 while streaming is active | Expect sharing video sharing to work. | Shares tuner link and updates frequency and exits. Video holds the resource when it exits. Passed |
Stop v4l2-ctl –stream-mmap –stream-count=1000 -d /dev/video0 | Expect sharing to work. It is possible an earlier video driver check might cause it bail out before looking for the resource | Exits with vidioc_s_fmt_vid_cap queue busy. Video capture continues. Resource is held. Passed |
Stop v4l2-ctl –stream-mmap –stream-count=1000 -d /dev/video0 exits | Should release resource | Resource is released. Passed |
Multiple vbi sessions testing
This is a test to make sure vbi sharing works correctly and as vbi applications get started and stopped, the resource continues to be held as long as one application is active and gets released correctly when the last application exits.
Application | Expected Result | Result |
---|---|---|
Start qv4l2 -V /dev/vbi0 and start capture | Capture starts with resource held | Capture starts and resource is held |
Start qv4l2 -V /dev/vbi0 | Expect sharing to work | Shares the resource, capture doesn’t start and active capture stops when s_input is changed. This is a au0828 driver s_input problem to look into separately. Unrelated problem. |
Stop qv4l2 -V /dev/vbi0 | Resource should be held. | Resource is held. Passed |
Stop qv4l2 -V /dev/vbi0 | Should release resource | Resource is released. Passed |