WEBRTC前端播放 播放器组件封装

news2025/1/8 17:28:26

组件封装

<template>
  <div>
    <div class="option">
      <input v-model="useStun" type="checkbox" />
      <label for="use-stun">Use STUN server</label>
    </div>
    <button @click="startPlay">Start</button>
    <form @submit.prevent="sendMessage">
      <div>
        <p>input text</p>
        <textarea
          v-model="message"
          cols="2"
          rows="3"
          class="form-control"
          style="width: 600px; height: 50px"
        ></textarea>
      </div>
      <button type="submit">Send</button>
    </form>

    <div id="media">
      <h2>Media</h2>
      <video
        ref="rtcMediaPlayer"
        style="width: 600px"
        controls
        autoplay
      ></video>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { SrsRtcWhipWhepAsync } from "@/utils/srs.sdk"; // 确保路径正确

const useStun = ref(false);
const message = ref("");
const rtcMediaPlayer = ref(null);
let sdk = null;

const startPlay = async () => {
  rtcMediaPlayer.value.style.display = "block";

  if (sdk) {
    sdk.close();
  }

  sdk = new SrsRtcWhipWhepAsync();
  console.log(" sdk.stream", sdk.stream);
  rtcMediaPlayer.value.srcObject = sdk.stream;
  // sdk.stream
  //   .getTracks()
  //   .forEach((track) => rtcMediaPlayer.value.srcObject.addTrack(track));

  const host = window.location.hostname;
  const url = `http://10.3.208.9:1985/rtc/v1/whep/?app=live&stream=livestream`;

  try {
    await sdk.play(url);
  } catch (reason) {
    sdk.close();
    rtcMediaPlayer.value.style.display = "none";
    console.error(reason);
  }
};

const sendMessage = async () => {
  const response = await fetch("/human", {
    body: JSON.stringify({
      text: message.value,
      type: "echo",
    }),
    headers: {
      "Content-Type": "application/json",
    },
    method: "POST",
  });

  message.value = "";
  console.log("Message sent:", await response.json());
};

onMounted(() => {
  rtcMediaPlayer.value.style.display = "none";
});
</script>

<style scoped>
button {
  padding: 8px 16px;
}

video {
  width: 100%;
}

.option {
  margin-bottom: 8px;
}

#media {
  max-width: 1280px;
}
</style>

srs.sdk.js文件

//
// Copyright (c) 2013-2021 Winlin
//
// SPDX-License-Identifier: MIT
//

"use strict";

function SrsError(name, message) {
  this.name = name;
  this.message = message;
  this.stack = new Error().stack;
}
SrsError.prototype = Object.create(Error.prototype);
SrsError.prototype.constructor = SrsError;

