Skip to content

Commit 4a76bac

Browse files
committed
fix: port fixes towards MVP
1 parent 467260e commit 4a76bac

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

src/cli.py

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
"""Register with PNP server and wait for remote peers to connect."""
2-
32
import argparse
43
import asyncio
54
import logging
65
import time
7-
6+
from .peerroom import PeerRoom
87
from aiortc import RTCIceCandidate, RTCPeerConnection, RTCSessionDescription
9-
from aiortc.contrib.signaling import add_signaling_arguments, create_signaling
8+
from .pnpsignaling import AmbianicPnpSignaling
9+
10+
log = logging.getLogger(__name__)
1011

1112

1213
def _server_register(channel, t, message):
13-
"""Register this peer with PNP server."""
14+
"""Register this peer with signaling server."""
1415
print('Registering this client with peer discovery server')
1516

1617

@@ -28,20 +29,22 @@ def _channel_send(channel, message):
2829
channel.send(message)
2930

3031

31-
def @ps.on("message")
32-
async def handle_signal():
33-
if isinstance(obj, RTCSessionDescription):
34-
await pc.setRemoteDescription(obj)
35-
if obj.type == "offer":
36-
# send answer
37-
await pc.setLocalDescription(await pc.createAnswer())
38-
await signaling.send(pc.localDescription)
39-
elif isinstance(obj, RTCIceCandidate):
40-
pc.addIceCandidate(obj)
41-
elif obj is None:
42-
print("Exiting")
43-
break
32+
async def _consume_signaling(pc, signaling):
33+
while True:
34+
obj = await signaling.receive()
35+
36+
if isinstance(obj, RTCSessionDescription):
37+
await pc.setRemoteDescription(obj)
4438

39+
if obj.type == "offer":
40+
# send answer
41+
await pc.setLocalDescription(await pc.createAnswer())
42+
await signaling.send(pc.localDescription)
43+
elif isinstance(obj, RTCIceCandidate):
44+
pc.addIceCandidate(obj)
45+
elif obj is None:
46+
print("Exiting")
47+
break
4548

4649
time_start = None
4750

@@ -73,10 +76,12 @@ async def _run_offer(pc, signaling):
7376
await signaling.connect()
7477
channel = pc.createDataChannel("chat")
7578
_channel_log(channel, "-", "created by local party")
79+
7680
async def send_pings():
7781
while True:
7882
_channel_send(channel, "ping %d" % _current_stamp())
7983
await asyncio.sleep(1)
84+
8085
@channel.on("open")
8186
def on_open():
8287
asyncio.ensure_future(send_pings())
@@ -93,39 +98,39 @@ def on_message(message):
9398
await signaling.send(pc.localDescription)
9499
await _consume_signaling(pc, signaling)
95100

96-
async function discoverRemotePeerId ({ peer, state, commit }) {
97-
if (!state.remotePeerId) {
98-
// first try to find the remote peer ID in the same room
99-
const myRoom = new PeerRoom(peer)
100-
console.log('Fetching room members', myRoom)
101-
const peerIds = [] // await myRoom.getRoomMembers()
102-
console.log('myRoom members', peerIds)
103-
const remotePeerId = peerIds.find(
104-
pid => pid !== state.myPeerId)
105-
if (remotePeerId) {
106-
return remotePeerId
107-
} else {
108-
// unable to auto discover
109-
// ask user for help
110-
commit(USER_MESSAGE,
111-
`Still looking.
112-
Please make sure you are are on the same local network.
113-
`)
114-
}
115-
} else {
116-
return state.remotePeerId
117-
}
118-
}
101+
myPeerId = None
102+
remotePeerId = None
103+
104+
105+
async def discoverRemotePeerId(peer=None):
106+
"""Try to find a remote peer in the same local room."""
107+
global remotePeerId
108+
if not remotePeerId:
109+
# first try to find the remote peer ID in the same room
110+
myRoom = PeerRoom(peer)
111+
log.debug('Fetching room members', myRoom)
112+
peerIds = await myRoom.getRoomMembers()
113+
log.debug('myRoom members', peerIds)
114+
try:
115+
remotePeerId = [pid for pid in peerIds if pid != myPeerId][0]
116+
return remotePeerId
117+
except IndexError:
118+
# no other peers in room
119+
return None
120+
else:
121+
return remotePeerId
119122

120123
if __name__ == "__main__":
124+
args = None
121125
parser = argparse.ArgumentParser(description="Data channels ping/pong")
122126
parser.add_argument("role", choices=["offer", "answer"])
123127
parser.add_argument("--verbose", "-v", action="count")
124-
add_signaling_arguments(parser)
128+
# add_signaling_arguments(parser)
125129
args = parser.parse_args()
126130
if args.verbose:
127131
logging.basicConfig(level=logging.DEBUG)
128-
signaling = create_signaling(args)
132+
# signaling = create_signaling(args)
133+
signaling = AmbianicPnpSignaling(args)
129134
pc = RTCPeerConnection()
130135
if args.role == "offer":
131136
coro = _run_offer(pc, signaling)

src/peer-signaling.py renamed to src/pnpsignaling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from aiortc import RTCIceCandidate, RTCSessionDescription
99
from aiortc.sdp import candidate_from_sdp, candidate_to_sdp
1010

11-
logger = logging.getLogger("aiortc.contrib.signaling")
11+
log = logging.getLogger(__name__)
1212

1313

14-
class PeerSignaling(AsyncIOEventEmitter):
14+
class AmbianicPnpSignaling(AsyncIOEventEmitter):
1515
"""Manage signaling between peer and signaling server."""
1616

1717
def __init__(self, room):

0 commit comments

Comments
 (0)