101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
#
|
||
|
# Copyright 2010 Maurizio Porrato <maurizio.porrato@gmail.com>
|
||
|
# See LICENSE.txt for copyright info
|
||
|
|
||
|
import subprocess
|
||
|
from frn.protocol.client import FRNClient, FRNClientFactory
|
||
|
from frn.user import FRNUser
|
||
|
from twisted.internet import reactor, task
|
||
|
from twisted.internet.defer import DeferredList
|
||
|
from twisted.python import log
|
||
|
import os, string
|
||
|
from Queue import Queue
|
||
|
from twisted.internet.task import LoopingCall
|
||
|
import fcntl
|
||
|
|
||
|
STREAM_CMD="""tools/gsmdecode|ffmpeg -ar 8000 -ac 1 -f s16le -i /dev/stdin http://127.0.0.1:8090/feed.ffm"""
|
||
|
|
||
|
silence_frame = open("sounds/silence-gsm.gsm", "rb").read()
|
||
|
|
||
|
class FRNStream(FRNClient):
|
||
|
|
||
|
def __init__(self):
|
||
|
self._frames = Queue()
|
||
|
self._stream = subprocess.Popen(STREAM_CMD, shell=True,
|
||
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||
|
self._streamtimer = LoopingCall.withCount(self.sendStreamFrame)
|
||
|
self._streamtimer.start(0.20)
|
||
|
|
||
|
def getClientName(self, client_id):
|
||
|
if self.clientsById.has_key(client_id):
|
||
|
return self.clientsById[client_id]['on']
|
||
|
else:
|
||
|
return client_id
|
||
|
|
||
|
def audioFrameReceived(self, from_id, frames):
|
||
|
self._frames.put_nowait(frames)
|
||
|
self.pong()
|
||
|
|
||
|
def sendStreamFrame(self, count):
|
||
|
for i in range(count):
|
||
|
if self._frames.empty():
|
||
|
frames = silence_frame
|
||
|
else:
|
||
|
frames = self._frames.get_nowait()
|
||
|
self._stream.stdin.write(frames)
|
||
|
self._stream.stdin.flush()
|
||
|
self._stream.stdout.flush()
|
||
|
self._stream.stderr.flush()
|
||
|
|
||
|
def loginResponse(self, info):
|
||
|
log.msg("Login: %s" % info['al'])
|
||
|
|
||
|
def clientsListUpdated(self, clients):
|
||
|
self.clients = clients
|
||
|
self.clientsById = dict([(i['id'], i) for i in clients])
|
||
|
|
||
|
|
||
|
class FRNStreamFactory(FRNClientFactory):
|
||
|
protocol = FRNStream
|
||
|
reactor = reactor
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
import sys
|
||
|
from os.path import dirname, join as pjoin
|
||
|
from ConfigParser import ConfigParser
|
||
|
|
||
|
log.startLogging(sys.stderr)
|
||
|
|
||
|
basedir = dirname(__file__)
|
||
|
|
||
|
acfg = ConfigParser()
|
||
|
acfg.read(['/etc/grn/accounts.conf',
|
||
|
pjoin(basedir,'accounts.conf'), 'accounts.conf'])
|
||
|
|
||
|
scfg = ConfigParser()
|
||
|
scfg.read(['/etc/grn/servers.conf',
|
||
|
pjoin(basedir,'servers.conf'), 'servers.conf'])
|
||
|
|
||
|
argc = len(sys.argv)
|
||
|
if argc >= 3:
|
||
|
server_name, network_name = sys.argv[2].split(':',1)
|
||
|
account_cfg = acfg.items(sys.argv[1])+[('network', network_name)]
|
||
|
server_cfg = scfg.items(server_name)
|
||
|
server = scfg.get(server_name, 'server')
|
||
|
port = scfg.getint(server_name, 'port')
|
||
|
|
||
|
d = dict(account_cfg)
|
||
|
user = FRNUser(
|
||
|
EA=d['email'],
|
||
|
PW=d['password'], ON=d['operator'],
|
||
|
BC=d['transmission'], DS=d['description'],
|
||
|
NN=d['country'], CT=d['city'], NT=d['network'])
|
||
|
reactor.connectTCP(server, port, FRNStreamFactory(user))
|
||
|
reactor.run()
|
||
|
|
||
|
|
||
|
# vim: set et ai sw=4 ts=4 sts=4:
|