]> pere.pagekite.me Git - homepage.git/blob - blog/data/2018-07-12-kodi-linux-desktop-gstreamer.txt
Generated.
[homepage.git] / blog / data / 2018-07-12-kodi-linux-desktop-gstreamer.txt
1 Title: Simple streaming the Linux desktop to Kodi using GStreamer and RTP
2 Tags: english, debian, video, kodi
3 Date: 2018-07-12 17:55
4
5 <p>Last night, I wrote
6 <a href="https://people.skolelinux.org/pere/blog/Streaming_the_Linux_desktop_to_Kodi_using_VLC_and_RTSP.html">a
7 recipe to stream a Linux desktop using VLC to a instance of Kodi</a>.
8 During the day I received valuable feedback, and thanks to the
9 suggestions I have been able to rewrite the recipe into a much simpler
10 approach requiring no setup at all. It is a single script that take
11 care of it all.</p>
12
13 <p>This new script uses GStreamer instead of VLC to capture the
14 desktop and stream it to Kodi. This fixed the video quality issue I
15 saw initially. It further removes the need to add a m3u file on the
16 Kodi machine, as it instead connects to
17 <a href="https://kodi.wiki/view/JSON-RPC_API/v8">the JSON-RPC API in
18 Kodi</a> and simply ask Kodi to play from the stream created using
19 GStreamer. Streaming the desktop to Kodi now become trivial. Copy
20 the script below, run it with the DNS name or IP address of the kodi
21 server to stream to as the only argument, and watch your screen show
22 up on the Kodi screen. Note, it depend on multicast on the local
23 network, so if you need to stream outside the local network, the
24 script must be modified. Also note, I have no idea if audio work, as
25 I only care about the picture part.</p>
26
27 <blockquote><pre>
28 #!/bin/sh
29 #
30 # Stream the Linux desktop view to Kodi. See
31 # https://people.skolelinux.org/pere/blog/Streaming_the_Linux_desktop_to_Kodi_using_VLC_and_RTSP.html
32 # for backgorund information.
33
34 # Make sure the stream is stopped in Kodi and the gstreamer process is
35 # killed if something go wrong (for example if curl is unable to find the
36 # kodi server). Do the same when interrupting this script.
37 kodicmd() {
38 host="$1"
39 cmd="$2"
40 params="$3"
41 curl --silent --header 'Content-Type: application/json' \
42 --data-binary "{ \"id\": 1, \"jsonrpc\": \"2.0\", \"method\": \"$cmd\", \"params\": $params }" \
43 "http://$host/jsonrpc"
44 }
45 cleanup() {
46 if [ -n "$kodihost" ] ; then
47 # Stop the playing when we end
48 playerid=$(kodicmd "$kodihost" Player.GetActivePlayers "{}" |
49 jq .result[].playerid)
50 kodicmd "$kodihost" Player.Stop "{ \"playerid\" : $playerid }" > /dev/null
51 fi
52 if [ "$gstpid" ] && kill -0 "$gstpid" >/dev/null 2>&1; then
53 kill "$gstpid"
54 fi
55 }
56 trap cleanup EXIT INT
57
58 if [ -n "$1" ]; then
59 kodihost=$1
60 shift
61 else
62 kodihost=kodi.local
63 fi
64
65 mcast=239.255.0.1
66 mcastport=1234
67 mcastttl=1
68
69 pasrc=$(pactl list | grep -A2 'Source #' | grep 'Name: .*\.monitor$' | \
70 cut -d" " -f2|head -1)
71 gst-launch-1.0 ximagesrc use-damage=0 ! video/x-raw,framerate=30/1 ! \
72 videoconvert ! queue2 ! \
73 x264enc bitrate=8000 speed-preset=superfast tune=zerolatency qp-min=30 \
74 key-int-max=15 bframes=2 ! video/x-h264,profile=high ! queue2 ! \
75 mpegtsmux alignment=7 name=mux ! rndbuffersize max=1316 min=1316 ! \
76 udpsink host=$mcast port=$mcastport ttl-mc=$mcastttl auto-multicast=1 sync=0 \
77 pulsesrc device=$pasrc ! audioconvert ! queue2 ! avenc_aac ! queue2 ! mux. \
78 > /dev/null 2>&1 &
79 gstpid=$!
80
81 # Give stream a second to get going
82 sleep 1
83
84 # Ask kodi to start streaming using its JSON-RPC API
85 kodicmd "$kodihost" Player.Open \
86 "{\"item\": { \"file\": \"udp://@$mcast:$mcastport\" } }" > /dev/null
87
88 # wait for gst to end
89 wait "$gstpid"
90 </pre></blockquote>
91
92 <p>I hope you find the approach useful. I know I do.</p>
93
94 <p>As usual, if you use Bitcoin and want to show your support of my
95 activities, please send Bitcoin donations to my address
96 <b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>