hydra3333
Posts: 104
Joined: Thu Jan 10, 2013 11:48 pm

Pi 2 as a plain-jane home chromecast media server

Sat Feb 14, 2015 1:48 pm

Hello. I needed a local media server to serve up my library of home mp4s to my home Chromecast(tm) devices connected to my TVs.

I found that my new Pi 2 works fine as a "headless Chromecast(tm) mp4 web media server".
The only other thing I needed was a Tablet/Phone with
(i) google ChromeCast app installed from play store
(ii) google Chrome browser for Android beta, to browse the server and "cast" a URL to my Chromecast(tm) device ... the Chromecast(tm) would use the casted url to open the .specified mp4 directly from the Pi 2 web server.

No Plex, no fancy app needed on the Tablet/Phone, no fancy media server software like Plex or Bubble needed on the Pi 2 (provided your library of .mp4s are well formatted in the first place).

Anyway, here's the process I followed. Constructive feedback and suggestions welcomed.

Code: Select all

# How to setup a Raspberry Pi 2 as a "headless Chromecast(tm) mp4 web media server".
# as at 2015.02.14
#
# Now that Chrome(tm) Browser for Android (at least beta 41) is "reliable",
# Chrome(tm) for Android also does casting of HTML5 online videos from any website.
# So, we can now cast .mp4 videos directly from a web server to TV from the Chrome(tm) browser.
# 
# Imagine a Raspberry Pi 2
# - with a suitably configured web server allowing an android web browser to "walk" folder trees of .mp4 files
# - with samba file shares and an ftp server allowing your home PC to save .mp4s to the Pi 2
#
# Then with tablet or phone client and the Chrome(tm) Browser for Android, we can 
# - connect to the Pi 2 and browser the folder trees
# - click on a .mp4 file to load it to play inside the Chrome(tm) Browser as a html5 video
# - use the new "native" Chromecast(tm) icon (no coding necessary) cast the video to your chromecast device
#   the .mp4 will then "sucked" directly from the Pi 2 web server by the chromecast device
#   and the html5 will give you pause/play functions to control playback (no coding necessary)
# There are some limitations (which I can live with in the interest of simplicity and no coding)
# - only one .mp4 playback at a time, no queueing
# - if you close the controlling tab on your tablet/phone device, you have no way to control the video
#
# Does it work ?
# Yes, I've played and controlled a full movie no problems. 
# (the movie was a well-formatted mp4, h264/mp3 progressive no cabac and the moov atom at start of the mp4; 
#  it was pre-converted on a PC for that purpose)
#
# Needs:
# 1. Ideally, a Raspberry Pi 2 (it works with a model B+)
# 2. the latest Raspbian operating system, with a well-configured Apache2 web server on it
# 3. an external USB Hard Drive of sufficient capacity to hold a lots fo .mp4 files (eg a 4Tb)
# 4. a wired ethernet internet connection to your wireless router for the Pi 2 (for speed; wireless may do)
# 5. an Android Tablet or Phone, with the latest beta Chrome(tm) Browser installed from the play store
# 6. patience and persistence
# 7. well-formed .mp4 files for your video library - this is critical (see below) 
#   (... I use a Win7PC/ffmpeg to transcode video files into the correct format first)
#
# An fyi - Beginner's recap on how casting to a ChromeCast(tm) device actually works:
# 1. You already have
#    * a controlling Android Tablet or Phone connected to your local LAN
#    * a ChromeCast(tm) device plugged into your TV and already set up and working (test it with youtube)
#    * a Pi 2 web server with a USB disk containing your library of "well-formed" .mp4 files, on your local LAN
#    * IN A NUTSHELL:
#        - the Pi 2 contains a library of .mp4 files in a folder tree and a webserver to serve them up
#        - the android Tablet or Phone with Chrome Browser finds and "casts" the URL of one of those .mp4 files to the ChromeCast(tm) device
#        - the ChromeCast(tm) device then connects to the Pi 2 web server and starts playing the specified .mp4 file directly from the Pi 2
# 2. the android Tablet or Phone is the "Sender"
#    * this does NOT repeat NOT stream any video to the ChromeCast(tm) device (to be clear: it doesn't stream the video)
#    * it just sends command and control information to the ChromeCast(tm) device
#    * it hands over the URL of an .mp4 video to the ChromeCast(tm) device, 
#      and ChromeCast(tm) device connects and sucks it using the source url supplied to it
#    * on Android, this method simply uses Chrome(tm) Browser version beta 41 or higher 
#        - we rely on the "native" chromecasting features of this browser on Android 
#        - if it asks, say NO to "lower bandwidth testing", it may upload your video to google and then download it again
#    * thus:
#        - choose the URL a .mp4 file and play it in a new tab of the android Chrome(tm) browser
#        - the video appears in a native Chrome(tm) HTML5 media player inside the browser; pause the video if necessary
#        - choose the native "cast" button to "cast" it
#        - tap the native Chromecast(tm) button and choose which of your ChromeCast(tm) devices to cast the URL of the mp4 file to
#        - control the video playback using the Chrome(tm) HTML5 media player controls
#        - do NOT close the browser tab, or you will lose control of the playback 
#        - if you do lose control of the playback, you can use the android "ChromeCast(tm) App" 
#          to connect to the ChromeCast(tm) device and disconnect playback
# 3. the ChromeCast(tm) device plugged into your TV is the "Receiver"
#    * it receives command and control info from the android Tablet or Phone including a "video source url"
#    * the command and control info is formatted in special ways and is not for meant beginners or the faint of heart
#    * the android Tablet or Phone hands it the URL of an .mp4 file, and the ChromeCast(tm) device connects directly to that URL
#    * it only plays "well-formed" video; .mp4 is one main formats
#        - for .mp4, the "moov" atom MUST positioned at the beginnng inside of the .mp4 video container file
#        - it can be fussy about the mp4 file, eg "profile" and which audio coded it was created with (it can take .mp3)
#        - supported video/audio formats inside your .mp4 file: https://developers.google.com/cast/docs/media 
#    * a URL, handed over by the android Tablet or Phone, can be a link to a well-formed .mp4 file on our Raspberry Pi 2 web server
# 4. the Raspberry Pi is a "Media Server" running a well-configured Web server and file system
#    * like the ChromeCast(tm) device, it is configured as "open" to your home LAN with no security
#    * the ChromeCast(tm) device simply requests the .mp4 video from thr Pi web server using the source video url passed to it by the sender
#    * the Pi web server "streams" the .mp4 file to the ChromeCast(tm) device
#
# Caveats and Notes: eg for creating your own .mp4 files
#
# 1. for .mp4 files on ChromeCast(tm) devices, the "moov" atom MUST positioned at the *beginnng* inside of the .mp4 video container file 
#    * this means that the ChromeCast(tm) device can download theings like video size, etc, immediately
#        - otherwise it has to download the end of the .mp4 file first, and that will cause delays
#    * I use a Win7PC/ffmpeg to transcode video files into the correct format inside the .mp4 first (also cross-compatible with WVTV media players)
#        - and then I copy the well-formed video files into my mp4library on the Pi
#        - which is why I have setup SAMBA on the Pi 2 so I can use native Win7 file shares to copy files around
#        - and why I have an NTFS formatted USB3 drive, so I can plug it into the Win7 PC to copy Tbytes of data QUICKLY from time to time
#        - I download the latest STATIC 32bit or 64bit ffmpeg in .7z format from http://ffmpeg.zeranoe.com/builds/ and unzip it into "C:\ffmpeg"
#        - and then choose from example Win7 (dos) ffmpeg transcoding commandlines (well, I use .bat files to process all files in a folder):
#          EXAMPLES:
#          1. to copy video and audio and place the moov atom at the start
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v copy -c:a copy -movflags +faststart -y "output_file.mp4"
#          2. to transcode the audio to mp3 and place the moov atom at the start
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v copy -c:a libmp3lame -ar 44100 -ab 384k -movflags +faststart -y "output_file.mp3.mp4"
#          3. four versions of transcode progressive video to h264 and audio to mp3 and place the moov atom at the start (different quality encodes)
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v h264 -preset veryfast -profile:v high -level 4.1 -crf 22 -coder 0 -c:a libmp3lame -ar 44100 -ab 256k -movflags +faststart -y "output_file.mp3.mp4"
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v h264 -preset fast     -profile:v high -level 4.1 -crf 20 -coder 0 -c:a libmp3lame -ar 44100 -ab 256k -movflags +faststart -y "output_file.mp3.mp4"
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v h264 -preset medium   -profile:v high -level 4.1 -crf 18 -coder 0 -c:a libmp3lame -ar 44100 -ab 384k -movflags +faststart -y "output_file.mp3.mp4"
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mp4" -threads 0 -c:v h264 -preset slow     -profile:v high -level 4.1 -crf 16 -coder 0 -c:a libmp3lame -ar 44100 -ab 384k -movflags +faststart -y "output_file.mp3.mp4"
#          4. input interlaced (eg from TV captures) output still interlaced files require different treatment. Say you have an interlaced top-field-first mpeg2 .mpg file, then:
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mpg" -threads 0 -c:v h264 -flags +ildct+ilme -ildctcmp satd -top 1 -preset fast -profile:v high -level 4.1 -crf 20 -coder 0 -c:a libmp3lame -ar 44100 -ab 256k -movflags +faststart -y "output_interlaced_file.mp3.mp4"
#          5. input interlaced (eg **PAL only** TV captures) output deinterlaced require different treatment. Say you have an interlaced top-field-first PAL mpeg2 .mpg file, then:
# "C:\ffmpeg\bin\ffmpeg.exe" -i "input_file.mpg" -threads 0 -c:v h264 -filter:v yadif=1:-1:0,setdar=16:9 -r 50 -preset fast -profile:v high -level 4.1 -crf 20 -coder 0 -c:a libmp3lame -ar 44100 -ab 256k -movflags +faststart -y "output_interlaced_file.mp3.mp4"
#
#
# The comments below assume the Pi 2 and LAN config is:
#    Pi server name: RP03
#    Pi server IP  : 10.0.0.10  (fixed IP using a DHCP router reservation, in my case 10.0.0.10)
# 
#------------------------------ note for Win7 PC users --------------------------------
# Text files (.txt) aren't compatible between linux and Windows7
# So... after copying a Win7 .txt file to the Pi, 
# To convert a copied file from Win7 MS-DOS format into unix format
# which is understood by linux and editors such as nano,
# To get rid of MSDOS line formatting, do this to this file: 
#       sudo sed -i s/\\r//g /path/filename
# Or, if you are editing a .php or a .html file from a Win7 PC 
# via a file-share on the Pi, use Notepad++ on the Win7 PC. 
#------------------------------ note for Win7 PC users --------------------------------
#
# Pre-build actions
# -----------------
# 1. prepare the external USB drive (in my case a 4Tb external USB3 drive)
#
# On a Windows 7 PC, format the external 4Tb USB drive as NTFS
# Create a top-level folder called "mp4library" 
# In Windows Explorer, right-click on the drive letter and open "properties"
# click on the "Security" tab, then click the "Edit" button
# click the "Add" button, type in the word Everyone and click OK
# in the top "Group or user names" box, 
# click on Everyone, then tick the "Full control" tickbox
# one at a time click on each of the other items and click "Remove"
# until only "Everyone" is left.  Click Apply then click OK.
# We now have a fully unsecured USB3 drive ready for the Pi.
#
# Make subfolders under folder "mp4library" and copy  .mp4 files in there
# For example a folder tree:-
# mp4library
#          |-- classicmovies
#          |-- documentaries
#          |-- homemovies
#          |-- movies
#          |-- musicclips
#
# Finally, "Eject" the USB3 drive in the normal safe way, to ensure all chanegs are saved.
#
# A. Install and configure Raspbian in the usual way
# --------------------------------------------------
# Configure your defaults during initial setup, eg locale, timezone, etc
# - choose at least 32Mb of memory, if this is to be a headless Pi 2
# If you need to re-run the setup, login and use 
sudo raspi-config 

