Improving support for a worker thread and moving more code over to it
[theonering] / src / channel / call.py
1 import logging
2
3 import dbus
4 import telepathy
5
6 import tp
7 import util.go_utils as gobject_utils
8 import util.misc as misc_utils
9
10
11 _moduleLogger = logging.getLogger(__name__)
12
13
14 class CallChannel(
15                 tp.ChannelTypeStreamedMedia,
16                 tp.ChannelInterfaceGroup,
17                 tp.ChannelInterfaceCallState,
18                 tp.ChannelInterfaceHold,
19         ):
20
21         def __init__(self, connection, manager, props, contactHandle):
22                 self.__manager = manager
23                 self.__props = props
24                 self._delayedClose = gobject_utils.Timeout(self._on_close_requested)
25
26                 if telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle' in props:
27                         self._initiator = connection.get_handle_by_id(
28                                 telepathy.HANDLE_TYPE_CONTACT,
29                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
30                         )
31                 elif telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorID' in props:
32                         self._initiator = connection.get_handle_by_name(
33                                 telepathy.HANDLE_TYPE_CONTACT,
34                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
35                         )
36                 else:
37                         # Maemo 5 seems to require InitiatorHandle/InitiatorID to be set
38                         # even though I can't find them in the dbus spec.  I think its
39                         # generally safe to assume that its locally initiated if not
40                         # specified.  Specially for The One Ring, its always locally
41                         # initiated
42                         _moduleLogger.warning('InitiatorID or InitiatorHandle not set on new channel, assuming locally initiated')
43                         self._initiator = connection.GetSelfHandle()
44
45                 tp.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
46                 tp.ChannelInterfaceGroup.__init__(self)
47                 tp.ChannelInterfaceCallState.__init__(self)
48                 tp.ChannelInterfaceHold.__init__(self)
49                 self.__contactHandle = contactHandle
50                 self.__calledNumber = None
51
52                 self._implement_property_get(
53                         telepathy.interfaces.CHANNEL_INTERFACE,
54                         {
55                                 'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
56                                 'InitiatorID': lambda: self._initiator.name,
57                         },
58                 )
59                 self._add_immutables({
60                         'InitiatorHandle': telepathy.interfaces.CHANNEL_INTERFACE,
61                         'InitiatorID': telepathy.interfaces.CHANNEL_INTERFACE,
62                 })
63                 self._implement_property_get(
64                         telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
65                         {
66                                 'LocalPendingMembers': lambda: self.GetLocalPendingMembersWithInfo()
67                         },
68                 )
69                 self._implement_property_get(
70                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
71                         {
72                                 "InitialAudio": self.initial_audio,
73                                 "InitialVideo": self.initial_video,
74                         },
75                 )
76                 self._add_immutables({
77                         'InitialAudio': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
78                         'InitialVideo': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
79                 })
80
81                 self.GroupFlagsChanged(0, 0)
82                 added, removed = [self._conn.GetSelfHandle()], []
83                 localPending, remotePending = [], [contactHandle]
84                 self.MembersChanged(
85                         '', added, removed, localPending, remotePending,
86                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
87                 )
88
89         def initial_audio(self):
90                 return False
91
92         def initial_video(self):
93                 return False
94
95         @misc_utils.log_exception(_moduleLogger)
96         def Close(self):
97                 self.close()
98
99         def close(self):
100                 _moduleLogger.debug("Closing call")
101                 self._delayedClose.cancel()
102
103                 tp.ChannelTypeStreamedMedia.Close(self)
104                 self.remove_from_connection()
105
106                 if self.__calledNumber is not None:
107                         le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._cancel)
108                         le.start()
109
110         @misc_utils.log_exception(_moduleLogger)
111         def GetLocalPendingMembersWithInfo(self):
112                 info = dbus.Array([], signature="(uuus)")
113                 for member in self._local_pending:
114                         info.append((member, self._handle, 0, ''))
115                 return info
116
117         @misc_utils.log_exception(_moduleLogger)
118         def AddMembers(self, handles, message):
119                 _moduleLogger.info("Add members %r: %s" % (handles, message))
120                 for handle in handles:
121                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
122                                 _moduleLogger.info("Technically the user just accepted the call")
123
124         @misc_utils.log_exception(_moduleLogger)
125         def RemoveMembers(self, handles, message):
126                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
127
128         @misc_utils.log_exception(_moduleLogger)
129         def RemoveMembersWithReason(self, handles, message, reason):
130                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
131
132         @misc_utils.log_exception(_moduleLogger)
133         def ListStreams(self):
134                 """
135                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
136                 """
137                 return ()
138
139         @misc_utils.log_exception(_moduleLogger)
140         def RemoveStreams(self, streams):
141                 """
142                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
143                 """
144                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
145
146         @misc_utils.log_exception(_moduleLogger)
147         def RequestStreamDirection(self, stream, streamDirection):
148                 """
149                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
150
151                 @note Since streams are short lived, not bothering to implement this
152                 """
153                 _moduleLogger.info("A request was made to change the stream direction")
154                 raise telepathy.errors.NotImplemented("Cannot change directions")
155
156         @misc_utils.log_exception(_moduleLogger)
157         def RequestStreams(self, contactId, streamTypes):
158                 """
159                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
160
161                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
162                 """
163                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
164                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
165
166                 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
167                 le.start(contact)
168
169                 streamId = 0
170                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
171                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
172                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
173                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
174
175         @misc_utils.log_exception(_moduleLogger)
176         def _call(self, contact):
177                 contactNumber = contact.phoneNumber
178
179                 self.__calledNumber = contactNumber
180                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
181
182                 try:
183                         result = yield (
184                                 self._conn.session.backend.call,
185                                 (contactNumber, ),
186                                 {},
187                         )
188                 except Exception:
189                         _moduleLogger.exception(result)
190                         return
191
192                 self._delayedClose.start(seconds=0)
193                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
194
195         @misc_utils.log_exception(_moduleLogger)
196         def _cancel(self):
197                 _moduleLogger.debug("Cancelling call")
198                 try:
199                         result = yield (
200                                 self._conn.session.backend.cancel,
201                                 (self.__calledNumber, ),
202                                 {},
203                         )
204                 except Exception:
205                         _moduleLogger.exception(result)
206                         return
207
208         @misc_utils.log_exception(_moduleLogger)
209         def GetCallStates(self):
210                 """
211                 For org.freedesktop.Telepathy.Channel.Interface.CallState
212
213                 Get the current call states for all contacts involved in this call. 
214                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
215                 """
216                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
217
218         @misc_utils.log_exception(_moduleLogger)
219         def GetHoldState(self):
220                 """
221                 For org.freedesktop.Telepathy.Channel.Interface.Hold
222
223                 Get the current hold state
224                 @returns (HoldState, Reason)
225                 """
226                 return (
227                         telepathy.constants.LOCAL_HOLD_STATE_UNHELD,
228                         telepathy.constants.LOCAL_HOLD_STATE_REASON_NONE,
229                 )
230
231         @misc_utils.log_exception(_moduleLogger)
232         def RequestHold(self, Hold):
233                 """
234                 For org.freedesktop.Telepathy.Channel.Interface.Hold
235                 """
236                 if not Hold:
237                         return
238                 _moduleLogger.debug("Closing without cancel to get out of users way")
239                 self.__calledNumber = None
240                 self.close()
241
242         @misc_utils.log_exception(_moduleLogger)
243         def _on_close_requested(self, *args):
244                 _moduleLogger.debug("Cancel now disallowed")
245                 self.__calledNumber = None
246                 self.close()