// Depends on adapter-7.4.0.min.js from https://github.com/webrtc/adapter
// Async-awat-prmise based SRS RTC Publisher.
function SrsRtcPublisherAsync() {
  var self = {};

  // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
  self.constraints = {
    audio: true,
    video: {
      width: { ideal: 320, max: 576 },
    },
  };

  // @see https://github.com/rtcdn/rtcdn-draft
  // @url The WebRTC url to play with, for example:
  //      webrtc://r.ossrs.net/live/livestream
  // or specifies the API port:
  //      webrtc://r.ossrs.net:11985/live/livestream
  // or autostart the publish:
  //      webrtc://r.ossrs.net/live/livestream?autostart=true
  // or change the app from live to myapp:
  //      webrtc://r.ossrs.net:11985/myapp/livestream
  // or change the stream from livestream to mystream:
  //      webrtc://r.ossrs.net:11985/live/mystream
  // or set the api server to myapi.domain.com:
  //      webrtc://myapi.domain.com/live/livestream
  // or set the candidate(eip) of answer:
  //      webrtc://r.ossrs.net/live/livestream?candidate=39.107.238.185
  // or force to access https API:
  //      webrtc://r.ossrs.net/live/livestream?schema=https
  // or use plaintext, without SRTP:
  //      webrtc://r.ossrs.net/live/livestream?encrypt=false
  // or any other information, will pass-by in the query:
  //      webrtc://r.ossrs.net/live/livestream?vhost=xxx
  //      webrtc://r.ossrs.net/live/livestream?token=xxx
  self.publish = async function (url) {
    var conf = self.__internal.prepareUrl(url);
    self.pc.addTransceiver("audio", { direction: "sendonly" });
    self.pc.addTransceiver("video", { direction: "sendonly" });
    //self.pc.addTransceiver("video", {direction: "sendonly"});
    //self.pc.addTransceiver("audio", {direction: "sendonly"});

    if (
      !navigator.mediaDevices &&
      window.location.protocol === "http:" &&
      window.location.hostname !== "localhost"
    ) {
      throw new SrsError(
        "HttpsRequiredError",
        `Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`
      );
    }
    var stream = await navigator.mediaDevices.getUserMedia(self.constraints);

    // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
    stream.getTracks().forEach(function (track) {
      self.pc.addTrack(track);

      // Notify about local track when stream is ok.
      self.ontrack && self.ontrack({ track: track });
    });

    var offer = await self.pc.createOffer();
    await self.pc.setLocalDescription(offer);
    var session = await new Promise(function (resolve, reject) {
      // @see https://github.com/rtcdn/rtcdn-draft
      var data = {
        api: conf.apiUrl,
        tid: conf.tid,
        streamurl: conf.streamUrl,
        clientip: null,
        sdp: offer.sdp,
      };
      console.log("Generated offer: ", data);

      const xhr = new XMLHttpRequest();
      xhr.onload = function () {
        if (xhr.readyState !== xhr.DONE) return;
        if (xhr.status !== 200 && xhr.status !== 201) return reject(xhr);
        const data = JSON.parse(xhr.responseText);
        console.log("Got answer: ", data);
        return data.code ? reject(xhr) : resolve(data);
      };
      xhr.open("POST", conf.apiUrl, true);
      xhr.setRequestHeader("Content-type", "application/json");
      xhr.send(JSON.stringify(data));
    });
    await self.pc.setRemoteDescription(
      new RTCSessionDescription({ type: "answer", sdp: session.sdp })
    );
    session.simulator =
      conf.schema +
      "//" +
      conf.urlObject.server +
      ":" +
      conf.port +
      "/rtc/v1/nack/";

    return session;
  };

  // Close the publisher.
  self.close = function () {
    self.pc && self.pc.close();
    self.pc = null;
  };

  // The callback when got local stream.
  // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
  self.ontrack = function (event) {
    // Add track to stream of SDK.
    self.stream.addTrack(event.track);
  };

  // Internal APIs.
  self.__internal = {
    defaultPath: "/rtc/v1/publish/",
    prepareUrl: function (webrtcUrl) {
      var urlObject = self.__internal.parse(webrtcUrl);

      // If user specifies the schema, use it as API schema.
      var schema = urlObject.user_query.schema;
      schema = schema ? schema + ":" : window.location.protocol;

      var port = urlObject.port || 1985;
      if (schema === "https:") {
        port = urlObject.port || 443;
      }

      // @see https://github.com/rtcdn/rtcdn-draft
      var api = urlObject.user_query.play || self.__internal.defaultPath;
      if (api.lastIndexOf("/") !== api.length - 1) {
        api += "/";
      }

      var apiUrl = schema + "//" + urlObject.server + ":" + port + api;
      for (var key in urlObject.user_query) {
        if (key !== "api" && key !== "play") {
          apiUrl += "&" + key + "=" + urlObject.user_query[key];
        }
      }
      // Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
      apiUrl = apiUrl.replace(api + "&", api + "?");

      var streamUrl = urlObject.url;

      return {
        apiUrl: apiUrl,
        streamUrl: streamUrl,
        schema: schema,
        urlObject: urlObject,
        port: port,
        tid: Number(parseInt(new Date().getTime() * Math.random() * 100))
          .toString(16)
          .slice(0, 7),
      };
    },
    parse: function (url) {
      // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
      var a = document.createElement("a");
      a.href = url
        .replace("rtmp://", "http://")
        .replace("webrtc://", "http://")
        .replace("rtc://", "http://");

      var vhost = a.hostname;
      var app = a.pathname.substring(1, a.pathname.lastIndexOf("/"));
      var stream = a.pathname.slice(a.pathname.lastIndexOf("/") + 1);

      // parse the vhost in the params of app, that srs supports.
      app = app.replace("...vhost...", "?vhost=");
      if (app.indexOf("?") >= 0) {
        var params = app.slice(app.indexOf("?"));
        app = app.slice(0, app.indexOf("?"));

        if (params.indexOf("vhost=") > 0) {
          vhost = params.slice(params.indexOf("vhost=") + "vhost=".length);
          if (vhost.indexOf("&") > 0) {
            vhost = vhost.slice(0, vhost.indexOf("&"));
          }
        }
      }

      // when vhost equals to server, and server is ip,
      // the vhost is __defaultVhost__
      if (a.hostname === vhost) {
        var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
        if (re.test(a.hostname)) {
          vhost = "__defaultVhost__";
        }
      }

      // parse the schema
      var schema = "rtmp";
      if (url.indexOf("://") > 0) {
        schema = url.slice(0, url.indexOf("://"));
      }

      var port = a.port;
      if (!port) {
        // Finger out by webrtc url, if contains http or https port, to overwrite default 1985.
        if (schema === "webrtc" && url.indexOf(`webrtc://${a.host}:`) === 0) {
          port = url.indexOf(`webrtc://${a.host}:80`) === 0 ? 80 : 443;
        }

        // Guess by schema.
        if (schema === "http") {
          port = 80;
        } else if (schema === "https") {
          port = 443;
        } else if (schema === "rtmp") {
          port = 1935;
        }
      }

      var ret = {
        url: url,
        schema: schema,
        server: a.hostname,
        port: port,
        vhost: vhost,
        app: app,
        stream: stream,
      };
      self.__internal.fill_query(a.search, ret);

      // For webrtc API, we use 443 if page is https, or schema specified it.
      if (!ret.port) {
        if (schema === "webrtc" || schema === "rtc") {
          if (ret.user_query.schema === "https") {
            ret.port = 443;
          } else if (window.location.href.indexOf("https://") === 0) {
            ret.port = 443;
          } else {
            // For WebRTC, SRS use 1985 as default API port.
            ret.port = 1985;
          }
        }
      }

      return ret;
    },
    fill_query: function (query_string, obj) {
      // pure user query object.
      obj.user_query = {};

      if (query_string.length === 0) {
        return;
      }

      // split again for angularjs.
      if (query_string.indexOf("?") >= 0) {
        query_string = query_string.split("?")[1];
      }

      var queries = query_string.split("&");
      for (var i = 0; i < queries.length; i++) {
        var elem = queries[i];

        var query = elem.split("=");
        obj[query[0]] = query[1];
        obj.user_query[query[0]] = query[1];
      }

      // alias domain for vhost.
      if (obj.domain) {
        obj.vhost = obj.domain;
      }
    },
  };

  self.pc = new RTCPeerConnection(null);

  // To keep api consistent between player and publisher.
  // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
  // @see https://webrtc.org/getting-started/media-devices
  self.stream = new MediaStream();

  return self;
}

