Beginner Friendly WEB RTC Tutorial
Event Handlers, Methods and Properties of the MediaStream API
The MediaStream API in WebRTC provides a way to access and manipulate streams of audio and video data. Most online streaming applications that you probably use have the ability to enable or disable video or audio. This can be achieved by manipulating the MediaStream API. MediaStream objects represent streams of media content, and they are commonly used in the context of real-time communication applications such as video conferencing. Here are some key properties, methods, and event handlers associated with the MediaStream API that will be used later in this tutorial:
MediaStream Properties
- MediaStream.id
- Description: Represents a unique identifier for the stream.
- Type: DOMString.
- MediaStream.active
- Description: Indicates whether the stream is currently active.
- Type: Boolean.
- MediaStream.onaddtrack
- Description: An event handler that is called when a new track is added to the stream.
- Type: EventHandler.
- Mediastream.onremovetrack
- Description: An event handler that is called when a track is removed from the stream.
- Type: EventHandler.
MediaStream Methods
- MediaStream.getAudioTracks()
- Description: Returns an array of all audio tracks in the stream.
- Return Type: Array of MediaStreamTrack.
- MediaStream.getVideoTracks()
- Description: Returns an array of all video tracks in the stream.
- Return Type: Array of MediaStreamTrack.
- MediaStream.getTracks()
- Description: Returns an array of all tracks (both audio and video) in the stream.
- Return Type: Array of MediaStreamTrack.
- Mediastream.getTrackById(trackid)
- Description: Returns the track with the specified ID.
- Parameters: `trackId` (DOMString) - The ID of the track to retrieve.
- Return Type: MediaStreamTrack or null if not found.
- MediaStream.addTrack(track)
- Description: Adds a new track to the stream.
- Parameters: `track` (MediaStreamTrack) - The track to add.
- MediaStream.removeTrack(track)
- Description: Removes the specified track from the stream.
- Parameters: `track` (MediaStreamTrack) - The track to remove.
MediaStream Event Handlers
- MediaStream.onactive
- Description: An event handler that is called when the stream becomes active.
- MediaStream.oninactive
- Description: An event handler that is called when the stream becomes inactive.
- MediaStream.onended
- Description: An event handler that is called when the stream ends (no more tracks).
These properties, methods, and event handlers allow developers to interact with and manage media streams in WebRTC applications. They provide the necessary tools to work with audio and video tracks, add or remove tracks dynamically, and respond to changes in the stream's state.