-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
157 lines (130 loc) · 4.77 KB
/
example.py
File metadata and controls
157 lines (130 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
-------------------------
Initial SIPSIMPLE example
-------------------------
What does it do:
Default account (alice, see example.d config) calls sip:bob@example.com.
Setup of configuration file:
http://projects.ag-projects.com/projects/sipsimpleclient/wiki/SipConfigurationAPI#Account
```sh
mkdir example.d
cat >example.d/config <<EOF
Accounts:
alice@example.com:
display_name = Alice
enabled = True
auth:
password = 'secret_alice_password'
username = alice
SIPSimpleSettings:
default_account = alice@example.com
EOF
```
Originally sipsimple_hello_world.py from
https://github.com/saghul/sipsimple-examples
"""
import os.path
from threading import Event
from gevent import monkey
# Do this before importing anything else. Needed because python3-xcaplib does
# this too, but then it is too late.
monkey.patch_socket()
monkey.patch_ssl()
if True: # E402 module level import not at top of file
from application.notification import NotificationCenter
from sipsimple.account import AccountManager
from sipsimple.audio import WavePlayer, WavePlayerError
from sipsimple.application import SIPApplication
from sipsimple.configuration import ConfigurationManager
from sipsimple.core import SIPURI, ToHeader
from sipsimple.lookup import DNSLookup, DNSLookupError
from sipsimple.session import Session
from sipsimple.storage import FileStorage
from sipsimple.streams.rtp.audio import AudioStream
from sipsimple.threading.green import run_in_green_thread
CONFIGURATION_DIR = 'example.d'
AUDIO_FILE = os.path.realpath(
os.path.join(os.path.dirname(__file__), 'example.wav'))
class SimpleCallApplication(SIPApplication):
def __init__(self):
super().__init__()
self.started = Event()
self.ended = Event()
self.callee = None
self.session = None
self.player = None
notification_center = NotificationCenter()
notification_center.add_observer(self)
def call(self, callee):
self.callee = callee
self.start(FileStorage(CONFIGURATION_DIR))
@run_in_green_thread
def _NH_SIPApplicationDidStart(self, notification):
self.callee = ToHeader(SIPURI.parse(self.callee))
try:
routes = DNSLookup().lookup_sip_proxy(
self.callee.uri, ['udp']).wait()
except DNSLookupError as e:
print('DNS lookup failed: %s' % str(e))
else:
account = AccountManager().default_account
self.session = Session(account)
self.session.connect(self.callee, routes, [AudioStream()])
def _NH_SIPSessionGotRingIndication(self, notification):
print('Ringing! --', notification.name, notification.data)
def _NH_SIPSessionDidStart(self, notification):
print('Session started! --', notification.name, notification.data)
session = notification.sender
audio_stream = session.streams[0]
player = WavePlayer(
audio_stream.mixer, AUDIO_FILE, loop_count=3, initial_delay=1)
audio_stream.bridge.add(player)
print(' bridge contents =', [
i().__class__.__name__ for i in audio_stream.bridge.ports])
try:
print('Player starting', player, AUDIO_FILE)
player.play()
except WavePlayerError as e:
print('Player error', repr(e))
audio_stream.bridge.remove(player)
session.end()
else:
self.player = player
self.started.set()
def _NH_SIPSessionDidFail(self, notification):
print('Failed to connect! --', notification.name, notification.data)
self.stop()
def _NH_SIPSessionDidEnd(self, notification):
print('Session ended! --', notification.name, notification.data)
session = notification.sender
audio_stream = session.streams[0]
print(' bridge contents =', [
i().__class__.__name__ for i in audio_stream.bridge.ports])
self.stop()
def _NH_SIPApplicationDidEnd(self, notification):
print('Application ended! --', notification.name, notification.data)
self.started.set()
self.ended.set()
# Place a call to the specified URI
application = SimpleCallApplication()
print('Placing call...')
application.call('sip:bob@example.com')
if False:
cm = ConfigurationManager()
cm.save()
if False:
am = AccountManager()
acc = am.get_account('alice@example.com')
print(acc)
application.started.wait()
if application.player:
import sys
import time
while application.player.is_active:
time.sleep(0.5) # yuck.. don't do blocking sleep
sys.stderr.write('.')
sys.stderr.flush()
sys.stderr.write('\n')
if application.session:
application.session.end()
application.ended.wait()