Screen Mirroring in GNU Linux

Screen mirroring allows you to display your laptop screen on a TV via Wifi Direct. This is also known as screen casting, Miracast, Wifi Display or Wifi P2P. Usage of Wifi Direct means you don't necessarily need a router or a working network. The data is streamed using RTSP. However, it doesn't use the standard RTSP port. The port information is shared during connection process (usually defaults to 7236 as per Miracast specifications). Also, the RTSP server must support additional Miracast related negotiations.

Note: Feature-wise, Second Screen or DIAL (DIscovery And Launch) provides similar experience (primarily for Youtube and Netflix apps). But this (DIAL) requires both the devices to be in the same network.

Debian XFCE meets LG 4K TV (UJ632T)

The core of this technology in Linux is wpa_supplicant (or its variant - connmand or miracle-wifid). connmand (for WDS) and miracle-wifid (for MiracleCast) also manage your network interface. You might also have NetworkManager as network managing interface on your system. Be aware that these might interfere with each other. You can use gnome-network-displays (uses network manager) for quick testing. This starts its own GStreamer based RTSP server (wfd_media_factory_create_element) on port 7236. It uses 1920x1080 p30 display resolution (wfd-client.c:165).

wpa_supplicant is configured using a configuration file during startup or during runtime (e.g. changing networks) via DBus or UNIX socket based Control Interface. The package also provides wpa_cli for interactive usage. You can find some more sample usage here. Below is a sample wpa_cli session (bold text represents command).

> p2p_find
OK
> <2>P2P-DEVICE-FOUND 02:40:61:c2:f3:b7 p2p_dev_addr=02:40:61:c2:f3:b7
pri_dev_type=1-0050F204-1 name='Wireless Client 2' config_methods=0x18c
dev_capab=0x1 group_capab=0x0
<2>P2P-GO-NEG-REQUEST 02:40:61:c2:f3:b7
<2>P2P-GO-NEG-REQUEST 02:40:61:c2:f3:b7
> p2p_connect 02:40:61:c2:f3:b7 pbc
OK
<2>P2P-GO-NEG-SUCCESS 
<2>P2P-GROUP-FORMATION-SUCCESS 
<2>P2P-GROUP-STARTED sta0-p2p-0 client DIRECT-vM
> interface
Available interfaces:
sta0-p2p-0
sta0
> p2p_group_remove sta0-p2p-0
<2>P2P-GROUP-REMOVED sta0-p2p-0 client
OK
> term
OK


Building gnome-network-display

Follow these steps:

$ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-rtsp libgstrtspserver-1.0-dev libnm-dev libpulse-dev meson
$ git clone https://gitlab.gnome.org/GNOME/gnome-network-displays.git
$ cd gnome-network-displays
$ meson build
$ meson install -C build
$ gnome-network-displays

 

gnome-network-display Flatpak

You can also use flatpak if you don't want to compile as above.

$ sudo apt install flatpak
$ flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
$ flatpak install flathub org.gnome.NetworkDisplays
$ flatpak run org.gnome.NetworkDisplays

 

Debugging

