zhangzengfei
2023-11-29 0d3db253cad1fb49c4fae9b9a537c8c318c7172f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * WFS interface, Jeff Yang 2016.10
 */
/* eslint-disable */
"use strict";
 
import Event from "./events";
import FlowController from "./controller/flow-controller";
import BufferController from "./controller/buffer-controller";
import EventEmitter from "events";
// import XhrLoader from './utils/xhr-loader';
import WebsocketLoader from "./loader/websocket-loader";
 
class Wfs {
  static get version() {
    // replaced with browserify-versionify transform
    return "__VERSION__" + "v.0.0.0.1";
  }
 
  static isSupported() {
    return (
      window.MediaSource &&
      typeof window.MediaSource.isTypeSupported === "function" &&
      window.MediaSource.isTypeSupported('video/mp4; codecs="avc1.42c01f,mp4a.40.2"')
    );
  }
 
  static get Events() {
    return Event;
  }
 
  static get DefaultConfig() {
    if (!Wfs.defaultConfig) {
      Wfs.defaultConfig = {
        autoStartLoad: true,
        startPosition: -1,
        debug: false,
        // H264_TIMEBASE: 3600,
        // fLoader: undefined,
        // loader: XhrLoader,
        //loader: FetchLoader,
        // fmp4FileUrl: 'xxxx.mp4',
        fragLoadingTimeOut: 20000,
        fragLoadingMaxRetry: 6,
        fragLoadingRetryDelay: 1000,
        fragLoadingMaxRetryTimeout: 64000,
        fragLoadingLoopThreshold: 3,
        forceKeyFrameOnDiscontinuity: true,
        appendErrorMaxRetry: 3
      };
    }
    return Wfs.defaultConfig;
  }
 
  static set DefaultConfig(defaultConfig) {
    Wfs.defaultConfig = defaultConfig;
  }
 
  constructor(config = {}) {
    var defaultConfig = Wfs.DefaultConfig;
    for (var prop in defaultConfig) {
      if (prop in config) {
        continue;
      }
      config[prop] = defaultConfig[prop];
    }
    this.config = config;
    // observer setup
    var observer = (this.observer = new EventEmitter());
    observer.trigger = function trigger(event, ...data) {
      observer.emit(event, event, ...data);
    };
 
    observer.off = function off(event, ...data) {
      observer.removeListener(event, ...data);
    };
    this.on = observer.on.bind(observer);
    this.off = observer.off.bind(observer);
    this.trigger = observer.trigger.bind(observer);
 
    this.flowController = new FlowController(this);
    this.bufferController = new BufferController(this);
    //  this.fileLoader = new FileLoader(this);
    this.websocketLoader = new WebsocketLoader(this);
    this.mediaType = undefined;
    this.cameraInfo = {};
    this.playerStatus = -1;
  }
 
  destroy() {
    this.flowController.destroy();
    this.bufferController.destroy();
    //   this.fileLoader.destroy();
    this.websocketLoader.destroy();
  }
 
  attachMedia(media, channelName = "chX", mediaType = "H264Raw", websocketName = "ws", cameraInfo = {}) {
    // 'H264Raw' 'FMp4'
    this.mediaType = mediaType;
    this.media = media;
    this.cameraInfo = cameraInfo;
    this.trigger(Event.MEDIA_ATTACHING, {
      media: media,
      channelName: channelName,
      mediaType: mediaType,
      websocketName: websocketName,
      cameraInfo: cameraInfo
    });
  }
  attachWebsocket(websocket, channelName, cameraInfo) {
    this.trigger(Event.WEBSOCKET_ATTACHING, {
      websocket: websocket,
      mediaType: this.mediaType,
      channelName: channelName,
      cameraInfo: cameraInfo
    });
  }
}
 
export default Wfs;