librecast 3.38 KB
Newer Older
Carlos Remuzzi's avatar
Carlos Remuzzi committed
1
2
#!/usr/bin/env bash

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
valid_ip() {
  local  ip=$1
  local  stat=1

  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
      OIFS=$IFS
      IFS='.'
      ip=($ip)
      IFS=$OIFS
      [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
          && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
      stat=$?
  fi
  return $stat
}
18

19
20
21
22
23
24
25
invocar_nmcli() {
  echo "conectando a $AP_NAME"
  nmcli con up $AP_NAME
  if [ $? -eq 1 ];then
    exit 1
  fi
}
26

27
28
main() {
  if [ -z "$(pidof gst-launch-1.0)" ] ;then
Carlos Remuzzi's avatar
Carlos Remuzzi committed
29

30
    pkill -15 gst-launch-1.0
Carlos Remuzzi's avatar
Carlos Remuzzi committed
31

32
33
34
    # AP_NAME es necesario solo si se quiere usar Network Manager
    # para la conexion automatica al AP wifi  
    if [ -n "$AP_NAME" ] ;then
Carlos Remuzzi's avatar
Carlos Remuzzi committed
35
      if which nmcli >/dev/null 2>&1 ; then
36
37
38
39
        invocar_nmcli
       else
        echo "utilizando conexion manual"
      fi
40
    fi
Carlos Remuzzi's avatar
Carlos Remuzzi committed
41

42
43
44
45
46
47
    SIZE_STRING=$(xdpyinfo | awk '/dimensions/{print $2}')
    SCREEN_WIDTH="$(cut -d'x' -f1 <<<"$SIZE_STRING")"
    SCREEN_HEIGHT="$(cut -d'x' -f2 <<<"$SIZE_STRING")"
    # por defecto no realizamos ningun recorte
    CROP_STRING_X=""
    CROP_STRING_Y=""
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    # en caso el ancho supere lo maximo consentido de 1920
    if [ "$SCREEN_WIDTH" -gt 1920 ];then
      echo "recortando el ancho de la pantalla ..."
      SCREEN_WIDTH=1920
      CROP_STRING_X="startx=0 endx=1919"
      CROP_STRING_Y="starty=0 endy=$(( $SCREEN_HEIGHT-1 ))"
    fi

    # en caso el alto supere lo maximo consentido de 1080
    if [ "$SCREEN_HEIGHT" -gt 1080 ];then
      echo "recortando el alto de la pantalla..."
      SCREEN_HEIGHT=1080
      CROP_STRING_X="startx=0 endx=$(( $SCREEN_WIDTH-1 ))"
      CROP_STRING_Y="starty=0 endy=1079"
    fi

Carlos Remuzzi's avatar
Carlos Remuzzi committed
65
    # curl --connect-timeout 3 -s -k -X GET https://$AP_ADDRESS:5443/player/start | grep 'Player iniciado'
66
67
68
69
70
71
72
    if [ $? -eq 0 ];then

      pacmd unload-module module-null-sink
      pacmd load-module module-null-sink sink_name=libreCast
      pacmd update-sink-proplist libreCast device.description=libreCast
      pacmd set-default-sink libreCast

Carlos Remuzzi's avatar
Carlos Remuzzi committed
73
74
      gst-launch-1.0 ximagesrc use-damage=false $CROP_STRING_X $CROP_STRING_Y ! videoconvert ! videoscale ! video/x-raw,format=I420,width=$SCREEN_WIDTH,height=$SCREEN_HEIGHT,framerate=24/1,bitrate=6000 ! vaapih264enc ! rtph264pay config-interval=1 pt=96 ! udpsink host=$AP_ADDRESS port=1234 sync=false &
      # gst-launch-1.0 ximagesrc use-damage=false $CROP_STRING_X $CROP_STRING_Y ! videoconvert ! videoscale ! video/x-raw,format=I420,width=$SCREEN_WIDTH,height=$SCREEN_HEIGHT,framerate=24/1,bitrate=6000 ! jpegenc quality=80 ! rtpjpegpay ! udpsink host=$AP_ADDRESS port=1234 sync=false &
75
76
77
78
79
80
      gst-launch-1.0 -v pulsesrc device="libreCast.monitor" ! "audio/x-raw,rate=11500,channels=2,depth=16" ! udpsink host=$AP_ADDRESS port=5678 sync=false &
      exit 0
    else
      # the player is already running or the player is not reachable
      exit 1
    fi
81

Carlos Remuzzi's avatar
Carlos Remuzzi committed
82
83
  else

84
85
86
87
    echo "stopping ..."
    curl --connect-timeout 3 -s -k -X GET https://$AP_ADDRESS:5443/player/stop
    pkill -15 gst-launch-1.0
    pacmd unload-module module-null-sink
Carlos Remuzzi's avatar
Carlos Remuzzi committed
88

89
    if [ -n "$AP_NAME" ] ;then
Carlos Remuzzi's avatar
Carlos Remuzzi committed
90
      if which nmcli >/dev/null 2>&1 ; then
91
92
93
94
95
         nmcli con down $AP_NAME
       else
         echo "Por favor desconectarse de $AP_NAME"
      fi
    fi
96
  fi
97
}
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# por defecto apuntamos a la ip predefinida del AP wifi
AP_ADDRESS=192.168.12.1

if [ $# -eq 1 ];then
  if valid_ip $1;then
    AP_ADDRESS=$1
  else
    AP_NAME=$1
  fi
fi

if [ $# -eq 2 ];then
  AP_ADDRESS=$1
  AP_NAME=$2
fi

115
main