库房摄像头直播后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
  1. const Stream = require('node-rtsp-stream')
  2. const os = require('os');
  3. ///获取本机ip///
  4. function getIPAdress() {
  5. var interfaces = os.networkInterfaces();
  6. for (var devName in interfaces) {
  7. var iface = interfaces[devName];
  8. for (var i = 0; i < iface.length; i++) {
  9. var alias = iface[i];
  10. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  11. return alias.address;
  12. }
  13. }
  14. }
  15. }
  16. const args = []
  17. const requestManager = function () { }
  18. requestManager.prototype = {
  19. Open: function (arg) {
  20. let result = {}
  21. if (args.length == 0) {
  22. result = this._create(arg)
  23. result = this._openVideo(result)
  24. } else {
  25. args.forEach(a => {
  26. if (a.rtspUrl == arg.rtspUrl) {
  27. result = a
  28. }
  29. })
  30. if (result.port === undefined || result.rtspUrl === undefined) {
  31. result = this._create(arg)
  32. result = this._openVideo(result)
  33. }
  34. }
  35. result = Object.assign(result,{url:`ws:\\${getIPAdress()}:${result.port}`})
  36. return result;
  37. },
  38. Close: function (arg) {
  39. let result = {}
  40. let idx = -1
  41. idx = args.findIndex(a => a.rtspUrl == arg.rtspUrl)
  42. if (idx !== -1) {
  43. args[idx].stream.stop()
  44. result = args.splice(idx, 1)
  45. } else {
  46. }
  47. console.log(args)
  48. return result
  49. },
  50. _randomPort: function () {
  51. let port = Math.floor(Math.random() * (4001 - 3001) + 3001)
  52. return port
  53. },
  54. _openVideo: function (arg) {
  55. arg.stream = new Stream({
  56. name: 'name',
  57. //streamUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov',
  58. streamUrl: arg.rtspUrl,
  59. wsPort: arg.port,
  60. ffmpegOptions: { // options ffmpeg flags
  61. '-stats': '', // an option with no neccessary value uses a blank string
  62. '-r': 30, // options with required values specify the value after the key
  63. '-s': arg.size,
  64. '-codec:a': 'mp2',
  65. '-ar': 44100,
  66. '-ac': 1,
  67. '-b:a': '128k'
  68. }
  69. })
  70. return arg
  71. },
  72. _create: function (arg) {
  73. let target = {
  74. rtspUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov',
  75. port: this._randomPort(),
  76. size: '1024*768',
  77. stream: null
  78. }
  79. let source = {
  80. rtspUrl: arg.rtspUrl,
  81. port: this._randomPort(),
  82. size: arg.size,
  83. stream: null
  84. }
  85. Object.assign(target, source)
  86. args.push(target)
  87. return target
  88. }
  89. }
  90. module.exports = requestManager