<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Jason&#39;s Blog</title>
    <link>/</link>
    <description>Jason&#39;s Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    
      
        <managingEditor>jasoncarrete5@gmail.com
          
        </managingEditor>
      

      
    

    
    <lastBuildDate>Wed, 08 Jun 2022 23:08:31 -0400</lastBuildDate>
    
    <atom:link href="/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Using a Rasberry Pi 4 as a Webcam</title>
      <link>/posts/streaming-rpi/</link>
      <pubDate>Sun, 26 Sep 2021 00:00:00 +0000</pubDate>
      <author>jasoncarrete5@gmail.com</author>
      <guid>/posts/streaming-rpi/</guid>
      <description>&lt;p&gt;I wanted a way to share my video with my friends in a Discord call. Lacking a
proper webcam, I needed some other way to capture myself on video. Fortunately,
I happened to have a Raspberry Pi 4 and a camera module. Initially, I thought
maybe I could just connect a USB cable directly between my Raspberry Pi and my
computer and have my computer recognize the Pi as a camera with some software. A
quick google search helped me realize this is not such a good idea as connecting
devices together in this way can damage the connected devices. A special cable
typically labeled as a USB bridging cable would be required for this to work and
since I am trying to avoid spending money, I needed another solution.&lt;/p&gt;
&lt;h2 id=&#34;enter-gstreamer&#34; &gt;
&lt;div&gt;
    &lt;a href=&#34;#enter-gstreamer&#34;&gt;
        #
    &lt;/a&gt;
    Enter GStreamer
&lt;/div&gt;
&lt;/h2&gt;
&lt;p&gt;GStreamer is a multimedia framework that can be used to construct pipelines for
media data to flow through. In order to get the following pipelines to run, we
must have GStreamer installed on both the Pi and the host system. We&amp;rsquo;ll want to
get pretty much all the GStreamer plugins (at least the good and the bad) and
we&amp;rsquo;ll also need a kernel module to facilitate the creation of a video loopback
device. On Arch Linux, this kernel module can be installed from the
v4l2loopback-dkms package. I won&amp;rsquo;t go into the details about how to setup a v4l2
device here; There are other articles online that can explain the process.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=720,framerate=30/1 ! x264enc tune=zerolatency speed-preset=superfast ! rtph264pay ! queue ! udpsink host=jasonc-desktop.myfiosgateway.com
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above shell script fragment constructs a GStreamer pipeline that reads data
from the device file /dev/video0 and sends it to the specified host. While this
is running on the Pi, we can run another pipeline on the host computer to listen
for the incoming RTP packets and write them out to a device file.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;gst-launch-1.0 udpsrc ! application/x-rtp ! queue ! rtph264depay ! decodebin ! videoconvert ! video/x-raw,format=YUY2 ! v4l2sink device=/dev/video0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pay particular attention to the format specified in the caps filter just before
the v4l2sink! I spent a days trying to figure out why Discord wouldn&amp;rsquo;t accept my
video device. Turns out, the format was erroneously negotiated as I420 and I had
to specifically specify the format as YUY2 for Discord to accept the stream. The
caps filter after the v4l2src is also important as Discord will not accept a
stream with greater than 720p resolution and 30fps.&lt;/p&gt;
&lt;p&gt;With both pipelines running, we can choose our loopback video device as our
input video in Discord and we&amp;rsquo;ll be able to see our output in Discord!&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
