VideoTexture, only first frame shown (gst-launch/v4l2loopback -> Blender) #48692

Closed
opened 2016-06-21 01:34:42 +02:00 by Mario Mey · 24 comments

System
Ubuntu-Mate 14.04 64x, nVidia GT620.

Issue
I made a 3D scene (in BGE) with two VideoTextures: one shows the webcam image, the other one: a v4l2loopback device capturing second-display screen, using gst-launch-1.0. No matter what command I use (*), when BlenderPlayer starts, it only shows the first frame of the v4l2loopback device. Webcam works well.

What I tested:

  • Using VLC, webcam and any v4l2loopback works OK. I get smooth playback.
  • Using Blender, webcam and another capture device (EasyCap), also I get smooth playback of both devices.
  • Using two gst-launch-1.0 instances, both devices are displayed well in different windows, at the same time (I use "ximagesink" instead of "v4l2sink...")

(*) My command:

gst-launch-1.0 ximagesrc startx=1920 starty=0 endx=2943 endy=768 show-pointer=0 use-damage=0 \
! video/x-raw,framerate=30/1 \
! videoscale method=0 add-borders=false \
! video/x-raw,width=640,height=360 \
! videoflip method=horizontal-flip \
! v4l2sink device=/dev/video1

Also, I tried with the simplest one:

gst-launch videotestsrc ! v4l2sink device=/dev/video1

Blend file: 95-video-texture-v4l2loopback.blend

