EventTarget
Event handling of WebRTC objects.
The native WebRTC engine emits events on its own threads, which only schedule them. Handlers run on the asyncio event loop they were registered from.
- webrtc.utils.events.Handler
A handler, which is a function or a coroutine function called with the
webrtc.Eventof an eventalias of
Callable[[webrtc.Event],object]
- webrtc.utils.events.AnyHandler
Any handler, which takes the event class of its event, like
webrtc.RTCTrackEvent
- class webrtc.utils.events.HandlerDecorator(*args, **kwargs)
The decorator returned by
on()andonce()without a handler, which registers the decorated one.
- webrtc.utils.events.call_handler(loop, handler, event, *, message)
Calls a handler; exceptions, async ones too, go to the loopβs exception handler.
- class webrtc.utils.events.EventTarget
The base class of objects that emit events.
Handlers are registered with
on()oronce()by event name, from a running asyncio event loop, and are called on that loop with one event object. They can be functions or coroutine functions, and a coroutine is run as a task. An exception in a handler goes to the exception handler of the loop, and doesnβt stop the other handlers. A handler registered twice for an event is called once.See MDN: EventTarget.
Example:
@pc.on('icecandidate') async def on_candidate(event): await signaling.send(event.candidate) pc.on('track', lambda event: print(event.track))- off(name=None, handler=None)
Removes one handler or all of them, for one event or for all events.
See MDN: EventTarget/removeEventListener.
- Parameters:
- Raises:
ValueError β If the object has no such event.
- remove_listener(name, handler)
Removes a handler of an event. Removing one that isnβt registered does nothing.
See MDN: EventTarget/removeEventListener.
- Parameters:
- Raises:
ValueError β If the object has no such event.
- remove_all_listeners(name=None)
Removes every handler of an event, or of all events.
- Parameters:
name (
str, optional) β The name of the event. If omitted, every event.- Raises:
ValueError β If the object has no such event.
- class webrtc.utils.events.UniformEventTarget
An
EventTargetwhose events all have the same event class, which typed handlers take.- on(name: _N_contra, handler: None = None) HandlerDecorator[_E]
- on(name: _N_contra, handler: Callable[[_E], _R]) Callable[[_E], _R]
Registers a handler of an event. Can be used as a decorator.
See MDN: EventTarget/addEventListener.
- Parameters:
- Returns:
callableβ The handler, or a decorator registering it.- Raises:
ValueError β If the object has no such event.
RuntimeError β If called outside of a running event loop.
- Return type:
- once(name: _N_contra, handler: None = None) HandlerDecorator[_E]
- once(name: _N_contra, handler: Callable[[_E], _R]) Callable[[_E], _R]
Registers a handler that is removed before its first call. Can be used as a decorator.
See MDN: EventTarget/addEventListener.
- Parameters:
- Returns:
callableβ The handler, or a decorator registering it.- Raises:
ValueError β If the object has no such event.
RuntimeError β If called outside of a running event loop.
- Return type: