<div dir="ltr"><div dir="ltr">Yes video stream from my cameras decoded with H264.<br><br>Yu can see my connection code below</div><div dir="ltr"><span style="color:rgb(0,0,0);font-family:"Courier New""><br></span></div><div dir="ltr"><span style="color:rgb(0,0,0);font-family:"Courier New"">Few more questions: </span><br></div><div dir="ltr"><pre style="font-family:"Courier New""><ul><li><font color="#000000">What proper way to "share" videostream for example with my recorder instead of adding packets in a loop?
To implemend </font></li><li><font color="#000000"><b>prerecord feature(</b>for motion detection record<b>)</b> should I use </font>AVFormatContext buffer or something else?
</li><li><font color="#000000">Also maybe somewhere to be some best practices of - <b>"record video stream by time partitions"</b></font></li></ul></pre><div><br></div><div># # # # # # # # # </div><div><pre style="font-family:"Courier New""><span style="color:rgb(0,0,128);font-weight:bold">int </span><span style="color:rgb(0,128,128)">CameraConnection</span><font color="#000000">::Connect(</font><span style="color:rgb(0,0,128);font-weight:bold">char </span><font color="#000000">*newVideoURI) {<br>    </font><span style="color:rgb(31,84,46);font-weight:bold">LOGD</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"Connect"</span><font color="#000000">);<br>    </font><span style="color:rgb(55,31,128)">AVCodec </span><font color="#000000">*streamCodec = </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">; // </font><b style=""><font color="#ff0000">AV_CODEC_ID_H264</font></b><font color="#000000"><br>    </font><span style="color:rgb(102,14,122)">videoURI </span><font color="#000000">= newVideoURI;<br><br>    </font><span style="color:rgb(55,31,128)">AVDictionary </span><font color="#000000">*inputOptionsDict = </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">;<br>    av_dict_set(&inputOptionsDict, </font><span style="color:rgb(0,128,0);font-weight:bold">"rtsp_transport"</span><font color="#000000">, </font><span style="color:rgb(0,128,0);font-weight:bold">"tcp"</span><font color="#000000">, </font><span style="color:rgb(0,0,255)">0</span><font color="#000000">); </font><span style="color:rgb(128,128,128);font-style:italic">// working faster with TCP<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">av_dict_set(&inputOptionsDict, </font><span style="color:rgb(0,128,0);font-weight:bold">"reorder_queue_size"</span><font color="#000000">, </font><span style="color:rgb(0,128,0);font-weight:bold">"50"</span><font color="#000000">, </font><span style="color:rgb(0,0,255)">0</span><font color="#000000">);<br>    av_dict_set(&inputOptionsDict, </font><span style="color:rgb(0,128,0);font-weight:bold">"buffer_size"</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">buffer2sec</span><font color="#000000">, </font><span style="color:rgb(0,0,255)">0</span><font color="#000000">);<br>    </font><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(avformat_open_input(&</font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">videoURI</span><font color="#000000">, </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">, &inputOptionsDict)!=</font><span style="color:rgb(0,0,255)">0</span><font color="#000000">){<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"[%s] FAILED to open input"</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">videoURI</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Couldn't open file<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">}  </font><span style="color:rgb(128,128,128);font-style:italic">// Open video file / RTSP<br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(avformat_find_stream_info(</font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">, </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">)<</font><span style="color:rgb(0,0,255)">0</span><font color="#000000">){<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"[%s] FAILED to find stream info"</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">videoURI</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Couldn't find stream information<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">} </font><span style="color:rgb(128,128,128);font-style:italic">// Retrieve stream information<br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">av_dump_format(</font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">, </font><span style="color:rgb(0,0,255)">0</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">videoURI</span><font color="#000000">, </font><span style="color:rgb(0,0,255)">0</span><font color="#000000">); </font><span style="color:rgb(128,128,128);font-style:italic">// Dump information about file onto standard error<br></span><span style="color:rgb(128,128,128);font-style:italic"><br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><span style="color:rgb(102,14,122)">videoStreamIdx </span><font color="#000000">= -</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">;<br>    </font><span style="color:rgb(102,14,122)">audioStreamIdx </span><font color="#000000">= -</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">;<br>    </font><span style="color:rgb(0,0,128);font-weight:bold">int </span><font color="#000000">i;<br>    </font><span style="color:rgb(0,0,128);font-weight:bold">for</span><font color="#000000">(i=</font><span style="color:rgb(0,0,255)">0</span><font color="#000000">; i<</font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">-></font><span style="color:rgb(102,14,122)">nb_streams</span><font color="#000000">; i++) { </font><span style="color:rgb(128,128,128);font-style:italic">// Find the first video stream<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span><span style="color:rgb(0,128,128)">AVMediaType </span><font color="#000000">codecType = </font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">-></font><span style="color:rgb(102,14,122)">streams</span><font color="#000000">[i]-></font><span style="color:rgb(102,14,122)">codecpar</span><font color="#000000">-></font><span style="color:rgb(102,14,122)">codec_type</span><font color="#000000">;<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(codecType == </font><span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">AVMEDIA_TYPE_VIDEO</span><font color="#000000">) {<br>            </font><span style="color:rgb(102,14,122)">videoStreamIdx</span><font color="#000000">=i;<br>            </font><span style="color:rgb(0,0,128);font-weight:bold">break</span><font color="#000000">;<br>        }<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(codecType == </font><span style="color:rgb(102,14,122);font-weight:bold;font-style:italic">AVMEDIA_TYPE_AUDIO</span><font color="#000000">) {<br>            </font><span style="color:rgb(102,14,122)">audioStreamIdx</span><font color="#000000">=i;<br>            </font><span style="color:rgb(0,0,128);font-weight:bold">break</span><font color="#000000">;<br>        }<br>    }<br><br>    </font><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(</font><span style="color:rgb(102,14,122)">videoStreamIdx</span><font color="#000000">==-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">){<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"[%s] Didn't find a video stream"</span><font color="#000000">, </font><span style="color:rgb(102,14,122)">videoURI</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Didn't find a video stream<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">}<br>    </font><span style="color:rgb(102,14,122)">streamVideoCodecContext </span><font color="#000000">= </font><span style="color:rgb(102,14,122)">streamFormatContext</span><font color="#000000">-></font><span style="color:rgb(102,14,122)">streams</span><font color="#000000">[</font><span style="color:rgb(102,14,122)">videoStreamIdx</span><font color="#000000">]-></font><span style="color:rgb(102,14,122)">codec</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Get a pointer to the codec context for the video stream<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">streamCodec = avcodec_find_decoder(</font><span style="color:rgb(102,14,122)">streamVideoCodecContext</span><font color="#000000">-></font><span style="color:rgb(102,14,122)">codec_id</span><font color="#000000">); </font><span style="color:rgb(128,128,128);font-style:italic">// Find the decoder for the video stream<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(streamCodec == </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">) {<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"Unsupported codec"</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Codec not found<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">}<br><br>    </font><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(avcodec_open2(</font><span style="color:rgb(102,14,122)">streamVideoCodecContext</span><font color="#000000">, streamCodec, </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">)<</font><span style="color:rgb(0,0,255)">0</span><font color="#000000">){<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"Could not open codec"</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">; </font><span style="color:rgb(128,128,128);font-style:italic">// Could not open codec<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><font color="#000000">}<br><br><br>    </font><span style="color:rgb(102,14,122)">decodedFrame </span><font color="#000000">= av_frame_alloc(); </font><span style="color:rgb(128,128,128);font-style:italic">// Allocate video frame<br></span><span style="color:rgb(128,128,128);font-style:italic">    </span><span style="color:rgb(0,0,128);font-weight:bold">if</span><font color="#000000">(</font><span style="color:rgb(102,14,122)">decodedFrame </span><font color="#000000">== </font><span style="color:rgb(0,0,128);font-weight:bold">nullptr</span><font color="#000000">){<br>        </font><span style="color:rgb(31,84,46);font-weight:bold">LOGE</span><font color="#000000">(</font><span style="color:rgb(0,128,0);font-weight:bold">"AVFrame could not be allocated 1"</span><font color="#000000">);<br>        </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><font color="#000000">-</font><span style="color:rgb(0,0,255)">1</span><font color="#000000">;<br>    }<br><br>    </font><span style="color:rgb(102,14,122)">isConnected </span><font color="#000000">= </font><span style="color:rgb(0,0,128);font-weight:bold">true</span><font color="#000000">;<br>    StartDecodeThread();<br>    </font><span style="color:rgb(0,0,128);font-weight:bold">return </span><span style="color:rgb(0,0,255)">0</span><font color="#000000">;<br>}</font></pre><pre style="font-family:"Courier New""><font color="#000000">
</font></pre></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">пт, 15 мар. 2019 г. в 04:31, Carl Eugen Hoyos <<a href="mailto:ceffmpeg@gmail.com">ceffmpeg@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">2019-03-14 14:26 GMT+01:00, Ivan Lobanov <<a href="mailto:iokunversion0@gmail.com" target="_blank">iokunversion0@gmail.com</a>>:<br>
> I'm using libavcodec libreries for my android app.<br>
> To recive/decode/save/play RTSP video stream from cameras.<br>
> Everything work well but avcodec_send_packet() working a little bit slow<br>
> and cause video delay while try to play 1080p video stream.(no problem when<br>
> playing smaller video streams)<br>
> is there any way to speed up avcodec_send_packet() execution?<br>
<br>
You mean a decoder? Which one?<br>
<br>
The h264 decoder has seen recent speedups on arm64.<br>
<br>
Carl Eugen<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="https://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">https://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br>
To unsubscribe, visit link above, or email<br>
<a href="mailto:libav-user-request@ffmpeg.org" target="_blank">libav-user-request@ffmpeg.org</a> with subject "unsubscribe".</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="color:rgb(136,136,136)">Best regards,<br>Lobanov Ivan<br></div><div style="color:rgb(136,136,136)">GDG Perm Leader</div><div style="color:rgb(136,136,136)">Red Distribution</div><div style="color:rgb(136,136,136)">Тел.: <a value="+79129820101" style="color:rgb(17,85,204)">+7 961 759 89 71</a><br>Skype: ningen-des</div></div></div></div></div></div></div></div></div></div>