Ejemplo n.º 1
0
def main():
    from xpra.platform import init, clean
    init("Sound-Record")
    try:
        import os.path
        if len(sys.argv) not in (2, 3):
            print("usage: %s filename [codec]" % sys.argv[0])
            return 1
        filename = sys.argv[1]
        if not os.path.exists(filename):
            print("file %s does not exist" % filename)
            return 2
        if len(sys.argv) == 3:
            codec = sys.argv[2]
            if codec not in CODECS:
                print("invalid codec: %s" % codec)
                return 2
        else:
            codec = None
            parts = filename.split(".")
            if len(parts) > 1:
                extension = parts[-1]
                if extension.lower() in CODECS:
                    codec = extension.lower()
                    print("guessed codec %s from file extension %s" %
                          (codec, extension))
            if codec is None:
                print("assuming this is an mp3 file...")
                codec = MP3

        log.enable_debug()
        f = open(filename, "rb")
        data = f.read()
        f.close()
        print("loaded %s bytes from %s" % (len(data), filename))
        #force no leak since we push all the data at once
        global QUEUE_LEAK, GST_QUEUE_NO_LEAK, QUEUE_SILENT
        QUEUE_LEAK = GST_QUEUE_NO_LEAK
        QUEUE_SILENT = 1
        ss = SoundSink(codec=codec)
        ss.add_data(data)

        def eos(*args):
            print("eos")
            gobject.idle_add(gobject_mainloop.quit)

        ss.connect("eos", eos)
        ss.start()

        gobject_mainloop = gobject.MainLoop()
        gobject.threads_init()

        import signal

        def deadly_signal(*args):
            gobject.idle_add(gobject_mainloop.quit)

        signal.signal(signal.SIGINT, deadly_signal)
        signal.signal(signal.SIGTERM, deadly_signal)

        def check_for_end(*args):
            qtime = int(ss.queue.get_property("current-level-time") / MS_TO_NS)
            if qtime <= 0:
                log.info("underrun (end of stream)")
                thread.start_new_thread(ss.stop, ())
                gobject.timeout_add(500, gobject_mainloop.quit)
                return False
            return True

        gobject.timeout_add(1000, check_for_end)

        gobject_mainloop.run()
        return 0
    finally:
        clean()
Ejemplo n.º 2
0
def main():
    from xpra.platform import init, clean

    init("Sound-Record")
    try:
        args = sys.argv
        log.enable_debug()
        import os.path

        if len(args) not in (2, 3):
            print("usage: %s [-v|--verbose] filename [codec]" % sys.argv[0])
            return 1
        filename = args[1]
        if not os.path.exists(filename):
            print("file %s does not exist" % filename)
            return 2
        if len(args) == 3:
            codec = args[2]
            if codec not in CODECS:
                print("invalid codec: %s" % codec)
                return 2
        else:
            codec = None
            parts = filename.split(".")
            if len(parts) > 1:
                extension = parts[-1]
                if extension.lower() in CODECS:
                    codec = extension.lower()
                    print("guessed codec %s from file extension %s" % (codec, extension))
            if codec is None:
                print("assuming this is an mp3 file...")
                codec = MP3

        log.enable_debug()
        with open(filename, "rb") as f:
            data = f.read()
        print("loaded %s bytes from %s" % (len(data), filename))
        # force no leak since we push all the data at once
        global QUEUE_LEAK, GST_QUEUE_NO_LEAK, QUEUE_SILENT
        QUEUE_LEAK = GST_QUEUE_NO_LEAK
        QUEUE_SILENT = 1
        ss = SoundSink(codec=codec)
        ss.add_data(data)

        def eos(*args):
            print("eos")
            gobject.idle_add(gobject_mainloop.quit)

        ss.connect("eos", eos)
        ss.start()

        gobject_mainloop = gobject.MainLoop()
        gobject.threads_init()

        import signal

        def deadly_signal(*args):
            gobject.idle_add(gobject_mainloop.quit)

        signal.signal(signal.SIGINT, deadly_signal)
        signal.signal(signal.SIGTERM, deadly_signal)

        def check_for_end(*args):
            qtime = int(ss.queue.get_property("current-level-time") / MS_TO_NS)
            if qtime <= 0:
                log.info("underrun (end of stream)")
                thread.start_new_thread(ss.stop, ())
                gobject.timeout_add(500, gobject_mainloop.quit)
                return False
            return True

        gobject.timeout_add(1000, check_for_end)

        gobject_mainloop.run()
        return 0
    finally:
        clean()
