Skip to content

Commit 4884143

Browse files
authored
Cleanup RTP/RTCP servers on hangup (#45)
After hanging up a call the RTP/RTCP servers created for that call should be shut down as they are no longer needed.
1 parent a60cf07 commit 4884143

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.5
4+
5+
- Cleanup RTP/RTCP servers on hangup
6+
37
## 0.3.4
48

59
- Add tag parameter to To header if missing

voip_utils/voip.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def __init__(
4141
self.valid_protocol_factory = valid_protocol_factory
4242
self.invalid_protocol_factory = invalid_protocol_factory
4343
self._tasks: Set[asyncio.Future[Any]] = set()
44+
self._rtp_transport: Optional[asyncio.BaseTransport] = None
45+
self._rtcp_transport: Optional[asyncio.BaseTransport] = None
4446

4547
def is_valid_call(self, call_info: CallInfo) -> bool:
4648
"""Filter calls."""
@@ -108,6 +110,18 @@ def on_call(self, call_info: CallInfo):
108110
# Tell caller to start sending/receiving RTP audio
109111
self.answer(call_info, rtp_port)
110112

113+
def on_hangup(self, call_info: CallInfo):
114+
"""Handle the end of a call."""
115+
_LOGGER.debug("Clean up RTP/RTCP resources on hangup")
116+
if self._rtcp_transport:
117+
_LOGGER.debug("Shutting down RTCP transport")
118+
self._rtcp_transport.close()
119+
self._rtcp_transport = None
120+
if self._rtp_transport:
121+
_LOGGER.debug("Shutting down RTP transport")
122+
self._rtp_transport.close()
123+
self._rtp_transport = None
124+
111125
async def _create_rtp_server(
112126
self,
113127
protocol_factory: CallProtocolFactory,
@@ -121,13 +135,13 @@ async def _create_rtp_server(
121135
loop = asyncio.get_running_loop()
122136

123137
# RTCP server
124-
await loop.create_datagram_endpoint(
138+
self._rtcp_transport, _ = await loop.create_datagram_endpoint(
125139
lambda: RtcpDatagramProtocol(rtcp_state),
126140
(rtp_ip, rtp_port + 1),
127141
)
128142

129143
# RTP server
130-
await loop.create_datagram_endpoint(
144+
self._rtp_transport, _ = await loop.create_datagram_endpoint(
131145
partial(protocol_factory, call_info, rtcp_state),
132146
(rtp_ip, rtp_port),
133147
)

0 commit comments

Comments
 (0)