库房摄像头直播后台
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.

98 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 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. let stop = args[idx].stream.stop()
  44. if(stop.wsServer.clients.size === 1){
  45. result = args.splice(idx, 1)
  46. }
  47. } else {
  48. }
  49. console.log(args)
  50. return result
  51. },
  52. _randomPort: function (arg) {
  53. let port
  54. if(arg) {
  55. port = Number(arg.port) + Number(arg.videoRoute)
  56. }else{
  57. port = Math.floor(Math.random() * (4001 - 3001) + 3001)
  58. }
  59. return port
  60. },
  61. _openVideo: function (arg) {
  62. arg.stream = new Stream({
  63. name: 'name',
  64. //streamUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov',
  65. streamUrl: arg.rtspUrl,
  66. wsPort: arg.port,
  67. ffmpegOptions: { // options ffmpeg flags
  68. '-stats': '', // an option with no neccessary value uses a blank string
  69. '-r': 30, // options with required values specify the value after the key
  70. '-s': arg.size,
  71. '-codec:a': 'mp2',
  72. '-ar': 44100,
  73. '-ac': 1,
  74. '-b:a': '128k'
  75. }
  76. })
  77. return arg
  78. },
  79. _create: function (arg) {
  80. let target = {
  81. rtspUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov',
  82. port: this._randomPort(),
  83. size: '1024*768',
  84. stream: null
  85. }
  86. let source = {
  87. rtspUrl: arg.rtspUrl,
  88. port: this._randomPort(arg),
  89. size: arg.size,
  90. stream: null
  91. }
  92. Object.assign(target, source)
  93. args.push(target)
  94. return target
  95. }
  96. }
  97. module.exports = requestManager