media.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const nodeDataChannel = require('node-datachannel');
  3. const readline = require('readline');
  4. var dgram = require('dgram');
  5. var client = dgram.createSocket('udp4');
  6. // Read Line Interface
  7. const rl = readline.createInterface({
  8. input: process.stdin,
  9. output: process.stdout,
  10. });
  11. // Init Logger
  12. nodeDataChannel.initLogger('Debug');
  13. let peerConnection = new nodeDataChannel.PeerConnection('pc', {
  14. iceServers: [
  15. "stun:caner.top:3478",
  16. "turn:admin:123456@caner.top:3478",
  17. "stun:stun.l.google.com:19302",
  18. "stun:stun1.l.google.com:19302",
  19. "stun:stun2.l.google.com:19302",
  20. "stun:stun3.l.google.com:19302"
  21. ]
  22. });
  23. peerConnection.onStateChange((state) => {
  24. console.log('State: ', state);
  25. });
  26. peerConnection.onGatheringStateChange((state) => {
  27. // console.log('GatheringState: ', state);
  28. if (state == 'complete') {
  29. let desc = peerConnection.localDescription();
  30. console.log('');
  31. console.log('## Please copy the offer below to the web page:');
  32. console.log(JSON.stringify(desc));
  33. console.log('\n\n');
  34. console.log('## Expect RTP video traffic on localhost:5000');
  35. rl.question('## Please copy/paste the answer provided by the browser: \n', (sdp) => {
  36. let sdpObj = JSON.parse(sdp);
  37. peerConnection.setRemoteDescription(sdpObj.sdp, sdpObj.type);
  38. console.log(track.isOpen());
  39. rl.close();
  40. });
  41. }
  42. });
  43. let video = new nodeDataChannel.Video('video', 'RecvOnly');
  44. video.addH264Codec(96);
  45. video.setBitrate(3000);
  46. let track = peerConnection.addTrack(video);
  47. let session = new nodeDataChannel.RtcpReceivingSession();
  48. track.setMediaHandler(session);
  49. track.onMessage((msg) => {
  50. client.send(msg, 5000, '127.0.0.1', (err, n) => {
  51. if (err) console.log(err, n);
  52. });
  53. });
  54. peerConnection.setLocalDescription();