Skip to content

Commit

Permalink
Add datachannels test to aiortc snippet.
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Nov 10, 2020
1 parent a3b840f commit 817f536
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async def run(pc, player, session, bitrate=512000, record=False):
@pc.on('track')
def on_track(track):
logger.info(f'Track {track.kind} received')

@track.on('ended')
def on_ended():
print(f'Track {track.kind} ended')
Expand All @@ -199,6 +200,35 @@ def on_ice_state_change():
# attach to echotest plugin
plugin = await session.attach('janus.plugin.echotest')

# create data-channel
channel = pc.createDataChannel('JanusDataChannel')
logger.info(f'DataChannel ({channel.label}) created')
dc_probe_message = 'echo-ping'
dc_open = False
dc_probe_received = False

@channel.on('open')
def on_open():
nonlocal dc_open
dc_open = True
logger.info(f'DataChannel ({channel.label}) open')
logger.info(
f'DataChannel ({channel.label}) sending: {dc_probe_message}')
channel.send(dc_probe_message)

@channel.on('close')
def on_close():
nonlocal dc_open
dc_open = False
logger.info(f'DataChannel ({channel.label}) closed')

@channel.on('message')
def on_message(message):
logger.info(f'DataChannel ({channel.label}) received: {message}')
if dc_probe_message in message:
nonlocal dc_probe_received
dc_probe_received = True

# send offer
await pc.setLocalDescription(await pc.createOffer())
request = {'record': record, 'bitrate': bitrate}
Expand Down Expand Up @@ -246,6 +276,9 @@ def on_ice_state_change():
# Janus echoed the sent packets
assert rtp['audio']['out'] >= rtp['audio']['in'] > 0
assert rtp['video']['out'] >= rtp['video']['in'] > 0
# DataChannels worked
assert dc_open
assert dc_probe_received

logger.info('Ending the test now')

Expand Down

0 comments on commit 817f536

Please sign in to comment.