# now ensure thr Pi is fully up to date
sudo apt-get install -y rpi-update
sudo apt-get update -y 
sudo apt-get upgrade -y
# wait up to 30 minutes for it to finish
sudo rpi-update
# wait up to 10 minutes for it to finish
# reboot for it to work
sudo reboot

# Check the Pi's MAC/IP addresses and make a DHCP reservation for the Pi on your Home Router 
# so that the Pi always gets the same IP address on your Home LAN every time it powers on
# After logging ito th Pi, use command
ifconfig
# and look for the lines containing something like 
# "HWaddr b8:27:eb:39:ff:ee"  <-- this is your MAC address
# "inet addr:10.0.0.10"       <-- this is your current DHCP IP address
#
# Now go to your home router and make a DHCP reservation for our MAC address / IP address
#

# B. Configure the Pi by hand
# ---------------------------
#
# If you didn't already within raspi-config, change the password
passwd pi
#
# Change boot configuration settings
# note: this contains the boot options /boot/config.txt 
# initial boot, cpu, video setup
#
sudo nano /boot/config.txt
# with settings like these - modify by uncommenting or adding:
disable_overscan=1
hdmi_forcehotplug=1
config_hdmi_boost=4
hdmi_force_edid_audio=1
#control O
#control X

# http://www.raspberrypi.org/forums/viewtopic.php?p=692333&sid=99a91cce620fe3416da27d0a149d0deb#p692333
# fix for fstab not mounting USB hard drive -
# Seems adding "rootwait=5" to end of cmdline.txt is our best solution to this. 
# (Increase to 10 if that doesn't work).
sudo nano /boot/cmdline.txt
# add 
rootdelay=5
# to the end of the line (rootwait was already there)
#control O
#control X

