Tinkster Logo
advancedLinux & Homelab

Creating a TV Server at Home on Linux

Author
Savva
Tashkent, UZ
1 day
--
1
Cover
Friends, today we'll talk about how to create your own TV server for subsequently distributing video streams to any devices on your home network, as well as beyond it to Android devices.
The idea to create my own TV server came about because my Sony TV lacks a TV tuner for the standard DVB-T2, and only has DVB-T. I could have bought a regular TV tuner for 1000 rubles and connected it to the TV, but then I would only have digital TV on one device, so I decided to go a bit further and expand this functionality. I had to buy 2 TV tuners, because over-the-air digital TV in Russia consists of two multiplexes, and to watch TV on all devices simultaneously, you need a separate tuner for each multiplex, since the frequency of the multiplexes is different — for the first one it is 474MHz, and for the second one 658MHz.

What you'll need

Materials

Steps

1

Setting up CentOS 7 OS

[object Object]
We will install CentOS 7 from a USB flash drive. For this, we need to download the CentOS 7 distribution without a graphical user interface (GUI):
Next, download the Rufus program for Windows:
Launch Rufus on a separate Windows machine and specify the operating system ISO file:
Click the START button and create a bootable USB flash drive.
Next, enter the BIOS of the server's motherboard (usually by pressing the F2 or F10 keys during boot) and set the USB flash drive as the disk from which the system should boot. Save the settings and reboot the server. Follow the installer's instructions and install CentOS 7. Reboot the system and log into CentOS using the username and password set during installation.
2

Compiling the latest kernel for CentOS 7

Compiling a fresh kernel is necessary so that it ultimately 'sees' the TV tuner driver files in the folder /lib/firmware/. The kernel that comes by default with CentOS 7 distributions for some reason does not 'see' the TV tuner drivers. In my case, I installed kernel version 5.1.15. Download the kernel source code. Go to the terminal and type:
Unpack the archive:
1
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.15.tar.xz
Next, download the newest compiler from the devtoolset-8 set and development tools for kernel compilation:
1
tar -xvf linux-5.1.15.tar.xz
Activate the compiler for the current terminal session:
1
sudo yum update
2
sudo yum groupinstall 'Development Tools'
3
sudo yum install centos-release-scl
4
sudo yum install devtoolset-8
5
sudo yum install ncurses-devel make gcc bc bison flex elfutils-libelf-devel openssl-devel grub2
Check the compiler version:
1
sudo scl enable devtoolset-8 bash
Before starting the kernel build, you need to configure the Linux kernel configuration. You should also specify which kernel modules (drivers) are needed for your system. I suggest you copy the existing configuration file using the cp command:
1
gcc --version
1
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
2
Copyright (C) 2018 Free Software Foundation, Inc.
3
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Start compiling the new kernel:
1
cd linux-5.1.15
2
cp -v /boot/config-$(uname -r) .config
Guys, compiling and building the Linux kernel takes a significant amount of time. The build time depends on your system's resources, such as the number of processor cores and the current system load. So pour some coffee and have a bun :)
1
make -j $(nproc)
Install the Linux kernel modules:
And finally, install the Linux kernel itself:
1
sudo make modules_install
This command will install three files in the /boot folder and also make changes to your grub configuration file:
1
sudo make install
You also need to change the Grub 2 bootloader settings. Enter the following command in the command line according to your Linux distribution:
1
initramfs-5.1.15.img
2
System.map-5.1.15
3
vmlinuz-5.1.15
Reboot the server:
1
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
2
sudo grubby --set-default /boot/vmlinuz-5.1.15
Check the current kernel version with the command:
1
sudo reboot
Activate the compiler for the current terminal session again:
1
uname -mrs
1
Linux 5.1.15 x86_64
Enable the compiler for the current terminal session again:
1
sudo scl enable devtoolset-8 bash
3

Installing V4L-DVB drivers for TV tuners

Installing V4L-DVB drivers for TV tuners
Insert TV tuners into USB ports on the server and connect them to the external outdoor antenna via a TV splitter.
Then enter in the terminal:
1
git clone --depth=1 git://linuxtv.org/media_build.git
2
cd media_build
3
sudo ./build
Reboot the server.
1
sudo reboot
4

Setting up the TV server TVHeadend

The easiest way to install a TV server for CentOS 7 is through Snap.
In the terminal, enter:
1
sudo yum install epel-release
2
sudo yum install snapd
3
sudo systemctl enable --now snapd.socket
4
sudo ln -s /var/lib/snapd/snap /snap
5
sudo snap install tvheadend
Next, open ports 9981, 80, and 443 on the server:
1
sudo firewall-cmd --add-port=9981/tcp --permanent --zone=public
2
sudo firewall-cmd --add-port=80/tcp --permanent --zone=public
3
sudo firewall-cmd --add-port=443/tcp --permanent --zone=public
4
sudo firewall-cmd —reload
After installation, open a browser on another computer connected to the local network and enter the address:
http://[IP address]:9981
where [IP address] is the local IP address of your CentOS server.
Follow the instructions of the Assistant, but I want to highlight the main points:
In the field Language: select either Russian or English
Allowed network: enter 0.0.0.0/0
Admin username: create a username in English
Admin password: create a password in English
Tuner: select DVB-T
Predefined-muxes: select DTT — Russian Federation
After all manipulations, the multiplex scanning process will begin.
Next, check the boxes for:
Map all services
Create provider tags
Create network tags
The simplest way to check the configured channels is to do it through the VLC player. Install it on another Linux machine with Ubuntu via snap (it is also available for Windows):
1
sudo snap install vlc
Next, in the home folder of this Ubuntu machine, create a file TV.m3u:
1
nano ~/TV.m3u
and enter just one line in it:
1
http://[IP-address]:9981/playlist/channels
Where [IP address] is the local IP address of your CentOS server.
Save the file.
Open it in VLC by double-clicking. Next, VLC will ask you to enter a username and password. Enter the Admin username and Admin password that you created earlier.
On the TV, I connected to the TV server through a KODI set-top box and the TVHeadend client application. Everything there is extremely simple.
5