To reproduce the "issue"

  • Install v4l2loopback module (https://github.com/umlaeute/v4l2loopback)
  • Connect a webcam.
  • Run the simple gst-launch command mentioned above.
  • Open VLC and test both devices.
  • Close VLC.
  • Open the blend file in Blender.
  • Start Game Engine.

What it should do

  • One object should show the webcam device and, the other, a video-test-pattern with motion.

I also reported in v4l2loopback GitHub issues page: https://github.com/umlaeute/v4l2loopback/issues/115

**System** Ubuntu-Mate 14.04 64x, nVidia GT620. **Issue** I made a 3D scene (in BGE) with two VideoTextures: one shows the webcam image, the other one: a v4l2loopback device capturing second-display screen, using gst-launch-1.0. No matter what command I use (*), when BlenderPlayer starts, it only shows the first frame of the v4l2loopback device. Webcam works well. What I tested: - Using VLC, webcam and any v4l2loopback works OK. I get smooth playback. - Using Blender, webcam and another capture device (EasyCap), also I get smooth playback of both devices. - Using two gst-launch-1.0 instances, both devices are displayed well in different windows, at the same time (I use "ximagesink" instead of "v4l2sink...") (*) My command: ``` gst-launch-1.0 ximagesrc startx=1920 starty=0 endx=2943 endy=768 show-pointer=0 use-damage=0 \ ! video/x-raw,framerate=30/1 \ ! videoscale method=0 add-borders=false \ ! video/x-raw,width=640,height=360 \ ! videoflip method=horizontal-flip \ ! v4l2sink device=/dev/video1 ``` Also, I tried with the simplest one: ``` gst-launch videotestsrc ! v4l2sink device=/dev/video1 ``` Blend file: [95-video-texture-v4l2loopback.blend](https://archive.blender.org/developer/F317915/95-video-texture-v4l2loopback.blend) **To reproduce the "issue"** - Install v4l2loopback module (https://github.com/umlaeute/v4l2loopback) - Connect a webcam. - Run the simple gst-launch command mentioned above. - Open VLC and test both devices. - Close VLC. - Open the blend file in Blender. - Start Game Engine. **What it should do** - One object should show the webcam device and, the other, a video-test-pattern with motion. I also reported in v4l2loopback GitHub issues page: https://github.com/umlaeute/v4l2loopback/issues/115
Author

Changed status to: 'Open'

Changed status to: 'Open'
Benoit Cousson was assigned by Mario Mey 2016-06-21 01:34:42 +02:00
Author

Added subscriber: @MarioSottile

Added subscriber: @MarioSottile
Author

This file works in Blender > 2.66.
The bug is there and all the later versions. Included the last one 2.77a.

This file works in Blender > 2.66. The bug is there and all the later versions. Included the last one 2.77a.
Author

Added subscriber: @BenoitBolsee

Added subscriber: @BenoitBolsee
Author

I think "this Benoit" was the one that Dalai told me to add here...

I think "this Benoit" was the one that Dalai told me to add here...
Benoit Cousson was unassigned by Mario Mey 2016-06-22 01:25:35 +02:00
Benoit Bolsee was assigned by Mario Mey 2016-06-22 01:25:35 +02:00
Author

Added subscriber: @benoit-2

Added subscriber: @benoit-2

Added subscriber: @Sergey

Added subscriber: @Sergey

@BenoitBolsee, are you looking into this issue?

@BenoitBolsee, are you looking into this issue?
Member

not yet, I need to get hold of a webcam to reproduce.

not yet, I need to get hold of a webcam to reproduce.
Author

@BenoitBolsee, you don't really need a camera to test the bug. The webcam is there to show that /dev/video0 is working and /dev/video1 isn't.

Just comment these lines and run it:

14: monitor_webcam()
27: bge.logic.webcam_texture.refresh(True)

@BenoitBolsee, you don't really need a camera to test the bug. The webcam is there to show that /dev/video0 is working and /dev/video1 isn't. Just comment these lines and run it: ``` 14: monitor_webcam() 27: bge.logic.webcam_texture.refresh(True) ```
Member

Thanks for the instructions; I was able to reproduce the bug.
It appears that multi-thread caching doesn't work well with that particular v4l source.
I was able to get a smooth output by disabling caching.

I will analyze further and determine if I need to fix caching or disable it or make it selectable by API (caching is known to work with webcam source).

Thanks for the instructions; I was able to reproduce the bug. It appears that multi-thread caching doesn't work well with that particular v4l source. I was able to get a smooth output by disabling caching. I will analyze further and determine if I need to fix caching or disable it or make it selectable by API (caching is known to work with webcam source).
Author

Is there any way to disable caching without modifying code and recompiling...?
If not (and if it seems to be stable to do that), could you make a diff (patch) file for me to compile Blender here?

(I don't want to seem anxious, but it would be very-great if I can make it work here during these days... it's for a particular job)

Thanks!

Is there any way to disable caching without modifying code and recompiling...? If not (and if it seems to be stable to do that), could you make a diff (patch) file for me to compile Blender here? (I don't want to seem anxious, but it would be very-great if I can make it work here during these days... it's for a particular job) Thanks!
Member

Cache is enabled automatically as soon as Blender detects that the CPU has more than 1 core.
To disable it you must change the code. The diff is extremely simple: just comment the two line with m_isThreaded = true; in VideoFFmpeg.cpp:

diff --git a/source/gameengine/VideoTexture/VideoFFmpeg.cpp b/source/gameengine/VideoTexture/VideoFFmpeg.cpp
index 083e9e2..5a470be 100644
--- a/source/gameengine/VideoTexture/VideoFFmpeg.cpp
+++ b/source/gameengine/VideoTexture/VideoFFmpeg.cpp
@@ -581,7 +581,7 @@ void VideoFFmpeg::openFile (char *filename)
 	{
 		*never thread image: there are no frame to read ahead* no need to thread if the system has a single core
-		m_isThreaded =  true;
+		//m_isThreaded =  true;
 	}
 }
 
@@ -673,7 +673,7 @@ void VideoFFmpeg::openCam (char *file, short camIdx)
 	if (BLI_system_thread_count() > 1)
 	{
 		// no need to thread if the system has a single core
-		m_isThreaded =  true;
+		//m_isThreaded =  true;
 	}
 
 	av_dict_free(&formatParams);
Cache is enabled automatically as soon as Blender detects that the CPU has more than 1 core. To disable it you must change the code. The diff is extremely simple: just comment the two line with m_isThreaded = true; in VideoFFmpeg.cpp: ``` diff --git a/source/gameengine/VideoTexture/VideoFFmpeg.cpp b/source/gameengine/VideoTexture/VideoFFmpeg.cpp index 083e9e2..5a470be 100644 --- a/source/gameengine/VideoTexture/VideoFFmpeg.cpp +++ b/source/gameengine/VideoTexture/VideoFFmpeg.cpp @@ -581,7 +581,7 @@ void VideoFFmpeg::openFile (char *filename) { *never thread image: there are no frame to read ahead* no need to thread if the system has a single core - m_isThreaded = true; + //m_isThreaded = true; } } @@ -673,7 +673,7 @@ void VideoFFmpeg::openCam (char *file, short camIdx) if (BLI_system_thread_count() > 1) { // no need to thread if the system has a single core - m_isThreaded = true; + //m_isThreaded = true; } av_dict_free(&formatParams); ```
Author

It worked. ;)