# to add any coded licenses we bought
#To find your serial number, type cat /proc/cpuinfo at the command line as shown below:
cat /proc/cpuinfo
# -->> Serial          : 000000000000000d
#then order the mpeg2 and vc1 licenses from raspberry pi foundation
# then add then to the config
sudo nano /boot/config.txt
# add: these (decode_ only relevant if you bought the cheap Pi decider licenses ... recommended)
# note: the texts after the "=0x" are your own purchased license keys you bought from Pi Foundation
# Pi2 (2 of them Feb 2015) - change a b c d to your license codes
decode_MPG2=0xaaaaaaaa,bbbbbbbbbb
decode_WVC1=0xcccccccc,dddddddddd
#control O
#control X
#
# After a reboot yuou can verify that the video codecs are now enabled, 
# the following commands will report their status:
#
vcgencmd codec_enabled MPG2
vcgencmd codec_enabled WVC1

#--- get fat32 drivers (may already be installed)
sudo apt-get install -y dosfstools

#--- get ntfs drivers for the external drive 
# (I use an NTFS USB drive since I can plug it into a Windows PC if I want, no hassle)
sudo apt-get install -y ntfs-3g

#--- get Chrome browser - not needed but why not, may want it later
sudo apt-get install -y chromium

#--- get youtube support - not needed but why not, may want it later
sudo apt-get install -y youtube-dl

