Reputation: 1
I am trying to capture the canvas on browser (canvas.captureStream), add audio ( captured using getUserMedia and added to stream using canvasStream.addTracks) and send it to server. Server sends the stream ( after encoding with H264 ) to facebook live using ffmpeg. However the stream is not stable and gets disconnected within minutes. If the video along with audio tracks (using getMediaUser) is directly(without canvas.captureStream) sent it works fine. I am suspecting it has to do with the audio track not being proper and hence facebook rejecting it ( no particular error from ffmpeg, it just exits with IO error). Need help in figuring out the right way to send canvas stream along with audio.
Current ffmpeg command is as below
ffmpeg -i - -c:v libx264 -crf 23 -preset ultrafast \
-tune zerolatency -max_muxing_queue_size 1000 \
-vsync cfr -async 1 -bufsize 2M -r 30 -g 60 -keyint_min 30 \
-x264opts keyint=30 -pix_fmt yuv420p -level 3 \
-c:a aac -b:a 96k -ar 96000 \
-f tee -map 0:v -map 0:a [f=flv] <rtmp url 1> | [f=flv] <rtmp url 2>
Note: In the above command <rtmp url 1> and <rtmp url 2> are replaced with actual urls ( including the session key required )
My settings to capture canvas and audio on browser are as below
canvasStream = canvas.captureStream(30);
audioTrack = stream.getTracks().filter( (track) => {
return track.kind === 'audio';
})[0];
canvasStream.addTrack(audioTrack);
Note: Audio constraints given in getUserMedia are as below
audio: {
sampleRate: 44100,
echoCancellation: true
}
Error given by ffmpeg while exiting is as below
FFMPEG:[flv @ 0x5626ecf0f7c0] Failed to update header with correct duration.
[flv @ 0x5626ecf0f7c0] Failed to update header with correct filesize.
FFMPEG:[tee @ 0x5626ec8550c0] Slave muxer #1 failed, aborting.
av_interleaved_write_frame(): Broken pipe
FFMPEG:[flv @ 0x5626ecbe8840] Failed to update header with correct duration.
FFMPEG:[flv @ 0x5626ecbe8840] Failed to update header with correct filesize.
FFMPEG:frame= 2378 fps= 22 q=22.0 Lsize=N/A time=00:01:21.17 bitrate=N/A dup=2202 drop=5 speed=0.745x
video:18916kB audio:959kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
FFMPEG:[libx264 @ 0x5626ec855b80] frame I:80 Avg QP:13.97 size:115968
[libx264 @ 0x5626ec855b80] frame P:2298 Avg QP:16.77 size: 4392
[libx264 @ 0x5626ec855b80] mb I I16..4: 100.0% 0.0% 0.0%
FFMPEG:[libx264 @ 0x5626ec855b80] mb P I16..4: 3.8% 0.0% 0.0% P16..4: 9.5% 0.0% 0.0% 0.0% 0.0% skip:86.7%
[libx264 @ 0x5626ec855b80] coded y,uvDC,uvAC intra: 26.3% 42.2% 13.6% inter: 2.9% 6.1% 0.1%
[libx264 @ 0x5626ec855b80] i16 v,h,dc,p: 34% 26% 23% 17%
[libx264 @ 0x5626ec855b80] i8c dc,h,v,p: 46% 27% 20% 7%
[libx264 @ 0x5626ec855b80] kb/s:1954.96
[aac @ 0x5626ec84d200] Qavg: 122.161
Conversion failed!
Appreciate any help with this.
Upvotes: 0
Views: 953
Reputation: 163593
I am suspecting it has to do with the audio track not being proper and hence facebook rejecting it
Yes, this is correct. Facebook requires an audio track and will drop your stream if it isn't present.
For starters, drop -ar 96000
from your FFmpeg command. This would be setting a sample rate of 96 kHz, which Facebook isn't going to want. You really don't want to resample at FFmpeg anyway... just let it use whatever sample rate it gets from the browser.
Next, set your audio sample rate to 48 kHz rather than 44.1 kHz on your getUserMedia
constraints. Facebook doesn't handle 44.1 kHz audio on the RTMP ingest well. (Generally these days, you want to use 48 kHz for everything unless you're targeting CD audio, which would be 44.1 kHz.)
Next, your FFmpeg audio command is outputting to two RTMP URLs. If one fails, they will both fail. If one gets behind, they will both be stalled. This is probably not what you want. :-) Drop one of them.
Now, make sure you have a decent and consistent frame rate. Facebook is very picky about this and will drop your stream.
To test/debug, replace your RTMP URL output with a simple FLV file on disk, to ensure things are working from the browser up through FFmpeg. Once you have that all smooth and working, you can reconnect to Facebook and go from there.
Upvotes: 1