haoxuan
2023-08-31 401524fb5661d57ffb2229d683fe4de85b65fd1c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Buffer Controller
 */
/* eslint-disable */
import Event from '../events';
import EventHandler from '../event-handler';
 
class BufferController extends EventHandler {
  constructor(wfs) {
    super(
      wfs,
      Event.MEDIA_ATTACHING,
      Event.BUFFER_APPENDING,
      Event.BUFFER_RESET
    );
 
    this.mediaSource = null;
    this.media = null;
    this.pendingTracks = {};
    this.sourceBuffer = {};
    this.segments = [];
 
    this.appended = 0;
    this._msDuration = null;
 
    // Source Buffer listeners
    this.onsbue = this.onSBUpdateEnd.bind(this);
 
    this.browserType = 0;
    if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
      this.browserType = 1;
    }
    this.mediaType = 'H264Raw';
 
    this.websocketName = undefined;
    this.channelName = undefined;
    this.cameraInfo = {};
  }
 
  destroy() {
    EventHandler.prototype.destroy.call(this);
  }
 
  onMediaAttaching(data) {
    let media = (this.media = data.media);
    this.mediaType = data.mediaType;
    this.websocketName = data.websocketName;
    this.channelName = data.channelName;
    this.cameraInfo = data.cameraInfo;
    if (media) {
      // setup the media source
      var ms = (this.mediaSource = new MediaSource());
      //Media Source listeners
      this.onmso = this.onMediaSourceOpen.bind(this);
      this.onmse = this.onMediaSourceEnded.bind(this);
      this.onmsc = this.onMediaSourceClose.bind(this);
      ms.addEventListener('sourceopen', this.onmso);
      ms.addEventListener('sourceended', this.onmse);
      ms.addEventListener('sourceclose', this.onmsc);
      // link video and media Source
      media.src = URL.createObjectURL(ms);
    }
  }
 
  onMediaDetaching() {}
 
  onBufferAppending(data) {
    if (!this.segments) {
      this.segments = [data];
    } else {
      this.segments.push(data);
    }
    this.doAppending();
  }
 
  onMediaSourceClose() {
    console.log('media source closed');
  }
 
  onMediaSourceEnded() {
    console.log('media source ended');
  }
 
  onSBUpdateEnd(event) {
    // Firefox
    if (this.browserType === 1) {
      this.mediaSource.endOfStream();
      this.media.play();
    }
 
    this.appending = false;
    this.doAppending();
    this.updateMediaElementDuration();
  }
 
  updateMediaElementDuration() {}
 
  onMediaSourceOpen() {
    let mediaSource = this.mediaSource;
    if (mediaSource) {
      // once received, don't listen anymore to sourceopen event
      mediaSource.removeEventListener('sourceopen', this.onmso);
    }
 
    if (this.mediaType === 'FMp4') {
      this.checkPendingTracks();
    }
 
    this.wfs.trigger(Event.MEDIA_ATTACHED, {
      media: this.media,
      channelName: this.channelName,
      mediaType: this.mediaType,
      websocketName: this.websocketName,
      cameraInfo: this.cameraInfo
    });
  }
 
  checkPendingTracks() {
    this.createSourceBuffers({ tracks: 'video', mimeType: '' });
    this.pendingTracks = {};
  }
 
  onBufferReset(data) {
    if (this.mediaType === 'H264Raw') {
      this.createSourceBuffers({ tracks: 'video', mimeType: data.mimeType });
    }
  }
 
  createSourceBuffers(tracks) {
    var sourceBuffer = this.sourceBuffer,
      mediaSource = this.mediaSource;
    let mimeType;
    if (tracks.mimeType === '') {
      mimeType = 'video/mp4;codecs=avc1.420028'; // avc1.42c01f avc1.42801e avc1.640028 avc1.420028
    } else {
      mimeType = 'video/mp4;codecs=' + tracks.mimeType;
    }
 
    try {
      let sb = (sourceBuffer['video'] = mediaSource.addSourceBuffer(mimeType));
      sb.addEventListener('updateend', this.onsbue);
      track.buffer = sb;
    } catch (err) {}
    this.wfs.trigger(Event.BUFFER_CREATED, { tracks: tracks });
    this.media.play();
  }
 
  doAppending() {
    var wfs = this.wfs,
      sourceBuffer = this.sourceBuffer,
      segments = this.segments;
    if (Object.keys(sourceBuffer).length) {
      if (this.media.error) {
        this.segments = [];
        console.log(
          'trying to append although a media error occured, flush segment and abort'
        );
        return;
      }
      if (this.appending) {
        return;
      }
 
      if (segments && segments.length) {
        var segment = segments.shift();
        try {
          if (sourceBuffer[segment.type]) {
            this.parent = segment.parent;
            sourceBuffer[segment.type].appendBuffer(segment.data);
            this.appendError = 0;
            this.appended++;
            this.appending = true;
          } else {
          }
        } catch (err) {
          // in case any error occured while appending, put back segment in segments table
          segments.unshift(segment);
          var event = { type: ErrorTypes.MEDIA_ERROR };
          if (err.code !== 22) {
            if (this.appendError) {
              this.appendError++;
            } else {
              this.appendError = 1;
            }
            event.details = ErrorDetails.BUFFER_APPEND_ERROR;
            event.frag = this.fragCurrent;
            if (this.appendError > wfs.config.appendErrorMaxRetry) {
              segments = [];
              event.fatal = true;
              return;
            } else {
              event.fatal = false;
            }
          } else {
            this.segments = [];
            event.details = ErrorDetails.BUFFER_FULL_ERROR;
            return;
          }
        }
      }
    }
  }
}
 
export default BufferController;