[Libav-user] Parsing frames from camera

BradJones b.mcdonagh at sky.com
Tue Apr 25 18:05:29 EEST 2017


When trying to create a simple loop which continously plays frames to screen
parsed from TCP socket I have gotten to the point where I am getting a
unsupported media container error from the player I am feeding with packets
and the thing is this error is sometimes after 5 seconds of stream playing
fine or sometimes up to even 30 seconds of stream playing fine before it
errors out. I have pasted my code and was hoping somebody could have a look
and see if they see anything I am doing wrong myself in the code which I
suspect I am.

 int frame, got_picture, len;
		               int inbuf_len = 0;
		               int inbuf_start = 0;
		               bool running = true;
		               AVFrame *picture;
		               AVCodec *codec;
		               AVCodecContext *c= NULL;
		               codec = avcodec_find_decoder(AV_CODEC_ID_H264);
		               if (!codec) {
		                   Logger::Error( "codec not found\n");
		                   return;// -1;//return -1exit(1);
		               }

		               c = avcodec_alloc_context3(codec);


		               picture = av_frame_alloc();

		               if (avcodec_open2(c, codec, NULL) < 0) {
		            	   Logger::Info("could not open codec\n");
		                   return;// -1;
		               }

		                 Logger::Info("playStreamToScreen after avopen ");
		                 frame = 0;
		                 AVPacket packet2;
		                 AVCodecParserContext* parser =
av_parser_init(AV_CODEC_ID_H264);
		                 parser->flags |= PARSER_FLAG_ONCE;

		                 float inv_fps2;// = 4;
		                 int ii = 0;
		                 inv_fps2 = 1e6/9.98;;//av_q2d(c->time_base);
		                 c->time_base = (AVRational){1,10};
		                 c->bit_rate = 251000;
		                 c->width = 352;
		                 c->height = 288;
		                 c->pix_fmt = AV_PIX_FMT_YUV420P;
		                 hasLoggedInz = true;
		                 int gotPictureAmount = 0;
		                 bool hasStartedPlaying = false;
		                 int pictureCounter = 0;
		                 AVPacket* pkt = av_packet_alloc();
		                 struct timeval t;
		                 int BUF_SIZE = 16384 * 10;
		                 gettimeofday(&t, 0);
		                 uint8_t inbuf[BUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
		                 //memset(inbuf + BUF_SIZE, 0,
AV_INPUT_BUFFER_PADDING_SIZE);
		                 uint8_t *indata;
		                 while (running)
		                 {



		                		    size_t read = tcp_socket_.Read((char*)inbuf,
BUF_SIZE, pp::BlockUntilComplete());
		                        	indata = inbuf;
		                        	 bool has_packets_ = false;
		                             while (read > 0)
		                             {

		                            	 	 int   lenz = av_parser_parse2(parser, c,
&pkt->data, &pkt->size,
		                                             indata, read,
													 0, 0, AV_NOPTS_VALUE);


		                                	   indata  += lenz;
		                                	   read -= lenz;


		                                 if (pkt->size)
		                                 {
		                                	     //Logger::Error("%d is pts resparser
= %d and size is %d", pkt->pts, lenz, pkt->size);
		                                         gotPictureAmount++;
		                                         auto es_packet =
MakeUnique<ElementaryStreamPacket>(pkt->data, pkt->size);
		                                    	 es_packet->demux_id = 0;
		                                    	 int timestamp_ = 0;
		                                    	
es_packet->SetDuration(ToTimeTicks2(1, c->time_base));
		                                    	 es_packet->SetKeyFrame(pkt->flags ==
1);

		                                    	 auto pts =
ToTimeTicks2(gotPictureAmount, c->time_base);
		                                    	 auto dts =
ToTimeTicks2(gotPictureAmount, c->time_base);

		                                    	 es_packet->SetPts(pts);
		                                    	 es_packet->SetDts(dts);
		                                    	 pictureCounter++;

		                                    	 int32_t re =
video_stream->AppendPacket(es_packet->GetESPacket());
		                                    	 Logger::Error("res = %d appending
flags = %d", re, pkt->flags);
		                                    	 while(get_time_diff(t) < inv_fps2 )
{
		                                    		 usleep(100);
		                                    	 }

		                                    	  gettimeofday(&t, 0);
		                                    	 if(pictureCounter > 100 &&
!hasStartedPlaying){

		                                    		 int rt = player_->Play();
		                                    		 Logger::Error("play message result
%d", rt);
		                                    		 hasStartedPlaying = true;
		                                    	 }
		                                    	 	 break;
		                                    	  }
		                             }
		                     }



--
View this message in context: http://libav-users.943685.n4.nabble.com/Parsing-frames-from-camera-tp4663317.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list