I have an Android device, which will send raw live H264 AnnexB NAL unit stream like [0,0,0,1,103,...][0,0,0,104,...][0,0,0,101,...][0,0,0,1,65,...][0,0,0,1,65,...] and try to mux them into flv container and send it to nginx-with rtmp module use libavformat of ffmpeg.
If I save the received live stream to a local file, say test.h264. I can mux it to server OK using ffmpeg command "ffmpeg -i test.h264 -f flv rtmp://my/server/url". But I don't known how to handle live stream.
I noticed ffmpeg/libavformat/avc.c have 2 functions that seem achieve my goal. But I'm not sure.
Here is the code of ffmpeg
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
{
const uint8_t *p = buf_in;
const uint8_t *end = p + size;
const uint8_t *nal_start, *nal_end;
size = 0;
nal_start = ff_avc_find_startcode(p, end);
for (;;) {
while (nal_start < end && !*(nal_start++));
if (nal_start == end)
break;
nal_end = ff_avc_find_startcode(nal_start, end);
avio_wb32(pb, nal_end - nal_start);
avio_write(pb, nal_start, nal_end - nal_start);
size += 4 + nal_end - nal_start;
nal_start = nal_end;
}
return size;
}
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
{
AVIOContext *pb;
int ret = avio_open_dyn_buf(&pb);
if(ret < 0)
return ret;
ff_avc_parse_nal_units(pb, buf_in, *size);
av_freep(buf);
*size = avio_close_dyn_buf(pb, buf);
return 0;
}
Any useful reply are appreciated.
Thank you!