So, I need to start streaming the currently recording video. I mean,
- Start recording
- start streaming to server
- stop recording
- send the remaining file chunks, because 4g connection is much slower that realtime
- wait while all the network requests are finished
We need to process the video frames and provide some analysis (audio is required). I cannot allow myself to lose a single frame from that video (while working in the unstable 4g environment), that's why I cannot use a traditional streaming approach like "send and forget".
It would be easy for me, if the Camera plugin allowed us to access the file which is currently recording, so I can open it as a read stream and do everything I want. But it doesn't.
I found some code from 2020, which tells me that in that days we were able to provide our own path into the cameraController.startVideoRecording(pathToSave)
method. Or at least final pathToSave = cameraController.startVideoRecording()
, which is also acceptable for me. The problem would be solved:
final pathToSave = cameraController.startVideoRecording();
startStreamingTheFile(pathToSave);
But right now the only way to access the saved file is final filePath = stopVideoRecording();
. I cannot wait for the end of recording!
I tried to record the video in chunks:
- record a video fragment nearly 2 minutes long
- continue recording data into another fragment
- send the first fragment
- loop
The problem is, Flutter Camera doesn't allow us to have 2 instances CameraController
for the same CameraDescription
, see SO question here. And there is a noticable gap between calling stopRecording()
and actually receiving the file handle (frames are lost). There is also a gap between calling startVideoRecording()
and actually starting the video recording, so I cannot use that "start-stop-loop" approach in the middle of my stream.
Now the only approach that is left is the following (too much work, there must be an easier way):
startImageStream()
, listen for each frame. Also listen for the microphone stream.- save the frames as images somewhere in the file system
- every 2 minutes, concat the images and the microphone output into one video fragment with
ffmpeg
- send the fragment, loop. Not a single frame is lost.
Many there is some way to access the recorded file BEFORE the end of recording? Or the recorded file becomes the actual file just before the stopRecording()
?