Untitled

 avatar
unknown
javascript
2 years ago
2.5 kB
6
Indexable
var localTracks = {
  videoTrack: null,
  audioTrack: null
};

/*
 *  On initiation no users are connected.
 */
var remoteUsers = {};

/*
 *  On initiation. `client` is not attached to any project or channel for any specific user.
 */
var options = {
  appid: null,
  channel: null,
  uid: null,
  token: null
};
var currentStream = null;

async function join() {
  // Add an event listener to play remote tracks when remote user publishes.
  client.on("user-published", handleUserPublished);
  client.on("user-unpublished", handleUserUnpublished);

  // Default publish local microphone audio track to both options.
  localTracks.audioTrack = await AgoraRTC.createMicrophoneAudioTrack();
  if (currentStream == "camera") {
    // Join a channel and create local tracks. Best practice is to use Promise.all and run them concurrently.
    [options.uid, localTracks.videoTrack] = await Promise.all([
    // Join the channel.
    client.join(options.appid, options.channel, options.token || null, options.uid || null),
    // Create tracks to the localcamera.
    AgoraRTC.createCameraVideoTrack()]);

    // Publish the local video and audio tracks to the channel.

    $("#joined-setup").css("display", "flex");
  } else {
    var videoFromDiv = document.getElementById("sample-video");
    // https://developers.google.com/web/updates/2016/10/capture-stream - captureStream() 
    // can only be called after the video element is able to play video;
    try {
      videoFromDiv.play();
    } catch (e) {
      console.log(error);
    }
    //specify mozCaptureStream for Firefox.
    var videoStream = videoFromDiv.captureStream();
    AgoraRTC.createCustomAudioTrack({
      mediaStreamTrack: videoStream.getAudioTracks()[0]
    }),
    [options.uid, localTracks.videoTrack] = await Promise.all([
    // Join the channel.
    client.join(options.appid, options.channel, options.token || null, options.uid || null),
    
    // Create tracks to the customized video source.
    AgoraRTC.createCustomVideoTrack({
      mediaStreamTrack: videoStream.getVideoTracks()[0]
    })]);
    
    //await AgoraRTC.createMicrophoneAudioTrack();
  }
  await client.publish(Object.values(localTracks));
  //await AgoraRTC.createMicrophoneAudioTrack();
  // Play the local video track to the local browser and update the UI with the user ID.
  localTracks.videoTrack.play("local-player");
  $("#local-player-name").text(`localVideo(${options.uid})`);
  console.log("publish success");
}
Editor is loading...