Mkchromecast for Debian
----------------------

Using this program
==================

In order to cast you have to install either mkchromecast-pulseaudio, or install
mkchromecast-alsa to pull the needed additional dependencies. ALSA users will
need to create a .asoundrc file, a Loopback virtual device, and build the
hw:X,Y devices. Pulseaudio users don't need any additional configuration.

The user interaction from console works as follows:

    1. Execute mkchromecast as a local user.
    2. After startup, the program will detect your google cast devices in your
       LAN that you can select to cast.
    3. A new sink called Mkchromecast is created that need to be selected in
       pavucontrol to cast audio (for Pulseaudio users only).
    4. If you stop Mkchromecast, the audio will be back sent to the default
       playback device.


ALSA users
==========

You need to install mkchromecast-alsa to pull the needed dependencies.

Mkchromecast can cast to google cast without the need of pulseaudio. One
advantage I have seen, is that delay is maximum 4 seconds. For that, you have
to use the ffmpeg or avconv backends together with the mkhcromecast's
`--alsa-device` flag.  You also need to install mkchromecast-alsa to pull the
needed dependencies.

Example:

    mkchromecast.py --encoder-backend ffmpeg --alsa-device hw:2,1


However, unlike Pulseaudio users, some previous configuration is mandatory. We
will try to figure out how to build the hw:X1,Y1 alsa device needed to complete
a .asoundrc configuration file, and the virtual alsa device hw:X2,Y2 needed by
the --alsa-device flag in Mkchromecast. It seems that always Y1 has to be
0 while Y2 is 1.

How to determine hw:X1,Y1
-------------------------

First, check what are the cards available in your system in /proc/asound/cards.
Below an example:

```
     % cat /proc/asound/cards
    0 [PCH            ]: HDA-Intel - HDA Intel PCH
                         HDA Intel PCH at 0xc1814000 irq 57
    1 [HDMI           ]: HDA-Intel - HDA Intel HDMI
                         HDA Intel HDMI at 0xc1810000 irq 61
```

In the case above, the principal card is 0 [PCH]: HDA-Intel - HDA Intel PCH for
which the index X1 is 0. Normally, our needed alsa sound card (hw:X1,Y1) can be
built as hw:0,0. We will need it in the next step.

The ~/.asoundrc file
--------------------

Second, we need to create the ~/.asoundrc shown below. Note that this file may
not exist. You will need to change hw:X1,Y1 accordingly (in the case shown in
this document is hw:0,0):

```
    pcm.!default {
      type asym
      playback.pcm "LoopAndReal"
      #capture.pcm "looprec"
      capture.pcm "hw:X1,Y1"
    }

    pcm.looprec {
        type hw
        card "Loopback"
        device 1
        subdevice 0
    }


    pcm.LoopAndReal {
      type plug
      slave.pcm mdev
      route_policy "duplicate"
    }


    pcm.mdev {
      type multi
      slaves.a.pcm pcm.MixReale
      slaves.a.channels 2
      slaves.b.pcm pcm.MixLoopback
      slaves.b.channels 2
      bindings.0.slave a
      bindings.0.channel 0
      bindings.1.slave a
      bindings.1.channel 1
      bindings.2.slave b
      bindings.2.channel 0
      bindings.3.slave b
      bindings.3.channel 1
    }

    pcm.MixReale {
      type dmix
      ipc_key 1024
      slave {
        pcm "hw:X1,Y1"
        rate 48000
        #rate 44100
        periods 128
        period_time 0
        period_size 1024 # must be power of 2
        buffer_size 8192
      }
    }

    pcm.MixLoopback {
      type dmix
      ipc_key 1025
      slave {
        pcm "hw:Loopback,0,0"
        rate 48000
        #rate 44100
        periods 128
        period_time 0
        period_size 1024 # must be power of 2
        buffer_size 8192
      }
    }

```

Test that this is working. For that, open any audio file to see if it is
playing correctly.

How to determine hw:X2,Y2 and the Loopback device
-------------------------------------------------

We now need to load the snd-aloop module:

```
    sudo modprobe snd-aloop
```

This will create a new virtual device called Loopback that we above referenced
as hw:X2,Y2 and we will pass it to the --alsa-device flag of Mkchromecast:


```
     % cat /proc/asound/cards

    0 [PCH            ]: HDA-Intel - HDA Intel PCH
                         HDA Intel PCH at 0xc1814000 irq 57
    1 [HDMI           ]: HDA-Intel - HDA Intel HDMI
                         HDA Intel HDMI at 0xc1810000 irq 61
    2 [Loopback       ]: Loopback - Loopback
                         Loopback 1
```

As it can be seen, Loopback has an index X2 equals 2. This device is then
hw:2,1. This won't survive reboot. If you want to make this permanent, add
snd-aloop to /etc/modules.

Now we can execute Mkchromecast passing all desired parameters:

```
    mkchromecast.py --encoder-backend ffmpeg --alsa-device hw:2,1 -c aac --volume
```

Happy Casting!


Supported encoders
==================

+---------+------------------------------+------------+
| Encoder |         Description          | Identifier |
+---------+------------------------------+------------+
| lame    | MPEG Audio Layer III         | mp3        |
| oggenc  | Ogg Vorbis                   | ogg        |
| flac    | Free Lossless Audio Codec    | flac       |
| sox     | Waveform Audio File Format   | wav        |
| faac    | Advanced Audio Coding        | aac        |
| opus    | Opus Interactive Audio Codec | opus       |
+---------+------------------------------+------------+

Note: faac is not added as dependency to the Debian package, but you can
install it if you would like to use such encoder.

 -- Muammar El Khatib <muammar@debian.org>  Fri, 18 Sep 2016 15:47:56 +0200