#
# C. Mount the external USB drive so it auto-mounts on boot
# ---------------------------------------------------------
#
# Plug in NTFS drive into the hub then create a location for mount point.
# note the 777, since I want it to be fully writable by anyone, on purpose
sudo mkdir /media/USB4TB
sudo chmod 777 /media/USB4TB

#Find the uid, gid for "pi" user and group  
# (it is usually 1000 ... remember this for other steps below)
sudo id

# Note the dislpay user Id for pi.
# I want an NTFS drive since I'll be moving it between Win7 and the Pi so that I can 
# copy lots of large Gb files to it fast (a lot faster than across the network to the Pi).
# Manually mount the NTFS drive ...
# Note: the umask=000 disables "no access" ... so the web server PHP can see all the folders 
#
sudo mount -t ntfs-3g -o auto,rw,uid=1000,gid=1000,umask=000 /dev/sda1 /media/USB4TB

#Note: possible values
#ntfs-3g for NTFS Drives (makes the drive easily portable to plug into Windows PCs)
#vfat for FAT32 Drives
#ext4 for ext4 Drives
#
# Find the id of the newly mounted drive 
df -h
ls -l /dev/disk/by-uuid/
# locate the long string id and make a careful note of it, eg 9AF2BF43F2BF2285
#
# Put a line in the fstab file so it automounts next time
# Configure the Pi to do this after every reboot
# Take a backup of current fstab and then edit fstab:
sudo cp /etc/fstab /etc/fstab.backup
sudo nano /etc/fstab
# Add the mount information in the fstab file (replace UUID with your own)
# Note: the umask=000 disables no access ... so the web server PHP can see all the folders OK
# for me, a 4TB external USB3 drive formatted NTFS, we will name "USB4TB"
UUID=9AF2BF43F2BF2285 /media/USB4TB ntfs-3g auto,rw,uid=1000,gid=1000,umask=000 0 0
#control O
#control X