Watching TV on an Android smartphone outside the home network

Screenshot from my smartphone
To enable viewing TV on an Android smartphone outside the home network, you need to:
1. Install VLC application on the smartphone.
2. Have a fixed IP address for your home router and set up port forwarding for ports 80 and 443 to the server.
3. Have a domain name linked to your router's IP address.
4. Install the nginx web server.
1
sudo yum install nginx
Open the file and insert the following lines into it:
1
sudo nano /etc/nginx/nginx.conf
And add the following lines to it:
1
user nginx;
2
worker_processes auto;
3
4
error_log /var/log/nginx/error.log;
5
pid /run/nginx.pid;
6
7
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
8
include /usr/share/nginx/modules/*.conf;
9
10
events {
11
worker_connections 1024;
12
}
13
14
http {
15
client_max_body_size 256M;
16
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
17
'$status $body_bytes_sent "$http_referer" '
18
'"$http_user_agent" "$http_x_forwarded_for"';
19
server_names_hash_bucket_size 64;
20
access_log /var/log/nginx/access.log main;
21
22
sendfile on;
23
tcp_nopush on;
24
tcp_nodelay on;
25
keepalive_timeout 65;
26
types_hash_max_size 2048;
27
28
include /etc/nginx/mime.types;
29
default_type application/octet-stream;
30
31
# Load modular configuration files from the /etc/nginx/conf.d directory.
32
# See http://nginx.org/en/docs/ngx_core_module.html#include
33
# for more information.
34
include /etc/nginx/conf.d/*.conf;
35
server_tokens off;
36
37
server {
38
listen 80 default_server;
39
listen [::]:80 default_server;
40
server_name _;
41
root /usr/share/nginx/html;
42
43
# Load configuration files for the default server block.
44
include /etc/nginx/default.d/*.conf;
45
46
location / {
47
index index.php index.html index.htm;
48
}
49
50
error_page 404 /404.html;
51
location = /40x.html {
52
}
53
54
error_page 500 502 503 504 /50x.html;
55
location = /50x.html {
56
}
57
}
58
}
In the /etc/nginx/conf.d/ folder, create a file named tvheadend.conf:
1
sudo nano /etc/nginx/conf.d/tvheadend.conf
Add these lines to the file:
1
server {
2
listen 80;
3
server_name domain.ru;
4
set $root_path /var/www/domain.ru/html;
5
root $root_path;
6
}
domain.ru - will be your own domain name.
Next, create a folder for the server:
1
mkdir /var/www/domain.ru/html/
2
mkdir /var/www/domain.ru/log/
3
sudo chown nginx:nginx /var/www/domain.ru/html/*
4
sudo chown nginx:nginx /var/www/domain.ru/log/*
Start nginx and add it to autostart:
1
sudo systemctl enable nginx
2
sudo systemctl start nginx
5. Install an SSL certificate for Nginx using Let’s Encrypt:
Guys, you don't want someone to intercept your TV server passwords, say, when you connect to some public wireless internet, and then without your permission some cool hacker connects to you, do you? I think not :)
Install Let’s Encrypt and obtain the secret and public key:
1
sudo yum install certbot python2-certbot-nginx
2
sudo certbot --nginx
In the terminal, follow the assistant's instructions.
6. Open the tvheadend.conf file again:
1
sudo nano /etc/nginx/conf.d/tvheadend.conf
erase everything and insert the following content:
1
server {
2
listen 80;
3
server_name domain.ru;
4
return 301 https://domain.ru$request_uri;
5
}
6
7
server {
8
listen 443 ssl;
9
server_name domain.ru;
10
set $root_path /var/www/domain.ru/html;
11
12
ssl_certificate /etc/letsencrypt/live/domain.ru/fullchain.pem;
13
ssl_certificate_key /etc/letsencrypt/live/domain.ru/privkey.pem;
14
15
access_log /var/www/domain.ru/log/nginx-ssl-requests.log;
16
error_log /var/www/domain.ru/log/nginx-ssl-error.log;
17
18
root $root_path;
19
20
location /tvh/ {
21
proxy_pass http://127.0.0.1:9981/;
22
proxy_redirect off;
23
proxy_set_header Host $host;
24
proxy_set_header X-Forwarded-Proto $scheme;
25
proxy_set_header X-Real-IP $remote_addr;
26
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
27
}
28
29
location /stream/ {
30
proxy_pass http://127.0.0.1:9981/stream/;
31
proxy_redirect off;
32
proxy_set_header Host $host;
33
proxy_set_header X-Forwarded-Proto $scheme;
34
proxy_set_header X-Real-IP $remote_addr;
35
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36
}
37
38
}
Reload nginx:
1
sudo systemctl restart nginx
7. On the smartphone, in the folder /storage/emulated/0/Download/ create a file named TV.m3u using the application ES File Explorer and paste this line:
1
https://domain.ru/tvh/playlist/channels
Save the file and launch it using VLC. The program will also request a login and password. Enter them and watch your home television from anywhere in the world.

Discussion (0)

No comments yet. Be the first!

Maker

Avatar
Savva
Tashkent, UZ

Anton is the Managing Partner of Tinkster. He studied oil and gas engineering in the United States and also holds two honors degrees from Tomsk Polytechnic University.