Track generators

Tracks of media the application writes, with VideoTrackGenerator and the non-standard MediaStreamTrackGenerator.

class webrtc.interfaces.track_generator.VideoTrackGenerator

A video track of the frames the application writes to a stream.

The visible rect of each webrtc.VideoFrame written is sent on track, and the frame is closed. Writing anything else or a closed frame errors writable. Closing or aborting writable ends the track.

See MDN: VideoTrackGenerator.

Example:

generator = webrtc.VideoTrackGenerator()
pc.add_track(generator.track)
writer = generator.writable.get_writer()
init = webrtc.VideoFrameBufferInit(format='I420', coded_width=640, coded_height=480, timestamp=0)
await writer.write(webrtc.VideoFrame(i420, init))
property track: MediaStreamTrack

The video track carrying the frames written.

See MDN: VideoTrackGenerator/track.

Type:

webrtc.MediaStreamTrack

property writable: WritableStream

The stream to write webrtc.VideoFrame objects to.

See MDN: VideoTrackGenerator/writable.

Type:

webrtc.WritableStream

property muted: bool

Whether written frames are dropped. track stays muted while itโ€™s True.

It starts as False.

See MDN: VideoTrackGenerator/muted.

Type:

bool

class webrtc.interfaces.track_generator.MediaStreamTrackGeneratorInit(kind)

The options of a MediaStreamTrackGenerator.

Parameters:

kind (webrtc.MediaType) โ€“ The kind of the track, 'audio' or 'video'.

class webrtc.interfaces.track_generator.MediaStreamTrackGenerator(kind)

A track of the webrtc.VideoFrame or webrtc.AudioData objects the application writes to a stream.

Itโ€™s non-standard, but itโ€™s the only way to generate audio. For video, VideoTrackGenerator is the standard choice. Unlike VideoTrackGenerator, this generator is the track itself.

Each object written is sent and closed. Audio is converted to 16-bit samples and sent in 10 ms frames. Samples that donโ€™t fill a frame wait for the next write. Writing the wrong kind of object or a closed one errors writable. So does a sample rate the library canโ€™t send, with webrtc.NotSupportedError. Closing or aborting writable ends the track.

See MDN: MediaStreamTrackGenerator.

Parameters:

kind (webrtc.MediaType, str or MediaStreamTrackGeneratorInit) โ€“ 'audio' or 'video', or an init that holds it.

Raises:

TypeError โ€“ If the kind isnโ€™t audio or video.

Events

Handlers are registered with on().

property writable: WritableStream

The stream to write webrtc.VideoFrame or webrtc.AudioData to.

See MDN: MediaStreamTrackGenerator/writable.

Type:

webrtc.WritableStream