Helpers

These classes are not part of the WebRTC or ORTC API, but provide higher-level helpers for tasks like manipulating media streams.

Media sources

class aiortc.contrib.media.MediaPlayer(file, format=None, options=None, timeout=None, loop=False, decode=True)

A media source that reads audio and/or video from a file.

Examples:

# Open a video file.
player = MediaPlayer('/path/to/some.mp4')

# Open an HTTP stream.
player = MediaPlayer(
    'http://download.tsi.telecom-paristech.fr/'
    'gpac/dataset/dash/uhd/mux_sources/hevcds_720p30_2M.mp4')

# Open webcam on Linux.
player = MediaPlayer('/dev/video0', format='v4l2', options={
    'video_size': '640x480'
})

# Open webcam on OS X.
player = MediaPlayer('default:none', format='avfoundation', options={
    'video_size': '640x480'
})

# Open webcam on Windows.
player = MediaPlayer('video=Integrated Camera', format='dshow', options={
    'video_size': '640x480'
})
Parameters:
  • file – The path to a file, or a file-like object.

  • format – The format to use, defaults to autodect.

  • options – Additional options to pass to FFmpeg.

  • timeout – Open/read timeout to pass to FFmpeg.

  • loop – Whether to repeat playback indefinitely (requires a seekable file).

property audio: MediaStreamTrack

A aiortc.MediaStreamTrack instance if the file contains audio.

property video: MediaStreamTrack

A aiortc.MediaStreamTrack instance if the file contains video.

Media sinks

class aiortc.contrib.media.MediaRecorder(file, format=None, options=None)

A media sink that writes audio and/or video to a file.

Examples:

# Write to a video file.
player = MediaRecorder('/path/to/file.mp4')

# Write to a set of images.
player = MediaRecorder('/path/to/file-%3d.png')
Parameters:
  • file – The path to a file, or a file-like object.

  • format – The format to use, defaults to autodect.

  • options – Additional options to pass to FFmpeg.

addTrack(track)

Add a track to be recorded.

Parameters:

track – A aiortc.MediaStreamTrack.

await start()

Start recording.

await stop()

Stop recording.

class aiortc.contrib.media.MediaBlackhole

A media sink that consumes and discards all media.

addTrack(track)

Add a track whose media should be discarded.

Parameters:

track – A aiortc.MediaStreamTrack.

await start()

Start discarding media.

await stop()

Stop discarding media.

Media transforms

class aiortc.contrib.media.MediaRelay

A media source that relays one or more tracks to multiple consumers.

This is especially useful for live tracks such as webcams or media received over the network.

subscribe(track, buffered=True)

Create a proxy around the given track for a new consumer.

Parameters:
  • track (MediaStreamTrack) – Source MediaStreamTrack which is relayed.

  • buffered (bool) – Whether there need a buffer between the source track and relayed track.

Return type:

class:

MediaStreamTrack