For the moment... Is it safe to disable this caching? Just optimization purpose...?

**It worked.** ;) For the moment... Is it safe to disable this caching? Just optimization purpose...?
Member

Yes, caching is just for optimization. It is safe to disable it.

I did more tests and the problem is caused by the v4l2loopback not providing correct timestamp on frames: vt thinks that the frames are in the past and drops them.
There is a flag to fix this: m_isStreaming; this flags tells vt to ignore timestamp. Then caching works but not smoothly: there is a strange jitter in the stream as if the frames were not properly ordered. It seems that the loopback device doesn't implement a proper frame queue.

I'll investigate further but the best option apparently is to make caching optional by API.

Yes, caching is just for optimization. It is safe to disable it. I did more tests and the problem is caused by the v4l2loopback not providing correct timestamp on frames: vt thinks that the frames are in the past and drops them. There is a flag to fix this: m_isStreaming; this flags tells vt to ignore timestamp. Then caching works but not smoothly: there is a strange jitter in the stream as if the frames were not properly ordered. It seems that the loopback device doesn't implement a proper frame queue. I'll investigate further but the best option apparently is to make caching optional by API.
Author

As I reported this same bug in v4l2loopback issues github page, can I add this information that you are telling to me? Is there any other further information to post there?

Maybe v4l2loopback also can be better by providing the correct timestamp on frames.

As I reported this same bug in v4l2loopback issues github page, can I add this information that you are telling to me? Is there any other further information to post there? Maybe v4l2loopback also can be better by providing the correct timestamp on frames.
Member

Yes you can pass that information to them. They will probably find useful to look at the source code that's doing the caching. It's all in the VideoFFmpeg::cacheThread() function.

I noticed the following anomalies:
av_read_frame() never returns < 0, which would mean 'no frame available at this time' no matter how frequently I call this function and despite the fact that the AVFMT_FLAG_NONBLOCK flag is set in the format context. This is abnormal, v4l2loopback should not return more frames than the target frame rate that is set in the configuration of the device. After decoding with avcodec_decode_video2(), the resulting AVPacket has a dts field set to a fixed value. This is abnormal, I expect dts to be a proper timestamp of the frame. It should advance as a real time clock.

Yes you can pass that information to them. They will probably find useful to look at the source code that's doing the caching. It's all in the VideoFFmpeg::cacheThread() function. I noticed the following anomalies: **av_read_frame() never returns < 0, which would mean 'no frame available at this time' no matter how frequently I call this function and despite the fact that the AVFMT_FLAG_NONBLOCK flag is set in the format context. This is abnormal, v4l2loopback should not return more frames than the target frame rate that is set in the configuration of the device.** After decoding with avcodec_decode_video2(), the resulting AVPacket has a dts field set to a fixed value. This is abnormal, I expect dts to be a proper timestamp of the frame. It should advance as a real time clock.
Author

@BenoitBolsee : I reported this issue in v4l2loopback github page (in June). Now, there is a new version of this module: 0.10.0.

Umlaeute (v4l2loopback dev) ask me for trying this new version with this issue. I installed new version and tested the file with last Blender version... but the issue is still there.

I don't know if Blender "multi-thread caching" issue is still there and it is the main problem... is it resolvable?

Could you try this?

@BenoitBolsee : I reported this issue in v4l2loopback github page (in June). Now, there is a new version of this module: 0.10.0. Umlaeute (v4l2loopback dev) ask me for trying this new version with this issue. I installed new version and tested the file with last Blender version... but the issue is still there. I don't know if Blender "multi-thread caching" issue is still there and it is the main problem... is it resolvable? Could you try this?
Author