Ejemplo n.º 3
0
def main():
    from xpra.platform import init, clean
    init("Xpra-Sound-Source")
    try:
        import os.path
        if "-v" in sys.argv:
            log.enable_debug()
            sys.argv.remove("-v")

        if len(sys.argv) not in (2, 3):
            log.error("usage: %s filename [codec] [--encoder=rencode]", sys.argv[0])
            return 1
        filename = sys.argv[1]
        if filename=="-":
            from xpra.os_util import disable_stdout_buffering
            disable_stdout_buffering()
        elif os.path.exists(filename):
            log.error("file %s already exists", filename)
            return 1
        codec = None

        if len(sys.argv)==3:
            codec = sys.argv[2]
            if codec not in CODECS:
                log.error("invalid codec: %s, codecs supported: %s", codec, CODECS)
                return 1
        else:
            parts = filename.split(".")
            if len(parts)>1:
                extension = parts[-1]
                if extension.lower() in CODECS:
                    codec = extension.lower()
                    log.info("guessed codec %s from file extension %s", codec, extension)
            if codec is None:
                codec = MP3
                log.info("using default codec: %s", codec)

        #in case we're running against pulseaudio,
        #try to setup the env:
        try:
            from xpra.platform.paths import get_icon_filename
            f = get_icon_filename("xpra.png")
            from xpra.sound.pulseaudio_util import add_audio_tagging_env
            add_audio_tagging_env(f)
        except Exception as e:
            log.warn("failed to setup pulseaudio tagging: %s", e)

        from threading import Lock
        if filename=="-":
            f = sys.stdout
        else:
            f = open(filename, "wb")
        ss = SoundSource(codecs=[codec])
        lock = Lock()
        def new_buffer(ss, data, metadata):
            log.info("new buffer: %s bytes (%s), metadata=%s", len(data), type(data), metadata)
            with lock:
                if f:
                    f.write(data)
                    f.flush()

        gobject_mainloop = gobject.MainLoop()
        gobject.threads_init()

        ss.connect("new-buffer", new_buffer)
        ss.start()

        import signal
        def deadly_signal(sig, frame):
            log.warn("got deadly signal %s", SIGNAMES.get(sig, sig))
            gobject.idle_add(ss.stop)
            gobject.idle_add(gobject_mainloop.quit)
        signal.signal(signal.SIGINT, deadly_signal)
        signal.signal(signal.SIGTERM, deadly_signal)

        try:
            gobject_mainloop.run()
        except Exception as e:
            log.error("main loop error: %s", e)
        ss.stop()

        f.flush()
        if f!=sys.stdout:
            log.info("wrote %s bytes to %s", f.tell(), filename)
        with lock:
            f.close()
            f = None
        return 0
    finally:
        clean()
