Encountered the following problem: From the client side using websocket I drive the video bit by bit to the server:
mediaRecorder.ondataavailable = function (e) {
if (ws && ws.readyState === WebSocket.OPEN && e.data && e.data.size > 0) {
ws.send(e.data);
}
else {
//...
}
}
mediaRecorder.start(100);
On the server in a loop, I get these parts and write them to a file.
while (true)
{
buffer = new ArraySegment<byte>(new byte[500000]);
result = await socket.ReceiveAsync(buffer, CancellationToken.None);
if (socket.State == WebSocketState.CloseReceived)
{
bw.Close();
fs.Close();
break;
}
if (Encoding.UTF8.GetString(buffer.Array, 0, result.Count) == "start record")
{
writeStatus = "start record";
fullPath = $"{context.Server.MapPath("~")}{fileName}.{fileExt}";
fs = new FileStream(fullPath, FileMode.Append, FileAccess.Write);
bw = new BinaryWriter(fs);
continue;
}
if (Encoding.UTF8.GetString(buffer.Array, 0, result.Count) == "stop record")
{
bw.Close();
fs.Close();
writeStatus = "none";
continue;
}
if (writeStatus == "start record")
{
bw.Write(buffer.Array, 0, result.Count);
pos += result.Count;
}
}
The problem is that I need to keep writing videos to the same file every time the page is reloaded. As I understand it, just adding to the end of the file is not an option, since in this case all the bytes added are not played when playing the recorded video file. Tell me, please, how can I continue recording video in the same file?