# reboot to see it automount. 
sudo reboot

# After booting we can now access the external USB drive via the mount point
df -h
ls -al /media/USB4TB

# I still need to find a working method to spin-down IDLE USB drives on the Pi.
# Since my Seagate stays spinning all the time even though it's used less than 5% of the time.
# Grr, This doesn't work for me - 
#setup power management so the USB drive powers own after 10 mins
#sudo apt-get install hdparm -y
#sudo hdparm -Y /dev/sda1
#gives this message
#  issuing sleep command
#  HDIO_DRIVE_CMD(sleep) failed: Invalid argument
# I must eventually ask someone for a method that works.
#
#
# D. Install and configure Apache2 (v2.2.22 as at 2015.02.14)
# -----------------------------------------------------------
#
# Also refer http://www.raspberryconnect.com/raspbian-packages-list/item/86-raspbian-php
#
sudo apt-get install -y apache2 apache2-doc apache2-utils
sudo apt-get install -y libxml2 libxml2-dev libxml2-utils
sudo apt-get install -y libaprutil1 libaprutil1-dev

# add a user-group and users to own folders
sudo groupadd -f -g33 www-data
sudo usermod -G www-data root
sudo usermod -G www-data pi
# Instead of "/media/USB4TB/mp4library" below, use your own path to a folder 
# somewhere on your external USB hard drive
sudo chown pi:www-data /var/www
sudo chown pi:www-data /var/www/yaft
sudo chown pi:www-data /var/www/yaft/images
sudo chown pi:www-data /var/www/images
sudo chmod -c -R 777 /var/www
sudo chown pi:www-data /media/USB4TB/mp4library
sudo chmod -c -R 777 /media/USB4TB/mp4library
# note the 777 to make it all writable for samba based Win7 PCs

# Find the Pi's Ip address
ifconfig 

# and look for the lines containing something like 
# "inet addr:10.0.0.10"       <-- this looks like the current IP address
# Now you your Pi's web server should be working. 
# from another PC, use a browser to visit Raspberry Pi’s IP to check whether it is working.
#    eg in a chrome browser visit http://x.x.x.x/ (plug in it's IP address)
#
# If it doesn't work, look for errors by 
cat /var/log/apache2/error.log
# References
# http://wiki.apache.org/httpd/FrontPage
# http://httpd.apache.org/
# http://www.html5rocks.com/en/tutorials/video/basics/
# http://diveintohtml5.info/video.html
# http://www.w3schools.com/html/html5_video.asp
# 
# Notes on Configuring Apache2.22
# 
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.
# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
#       /etc/apache2/
#       |-- apache2.conf
#       |       `--  ports.conf
#       |-- mods-enabled
#       |       |-- *.load
#       |       `-- *.conf
#       |-- conf.d
#       |       `-- *
#       `-- sites-enabled
#               `-- *
#
# Note: the folder below contains sym links to the actual SITE config files to be used
# so see inside it
ls -al /etc/apache2/sites-enabled

# Note: the folder below contains the actual SITE config files which are available
# so see inside it
ls -al /etc/apache2/sites-available

# check the mp4 mime type is in place
ls -al sudo nano /etc/mime.types
sudo nano /etc/mime.types
# and check to see that a line exists with this "video/mp4   mp4"
# hint, use control W which is nano's "find text"
#control X

# Change the apache2 global configuration by editing /etc/apache2/apache2.conf
ls -al /etc/apache2/
sudo nano /etc/apache2/apache2.conf

# just underneath the line 
	#ServerRoot "/etc/apache2"
# add this line to say the web server name is going to be RP03
ServerName RP03
# where RP03 is to be the name of your web server

# And locate/change the following items to have new values
#
# change timeout from 5 mins to 2 hours
#Timeout 300
Timeout 7200
#
# change keepalives to infinite
#MaxKeepAliveRequests 100
MaxKeepAliveRequests 0
#
# change to 60 secs
#KeepAliveTimeout 5
KeepAliveTimeout 60
#then
#control O
#control X


