Skip to content

Commit

Permalink
add more add_track tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX committed Mar 4, 2022
1 parent 3adaf5d commit de32355
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Contributions of any sizes are welcome.
### Special thanks to

- [Authors](https://github.com/node-webrtc/node-webrtc/blob/develop/AUTHORS) of [node-webrtc](https://github.com/node-webrtc/node-webrtc).
- Authors of [web-platform-tests](https://github.com/web-platform-tests/wpt).

### License

Expand Down
14 changes: 8 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
import webrtc


# alias for rtc_peer_connection
@pytest.fixture
def pc(rtc_peer_connection):
return rtc_peer_connection


@pytest.fixture
def rtc_peer_connection(request):
pc = webrtc.RTCPeerConnection()
Expand All @@ -28,6 +22,10 @@ def close_pc():
return pc


# aliases
pc = caller = callee = rtc_peer_connection


def get_stream(constraints, request):
# TODO pass constraints when cpp part will be ready
stream = webrtc.get_user_media()
Expand All @@ -46,6 +44,10 @@ def audio_stream(request):
return get_stream(None, request)


# alias
audio_stream2 = audio_stream


@pytest.fixture
def video_stream(request):
return get_stream(None, request)
169 changes: 149 additions & 20 deletions tests/rtc_peer_connection/test_add_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,180 @@
import webrtc


def test_on_closed_pc(pc, audio_stream):
def test_1(pc, audio_stream):
"""addTrack when pc is closed should throw PythonWebRTCException with invalid state"""
track, *_ = audio_stream.get_audio_tracks()
pc.close()

with pytest.raises(webrtc.PythonWebRTCException):
# invalid pc state
pc.add_track(track, audio_stream)


def test_sender_creation(pc, audio_stream):
track, *_ = audio_stream.get_audio_tracks()
def test_2(pc, audio_stream):
"""add_track with single track argument and no stream should succeed"""
track, *_ = audio_stream.get_tracks()

sender = pc.add_track(track)

assert isinstance(sender, webrtc.RTCRtpSender), 'Expect sender to be instance of RTCRtpSender'

assert track == sender.track, 'Expect sender\'s track to be the added track'

transceivers = pc.get_transceivers()
assert len(transceivers) == 1, 'Expect only one transceiver with sender added'

transceiver, *_ = transceivers
assert transceiver.sender == sender, 'Expect only one sender with given track added'

assert [sender] == pc.get_senders()

receiver = transceiver.receiver
assert receiver.track.kind == webrtc.MediaType.audio

assert [receiver] == pc.get_receivers(), 'Expect only one receiver associated with transceiver added'


def test_3(pc, audio_stream):
"""add_track with single track argument and single stream should succeed"""
track, *_ = audio_stream.get_tracks()

sender = pc.add_track(track, audio_stream)

assert isinstance(sender, webrtc.RTCRtpSender)
assert isinstance(sender, webrtc.RTCRtpSender), 'Expect sender to be instance of RTCRtpSender'

assert sender.track == track
assert sender.track == track, 'Expect sender\'s track to be the added track'


def test_adding_of_the_same_track(pc, audio_stream):
track, *_ = audio_stream.get_audio_tracks()
def test_4(pc, audio_stream):
"""add_track with single track argument and multiple streams should succeed"""
track, *_ = audio_stream.get_tracks()

# FIXME
return

stream2 = webrtc.MediaStream(track)
sender = pc.add_track(track, [audio_stream, stream2])

assert isinstance(sender, webrtc.RTCRtpSender), 'Expect sender to be instance of RTCRtpSender'

assert sender.track == track, 'Expect sender\'s track to be the added track'


def test_5(pc, audio_stream):
"""Adding the same track multiple times should throw RTCException"""
track, *_ = audio_stream.get_tracks()

pc.add_track(track, audio_stream)

with pytest.raises(webrtc.RTCException):
pc.add_track(track, audio_stream)


def test_sender_and_transceivers(pc, audio_stream):
track, *_ = audio_stream.get_audio_tracks()
def test_6(pc, audio_stream):
"""add_track with existing sender with None track, same kind, and recvonly direction should reuse sender"""
init = webrtc.RtpTransceiverInit(direction=webrtc.TransceiverDirection.recvonly)
transceiver = pc.add_transceiver(webrtc.MediaType.audio, init)

sender = pc.add_track(track, audio_stream)
assert transceiver.sender.track is None
assert transceiver.direction == webrtc.TransceiverDirection.recvonly

assert isinstance(sender, webrtc.RTCRtpSender)
track, *_ = audio_stream.get_tracks()
sender = pc.add_track(track)

assert track == sender.track
assert sender == transceiver.sender
assert sender.track == track
assert transceiver.direction == webrtc.TransceiverDirection.sendrecv
assert [sender] == pc.get_senders()

transceivers = pc.get_transceivers()
assert len(transceivers) == 1

transceiver, *_ = transceivers
assert transceiver.sender == sender
def test_7(pc, audio_stream):
"""add_track with existing sender that has not been used to send should reuse the sender"""
transceiver = pc.add_transceiver(webrtc.MediaType.audio)
assert transceiver.sender.track is None
assert transceiver.direction == webrtc.TransceiverDirection.sendrecv

assert [sender] == pc.get_senders()
track, *_ = audio_stream.get_tracks()
sender = pc.add_track(track)

receiver = transceiver.receiver
assert receiver.track.kind == webrtc.MediaType.audio
assert sender.track == track
assert sender == transceiver.sender


@pytest.mark.asyncio
async def test_8(caller, callee, audio_stream):
"""add_track with existing sender that has been used to send should create new sender"""
track, *_ = audio_stream.get_tracks()
transceiver = caller.add_transceiver(track)

offer = await caller.create_offer()
await caller.set_local_description(offer)
await callee.set_remote_description(offer)
answer = await callee.create_answer()
await callee.set_local_description(answer)
await caller.set_remote_description(answer)

assert transceiver.current_direction == webrtc.TransceiverDirection.sendonly

# TODO when remove track method will be ready
return
caller.remove_track(transceiver.sender)

# .... and more


def test_9(pc, audio_stream):
"""add_track with existing sender with null track, different kind,
and recvonly direction should create new sender"""
init = webrtc.RtpTransceiverInit(direction=webrtc.TransceiverDirection.recvonly)
transceiver = pc.add_transceiver(webrtc.MediaType.video, init)

assert transceiver.sender.track is None
assert transceiver.direction == webrtc.TransceiverDirection.recvonly

track, *_ = audio_stream.get_tracks()
sender = pc.add_track(track)

assert sender.track == track
assert sender != transceiver.sender

senders = pc.get_senders()

assert len(senders) == 2, 'Expect 2 senders added to connection'
assert sender in senders, 'Expect senders list to include sender'
assert transceiver.sender in senders, 'Expect senders list to include first transceiver\'s sender'


@pytest.mark.asyncio
async def test_10(caller, callee, audio_stream, audio_stream2):
"""Adding more tracks does not generate more candidates if bundled"""
track, *_ = audio_stream.get_tracks()
transceiver = caller.add_transceiver(track)

offer = await caller.create_offer()
await caller.set_local_description(offer)
await callee.set_remote_description(offer)
answer = await callee.create_answer()
await callee.set_local_description(answer)
await caller.set_remote_description(answer)

assert transceiver.current_direction == webrtc.TransceiverDirection.sendonly

# TODO need to wait for icegatheringstatechange with complete event!

second_track, *_ = audio_stream2.get_tracks()

# TODO onicecandidate event should not be occurred

caller.add_track(second_track)

offer = await caller.create_offer()
await caller.set_local_description(offer)
await callee.set_remote_description(offer)
answer = await callee.create_answer()
await callee.set_local_description(answer)
await caller.set_remote_description(answer)

assert [receiver] == pc.get_receivers()
first_transceiver, second_transceiver, *_ = caller.get_transceivers()
assert first_transceiver.receiver.transport == second_transceiver.receiver.transport
assert first_transceiver.sender.transport == second_transceiver.sender.transport

0 comments on commit de32355

Please sign in to comment.