MediaStreamTrack

MediaStreamTrack of Media Capture and Streams, and the constraints of its synthetic sources.

webrtc.interfaces.media_stream_track.CAMERA_DEVICE_ID = 'synthetic-camera'

The device ID of the synthetic camera, which feeds the video tracks of webrtc.MediaDevices.get_user_media()

webrtc.interfaces.media_stream_track.MICROPHONE_DEVICE_ID = 'synthetic-microphone'

The device ID of the synthetic microphone, which feeds the audio tracks of webrtc.MediaDevices.get_user_media()

class webrtc.interfaces.media_stream_track.MediaStreamTrack(native_obj=None)

A single audio or video track. It comes from a synthetic device or a generator, or from a remote peer.

See MDN: MediaStreamTrack.

Events:
  • mute and unmute (webrtc.Event) – The value of muted changed. A remote track is muted until media arrives, and again when it’s no longer negotiated.

  • ended (webrtc.Event) – The track ended for a reason other than stop(), for example because the remote peer stopped sending it.

A live track with handlers stays alive until it ends or its event loop closes.

Events

Handlers are registered with on().

property enabled: bool

Whether the track carries its source. A disabled track carries silence or black frames.

It can still be set after the track has ended, but that has no effect.

See MDN: MediaStreamTrack/enabled.

Type:

bool

property id: str

The ID of the track. A remote track gets a random UUID and doesn’t use the ID its peer gave.

See MDN: MediaStreamTrack/id.

Type:

str

property label: str

The label of the source. It’s empty for a generated track.

A track of webrtc.MediaDevices.get_user_media() has the label of its device, and a remote track has 'remote audio' or 'remote video'. A clone keeps the label.

See MDN: MediaStreamTrack/label.

Type:

str

property kind: webrtc.MediaType

Whether the track carries audio or video. It stays set after the track ends.

See MDN: MediaStreamTrack/kind.

Type:

webrtc.MediaType

property ready_state: webrtc.MediaStreamTrackState

Whether the track is live or has ended. Ending is final.

See MDN: MediaStreamTrack/readyState.

Type:

webrtc.MediaStreamTrackState

property muted: bool

Whether the source can’t provide media for now. A remote track is muted until media arrives.

Unlike enabled, the application can’t change it.

See MDN: MediaStreamTrack/muted.

Type:

bool

property content_hint: str

What the track carries, so encoders can optimize for it. The default is empty, for unknown.

Audio accepts 'speech', 'speaking' and 'music'. Video accepts 'motion', 'detail' and 'text'. Other values are ignored, as are the values meant for the other kind.

See MDN: MediaStreamTrack/contentHint.

Type:

str

get_settings()

Returns what the track carries now.

The settings hold the size and frame rate of the last frames seen and the format of the audio. Tracks of webrtc.MediaDevices.get_user_media() also report the device, and its format before media flows. Members that aren’t known yet are None.

See MDN: MediaStreamTrack/getSettings.

Returns:

webrtc.MediaTrackSettings – The settings.

Return type:

MediaTrackSettings

get_capabilities()

Returns what the source of the track can do.

Only the synthetic camera and microphone of webrtc.MediaDevices.get_user_media() have capabilities. For remote and generated tracks every member is None.

See MDN: MediaStreamTrack/getCapabilities.

Returns:

webrtc.MediaTrackCapabilities – The capabilities.

Return type:

MediaTrackCapabilities

get_constraints()

Returns the last constraints of apply_constraints() or get_user_media().

See MDN: MediaStreamTrack/getConstraints.

Returns:

webrtc.MediaTrackConstraints – The constraints, none by default.

Return type:

MediaTrackConstraints

apply_constraints(constraints=None)

Applies constraints to the track, checking them against get_capabilities() or the current settings.

Only the synthetic camera of webrtc.MediaDevices.get_user_media() reacts, by changing its size and frame rate. The sources of other tracks stay as they are, and the constraints are only checked and kept. On an ended track, only the numbers are checked and the constraints aren’t kept.

See MDN: MediaStreamTrack/applyConstraints.

Parameters:

constraints (webrtc.MediaTrackConstraints, optional) – The constraints, none to remove them.

Returns:

asyncio.Future – Done once applied. It fails with webrtc.OverconstrainedError if a required constraint can’t be satisfied, or with TypeError if a double isn’t finite. A failure leaves the track as it was.

Return type:

Future[None]

clone()

Returns a new track with a new id, sharing the source of this one.

The clone keeps label, enabled, content_hint, the constraints and the device, and ends if this does.

See MDN: MediaStreamTrack/clone.

Returns:

webrtc.MediaStreamTrack – The clone.

Return type:

webrtc.MediaStreamTrack

stop()

Stops the track and detaches it from its source. ready_state becomes ended with no event.

Its sender sends silence or a black frame a second; clones keep their media. See MDN: MediaStreamTrack/stop.