// Depends on adapter-7.4.0.min.js from https://github.com/webrtc/adapter
// Async-await-promise based SRS RTC Player.
function SrsRtcPlayerAsync() {
  var self = {};

  // @see https://github.com/rtcdn/rtcdn-draft
  // @url The WebRTC url to play with, for example:
  //      webrtc://r.ossrs.net/live/livestream
  // or specifies the API port:
  //      webrtc://r.ossrs.net:11985/live/livestream
  //      webrtc://r.ossrs.net:80/live/livestream
  // or autostart the play:
  //      webrtc://r.ossrs.net/live/livestream?autostart=true
  // or change the app from live to myapp:
  //      webrtc://r.ossrs.net:11985/myapp/livestream
  // or change the stream from livestream to mystream:
  //      webrtc://r.ossrs.net:11985/live/mystream
  // or set the api server to myapi.domain.com:
  //      webrtc://myapi.domain.com/live/livestream
  // or set the candidate(eip) of answer:
  //      webrtc://r.ossrs.net/live/livestream?candidate=39.107.238.185
  // or force to access https API:
  //      webrtc://r.ossrs.net/live/livestream?schema=https
  // or use plaintext, without SRTP:
  //      webrtc://r.ossrs.net/live/livestream?encrypt=false
  // or any other information, will pass-by in the query:
  //      webrtc://r.ossrs.net/live/livestream?vhost=xxx
  //      webrtc://r.ossrs.net/live/livestream?token=xxx
  self.play = async function (url) {
    var conf = self.__internal.prepareUrl(url);
    self.pc.addTransceiver("audio", { direction: "recvonly" });
    self.pc.addTransceiver("video", { direction: "recvonly" });
    //self.pc.addTransceiver("video", {direction: "recvonly"});
    //self.pc.addTransceiver("audio", {direction: "recvonly"});

    var offer = await self.pc.createOffer();
    await self.pc.setLocalDescription(offer);
    var session = await new Promise(function (resolve, reject) {
      // @see https://github.com/rtcdn/rtcdn-draft
      var data = {
        api: conf.apiUrl,
        tid: conf.tid,
        streamurl: conf.streamUrl,
        clientip: null,
        sdp: offer.sdp,
      };
      console.log("Generated offer: ", data);

      const xhr = new XMLHttpRequest();
      xhr.onload = function () {
        if (xhr.readyState !== xhr.DONE) return;
        if (xhr.status !== 200 && xhr.status !== 201) return reject(xhr);
        const data = JSON.parse(xhr.responseText);
        console.log("Got answer: ", data);
        return data.code ? reject(xhr) : resolve(data);
      };
      xhr.open("POST", conf.apiUrl, true);
      xhr.setRequestHeader("Content-type", "application/json");
      xhr.send(JSON.stringify(data));
    });
    await self.pc.setRemoteDescription(
      new RTCSessionDescription({ type: "answer", sdp: session.sdp })
    );
    session.simulator =
      conf.schema +
      "//" +
      conf.urlObject.server +
      ":" +
      conf.port +
      "/rtc/v1/nack/";

    return session;
  };

  // Close the player.
  self.close = function () {
    self.pc && self.pc.close();
    self.pc = null;
  };

  // The callback when got remote track.
  // Note that the onaddstream is deprecated, @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onaddstream
  self.ontrack = function (event) {
    // https://webrtc.org/getting-started/remote-streams
    self.stream.addTrack(event.track);
  };

  // Internal APIs.
  self.__internal = {
    defaultPath: "/rtc/v1/play/",
    prepareUrl: function (webrtcUrl) {
      var urlObject = self.__internal.parse(webrtcUrl);

      // If user specifies the schema, use it as API schema.
      var schema = urlObject.user_query.schema;
      schema = schema ? schema + ":" : window.location.protocol;

      var port = urlObject.port || 1985;
      if (schema === "https:") {
        port = urlObject.port || 443;
      }

      // @see https://github.com/rtcdn/rtcdn-draft
      var api = urlObject.user_query.play || self.__internal.defaultPath;
      if (api.lastIndexOf("/") !== api.length - 1) {
        api += "/";
      }

      var apiUrl = schema + "//" + urlObject.server + ":" + port + api;
      for (var key in urlObject.user_query) {
        if (key !== "api" && key !== "play") {
          apiUrl += "&" + key + "=" + urlObject.user_query[key];
        }
      }
      // Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
      apiUrl = apiUrl.replace(api + "&", api + "?");

      var streamUrl = urlObject.url;

      return {
        apiUrl: apiUrl,
        streamUrl: streamUrl,
        schema: schema,
        urlObject: urlObject,
        port: port,
        tid: Number(parseInt(new Date().getTime() * Math.random() * 100))
          .toString(16)
          .slice(0, 7),
      };
    },
    parse: function (url) {
      // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
      var a = document.createElement("a");
      a.href = url
        .replace("rtmp://", "http://")
        .replace("webrtc://", "http://")
        .replace("rtc://", "http://");

      var vhost = a.hostname;
      var app = a.pathname.substring(1, a.pathname.lastIndexOf("/"));
      var stream = a.pathname.slice(a.pathname.lastIndexOf("/") + 1);

      // parse the vhost in the params of app, that srs supports.
      app = app.replace("...vhost...", "?vhost=");
      if (app.indexOf("?") >= 0) {
        var params = app.slice(app.indexOf("?"));
        app = app.slice(0, app.indexOf("?"));

        if (params.indexOf("vhost=") > 0) {
          vhost = params.slice(params.indexOf("vhost=") + "vhost=".length);
          if (vhost.indexOf("&") > 0) {
            vhost = vhost.slice(0, vhost.indexOf("&"));
          }
        }
      }

      // when vhost equals to server, and server is ip,
      // the vhost is __defaultVhost__
      if (a.hostname === vhost) {
        var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
        if (re.test(a.hostname)) {
          vhost = "__defaultVhost__";
        }
      }

      // parse the schema
      var schema = "rtmp";
      if (url.indexOf("://") > 0) {
        schema = url.slice(0, url.indexOf("://"));
      }

      var port = a.port;
      if (!port) {
        // Finger out by webrtc url, if contains http or https port, to overwrite default 1985.
        if (schema === "webrtc" && url.indexOf(`webrtc://${a.host}:`) === 0) {
          port = url.indexOf(`webrtc://${a.host}:80`) === 0 ? 80 : 443;
        }

        // Guess by schema.
        if (schema === "http") {
          port = 80;
        } else if (schema === "https") {
          port = 443;
        } else if (schema === "rtmp") {
          port = 1935;
        }
      }

      var ret = {
        url: url,
        schema: schema,
        server: a.hostname,
        port: port,
        vhost: vhost,
        app: app,
        stream: stream,
      };
      self.__internal.fill_query(a.search, ret);

      // For webrtc API, we use 443 if page is https, or schema specified it.
      if (!ret.port) {
        if (schema === "webrtc" || schema === "rtc") {
          if (ret.user_query.schema === "https") {
            ret.port = 443;
          } else if (window.location.href.indexOf("https://") === 0) {
            ret.port = 443;
          } else {
            // For WebRTC, SRS use 1985 as default API port.
            ret.port = 1985;
          }
        }
      }

      return ret;
    },
    fill_query: function (query_string, obj) {
      // pure user query object.
      obj.user_query = {};

      if (query_string.length === 0) {
        return;
      }

      // split again for angularjs.
      if (query_string.indexOf("?") >= 0) {
        query_string = query_string.split("?")[1];
      }

      var queries = query_string.split("&");
      for (var i = 0; i < queries.length; i++) {
        var elem = queries[i];

        var query = elem.split("=");
        obj[query[0]] = query[1];
        obj.user_query[query[0]] = query[1];
      }

      // alias domain for vhost.
      if (obj.domain) {
        obj.vhost = obj.domain;
      }
    },
  };

  self.pc = new RTCPeerConnection(null);

  // Create a stream to add track to the stream, @see https://webrtc.org/getting-started/remote-streams
  self.stream = new MediaStream();

  // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/ontrack
  self.pc.ontrack = function (event) {
    if (self.ontrack) {
      self.ontrack(event);
    }
  };

  return self;
}

