]> pere.pagekite.me Git - homepage.git/blob - blog/data/2018-07-12-kodi-linux-desktop-vlc.txt
Minor typo.
[homepage.git] / blog / data / 2018-07-12-kodi-linux-desktop-vlc.txt
1 Title: Streaming the Linux desktop to Kodi using VLC and RTSP
2 Tags: english, debian, video
3 Date: 2018-07-12 02:00
4
5 <p>A while back, I was asked by a friend how to stream the desktop to
6 my projector connected to Kodi. I sadly had to admit that I had no
7 idea, as it was a task I never had tried. Since then, I have been
8 looking for a way to do so, preferable without much extra software to
9 install on either side. Today I found a way that seem to kind of
10 work. Not great, but it is a start.</p>
11
12 <p>I had a look at several approaches, for example
13 <a href="https://github.com/mfoetsch/dlna_live_streaming">using uPnP
14 DLNA as described in 2011</a>, but it required a uPnP server, fuse and
15 local storage enough to store the stream locally. This is not going
16 to work well for me, lacking enough free space, and it would
17 impossible for my friend to get working.</p>
18
19 <p>Next, it occurred to me that perhaps I could use VLC to create a
20 video stream that Kodi could play. Preferably using
21 broadcast/multicast, to avoid having to change any setup on the Kodi
22 side when starting such stream. Unfortunately, the only recipe I
23 could find using multicast used the rtp protocol, and this protocol
24 seem to not be supported by Kodi.</p>
25
26 <p>On the other hand, the rtsp protocol is working! Unfortunately I
27 have to specify the IP address of the streaming machine in both the
28 sending command and the file on the Kodi server. But it is showing my
29 desktop, and thus allow us to have a shared look on the big screen at
30 the programs I work on.</p>
31
32 <p>I did not spend much time investigating codeces. I combined the
33 rtp and rtsp recipes from
34 <a href="https://wiki.videolan.org/Documentation:Streaming_HowTo/Command_Line_Examples/">the
35 VLC Streaming HowTo/Command Line Examples</a>, and was able to get
36 this working on the desktop/streaming end.</p>
37
38 <blockquote><pre>
39 vlc screen:// --sout \
40 '#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128}:rtp{dst=projector.local,port=1234,sdp=rtsp://192.168.11.4:8080/test.sdp}'
41 </pre></blockquote>
42
43 <p>I ssh-ed into my Kodi box and created a file like this with the
44 same IP address:</p>
45
46 <blockquote><pre>
47 echo rtsp://192.168.11.4:8080/test.sdp \
48 > /storage/videos/screenstream.m3u
49 </pre></blockquote>
50
51 <p>Note the 192.168.11.4 IP address is my desktops IP address. As far
52 as I can tell the IP must be hardcoded for this to work. In other
53 words, if someone elses machine is going to do the steaming, you have
54 to update screenstream.m3u on the Kodi machine and adjust the vlc
55 recipe. To get started, locate the file in Kodi and select the m3u
56 file while the VLC stream is running. The desktop then show up in my
57 big screen. :)</p>
58
59 <p>When using the same technique to stream a video file with audio,
60 the audio quality is really bad. No idea if the problem is package
61 loss or bad parameters for the transcode. I do not know VLC nor Kodi
62 enough to tell.</p>
63
64 <p><strong>Update 2018-07-12</strong>: Johannes Schauer send me a few
65 succestions and reminded me about an important step. The "screen:"
66 input source is only available once the vlc-plugin-access-extra
67 package is installed on Debian. Without it, you will see this error
68 message: "VLC is unable to open the MRL 'screen://'. Check the log
69 for details." He further found that it is possible to drop some parts
70 of the VLC command line to reduce the amount of hardcoded information.
71 It is also useful to consider using cvlc to avoid having the VLC
72 window in the desktop view. In sum, this give us this command line on
73 the source end
74
75 <blockquote><pre>
76 cvlc screen:// --sout \
77 '#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128}:rtp{sdp=rtsp://:8080/}'
78 </pre></blockquote>
79
80 <p>and this on the Kodi end<p>
81
82 <blockquote><pre>
83 echo rtsp://192.168.11.4:8080/ \
84 > /storage/videos/screenstream.m3u
85 </pre></blockquote>
86
87 <p>Still bad image quality, though. But I did discover that streaming
88 a DVD using dvdsimple:///dev/dvd as the source had excellent video and
89 audio quality, so I guess the issue is in the input or transcoding
90 parts, not the rtsp part. I've tried to change the vb and ab
91 parameters to use more bandwidth, but it did not make a
92 difference.</p>
93
94 <p>I further received a suggestion from Einar Haraldseid to try using
95 gstreamer instead of VLC, and this proved to work great! He also
96 provided me with the trick to get Kodi to use a multicast stream as
97 its source. By using this monstrous oneliner, I can stream my desktop
98 with good video quality in reasonable framerate to the 239.255.0.1
99 multicast address on port 1234:
100
101 <blockquote><pre>
102 gst-launch-1.0 ximagesrc use-damage=0 ! video/x-raw,framerate=30/1 ! \
103 videoconvert ! queue2 ! \
104 x264enc bitrate=8000 speed-preset=superfast tune=zerolatency qp-min=30 \
105 key-int-max=15 bframes=2 ! video/x-h264,profile=high ! queue2 ! \
106 mpegtsmux alignment=7 name=mux ! rndbuffersize max=1316 min=1316 ! \
107 udpsink host=239.255.0.1 port=1234 ttl-mc=1 auto-multicast=1 sync=0 \
108 pulsesrc device=$(pactl list | grep -A2 'Source #' | \
109 grep 'Name: .*\.monitor$' | cut -d" " -f2|head -1) ! \
110 audioconvert ! queue2 ! avenc_aac ! queue2 ! mux.
111 </pre></blockquote>
112
113 <p>and this on the Kodi end<p>
114
115 <blockquote><pre>
116 echo udp://@239.255.0.1:1234 \
117 > /storage/videos/screenstream.m3u
118 </pre></blockquote>
119
120 <p>Note the trick to pick a valid pulseaudio source. It might not
121 pick the one you need. This approach will of course lead to trouble
122 if more than one source uses the same multicast port and address.
123 Note the ttl-mc=1 setting, which limit the multicast packages to the
124 local network. If the value is increased, your screen will be
125 broadcasted further, one network "hop" for each increase (read up on
126 multicast to learn more. :)!</p>
127
128 <p>Having cracked how to get Kodi to receive multicast streams, I
129 could use this VLC command to stream to the same multicast address.
130 The image quality is way better than the rtsp approach, but gstreamer
131 seem to be doing a better job.</p>
132
133 <blockquote><pre>
134 cvlc screen:// --sout '#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128}:rtp{mux=ts,dst=239.255.0.1,port=1234,sdp=sap}'
135 </pre></blockquote>
136
137 <p>As usual, if you use Bitcoin and want to show your support of my
138 activities, please send Bitcoin donations to my address
139 <b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>