#!/bin/bash

function record() {
    avconv -y -f jack -i ${JACK_APP_NAME} -f x11grab -i "$DISPLAY" \
        -r $VRATE -vcodec libx264 \
        ${OUTPUT_FILE} 2>/dev/null 1>/dev/null &
}

function play_smth() {
    mplayer ~/samples/Documentaire_Nom_De_Code_Linux_-_HD.mp4 1>/dev/null &
    #mplayer ~/samples/01_Utopia_Overture.flac 1>/dev/null &
}

# start jackd
jackd -r -d dummy 2>/dev/null 1>/dev/null &

sleep 1

export RATE=48000
export VRATE=24
export RESOLUTION="1024x720"
export JACK_APP_NAME=myrecord
export OUTPUT_FILE=record.mkv

alsa_in -j cloop -dcloop -r $RATE 2>/dev/null 1>/dev/null &
alsa_out -j ploop -dploop -r $RATE 2>/dev/null 1>/dev/null &

sleep 1

Xvfb -screen 0 ${RESOLUTION}x${VRATE} 1>/dev/null 2>/dev/null &
export DISPLAY=:0

sleep 1

record
play_smth

sleep 1

jack_connect cloop:capture_1 ${JACK_APP_NAME}:input_1
jack_connect cloop:capture_2 ${JACK_APP_NAME}:input_2

exit 0
