<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi<div><br></div><div>I am attempting to stream my webcam via rtsp to VLC on another PC, I know it is possible with ffmpeg, but there is no documentation etc. that I could find to help with this.</div><div><br></div><div>Any help would be greatly appreciated!</div><div><br></div><div>Neerav</div><div><br></div><div>I am trying to do the following:</div><div><br></div><div><div> AVFormatContext *oc;</div><div> const char *filename = "rtsp://127.0.0.1:8554/test";</div><div><br></div><div> avformat_alloc_output_context2(&oc, NULL, "rtsp", NULL);</div><div><br></div><div> if (!oc) {</div><div> printf("Could not deduce output format from file extension: using MPEG.\n");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);</div><div> }</div><div><br></div><div> AVOutputFormat *fmt;</div><div> AVStream *video_st;</div><div> AVCodec *video_codec;</div><div> fmt = oc->oformat;</div><div><br></div><div> int out_codec = fmt->video_codec;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div> video_st = add_stream(oc, &video_codec, fmt->video_codec);</div><div><br></div><div> open_video(oc, video_codec, video_st);</div><div><br></div><div> char errorBuff[80];</div><div><br></div><div> if (!(fmt->flags & AVFMT_NOFILE)) { <------------------------------------ IT NEVER GOES INTO THIS DUE TO THIS CONDITION FAILING</div><div><span style="font-size: 12pt;"> int ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);</span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if (ret < 0) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <span style="font-size: 12pt;">fprintf(stderr, "Could not open outfile '%s': %s",filename,av_make_error_string(errorBuff,80,ret));</span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> return 1;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div> }</div></div><div><br></div><div><br></div><div>// -------------------------------------------</div><div><br></div><div>add_stream and open_video functions</div><div><br></div><div><div>static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,</div><div> enum AVCodecID codec_id)</div><div>{</div><div> AVCodecContext *c;</div><div> AVStream *st;</div><div><br></div><div> /* find the encoder */</div><div> *codec = avcodec_find_encoder(codec_id);</div><div> if (!(*codec)) {</div><div> fprintf(stderr, "Could not find encoder for '%s'\n",</div><div> avcodec_get_name(codec_id));</div><div> exit(1);</div><div> }</div><div><br></div><div> st = avformat_new_stream(oc, *codec);</div><div> if (!st) {</div><div> fprintf(stderr, "Could not allocate stream\n");</div><div> exit(1);</div><div> }</div><div> st->id = oc->nb_streams-1;</div><div> c = st->codec;</div><div><br></div><div> switch ((*codec)->type) {</div><div> case AVMEDIA_TYPE_AUDIO:</div><div> c->sample_fmt = (*codec)->sample_fmts ?</div><div> (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;</div><div> c->bit_rate = 64000;</div><div> c->sample_rate = 44100;</div><div> c->channels = 2;</div><div> break;</div><div><br></div><div> case AVMEDIA_TYPE_VIDEO:</div><div> c->codec_id = codec_id;</div><div> if(codec_id == CODEC_ID_H264)</div><div> printf("Codec ID %x", (AVCodecID)codec_id);</div><div> c->bit_rate = 400000;</div><div> /* Resolution must be a multiple of two. */</div><div> c->width = 320*5;</div><div> c->height = 720;</div><div> /* timebase: This is the fundamental unit of time (in seconds) in terms</div><div> * of which frame timestamps are represented. For fixed-fps content,</div><div> * timebase should be 1/framerate and timestamp increments should be</div><div> * identical to 1. */</div><div> c->time_base.den = 25;</div><div> c->time_base.num = 1;</div><div> c->gop_size = 12; /* emit one intra frame every twelve frames at most */</div><div> c->pix_fmt = AV_PIX_FMT_YUV420P;</div><div> if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {</div><div> /* just for testing, we also add B frames */</div><div> c->max_b_frames = 2;</div><div> }</div><div> if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {</div><div> /* Needed to avoid using macroblocks in which some coeffs overflow.</div><div> * This does not happen with normal video, it just happens here as</div><div> * the motion of the chroma plane does not match the luma plane. */</div><div> c->mb_decision = 2;</div><div> }</div><div> break;</div><div><br></div><div> default:</div><div> break;</div><div> }</div><div><br></div><div> /* Some formats want stream headers to be separate. */</div><div> if (oc->oformat->flags & AVFMT_GLOBALHEADER)</div><div> c->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div><br></div><div> return st;</div><div>}</div><div><br></div><div>static AVFrame *frame;</div><div>static AVPicture src_picture, dst_picture;</div><div><br></div><div>static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)</div><div>{</div><div> int ret;</div><div> AVCodecContext *c = st->codec;</div><div><br></div><div> /* open the codec */</div><div> ret = avcodec_open2(c, codec, NULL);</div><div> if (ret < 0) {</div><div> fprintf(stderr, "Could not open video codec: ");</div><div> exit(1);</div><div> }</div><div><br></div><div> /* allocate and init a re-usable frame */</div><div> frame = av_frame_alloc();</div><div> if (!frame) {</div><div> fprintf(stderr, "Could not allocate video frame\n");</div><div> exit(1);</div><div> }</div><div> frame->format = c->pix_fmt;</div><div> frame->width = c->width;</div><div> frame->height = c->height;</div><div><br></div><div> /* Allocate the encoded raw picture. */</div><div> ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);</div><div> if (ret < 0) {</div><div> fprintf(stderr, "Could not allocate picture: ");</div><div> exit(1);</div><div> }</div><div><br></div><div> /* If the output format is not YUV420P, then a temporary YUV420P</div><div> * picture is needed too. It is then converted to the required</div><div> * output format. */</div><div> if (c->pix_fmt != AV_PIX_FMT_YUV420P) {</div><div> ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);</div><div> if (ret < 0) {</div><div> fprintf(stderr, "Could not allocate temporary picture:");</div><div> exit(1);</div><div> }</div><div> }</div><div><br></div><div> /* copy data and linesize picture pointers to frame */</div><div> *((AVPicture *)frame) = dst_picture;</div><div>}</div></div> </div></body>
</html>