Building a home media streaming server. Part 1: The media server

This will be a multi part blogging of my journy to build a Home Theatre PC (HTPC), becasue so many things was learned to put all in a single post.

The context

The TV

After a very long long long time of ear whispering and diner table talking, I finally persuade mom to get rid of the old junk CRT TV to make space for my new 4K TV.  Even though I had long saved up enough for that TV, I simply couldn’t find a place to put it because the house is cramping and Mom was reluctant to part way with the good old  and sentimentally valuable furniture.

So I quickly dash to the electronic store and bring back the Panasonic TH-43EX605V 

The TV look good but the room in the advertisement look even better

It was the cheapest, none Chinese brand, 4K TV with HDR support in the store, nothing fancy but to the one who never use big screen before, it was HUGE!!!

The TV in action

The plan

So the challenge is finding content to be watched on this TV. My cable company provide TV services but they are capped at FullHD 1080p. The majority of my 4K content come from private torrent tracker where  I can leech a bunch of movies or TV-series RIP in 4K x265 format:

some movie in my favorite tracker

Even though my TV promote itself as “smart”-TV, I pretty much don’t care about those features since it has a slow as hell processor and completely helpless against file with x265 codec. My long term goal is to build a HTPC and connect it to the TV  since I has got tons of experience using PC anyway, it would be a less step learning curve than having to deal with the TV’s peky apps. The plan is to upgrade my current PC then scavenge old parts to build the HTPC. But with the meltdown and spectre haunting all CPU giants, it’s best to delay upgrading the PC until new hardware with no vulnerable come out.

And so, I decided to use my laptop to double as the HTPC and my current PC will double as the Home media server (HMS). And this blog post will document what change need to be made for those to computer to serve as their double role.

The server

The easy start

First of all, since I’m pretty much the sole user of this HTPC-HMS setup, there are a lots of use-case that I won’t have to wory about. First of, I don’t have to get all of my movie archive online at all time. There’s no need to setup NAS or huge storage server, I can keep the film I already watched archive and stashed away in backup hard disk, only hot-swap them in the server when I’m really in the mood. So it’s pretty much “just install openssh” and the server is done.

On the server, install openssh and start it

# pacman -S openssh 
# systemctl start sshd

Then on the client (my laptop) just open Nautilus and connect:

In just type the ip address, preceded by sftp:// and then enter your server’s account username and password then you’re all set. In fact I love file sharing on linux so much more than it is on windows. No twiddling with “Network and sharing settings” and stuff. Then same protocol that’s used to control computer can be used for file sharing with traffic encryption and all. After you connect, it’s only the matter of drag and drop the mkv file into your favourite media player on the laptop.

However, I soon ran into trouble when I download a movie that was encode with so high bitrate my laptop can’t open :O

The Problem.

My laptop is the first-gen Dell XPS13 (9343 model) with only a Broadwell CPU (i5-5200U), and only the next gen with Skylake CPU would support decoding HEVC (x265) 10bit profile. 

The 3:10 to Yuma with it’s 14204 kbps bitrate would drive my laptop CPU to 99% usage and the fan was screeching like crazy.

So, Dunkirk with its  24258 kbps is simply too much for my laptop CPU to handle. Frame was dropping left and right and audio was going a full second and a half before video can catch up. Totally unwatchable.

Now my PC doesn’t fair much better, neither the i5-3550 nor the Geforce GTX750 have native x265 decoding capability but at least my PC CPU can open that file with 60% CPU usage.  So now I’m left with a few choice while saving up for new hardware:

  1. Buy a 15 meters HDMI cable to connect the TV to PC.  This would ensure best performance but that’s almost the maximum length on HDMI 1.2 cable and it won’t come cheap. Moreover, zigzagging 15m of hard cable around the house is no fun job! Where’s the “coolness” in this method? It quickly passed up.
  2. Decode the file on my PC and stream the decoded video over wifi to the laptop. Now that solution would stretch my tech exprience to it’s limit and one cannot expect a more cool solution. I got a chance to face-off with my old enemy “the task of setting up media streaming server”.

And it’s time to wrap up part 1. Part 2 will document my expermiment with various streaming solution.

 

[Vestacp] Increase database username length limit

For so long mysql has a harcoded 16 characters length limit on username. And vestacp enforce this limitation, every database username have to be less than 17 characters long.

The problem is mysql has increase this length limit to 32 charcaters and mariadb even remove this limit completely but vestacp hadn’t lift their limit yet for the sake of compatibility. So, until this issue closed: https://github.com/serghey-rodin/vesta/issues/860, if you have mysql 5.7+ and want to make really long username, you have to change vesatacp’s code.