Ejemplo n.º 4
0
def main():
    from xpra.platform import init, clean
    init("Sound-Play")
    try:
        import os.path
        if len(sys.argv) not in (2, 3):
            print("usage: %s filename [codec]" % sys.argv[0])
            return 1
        filename = sys.argv[1]
        if os.path.exists(filename):
            print("file %s already exists" % filename)
            return 2
        codec = None
        if len(sys.argv)==3:
            codec = sys.argv[2]
            if codec not in CODECS:
                print("invalid codec: %s, codecs supported: %s" % (codec, CODECS))
                return 2
        else:
            parts = filename.split(".")
            if len(parts)>1:
                extension = parts[-1]
                if extension.lower() in CODECS:
                    codec = extension.lower()
                    print("guessed codec %s from file extension %s" % (codec, extension))
            if codec is None:
                codec = MP3
                print("using default codec: %s" % codec)

        log.enable_debug()
        from threading import Lock
        f = open(filename, "wb")
        from xpra.sound.pulseaudio_util import get_pa_device_options
        monitor_devices = get_pa_device_options(True, False)
        log.info("found pulseaudio monitor devices: %s", monitor_devices)
        if len(monitor_devices)==0:
            log.warn("could not detect any pulseaudio monitor devices - will use a test source")
            ss = SoundSource("audiotestsrc", src_options={"wave":2, "freq":100, "volume":0.4}, codec=codec)
        else:
            monitor_device = monitor_devices.items()[0][0]
            log.info("using pulseaudio source device: %s", monitor_device)
            ss = SoundSource("pulsesrc", {"device" : monitor_device}, codec)
        lock = Lock()
        def new_buffer(ss, data, metadata):
            log.info("new buffer: %s bytes, metadata=%s" % (len(data), metadata))
            try:
                lock.acquire()
                if f:
                    f.write(data)
            finally:
                lock.release()
        ss.connect("new-buffer", new_buffer)
        ss.start()

        gobject_mainloop = gobject.MainLoop()
        gobject.threads_init()

        import signal
        def deadly_signal(*args):
            gobject.idle_add(gobject_mainloop.quit)
        signal.signal(signal.SIGINT, deadly_signal)
        signal.signal(signal.SIGTERM, deadly_signal)

        gobject_mainloop.run()

        f.flush()
        log.info("wrote %s bytes to %s", f.tell(), filename)
        try:
            lock.acquire()
            f.close()
            f = None
        finally:
            lock.release()
        return 0
    finally:
        clean()
Ejemplo n.º 5
0
def main():
    from xpra.platform import init, clean
    init("Xpra-Sound-Source")
    try:
        import os.path
        if "-v" in sys.argv:
            log.enable_debug()
            sys.argv.remove("-v")

        if len(sys.argv) not in (2, 3):
            log.error("usage: %s filename [codec] [--encoder=rencode]",
                      sys.argv[0])
            return 1
        filename = sys.argv[1]
        if filename == "-":
            from xpra.os_util import disable_stdout_buffering
            disable_stdout_buffering()
        elif os.path.exists(filename):
            log.error("file %s already exists", filename)
            return 1
        codec = None

        if len(sys.argv) == 3:
            codec = sys.argv[2]
            if codec not in CODECS:
                log.error("invalid codec: %s, codecs supported: %s", codec,
                          CODECS)
                return 1
        else:
            parts = filename.split(".")
            if len(parts) > 1:
                extension = parts[-1]
                if extension.lower() in CODECS:
                    codec = extension.lower()
                    log.info("guessed codec %s from file extension %s", codec,
                             extension)
            if codec is None:
                codec = MP3
                log.info("using default codec: %s", codec)

        #in case we're running against pulseaudio,
        #try to setup the env:
        try:
            from xpra.platform.paths import get_icon_filename
            f = get_icon_filename("xpra.png")
            from xpra.sound.pulseaudio_util import add_audio_tagging_env
            add_audio_tagging_env(f)
        except Exception as e:
            log.warn("failed to setup pulseaudio tagging: %s", e)

        from threading import Lock
        if filename == "-":
            f = sys.stdout
        else:
            f = open(filename, "wb")
        ss = SoundSource(codecs=[codec])
        lock = Lock()

        def new_buffer(ss, data, metadata):
            log.info("new buffer: %s bytes (%s), metadata=%s", len(data),
                     type(data), metadata)
            with lock:
                if f:
                    f.write(data)
                    f.flush()

        gobject_mainloop = gobject.MainLoop()
        gobject.threads_init()

        ss.connect("new-buffer", new_buffer)
        ss.start()

        import signal

        def deadly_signal(sig, frame):
            log.warn("got deadly signal %s", SIGNAMES.get(sig, sig))
            gobject.idle_add(ss.stop)
            gobject.idle_add(gobject_mainloop.quit)

        signal.signal(signal.SIGINT, deadly_signal)
        signal.signal(signal.SIGTERM, deadly_signal)

        try:
            gobject_mainloop.run()
        except Exception as e:
            log.error("main loop error: %s", e)
        ss.stop()

        f.flush()
        if f != sys.stdout:
            log.info("wrote %s bytes to %s", f.tell(), filename)
        with lock:
            f.close()
            f = None
        return 0
    finally:
        clean()