I realized that, in june 2016, I wanted to write here... and I wrote in another report (in #46755)! It says:


Wow, it seems to be a big optimization... or a a big bug.

Using the same file, showing the WebCam, there's a big difference in performance between BlenderPlayer with and without caching... but only with WebCam! Look at these window-shots:

Only Webcam, with caching: 40fps.
Pantallazo-ctrl-0.png
Only Webcam, without caching (aka, commented thread line): 10-14fps.
Pantallazo-ctrl-1.png
Webcam and SecondDisplay-Capture, without caching: also 10-14fps.
Pantallazo-ctrl-2.png
Only SecondDisplay-Capture, without caching: 50-60fps.
Pantallazo-ctrl-3.png
So, now I have a problem with WebCam. Where is the problem now? :(

Notes:

  • In the second monitor (TV) there's a blenderplayer instance with a Digital Puppet. The captures below are about a blend file that control the puppet (receive the mouse and keyboard and send OSC messages with input information).
  • My webcam is a Logitech C170. It captures in 640x480.
I realized that, in june 2016, I wanted to write here... and I wrote in another report (in #46755)! It says: *<start of the comment>* Wow, it seems to be a big optimization... or a a big bug. Using the same file, showing the WebCam, there's a big difference in performance between BlenderPlayer with and without caching... but only with WebCam! Look at these window-shots: Only Webcam, with caching: 40fps. ![Pantallazo-ctrl-0.png](https://archive.blender.org/developer/F636038/Pantallazo-ctrl-0.png) Only Webcam, without caching (aka, commented thread line): 10-14fps. ![Pantallazo-ctrl-1.png](https://archive.blender.org/developer/F636040/Pantallazo-ctrl-1.png) Webcam and SecondDisplay-Capture, without caching: also 10-14fps. ![Pantallazo-ctrl-2.png](https://archive.blender.org/developer/F636042/Pantallazo-ctrl-2.png) Only SecondDisplay-Capture, without caching: 50-60fps. ![Pantallazo-ctrl-3.png](https://archive.blender.org/developer/F636045/Pantallazo-ctrl-3.png) So, now I have a problem with WebCam. Where is the problem now? :( Notes: - In the second monitor (TV) there's a blenderplayer instance with a Digital Puppet. The captures below are about a blend file that control the puppet (receive the mouse and keyboard and send OSC messages with input information). - My webcam is a Logitech C170. It captures in 640x480.
Author

Also, for a reason that I don't understand... this disabling cache workaround is not working in this new Ubuntu version (previous: 14.04, actual: 16.04).

Finally, I compiled Blender in this distro. The line about threading is still commented:

		//m_isThreaded =  true;

... but I run the blend file and it is doing the same as when the report was created: the image is still, not moving at all.

Blender source is the same, same version-commit, v4l2loopback does not have any new commit from that moment (june 2016).

What could be happening?

Finally... where is the problem? in v4l2loopback source code?

Also, for a reason that I don't understand... this disabling cache workaround is not working in this new Ubuntu version (previous: 14.04, actual: 16.04). Finally, I compiled Blender in this distro. The line about threading is still commented: ``` //m_isThreaded = true; ``` ... but I run the blend file and it is doing the same as when the report was created: the image is still, not moving at all. Blender source is the same, same version-commit, v4l2loopback does not have any new commit from that moment (june 2016). What could be happening? Finally... where is the problem? in v4l2loopback source code?
Author

@BenoitBolsee I need your hand.

In v4l2loopback bug report, umlaeute (dev) asks this:

i think one of the issues is that it is unclear who should actually provide the timestamp: the module or the producer? (I tend towards the producer, but I don't know whether there is a single producer out there that actually does it)

@BenoitBolsee I need your hand. In v4l2loopback bug report, umlaeute (dev) asks this: > i think one of the issues is that it is unclear who should actually provide the timestamp: the module or the producer? (I tend towards the producer, but I don't know whether there is a single producer out there that actually does it)
Member

Added subscriber: @Blendify

Added subscriber: @Blendify
Member

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Member

This task is being closed because the BGE has been removed in Blender 2.8.

This task is being closed because the BGE has been removed in Blender 2.8.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#48692
No description provided.