Open file /usr/local/vesta/func/main.sh

Find those line

 if [ 17 -le ${#1} ]; then
 check_result $E_INVALID "mysql username can be up to 16 characters long"
 fi

Now change the number 17 to 33 and you essentially double the username length limit.

Glass castle film review.

I seldom write film review, even though I pirated and watched lots of film, mostly because I am lazy, but more often than not I only talk about film than I can reflect upon my own life, like “Rain man” for example.

Recently I stumble upon Glass castle (http://www.imdb.com/title/tt2378507/)

The rating was mediocre but I love it because I can relate to the protagonist feeling about her alcoholic father. If an alcoholic can manage to get a family, you can be sure that he/she is very charming when given enough liquor to function. Really, an unattractive alcoholic can only end up in a grave or a prison cell alone, no one would come near them. A charming and family alcoholic man on the other hand, is a roller coaster ride of emotion. He can give you hope that never materialize. He can give you joy that immediately follow by sadness. He can make you feel safe, only to terrifies you himself moment later. And he is never never reliable.

The film was based on a memoir of a famous writer, so it’s good source material. And the acting was superb. Man! If it was up to me I would award an Oscar for both leading actor and actress. Even though her screen time was limited, Brie Larson did a good job in the film. She has already kick Jennifer Lawrence out of my heart, and only rival with Emma Watson. I will be sure to follow her films in the future. The lead actor has been twice nominated for Oscar but never won. He did a good job in this film, I hope it can be his time to shine.

And finally I’m always bias toward film that I can relate to my own life but this film was really a good watch.

Using Code Igniter pagination class with bootstrap 4.0-beta2

Code igniter have a helper class to generate pagination for a page. This class is fairly primitve but highly customizable, and since I’m lazy mood I gooogle around finding some ways to customize it to work with bootstrap 4.0-beta2. So at first I came across this snippet: https://github.com/zulacom/ci-paging-bootstrap-4/blob/master/pagination.php but this snippet do well in mimicking the look but was annoying to use.
The problem is: Codeiginiter pagination class allow you to wrap the link ( <a> tag) between some others custom tags. Bootstrap however require you add class .page-link in those tag directly. The snippet above solve this by wrapping tag around a span.page-link tag, produce something like this:


This look good but you cannot click on the <span> square, you have to click on the number itself to change page. And this is quite annoying on touch interface. And so I set out to create my own snippet. And the first thing in order  is to get rid of all those <span> wrapping class. Then we can add all those class with a simple javascript line:

<script type="text/javascript">$("nav > ul.pagination a").addClass("page-link");</script>

The full snippet can be found here: https://gist.github.com/truongan/b302641169e98f8a88b62a9f7d3582ee

This is definitely not some complex hack. But since the problem is so subtle that it slips pass my notice for months, I thougth it would worth the effort to take note in case I ran into this problem sometimes later.

 

Something I learnt about openssl

I’ve been starting to use vestacp to manage my server. Mostly because it’s open source and simple design. But recently it’s SSL feature just… fail. It reject every certificate I throw at it, either from Let’s Encrypt or self sign. So after many lazy-ish hesitant, I decided to dig in.
Turn out that vesta use openssl extensively to check and verify certificate before enable them. In the file func/domain.sh it has function is_web_cert_valid with this snippet of code:
openssl s_server -quiet -cert $ssl_dir/$domain.crt \
-key $ssl_dir/$domain.key >> /dev/null 2>&1 &
pid=$!
sleep 0.5
disown &> /dev/null
kill $pid &> /dev/null
check_result $? "ssl certificate key pair is not valid" $E_INVALID

 

This start a dry run ssl server using openssl feature. And if this dry run result in any abnormal return code, the certificate got reject. But openssl by defaults start its testing server using port 4433, this is also the default port for ssl torrent use by deluge with libtorrent < 1.1. Since I got deluge autostart, openssl can never got hold of port 4433 and quit. And thus vesta reject all of my generated certificate.

Now I find no way to disable port 4433 in deluge. That feature seem to have been assigned to deluge 1.4 milestone, some said of install libtorrent 1.1 (which turn off ssl torrents by default), but that’s too much of a hassle. Fortunately, openssl can change its port, so I edit line ~340 in file funct/domain.sh and add one argument to the openssl command:-accept 44333

From

openssl s_server -quiet -cert $ssl_dir/$domain.crt \
-key $ssl_dir/$domain.key >> /dev/null 2>&1 &

To:

openssl s_server -quiet -accept 44333 -cert $ssl_dir/$domain.crt \
-key $ssl_dir/$domain.key >> /dev/null 2>&1 &

And that seem to do the trick.