// Depends on adapter-7.4.0.min.js from https://github.com/webrtc/adapter
// Async-awat-prmise based SRS RTC Publisher by WHIP.
function SrsRtcWhipWhepAsync() {
  var self = {};

  // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
  self.constraints = {
    audio: true,
    video: {
      width: { ideal: 320, max: 576 },
    },
  };

  // See https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
  // @url The WebRTC url to publish with, for example:
  //      http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream
  // @options The options to control playing, supports:
  //      videoOnly: boolean, whether only play video, default to false.
  //      audioOnly: boolean, whether only play audio, default to false.
  self.publish = async function (url, options) {
    if (url.indexOf("/whip/") === -1)
      throw new Error(`invalid WHIP url ${url}`);
    if (options?.videoOnly && options?.audioOnly)
      throw new Error(
        `The videoOnly and audioOnly in options can't be true at the same time`
      );

    if (!options?.videoOnly) {
      self.pc.addTransceiver("audio", { direction: "sendonly" });
    } else {
      self.constraints.audio = false;
    }

    if (!options?.audioOnly) {
      self.pc.addTransceiver("video", { direction: "sendonly" });
    } else {
      self.constraints.video = false;
    }

    if (
      !navigator.mediaDevices &&
      window.location.protocol === "http:" &&
      window.location.hostname !== "localhost"
    ) {
      throw new SrsError(
        "HttpsRequiredError",
        `Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`
      );
    }
    var stream = await navigator.mediaDevices.getUserMedia(self.constraints);

    // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
    stream.getTracks().forEach(function (track) {
      self.pc.addTrack(track);

      // Notify about local track when stream is ok.
      self.ontrack && self.ontrack({ track: track });
    });

    var offer = await self.pc.createOffer();
    await self.pc.setLocalDescription(offer);
    const answer = await new Promise(function (resolve, reject) {
      console.log(`Generated offer: ${offer.sdp}`);

      const xhr = new XMLHttpRequest();
      xhr.onload = function () {
        if (xhr.readyState !== xhr.DONE) return;
        if (xhr.status !== 200 && xhr.status !== 201) return reject(xhr);
        const data = xhr.responseText;
        console.log("Got answer: ", data);
        return data.code ? reject(xhr) : resolve(data);
      };
      xhr.open("POST", url, true);
      xhr.setRequestHeader("Content-type", "application/sdp");
      xhr.send(offer.sdp);
    });
    await self.pc.setRemoteDescription(
      new RTCSessionDescription({ type: "answer", sdp: answer })
    );

    return self.__internal.parseId(url, offer.sdp, answer);
  };

  // See https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
  // @url The WebRTC url to play with, for example:
  //      http://localhost:1985/rtc/v1/whep/?app=live&stream=livestream
  // @options The options to control playing, supports:
  //      videoOnly: boolean, whether only play video, default to false.
  //      audioOnly: boolean, whether only play audio, default to false.
  self.play = async function (url, options) {
    if (url.indexOf("/whip-play/") === -1 && url.indexOf("/whep/") === -1)
      throw new Error(`invalid WHEP url ${url}`);
    if (options?.videoOnly && options?.audioOnly)
      throw new Error(
        `The videoOnly and audioOnly in options can't be true at the same time`
      );

    if (!options?.videoOnly)
      self.pc.addTransceiver("audio", { direction: "recvonly" });
    if (!options?.audioOnly)
      self.pc.addTransceiver("video", { direction: "recvonly" });

    var offer = await self.pc.createOffer();
    await self.pc.setLocalDescription(offer);
    const answer = await new Promise(function (resolve, reject) {
      console.log(`Generated offer: ${offer.sdp}`);

      const xhr = new XMLHttpRequest();
      xhr.onload = function () {
        if (xhr.readyState !== xhr.DONE) return;
        if (xhr.status !== 200 && xhr.status !== 201) return reject(xhr);
        const data = xhr.responseText;
        console.log("Got answer: ", data);
        return data.code ? reject(xhr) : resolve(data);
      };
      xhr.open("POST", url, true);
      xhr.setRequestHeader("Content-type", "application/sdp");
      xhr.send(offer.sdp);
    });
    await self.pc.setRemoteDescription(
      new RTCSessionDescription({ type: "answer", sdp: answer })
    );

    return self.__internal.parseId(url, offer.sdp, answer);
  };

  // Close the publisher.
  self.close = function () {
    self.pc && self.pc.close();
    self.pc = null;
  };

  // The callback when got local stream.
  // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
  self.ontrack = function (event) {
    // Add track to stream of SDK.
    self.stream.addTrack(event.track);
  };

  self.pc = new RTCPeerConnection(null);

  // To keep api consistent between player and publisher.
  // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
  // @see https://webrtc.org/getting-started/media-devices
  self.stream = new MediaStream();

  // Internal APIs.
  self.__internal = {
    parseId: (url, offer, answer) => {
      let sessionid = offer.substr(
        offer.indexOf("a=ice-ufrag:") + "a=ice-ufrag:".length
      );
      sessionid = sessionid.substr(0, sessionid.indexOf("\n") - 1) + ":";
      sessionid += answer.substr(
        answer.indexOf("a=ice-ufrag:") + "a=ice-ufrag:".length
      );
      sessionid = sessionid.substr(0, sessionid.indexOf("\n"));

      const a = document.createElement("a");
      a.href = url;
      return {
        sessionid: sessionid, // Should be ice-ufrag of answer:offer.
        simulator: a.protocol + "//" + a.host + "/rtc/v1/nack/",
      };
    },
  };

  // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/ontrack
  self.pc.ontrack = function (event) {
    if (self.ontrack) {
      self.ontrack(event);
    }
  };

  return self;
}

