shinyoshiaki / werift-webrtc

WebRTC Implementation for TypeScript (Node.js), includes ICE/DTLS/SCTP/RTP/SRTP/WEBM/MP4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Restart of MediaRecorder does not work correctly

kolserdav opened this issue · comments

When using MediaRecord, I may need to periodically restart it on a per-client basis, depending on client events. For example, the client turned on video playback and then turned it off. Thus, when client video playback is enabled, MediaRecord records in resolution, for example, 640 * 480 to one video file, and when it is disabled, then 1 * 1 to another video file, this is necessary so that at the end of the recording these parts must be assembled into one video file.

Here is my example file, in this example it only records the stream of the user who started the recording https://github.com/kolserdav/werift-sfu-react/blob/48449ba25324de48d55138b01f0f89013d9f0ef5/packages/server/src/addons/recordVideo.ts#L179

Then, at some point in the recording, the user turns on/off their camera, the onChangeVideoTrack https://github.com/kolserdav/werift-sfu-react/blob/48449ba25324de48d55138b01f0f89013d9f0ef5/packages/server/src/addons/recordVideo.ts#L47 event fires. With this event, I try to stop the current MediaRecorder and create a new one on the same stream, but in a different file.

Writing the first file is going great. But after stopping and starting to record another file from the same stream, the video never plays and has the icon as an unsupported video format.
https://iili.io/Hzz055x.png

Today, in order to understand the reason, I simplified the task a little, but also expanded it to test other aspects. For example, two users are connected to the room, both have video broadcast turned on, then recording starts https://github.com/kolserdav/werift-sfu-react/blob/4db3fc3ce1235e9290c51543469b9b7d26be1ed7/packages/server/src/addons/recordVideo.ts#L234 , records a few seconds and stops https://github.com/kolserdav/werift-sfu-react/blob/4db3fc3ce1235e9290c51543469b9b7d26be1ed7/packages/server/src/addons/recordVideo.ts#L255 . As you can see, everything went well.
https://iili.io/HzX7sQp.png
After a while, without interrupting the current conference, we start recording again, but at this moment it no longer records correctly.
https://iili.io/HzX7ijR.png
https://iili.io/HzX7PTv.png
It's as if the media recorders that worked with these media tracks made some changes to these media tracks, after which they can no longer be recorded until you re-create a new RTC connection with other media tracks.

I found the cause, but I don't know how to fix it yet. The point is that this listener is not cleared after the media recorder is stopped. I output this line

track.onReceiveRtp.subscribe((r) => rtpSource.push(r));
to the log, start and stop the recording, but the output to the log continues without stopping, although it would obviously be correct if it stopped its work after the recording stopped.
https://iili.io/HzhnQvp.png