media.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const nodeDataChannel = require('../../lib/index');
  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', { iceServers: [] });
  14. peerConnection.onStateChange((state) => {
  15. console.log('State: ', state);
  16. });
  17. peerConnection.onGatheringStateChange((state) => {
  18. // console.log('GatheringState: ', state);
  19. if (state == 'complete') {
  20. let desc = peerConnection.localDescription();
  21. console.log('');
  22. console.log('## Please copy the offer below to the web page:');
  23. console.log(JSON.stringify(desc));
  24. console.log('\n\n');
  25. console.log('## Expect RTP video traffic on localhost:5000');
  26. rl.question('## Please copy/paste the answer provided by the browser: \n', (sdp) => {
  27. let sdpObj = JSON.parse(sdp);
  28. peerConnection.setRemoteDescription(sdpObj.sdp, sdpObj.type);
  29. console.log(track.isOpen());
  30. rl.close();
  31. });
  32. }
  33. });
  34. let video = new nodeDataChannel.Video('video', 'RecvOnly');
  35. video.addH264Codec(96);
  36. video.setBitrate(3000);
  37. let track = peerConnection.addTrack(video);
  38. let session = new nodeDataChannel.RtcpReceivingSession();
  39. track.setMediaHandler(session);
  40. track.onMessage((msg) => {
  41. client.send(msg, 5000, '127.0.0.1', (err, n) => {
  42. if (err) console.log(err, n);
  43. });
  44. });
  45. peerConnection.setLocalDescription();