// Format the codec of RTCRtpSender, kind(audio/video) is optional filter.
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#getting_the_supported_codecs
function SrsRtcFormatSenders(senders, kind) {
  var codecs = [];
  senders.forEach(function (sender) {
    var params = sender.getParameters();
    params &&
      params.codecs &&
      params.codecs.forEach(function (c) {
        if (kind && sender.track.kind !== kind) {
          return;
        }

        if (
          c.mimeType.indexOf("/red") > 0 ||
          c.mimeType.indexOf("/rtx") > 0 ||
          c.mimeType.indexOf("/fec") > 0
        ) {
          return;
        }

        var s = "";

        s += c.mimeType.replace("audio/", "").replace("video/", "");
        s += ", " + c.clockRate + "HZ";
        if (sender.track.kind === "audio") {
          s += ", channels: " + c.channels;
        }
        s += ", pt: " + c.payloadType;

        codecs.push(s);
      });
  });
  return codecs.join(", ");
}

export {
  SrsError,
  SrsRtcPublisherAsync,
  SrsRtcWhipWhepAsync,
  SrsRtcFormatSenders,
  SrsRtcPlayerAsync,
};

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2272740.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小兔鲜儿:头部区域的logo,导航,搜索,购物车

头部&#xff1a;logo ,导航&#xff0c;搜索&#xff0c;购物车 头部总体布局: 设置好上下外边距以及总体高度&#xff0c; flex布局让总体一行排列 logo&#xff1a; logo考虑搜索引擎优化&#xff0c;所以要使用 h1中包裹 a 标签&#xff0c;a 里边写内容&#xff08;到时候…

