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

78 lines
2.3 KiB

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