Small sh "framework" to test some server responses
sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webradio.sh 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #copyright (c) 2016,2023 weber yann
  2. #
  3. #this program is free software; you can redistribute it and/or modify
  4. #it under the terms of the gnu general public license as published by
  5. #the free software foundation; either version 3 of the license, or
  6. #any later version.
  7. #
  8. #this program is distributed in the hope that it will be useful,
  9. #but without any warranty; without even the implied warranty of
  10. #merchantability or fitness for a particular purpose. see the
  11. #gnu general public license for more details.
  12. #
  13. #you should have received a copy of the gnu general public license
  14. #along with this program. if not, see <http://www.gnu.org/licenses/>.
  15. check_audiostream() {
  16. # Uses one of mplayer or mpv to retrieve a small amount of stream
  17. # and attempt to decode it.
  18. # $1 Stream URL
  19. MPLAYER="$(which mplayer)"
  20. MPV="$(which mpv)"
  21. if [ -x "$MPLAYER" ]
  22. then
  23. check_audiostream_mplayer "$1"
  24. elif [ -x "$MPV" ]
  25. then
  26. check_audiostream_mpv "$1"
  27. else
  28. fail "Unable to find mplayer nor mpv"
  29. fi
  30. }
  31. check_audiostream_mplayer() {
  32. # Uses mplayer to fetch 128Kb (by default) of stream
  33. # $1 Stream URL
  34. # $2 size in kb without the kb suffix
  35. # $3 mplayer path
  36. tmpfile=$(mktemp -t check_audiostream.XXXXXXXXX)
  37. sz="$2"
  38. if [ -z "$sz" ]
  39. then
  40. sz="128"
  41. fi
  42. MPLAYER="$3"
  43. if [ -z "$MPLAYER" ]
  44. then
  45. MPLAYER="$(which mplayer)"
  46. fi
  47. sz_kb="${sz}kb"
  48. expt_sz="$(( sz * 8 ))"
  49. if [ "$verbose" -gt 1 ]
  50. then
  51. logdate INFO "$tc_name: Running mplayer on '$1' for $sz_kb" 3
  52. fi
  53. $MPLAYER -endpos "$sz_kb" -ao "pcm:file=$tmpfile" "$1" 1>/dev/null 2>/dev/null
  54. res="$?"
  55. bytes=$(du -b "$tmpfile" | cut -f1)
  56. rm "$tmpfile" 2>/dev/null
  57. if [ "$bytes" -lt $expt_sz ]
  58. then
  59. fail "mplayer retrieved ${bytes}B of stream instead of expected $sz_kb"
  60. return
  61. fi
  62. if [ "$res" -eq 0 ]
  63. then
  64. success "mplayer retrieved $sz of stream on $1"
  65. else
  66. fail "mplayer failed to retrieve stream on $1"
  67. fi
  68. }
  69. check_audiostream_mpv() {
  70. # Uses mpv to fetch 4s of audio stream
  71. # $1 Stream URL
  72. # $2 time of stream to fetch
  73. # $3 mpv path
  74. time="$2"
  75. if [ -z "$time" ]
  76. then
  77. time="4"
  78. fi
  79. MPV="$3"
  80. if [ -z "$MPV" ]
  81. then
  82. MPV="$(which mpv)"
  83. fi
  84. if [ "$verbose" -gt 1 ]
  85. then
  86. logdate INFO "$tc_name: Running mpv on '$1' for ${time}s" 3
  87. fi
  88. start_time=$(date "+%s")
  89. $MPV --vo=null --ao=null --o=/dev/null --of=wav --length $time "$1" 1> /dev/null 2>/dev/null
  90. res=$?
  91. stop_time=$(date "+%s")
  92. run_time=$(( stop_time - start_time))
  93. if [ "$run_time" -lt "$time" ]
  94. then
  95. fail "mpv stopped running ${run_time} after launch but ${time}s should be received"
  96. return
  97. fi
  98. if [ "$res" -eq 0 ]
  99. then
  100. success "mpv retrieved ${time}s of stream on $1"
  101. else
  102. fail "mpv failed to retrieve ${time}s of stream on $1"
  103. fi
  104. }