# then, CHANGE the apache2 modules configuration 
ls -al /etc/apache2/mods-available
ls -al /etc/apache2/mods-enabled
# ENABLE some apache2 modules
sudo a2enmod headers
sudo a2enmod status
sudo a2enmod info
sudo a2enmod autoindex
# if you ever need to to DISABLE a module do something like "sudo a2dismod autoindex"
# restart the web server
sudo service apache2 restart

# The status module enabled above provides an overview of your server load and requests.
# Edit the configuration file in the "mods-available" directory with the following command:
sudo a2enmod status
sudo nano /etc/apache2/mods-available/status.conf
# Under the "Location /server-status" directive, 
# remove the "#" character from before the "192.0.2.0/24" 
# line and change it to the IP address of a the tablet on the LAN which you will be using to access the web server,
# eg
<Location /server-status>
	SetHandler server-status
	Order deny,allow
	Deny from all
	Allow from 127.0.0.1 ::1
# eg this line below
	Allow from 10.0.0.1/24
</Location>
#control O
#control X
sudo service apache2 restart
# Navigate to the server-status page (location) you have just defined 
# by typing the following into your web browser using your Pi's IP address
# http://xx.xx.xx.xx/server-status

#
# The info module enabled above provides an overview of your server.
# Edit the configuration file in the "mods-available" directory with the following command:
sudo a2enmod info
sudo nano /etc/apache2/mods-available/info.conf
# Under the "Location /server-info" directive, 
# Like above, remove the "#" character from before the "192.0.2.0/24" 
# line and change it to the IP address of a the tablet on the LAN which you will be using to access the web server,
# eg
<Location /server-info>
    SetHandler server-info
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1 ::1
# eg this line below
    Allow from 10.0.0.1/24
</Location>
#control O
#control X
sudo service apache2 restart
# Navigate to the server-info page (location) you have just defined 
# by typing the following into your web browser using your Pi's IP address
# http://10.0.0.10/server-status

#
# Enable apache autoindex setting because it's handy at times on a purely home web server
#
sudo a2enmod autoindex
cat nano /etc/apache2/mods-available/autoindex.conf

# Review the apache2 ports configuration 
# just to see what's in it, do a cat of the file
ls -al /etc/apache2
cat /etc/apache2/ports.conf

# CHANGE the apache2 DEFAULT SITE configuration. First find it.
ls -al /etc/apache2/sites-available
ls -al /etc/apache2/sites-enabled

# Note: config file
#     /etc/apache2/sites-enabled/000-default
# is a symlink to the actual config file named
#     /etc/apache2/sites-available/default
# The example below is for my virtualhost with 2 folders
# - the normal default www
# - the mp4library pointing to a folder on the external USB hard drive

# Edit the default configuration file and replace  everything in it
# with someting like the below
sudo nano /etc/apache2/sites-enabled/000-default
# and change it's contents to look like the virtualhost below
<VirtualHost *:80>
   ServerName RP03
   ServerAdmin [email protected]
   DocumentRoot /var/www/
   <Directory />
      Options Includes Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
   </Directory>
   <Directory /var/www/>
      Options Includes Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
   </Directory>

   Alias /mp4library /media/USB4TB/mp4library
   <Directory /media/USB4TB/mp4library>
      Options Includes Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
      #<ifModule mod_headers.c>
         Header set Access-Control-Allow-Origin "*"
         Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
      #</ifModule>
      #AuthType Basic
      #AuthName "Password Required"
      #AuthUserFile /usr/local/etc/apache_passwd
      #Require user johndoe
   </Directory>

   Alias /HomePics /media/USB4TB/HomePics
   <Directory /media/USB4TB/HomePics>
      Options Includes Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
      #<ifModule mod_headers.c>
         Header set Access-Control-Allow-Origin "*"
         Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
      #</ifModule>
      #AuthType Basic
      #AuthName "Password Required"
      #AuthUserFile /usr/local/etc/apache_passwd
      #Require user johndoe
   </Directory>

