publishAudioTrack method

Future<LocalTrackPublication<LocalAudioTrack>> publishAudioTrack(
  1. LocalAudioTrack track,
  2. {AudioPublishOptions? publishOptions}
)

Publish an AudioTrack to the Room. For most cases, using setMicrophoneEnabled would be simpler and recommended.

Implementation

Future<LocalTrackPublication<LocalAudioTrack>> publishAudioTrack(
  LocalAudioTrack track, {
  AudioPublishOptions? publishOptions,
}) async {
  if (audioTracks.any(
      (e) => e.track?.mediaStreamTrack.id == track.mediaStreamTrack.id)) {
    throw TrackPublishException('track already exists');
  }

  // Use defaultPublishOptions if options is null
  publishOptions =
      publishOptions ?? room.roomOptions.defaultAudioPublishOptions;

  final trackInfo = await room.engine.addTrack(
    cid: track.getCid(),
    name: publishOptions.name ?? AudioPublishOptions.defaultMicrophoneName,
    kind: track.kind,
    source: track.source.toPBType(),
    dtx: publishOptions.dtx,
  );

  await track.start();

  final transceiverInit = rtc.RTCRtpTransceiverInit(
    direction: rtc.TransceiverDirection.SendOnly,
    sendEncodings: [
      if (publishOptions.audioBitrate > 0)
        rtc.RTCRtpEncoding(maxBitrate: publishOptions.audioBitrate),
    ],
  );
  // addTransceiver cannot pass in a kind parameter due to a bug in flutter-webrtc (web)
  track.transceiver = await room.engine.publisher?.pc.addTransceiver(
    track: track.mediaStreamTrack,
    kind: rtc.RTCRtpMediaType.RTCRtpMediaTypeAudio,
    init: transceiverInit,
  );

  await room.engine.negotiate();

  final pub = LocalTrackPublication<LocalAudioTrack>(
    participant: this,
    info: trackInfo,
    track: track,
  );
  addTrackPublication(pub);

  // did publish
  await track.onPublish();
  await room.applyAudioSpeakerSettings();

  [events, room.events].emit(LocalTrackPublishedEvent(
    participant: this,
    publication: pub,
  ));

  return pub;
}