Skip to content

Commit f5e85ed

Browse files
authored
Add track publishing doc example (#1160)
1 parent 034ca3f commit f5e85ed

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
livekit: patch
3+
---
4+
5+
# Add track publishing doc example

livekit/src/room/participant/local_participant.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,77 @@ impl LocalParticipant {
281281
}
282282

283283
/// Publishes a media track.
284+
///
285+
/// # Examples
286+
///
287+
/// Publish an audio track:
288+
/// ```
289+
/// # use livekit::{
290+
/// # prelude::*,
291+
/// # options::TrackPublishOptions,
292+
/// # webrtc::{prelude::*, audio_source::native::NativeAudioSource}
293+
/// # };
294+
/// # async fn with_room(room: Room) -> RoomResult<()> {
295+
/// // 1. Define the audio source
296+
/// let source = NativeAudioSource::new(
297+
/// AudioSourceOptions::default(),
298+
/// 48_000, // Sample rate (hz)
299+
/// 1, // Number of channels
300+
/// 1000, // Buffer duration (ms)
301+
/// );
302+
///
303+
/// // 2. Create a track from the source
304+
/// let track = LocalAudioTrack::create_audio_track(
305+
/// "microphone", // Track name
306+
/// RtcAudioSource::Native(source.clone()),
307+
/// );
308+
///
309+
/// // 3. Publish the track in the room
310+
/// let options = TrackPublishOptions {
311+
/// source: TrackSource::Microphone,
312+
/// ..Default::default()
313+
/// };
314+
/// room.local_participant()
315+
/// .publish_track(LocalTrack::Audio(track), options)
316+
/// .await?;
317+
/// // Use the source to capture frames…
318+
/// # Ok(())
319+
/// # }
320+
/// ```
321+
///
322+
/// Publish a video track:
323+
/// ```
324+
/// # use livekit::{
325+
/// # prelude::*,
326+
/// # options::{TrackPublishOptions, VideoCodec},
327+
/// # webrtc::video_source::{RtcVideoSource, VideoResolution, native::NativeVideoSource}
328+
/// # };
329+
/// # async fn with_room(room: Room) -> RoomResult<()> {
330+
/// // 1. Define the video source
331+
/// let resolution = VideoResolution { width: 1920, height: 1080 };
332+
/// let source = NativeVideoSource::new(resolution, false);
333+
///
334+
/// // 2. Create a track from the source
335+
/// let track = LocalVideoTrack::create_video_track(
336+
/// "camera", // Track name
337+
/// RtcVideoSource::Native(source)
338+
/// );
339+
///
340+
/// // 3. Publish the track in the room
341+
/// let options = TrackPublishOptions {
342+
/// source: TrackSource::Camera,
343+
/// video_codec: VideoCodec::H264,
344+
/// simulcast: true, // Optionally enable simulcast for supported codecs
345+
/// ..Default::default()
346+
/// };
347+
/// room.local_participant()
348+
/// .publish_track(LocalTrack::Video(track), options)
349+
/// .await?;
350+
/// // Use the source to capture frames…
351+
/// # Ok(())
352+
/// # }
353+
/// ```
354+
///
284355
pub async fn publish_track(
285356
&self,
286357
track: LocalTrack,

0 commit comments

Comments
 (0)