- Ensure your hardware supports P2P-GO and P2P-client. Notice that some hardware also support interface combinations. They can have multiple roles (BSS, AP, P2P GO or P2P client) in the network simultaneously. If you create more interfaces than channels supported, some interfaces will have to share a channel.

 
root@laptop:~# iw list
Wiphy phy0
    wiphy index: 0
    max # scan SSIDs: 4
    max scan IEs length: 2243 bytes
    max # sched scan SSIDs: 0
    max # match sets: 0
    RTS threshold: 2347
    Retry short limit: 7
    Retry long limit: 4
    Coverage class: 0 (up to 0m)
    Device supports RSN-IBSS.
    Supported Ciphers:
        * WEP40 (00-0f-ac:1)
        * WEP104 (00-0f-ac:5)
        * TKIP (00-0f-ac:2)
        * CCMP-128 (00-0f-ac:4)
        * CCMP-256 (00-0f-ac:10)
        * GCMP-128 (00-0f-ac:8)
        * GCMP-256 (00-0f-ac:9)
        * CMAC (00-0f-ac:6)
        * CMAC-256 (00-0f-ac:13)
        * GMAC-128 (00-0f-ac:11)
        * GMAC-256 (00-0f-ac:12)
    Available Antennas: TX 0 RX 0
    Supported interface modes:
         * IBSS
         * managed
         * AP
         * AP/VLAN
         * monitor
         * mesh point
         * P2P-client
         * P2P-GO

    Band 1:
        Capabilities: 0x186e
            HT20/HT40
            SM Power Save disabled
            RX HT20 SGI
            RX HT40 SGI
            No RX STBC
            Max AMSDU length: 7935 bytes
            DSSS/CCK HT40
        Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
        Minimum RX AMPDU time spacing: 16 usec (0x07)
        HT Max RX data rate: 150 Mbps
        HT TX/RX MCS rate indexes supported: 0-7, 32
        Bitrates (non-HT):
            * 1.0 Mbps
            * 2.0 Mbps
            * 5.5 Mbps
            * 11.0 Mbps
            * 6.0 Mbps
            * 9.0 Mbps
            * 12.0 Mbps
            * 18.0 Mbps
            * 24.0 Mbps
            * 36.0 Mbps
            * 48.0 Mbps
            * 54.0 Mbps
        Frequencies:
            * 2412 MHz [1] (20.0 dBm)
            * 2417 MHz [2] (20.0 dBm)
            * 2422 MHz [3] (20.0 dBm)
            * 2427 MHz [4] (20.0 dBm)
            * 2432 MHz [5] (20.0 dBm)
            * 2437 MHz [6] (20.0 dBm)
            * 2442 MHz [7] (20.0 dBm)
            * 2447 MHz [8] (20.0 dBm)
            * 2452 MHz [9] (20.0 dBm)
            * 2457 MHz [10] (20.0 dBm)
            * 2462 MHz [11] (20.0 dBm)
            * 2467 MHz [12] (20.0 dBm)
            * 2472 MHz [13] (20.0 dBm)
            * 2484 MHz [14] (disabled)
    Band 2:
        Capabilities: 0x186e
            HT20/HT40
            SM Power Save disabled
            RX HT20 SGI
            RX HT40 SGI
            No RX STBC
            Max AMSDU length: 7935 bytes
            DSSS/CCK HT40
        Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
        Minimum RX AMPDU time spacing: 16 usec (0x07)
        HT Max RX data rate: 150 Mbps
        HT TX/RX MCS rate indexes supported: 0-7, 32
        VHT Capabilities (0x33c019a2):
            Max MPDU length: 11454
            Supported Channel Width: neither 160 nor 80+80
            short GI (80 MHz)
            TX STBC
            SU Beamformer
            SU Beamformee
            +HTC-VHT
            RX antenna pattern consistency
            TX antenna pattern consistency
        VHT RX MCS set:
            1 streams: MCS 0-9
            2 streams: not supported
            3 streams: not supported
            4 streams: not supported
            5 streams: not supported
            6 streams: not supported
            7 streams: not supported
            8 streams: not supported
        VHT RX highest supported: 434 Mbps
        VHT TX MCS set:
            1 streams: MCS 0-9
            2 streams: not supported
            3 streams: not supported
            4 streams: not supported
            5 streams: not supported
            6 streams: not supported
            7 streams: not supported
            8 streams: not supported
        VHT TX highest supported: 434 Mbps
        Bitrates (non-HT):
            * 6.0 Mbps
            * 9.0 Mbps
            * 12.0 Mbps
            * 18.0 Mbps
            * 24.0 Mbps
            * 36.0 Mbps
            * 48.0 Mbps
            * 54.0 Mbps
        Frequencies:
            * 5180 MHz [36] (30.0 dBm)
            * 5200 MHz [40] (30.0 dBm)
            * 5220 MHz [44] (30.0 dBm)
            * 5240 MHz [48] (30.0 dBm)
            * 5260 MHz [52] (30.0 dBm) (no IR, radar detection)
            * 5280 MHz [56] (30.0 dBm) (no IR, radar detection)
            * 5300 MHz [60] (30.0 dBm) (no IR, radar detection)
            * 5320 MHz [64] (30.0 dBm) (no IR, radar detection)
            * 5500 MHz [100] (30.0 dBm) (no IR, radar detection)
            * 5520 MHz [104] (30.0 dBm) (no IR, radar detection)
            * 5540 MHz [108] (30.0 dBm) (no IR, radar detection)
            * 5560 MHz [112] (30.0 dBm) (no IR, radar detection)
            * 5580 MHz [116] (30.0 dBm) (no IR, radar detection)
            * 5600 MHz [120] (30.0 dBm) (no IR, radar detection)
            * 5620 MHz [124] (30.0 dBm) (no IR, radar detection)
            * 5640 MHz [128] (30.0 dBm) (no IR, radar detection)
            * 5660 MHz [132] (30.0 dBm) (no IR, radar detection)
            * 5680 MHz [136] (30.0 dBm) (no IR, radar detection)
            * 5700 MHz [140] (30.0 dBm) (no IR, radar detection)
            * 5745 MHz [149] (30.0 dBm)
            * 5765 MHz [153] (30.0 dBm)
            * 5785 MHz [157] (30.0 dBm)
            * 5805 MHz [161] (30.0 dBm)
            * 5825 MHz [165] (30.0 dBm)
    Supported commands:
         * new_interface
         * set_interface
         * new_key
         * start_ap
         * new_station
         * new_mpath
         * set_mesh_config
         * set_bss
         * authenticate
         * associate
         * deauthenticate
         * disassociate
         * join_ibss
         * join_mesh
         * remain_on_channel
         * set_tx_bitrate_mask
         * frame
         * frame_wait_cancel
         * set_wiphy_netns
         * set_channel
         * set_wds_peer
         * probe_client
         * set_noack_map
         * register_beacons
         * start_p2p_device
         * set_mcast_rate
         * connect
         * disconnect
         * set_qos_map
         * set_multicast_to_unicast
    WoWLAN support:
         * wake up on magic packet
         * wake up on pattern match, up to 16 patterns of 13-128 bytes,
           maximum packet offset 0 bytes
    software interface modes (can always be added):
         * AP/VLAN
         * monitor
    interface combinations are not supported
    HT Capability overrides:
         * MCS: ff ff ff ff ff ff ff ff ff ff
         * maximum A-MSDU length
         * supported channel width
         * short GI for 40 MHz
         * max A-MPDU length exponent
         * min MPDU start spacing
    Device supports TX status socket option.
    Device supports HT-IBSS.
    Device supports SAE with AUTHENTICATE command
    Device supports low priority scan.
    Device supports scan flush.
    Device supports AP scan.
    Device supports per-vif TX power setting
    Driver supports full state transitions for AP/GO clients
    Driver supports a userspace MPM
    Device supports configuring vdev MAC-addr on create.
    max # scan plans: 1
    max scan plan interval: -1
    max scan plan iterations: 0
    Supported TX frame types:
         * IBSS: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * managed: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * AP: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * AP/VLAN: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * mesh point: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * P2P-client: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * P2P-GO: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
         * P2P-device: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0
    Supported RX frame types:
         * IBSS: 0x40 0xb0 0xc0 0xd0
         * managed: 0x40 0xb0 0xd0
         * AP: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
         * AP/VLAN: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
         * mesh point: 0xb0 0xc0 0xd0
         * P2P-client: 0x40 0xd0
         * P2P-GO: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0
         * P2P-device: 0x40 0xd0
    Supported extended features:
        * [ RRM ]: RRM
        * [ FILS_STA ]: STA FILS (Fast Initial Link Setup)
        * [ CONTROL_PORT_OVER_NL80211 ]: control port over nl80211
        * [ SCAN_RANDOM_SN ]: use random sequence numbers in scans
        * [ SCAN_MIN_PREQ_CONTENT ]: use probe request with only rate IEs in scans
        * [ CONTROL_PORT_NO_PREAUTH ]: disable pre-auth over nl80211 control port support
        * [ DEL_IBSS_STA ]: deletion of IBSS station support
        * [ SCAN_FREQ_KHZ ]: scan on kHz frequency support
        * [ CONTROL_PORT_OVER_NL80211_TX_STATUS ]: tx status for nl80211 control port support
 

 

- If you see following in logs, it means source address is 00:00:00:00:00:00 in MGMT frame which gets rejected by Linux kernel. See details.

nl80211: Frame command failed: ret=-22 (Invalid argument)  

 

- To temporarily change WPA config, use set command.

wpa_cli set p2p_no_group_iface 1

 

- Sample WFD server

Use this test server in python for either studying the flow or running your own server. wfd.in contains the sequence of requests from client. (Frame rate and display resolution are hard-coded in line 555).

$ python3 wfd.py

https://gitlab.com/atamariya/hostap/-/blob/tools/wpaspy/wfd.py

https://gitlab.com/atamariya/hostap/-/blob/tools/wpaspy/wfd.in


References

- Miracast specifications

- Wifi P2P mode support for hardware in Linux

- Wifi P2P documentation for TI WiLink8

- gnome-network-disply flatpak

 

 

Comments

Popular posts from this blog

GNU Emacs as a Comic Book Reader

Mozilla Readability in GNU Emacs

Data Visualization with GNU Emacs