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