| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- const io = require("socket.io-client")
- const { PeerConnection, Video, Audio } = require('node-datachannel');
- const { spawn } = require('child_process')
- const UDP = require('./lib/udp')
- class CarServer {
- constructor() {
- this.Peer = null
- this.videoChild = null
- this.videoUDP = null
- this.audioChild = null
- this.audioUDP = null
- this.socket = io('wss://**********', {
- auth: {
- roomID: 'test213',
- name: 'control'
- }
- });
- this.socket.on('connect', this.connected.bind(this))
- this.socket.on('msg', this.onMsg.bind(this))
- this.socket.on('leaved', this.onLeved.bind(this))
- this.socket.on('joined', this.startVideo.bind(this))
- }
- // socket 连接
- connected() {
- console.log('connected');
- this.Peer = new PeerConnection("Peer1", { iceServers: [] });
- // 发送offer
- this.Peer.onGatheringStateChange(state => {
- console.log('GatheringState: ', state);
- if (state === 'complete') {
- const offer = this.Peer.localDescription();
- this.socket.emit("msg", offer);
- }
- })
- }
- // 用户信息
- onMsg(data) {
- try {
- if (data.type == 'answer') {
- this.Peer.setRemoteDescription(data.sdp, data.type);
- } else if (data.type === 'startRTC') {
- this.startVideo()
- }
- } catch (error) {
- console.log('msg error:', error);
- }
- }
- // 用户离开
- onLeved() {
- if (this.videoChild) this.videoChild.kill()
- if (this.audioChild) this.audioChild.kill()
- if (this.videoUDP) this.videoUDP.close()
- if (this.audioUDP) this.audioUDP.close()
- this.videoChild = null
- this.videoUDP = null
- this.audioChild = null
- this.audioUDP = null
- process.exit(1)
- }
- // 开始视频
- startVideo() {
- try {
- if (this.videoChild) this.videoChild.kill()
- if (this.audioChild) this.audioChild.kill()
- if (this.videoUDP) this.videoUDP.close()
- if (this.audioUDP) this.audioUDP.close()
- // ------------video----------------
- const video = new Video('video', 'SendOnly')
- video.addH264Codec(97)
- video.addSSRC(43, "video-send")
- const videoTrack = this.Peer.addTrack(video)
- const videoPort = 47788
- const videoArgs = [
- "libcamerasrc",
- "video/x-raw,width=320,height=240",
- "videoconvert",
- "queue",
- "x264enc tune=zerolatency bitrate=1000 key-int-max=30",
- "video/x-h264, profile=constrained-baseline",
- "rtph264pay pt=97 mtu=1200 ssrc=43",
- `udpsink host=127.0.0.1 port=${videoPort}`,
- ].join(" ! ").split(" ")
- this.videoChild = spawn("gst-launch-1.0", videoArgs)
- this.videoUDP = new UDP(videoPort, data => {
- if (!videoTrack.isOpen()) return
- videoTrack.sendMessageBinary(data)
- })
- // -----------audio--------------
- const audio = new Audio('audio','SendOnly')
- audio.addOpusCodec(96)
- audio.addSSRC(42, "audio-send")
- const audioTrack = this.Peer.addTrack(audio)
- const audioPort = 47789
- // alsamixer F6 查看当前声卡,默认是3-0
- // "alsasrc device=plughw:1,0",
- const audioArgs = [
- 'alsasrc device=plughw:3,0',
- "audio/x-raw,rate=8000,channels=1",
- "audioconvert",
- "queue",
- "opusenc",
- "rtpopuspay",
- `udpsink host=127.0.0.1 port=${audioPort}`,
- ].join(" ! ").split(" ")
- this.audioChild = spawn("gst-launch-1.0", audioArgs)
- this.audioUDP = new UDP(audioPort, data => {
- if (!audioTrack.isOpen()) return
- audioTrack.sendMessageBinary(data)
- })
- // ---------
- this.Peer.setLocalDescription()
- } catch (error) {
- console.log('startvideo:', error)
- }
- }
- }
- new CarServer()
|