# sample where alias /preview also points to the mp4library
#      Alias /preview /media/USB4TB/mp4library
#      <LocationMatch /preview>
#         RewriteEngine On
#         RewriteRule ^(.+)\.mp4$ $1.mp4?start=400&end=500 [L]
#      </Location>
#
   # MSIE 6 and older cannot use keepalive
   BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
   # MSIE 7 and newer should be able to use keepalive
   BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
   <Directory "/usr/lib/cgi-bin">
      AllowOverride None
      Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
      Order allow,deny
      Allow from all
   </Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.
   LogLevel warn
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
#control O
#control X

# add an apache user, "Pi"
sudo htpasswd -c /usr/local/etc/apache_passwd Pi
# then enter your-intended-password twice

# rsetart the web server
sudo service apache2 restart

# check that web server Headers are returned by the Pi ie "Accept-Ranges: bytes"
curl --head 127.0.0.1
curl -I 127.0.0.1

# At this point the folder /var/www/ still contains an index file something like index.html
# remove it so that we can do directory browsing from the root (it is handy)
sudo rm /var/www/Index.*
sudo rm /var/www/index.*

# from another PC, use a browser to visit Raspberry Pi’s IP to check whether it is working.
#    eg in a chrome browser visit http://x.x.x.x/ (plug in it's IP address)
#    eg for me it is http://10.0.0.10/ 
#
# If it doesn't work, look for errors by 
cat /var/log/apache2/error.log

#
# D. Install and configure PHP5 (v5.4.4 as at 2014.09.21)
# -------------------------------------------------------
#
sudo apt-get install -y smbclient curl libcurl3
sudo apt-get install -y php5 libapache2-mod-php5 php5-common 
sudo apt-get install -y php5-cli php5-intl php5-curl php5-xsl php5-mcrypt php5-gd php5-recode php5-tidy php5-json

# If you're game, you can install this too. I'm not game.
#sudo apt-get install -y php5-ffmpeg 

# configure PHP
sudo nano /etc/php5/apache2/php.ini

# change
# max_execution_time = 30
# max_input_time = 60
# display_errors = Off
# log_errors_max_len = 1024
# default_socket_timeout = 60
# to  become
max_execution_time = 300
max_input_time = 300
display_errors = On
log_errors_max_len = 4096
default_socket_timeout = 300

# Check that PHP5 installed ok
php -v

# The default apache and php web directory in Raspberry Pi is /var/www. 
# To check PHP5 works under apache:
#
sudo nano /var/www/phpinfo.php
# Put these lines in the file.
<?php
phpinfo();
?>
#control O
#control X

# RESTART APACHE
sudo service apache2 restart

# Use a chrome browser on another computer 
# Navigate to the server-status page (location) you have just defined 
# by typing the following into your web browser using your Pi's IP address
# http://10.0.0.10/phpinfo.php

# check it with curl
curl 127.0.0.1/phpinfo.php

#
# E. Install and configure SAMBA so the USB drive is visible to Win7 PCs as an unsecured file share
# -------------------------------------------------------------------------------------------------
#http://vicidi.wordpress.com/2012/01/23/set-up-samba-without-using-deprecated-share-security-setting/
#
# note: this makes the exposed shares (including the web root) totally open and writable, by intent.
#
# Make sure all of the folders are "open" in samba so we can control files in them 
# from any windows PC on the home network
# (obviously, this assumes a secure LAN and all "family safe content")
#

# To install Samba on Raspberry Pi, run
sudo apt-get install samba samba-common-bin -y

# Find the ethernet interface name
cat /etc/network/interfaces
and check for line like     "iface eth0 inet dhcp"
and note the text "eth0" or equivalent name

# backup and then edit samba conf file
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old
sudo nano /etc/samba/smb.conf

# find and change/add lines etc to make this outcome:
[global]
   workgroup = WORKGROUP
   wins support = no
   dns proxy = no
   interfaces = eth0 127.0.0.0/8 10.0.0.0/24
   max log size = 5000
   syslog only = no
   security = user
   map to guest = bad user
   socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
   usershare allow guests = yes
   create mask = 0777
   directory mask = 0777
#   valid users = %S
# my stuff
preferred master = No
local master = No
guest ok = yes
browseable = yes
guest account = root
#valid users = @users

