This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
sendrecv.py: poll bus with asyncio #34
Open
MathieuDuponchelle
wants to merge
1
commit into
centricular:master
Choose a base branch
from
MathieuDuponchelle:python-bus-poll
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ def __init__(self, id_, peer_id, server): | |
self.id_ = id_ | ||
self.conn = None | ||
self.pipe = None | ||
self.bus = None | ||
self.webrtc = None | ||
self.peer_id = peer_id | ||
self.server = server or 'wss://webrtc.nirbheek.in:8443' | ||
|
@@ -105,6 +106,25 @@ def on_incoming_stream(self, _, pad): | |
decodebin.sync_state_with_parent() | ||
self.webrtc.link(decodebin) | ||
|
||
def poll_cb(self): | ||
done = False | ||
while self.bus.peek() != None: | ||
msg = self.bus.pop() | ||
|
||
if msg.type == Gst.MessageType.ERROR: | ||
err = msg.parse_error() | ||
print ("ERROR!!") | ||
print (err.gerror, err.debug) | ||
done = True | ||
elif msg == Gst.MessageType.EOS: | ||
done = True | ||
|
||
if done: | ||
pollfd = self.bus.get_pollfd() | ||
asyncio.get_event_loop().remove_reader(pollfd.fd) | ||
asyncio.ensure_future(self.conn.close()) | ||
break | ||
|
||
def start_pipeline(self): | ||
self.pipe = Gst.parse_launch(PIPELINE_DESC) | ||
self.webrtc = self.pipe.get_by_name('sendrecv') | ||
|
@@ -113,7 +133,11 @@ def start_pipeline(self): | |
self.webrtc.connect('pad-added', self.on_incoming_stream) | ||
self.pipe.set_state(Gst.State.PLAYING) | ||
|
||
async def handle_sdp(self, message): | ||
self.bus = self.pipe.get_bus() | ||
pollfd = self.bus.get_pollfd() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second user of the new API I added in 1.14 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I was pretty happy to see this had just been added, however I had to fix an annotation issue right after 1.14.2 was merged, which reminds me this will break unless master is used :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could put this in a try except block for now? |
||
asyncio.get_event_loop().add_reader(pollfd.fd, self.poll_cb) | ||
|
||
async def handle_json(self, message): | ||
assert (self.webrtc) | ||
msg = json.loads(message) | ||
if 'sdp' in msg: | ||
|
@@ -144,7 +168,7 @@ async def loop(self): | |
print (message) | ||
return 1 | ||
else: | ||
await self.handle_sdp(message) | ||
await self.handle_json(message) | ||
return 0 | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can directly pop or not? Without peek first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that I could, as poll_cb will get called again immediately if all messages haven't been read, not sure what's best?