Linux C编程——文件IO基础

文件IO基础 一、简单的文件 IO 示例二、文件描述符三、open 打开文件1. 函数原型2. 文件权限3. 宏定义文件权限4. 函数使用实例 四、write 写文件五、read 读文件六、close 关闭文件七、Iseek 绍 Linux 应用编程中最基础的知识&#xff0c;即文件 I/O&#xff08;Input、Outout…

论文解读 | NeurIPS'24 IRCAN:通过识别和重新加权上下文感知神经元来减轻大语言模型生成中的知识冲突...

点击蓝字 关注我们 AI TIME欢迎每一位AI爱好者的加入&#xff01; 点击 阅读原文 观看作者讲解回放&#xff01; 作者简介 史丹&#xff0c;天津大学博士生 内容简介 大语言模型&#xff08;LLM&#xff09;经过海量数据训练后编码了丰富的世界知识。最近的研究表明&#xff0c…

【51单片机零基础-chapter5:模块化编程】

模块化编程 将以往main中泛型的代码,放在与main平级的c文件中,在h中引用. 简化main函数 将原来main中的delay抽出 然后将delay放入单独c文件,并单独开一个delay头文件,里面放置函数的声明,相当于收纳delay的c文件里面写的函数的接口. 注意,单个c文件所有用到的变量需要在该文…

