airtime-test-stream.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import subprocess
  2. import os
  3. import pwd
  4. import grp
  5. import sys
  6. import getopt
  7. """
  8. we need to run the program as non-root because Liquidsoap refuses to run as root.
  9. It is possible to run change the effective user id (seteuid) before calling Liquidsoap
  10. but this introduces other problems (fake root user is not part of audio group after calling seteuid)
  11. """
  12. if os.geteuid() == 0:
  13. print "Please run this program as non-root"
  14. sys.exit(1)
  15. def printUsage():
  16. print "airtime-test-stream [-v] [-o icecast | shoutcast ] [-H hostname] [-P port] [-u username] [-p password] [-m mount]"
  17. print " Where: "
  18. print " -v verbose mode"
  19. print " -o stream server type (default: icecast)"
  20. print " -H hostname (default: localhost) "
  21. print " -P port (default: 8000) "
  22. print " -u user (default: source) "
  23. print " -p password (default: hackme) "
  24. print " -m mount (default: test) "
  25. print " -h show help menu"
  26. def find_liquidsoap_binary():
  27. """
  28. Starting with Airtime 2.0, we don't know the exact location of the Liquidsoap
  29. binary because it may have been installed through a debian package. Let's find
  30. the location of this binary.
  31. """
  32. rv = subprocess.call("which airtime-liquidsoap > /dev/null", shell=True)
  33. if rv == 0:
  34. return "airtime-liquidsoap"
  35. return None
  36. optlist, args = getopt.getopt(sys.argv[1:], 'hvo:H:P:u:p:m:')
  37. stream_types = set(["shoutcast", "icecast"])
  38. verbose = False
  39. stream_type = "icecast"
  40. host = "localhost"
  41. port = 8000
  42. user = "source"
  43. password = "hackme"
  44. mount = "test"
  45. for o, a in optlist:
  46. if "-v" == o:
  47. verbose = True
  48. if "-o" == o:
  49. if a.lower() in stream_types:
  50. stream_type = a.lower()
  51. else:
  52. print "Unknown stream type\n"
  53. printUsage()
  54. sys.exit(1)
  55. if "-h" == o:
  56. printUsage()
  57. sys.exit(0)
  58. if "-H" == o:
  59. host = a
  60. if "-P" == o:
  61. port = a
  62. if "-u" == o:
  63. user = a
  64. if "-p" == o:
  65. password = a
  66. if "-m" == o:
  67. mount = a
  68. try:
  69. print "Protocol: %s " % stream_type
  70. print "Host: %s" % host
  71. print "Port: %s" % port
  72. print "User: %s" % user
  73. print "Password: %s" % password
  74. if stream_type == "icecast":
  75. print "Mount: %s\n" % mount
  76. url = "http://%s:%s/%s" % (host, port, mount)
  77. print "Outputting to %s streaming server. You should be able to hear a monotonous tone on '%s'. Press ctrl-c to quit." % (stream_type, url)
  78. liquidsoap_exe = find_liquidsoap_binary()
  79. if liquidsoap_exe is None:
  80. raise Exception("Liquidsoap not found!")
  81. if stream_type == "icecast":
  82. command = "%s 'output.icecast(%%vorbis, host = \"%s\", port = %s, user= \"%s\", password = \"%s\", mount=\"%s\", sine())'" % (liquidsoap_exe, host, port, user, password, mount)
  83. else:
  84. command = "%s /usr/lib/airtime/pypo/bin/liquidsoap_scripts/library/pervasives.liq 'output.shoutcast(%%mp3, host=\"%s\", port = %s, user= \"%s\", password = \"%s\", sine())'" \
  85. % (liquidsoap_exe, host, port, user, password)
  86. if not verbose:
  87. command += " 2>/dev/null | grep \"failed\""
  88. else:
  89. print command
  90. #print command
  91. rv = subprocess.call(command, shell=True)
  92. #if we reach this point, it means that our subprocess exited without the user
  93. #doing a keyboard interrupt. This means there was a problem outputting to the
  94. #stream server. Print appropriate message.
  95. print "There was an error with your stream configuration. Please review your configuration " + \
  96. "and run this program again. Use the -h option for help"
  97. except KeyboardInterrupt, ki:
  98. print "\nExiting"
  99. except Exception, e:
  100. raise