# Share
[mp4library]
comment=RP03 mp4library
#force group = users
#guest only = Yes
guest ok = Yes
public = yes
#valid users = @users
path = /media/USB4TB/mp4library
available = yes
read only = no
browsable = yes
writable = yes
create mask = 0777
directory mask = 0777
[HomePics]
comment=RP03 HomePics
#force group = users
#guest only = Yes
guest ok = Yes
public = yes
#valid users = @users
path = /media/USB4TB/HomePics
available = yes
read only = no
browsable = yes
writable = yes
create mask = 0777
directory mask = 0777
[www]
comment=RP03 www home
#guest only = Yes
guest ok = Yes
public = yes
#valid users = @users
path = /var/www
available = yes
read only = No
browsable = yes
public = yes
writable = yes
create mask = 0777
directory mask = 0777

#control O
#control X

# Restart Samba using
sudo /etc/init.d/samba restart

# Test the samba config is OK
sudo testparm

# Add samba users to the local smbpasswd file which can be different to the Pi's password:
sudo smbpasswd -a pi
sudo smbpasswd -a root

# Restart Samba using
sudo /etc/init.d/samba restart

# List the new samba users, which can have different passwords to the Pi itself
sudo pdbedit -L -v

# You can now access the defined shares from a Windows machine 
# or from an app that supports the SMB protocol
# eg from Windows Explorer use address \\10.0.0.10\

#
# F. Install an ftp Server and make it as open as possible
# ---------------------------------------------------------------------
sudo apt-get -y install vsftpd
sudo nano /etc/vsftpd.conf
#
# https://security.appspot.com/vsftpd/vsftpd_conf.html
# local_umask ( 0011 ) is subtracted. 
# The umask essentially removes the permissions you don't want users to have.
#
# Find and make/add these settings to the config file
#
anon_mkdir_write_enable=yes
anon_other_write_enable=yes
anon_upload_enable=yes
delete_failed_uploads=yes
dirlist_enable=yes
download_enable=yes
ls_recurse_enable=yes
no_anon_password=yes
use_localtime=yes
xferlog_enable=yes

anonymous_enable=YES
local_enable=YES
write_enable=YES
force_dot_files=YES
local_umask=000
file_open_mode=0777

allow_writeable_chroot=YES
anon_root=/media/USB4TB/mp4library
ftp_username=pi
guest_username=pi

# restart the ftp service
sudo service vsftpd restart

# G. Finally, let's "walk" the mp4library in our browser
# ------------------------------------------------------
#
# Use a chrome browser to check if it is working.
# Navigate to the mp4library of the Pi web server 
# by typing the following into your web browser using your Pi's IP address
# http://10.0.0.10/mp4library
# and we should be able to navigate the mp4library folder tree
#
# To copy .mp4 files to/from the Pi via SAMBA (file shares) -
# From a Windows 7 PC, use Windows Explorer to open a share on 
# the Pi using its IP address, put it in the address bar of Windows Explorer
# \\10.0.0.10\
# and see two shares: 
#    mp4library
#    www
# Open the mp4library share. See the files and folders there.
# We can copy files to/from it just like any other windows folders.
#
#
# Fin.

TimHaydnJones
Posts: 2
Joined: Fri Apr 15, 2016 10:33 am

Re: Pi 2 as a plain-jane home chromecast media server

Fri Apr 15, 2016 10:36 am

Thanks for that.
Is there any way of eliminating the android element so that the pi can send the command and control info directly to the chromecast?

hydra3333
Posts: 104
Joined: Thu Jan 10, 2013 11:48 pm

Re: Pi 2 as a plain-jane home chromecast media server

Fri Apr 15, 2016 1:25 pm

TimHaydnJones wrote:Is there any way of eliminating the android element so that the pi can send the command and control info directly to the chromecast?
That'd be a bit of work but yes.
The chromecast command and control functions are implemented as a "sender" which actually makes calls to google chromecast APIs. There is example code (meant for apps and browsers) and samples programs over around this google link.
https://developers.google.com/cast/
Be prepared to do coding, unless the sample apps are a lot further along than when I last looked.

If you want something a little different, eg the Pi to replace the chromecast device plugged into the telly, things like this:
http://www.geek.com/android/picast-the- ... e-1564550/

Other links possibly of interest
https://www.ebower.com/wiki/Raspberry_P ... ast_Remote
viewtopic.php?f=38&t=73464
http://stackoverflow.com/questions/3086 ... ts-as-cast

cheers

Return to “Media centres”