扩散模型论文概述(三):Stability AI系列工作【学习笔记】

视频链接&#xff1a;扩散模型论文概述&#xff08;三&#xff09;&#xff1a;Stability AI系列工作_哔哩哔哩_bilibili 本期视频讲的是Stability AI在图像生成的工作。 同样&#xff0c;第一张图片是神作&#xff0c;总结的太好了&#xff01; 介绍Stable Diffusion之前&…

数据库软考历年上午真题与答案解析(2018-2024)

本题考查计算机总线相关知识。 总线&#xff08;Bus&#xff09;是计算机各种功能部件之间传送信息的公共通信干线&#xff0c;它是由导线组成的传输线束。 根据总线连接设备范围的不同&#xff0c; 分为&#xff1a;1.片内总线&#xff1a;芯片内部的总线&#xff1b; 2.系统…

【three.js】模型-几何体Geometry,材质Material

模型 在现实开发中&#xff0c;有时除了需要用代码创建模型之外&#xff0c;多数场景需要加载设计师提供的使用设计软件导出的模型。此时就需要使用模型加载器去加载模型&#xff0c;不同格式的模型需要引入对应的模型加载器&#xff0c;虽然加载器不同&#xff0c;但是使用方式…

彻底学会Gradle插件版本和Gradle版本及对应关系

看完这篇&#xff0c;保你彻底学会Gradle插件版本和Gradle版本及对应关系&#xff0c;超详细超全的对应关系表 需要知道Gradle插件版本和Gradle版本的对应关系&#xff0c;其实就是需要知道Gradle插件版本对应所需的gradle最低版本&#xff0c;详细对应关系如下表格&#xff0…

预测facebook签到位置

1.11 案例2&#xff1a;预测facebook签到位置 学习目标 目标 通过Facebook位置预测案例熟练掌握第一章学习内容 1 项目描述 本次比赛的目的是预测一个人将要签到的地方。 为了本次比赛&#xff0c;Facebook创建了一个虚拟世界&#xff0c;其中包括10公里*10公里共100平方公里的…

【万字详细教程】Linux to go——装在移动硬盘里的Linux系统(Ubuntu22.04)制作流程;一口气解决系统安装引导文件迁移显卡驱动安装等问题

Linux to go制作流程 0.写在前面 关于教程Why Linux to go&#xff1f;实际效果 1.准备工具2.制作步骤 下载系统镜像硬盘分区准备启动U盘安装系统重启完成驱动安装将系统启动引导程序迁移到移动硬盘上 3.可能出现的问题 3.1.U盘引导系统安装时出现崩溃3.2.不影响硬盘里本身已有…

在 macOS 上,你可以使用系统自带的 终端(Terminal) 工具,通过 SSH 协议远程连接服务器

文章目录 1. 打开终端2. 使用 SSH 命令连接服务器3. 输入密码4. 连接成功5. 使用密钥登录&#xff08;可选&#xff09;6. 退出 SSH 连接7. 其他常用 SSH 选项8. 常见问题排查问题 1&#xff1a;连接超时问题 2&#xff1a;权限被拒绝&#xff08;Permission denied&#xff09…

Linux CentOS 7系统如何修改panel 重新打开最小化的界面/软件/程序

CentOS 7系统下&#xff0c;部分用户可能一开始打开界面没有类似Windows的下方菜单栏&#xff0c;只有一个浮动的panel。一旦打开软件&#xff0c;然后点击最小化后&#xff0c;找不到重新打开的方法。 右键panel&#xff0c;点击Add New Items… 选择以下三个基本就可以了&am…

打造三甲医院人工智能矩阵新引擎(二):医学影像大模型篇--“火眼金睛”TransUNet

一、引言 1.1 研究背景与意义 在现代医疗领域,医学影像作为疾病诊断与治疗的关键依据,发挥着不可替代的作用。从传统的X射线、CT(计算机断层扫描)到MRI(磁共振成像)等先进技术,医学影像能够直观呈现人体内部结构,为医生提供丰富的诊断信息,涵盖疾病识别、病灶定位、…

基于Arduino的FPV头部追踪相机系统

构建FPV头部追踪相机&#xff1a;让你置身于遥控车辆之中&#xff01; 在遥控车辆和模型飞行器的世界中&#xff0c;第一人称视角&#xff08;FPV&#xff09;体验一直是爱好者们追求的目标。通过FPV头部追踪相机&#xff0c;你可以像坐在车辆或飞行器内部一样&#xff0c;自由…

使用 Three.js 创建动态粒子效果

今天&#xff0c;带大家使用粒子实现一个粒子飞毯的效果&#xff0c;我们先来看一下效果。 实现 初始化场景 首先创建一个场景&#xff0c;所有 3D 对象都会被添加到这个场景中。 const scene new THREE.Scene();相机和渲染器 配置相机和渲染器来捕捉和显示场景。 相机…

Linux双端口服务器:端口1的文件系统目录挂载到端口2

目录 一、服务器安装NFS服务并配置二、文件挂载三、持久化挂载总结为什么服务器配置多个端口 目前有一台服务器&#xff0c;不过他设置了两个SSH的端口&#xff0c;通过下面方法可以让这两个端口连接的主机能够共享同一个文件系统&#xff0c;原本这两个端口的文件系统是隔离的…

机器学习算法---贝叶斯学习

1.了解相关概念 先验概率&#xff1a;有数据集d,以及假设h,此时h是不确定的。在还没有训练数据之前h的初始概率记为P(h),类似地我们把P(d)表示训练数据d在任何假设都未知或不确定时的概率。P(d|h)表示已知假设h成立时d的概率。 后验概率&#xff1a;就是在数据d上经过学习之后…

[paddle] 非线性拟合问题的训练

利用paddlepaddle建立神经网络&#xff0c;模拟有限个数据的非线性拟合 本文仍然考虑 f ( x ) sin ⁡ ( x ) x f(x)\frac{\sin(x)}{x} f(x)xsin(x)​ 函数在区间 [-10,10] 上固定数据的拟合。 import paddle import paddle.nn as nn import numpy as np import matplotlib.…

GWAS数据和软件下载

这部分主要是数据获取,以及软件配置方法。 一、配套数据和代码 数据和代码目前在不断的更新,最新的教程可以私信,我通过后手动发送最新版的pdf和数据代码。发送的压缩包,有电子版的pdf和数据下载链接,里面是最新的百度网盘的地址,下载到本地即可。然后根据pdf教程,结合配套的…

win32汇编环境,在对话框中画五边形与六边形

;运行效果 ;win32汇编环境,在对话框中画五边形与六边形 ;展示五边形与六边形的画法 ;将代码复制进radasm软件里,直接编译可运行.重要部分加备注。 ;下面为asm文件 ;>>>>>>>>>>>>>>>>>>>>>>>>>&g…