added mutex to video processor

This commit is contained in:
csoler 2015-08-24 21:42:01 -04:00
parent 3f1ebca803
commit da69db39d6
2 changed files with 38 additions and 16 deletions

View File

@ -23,12 +23,13 @@ extern "C" {
#include <libavutil/mathematics.h> #include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h> #include <libavutil/samplefmt.h>
} }
#define DEBUG_MPEG_VIDEO 1 //#define DEBUG_MPEG_VIDEO 1
VideoProcessor::VideoProcessor() VideoProcessor::VideoProcessor()
:_encoded_frame_size(176,144) :_encoded_frame_size(176,144) , vpMtx("VideoProcessor")
{ {
_decoded_output_device = NULL ; _decoded_output_device = NULL ;
//_encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_JPEG_VIDEO; //_encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_JPEG_VIDEO;
//_encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_DDWT_VIDEO; //_encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_DDWT_VIDEO;
_encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_MPEG_VIDEO; _encoding_current_codec = VIDEO_PROCESSOR_CODEC_ID_MPEG_VIDEO;
@ -44,6 +45,19 @@ VideoProcessor::VideoProcessor()
_last_bw_estimate_out_TS = time(NULL) ; _last_bw_estimate_out_TS = time(NULL) ;
} }
VideoProcessor::~VideoProcessor()
{
// clear encoding queue
RS_STACK_MUTEX(vpMtx) ;
while(!_encoded_out_queue.empty())
{
_encoded_out_queue.back().clear() ;
_encoded_out_queue.pop_back() ;
}
}
bool VideoProcessor::processImage(const QImage& img,uint32_t size_hint) bool VideoProcessor::processImage(const QImage& img,uint32_t size_hint)
{ {
VideoCodec *codec ; VideoCodec *codec ;
@ -68,8 +82,8 @@ bool VideoProcessor::processImage(const QImage& img,uint32_t size_hint)
if(codec->encodeData(img.scaled(_encoded_frame_size,Qt::IgnoreAspectRatio,Qt::SmoothTransformation),size_hint,chunk) && chunk.size > 0) if(codec->encodeData(img.scaled(_encoded_frame_size,Qt::IgnoreAspectRatio,Qt::SmoothTransformation),size_hint,chunk) && chunk.size > 0)
{ {
RS_STACK_MUTEX(vpMtx) ;
_encoded_out_queue.push_back(chunk) ; _encoded_out_queue.push_back(chunk) ;
_total_encoded_size_out += chunk.size ; _total_encoded_size_out += chunk.size ;
} }
@ -77,6 +91,8 @@ bool VideoProcessor::processImage(const QImage& img,uint32_t size_hint)
if(now > _last_bw_estimate_out_TS) if(now > _last_bw_estimate_out_TS)
{ {
RS_STACK_MUTEX(vpMtx) ;
_estimated_bandwidth_out = uint32_t(0.75*_estimated_bandwidth_out + 0.25 * (_total_encoded_size_out / (float)(now - _last_bw_estimate_out_TS))) ; _estimated_bandwidth_out = uint32_t(0.75*_estimated_bandwidth_out + 0.25 * (_total_encoded_size_out / (float)(now - _last_bw_estimate_out_TS))) ;
_total_encoded_size_out = 0 ; _total_encoded_size_out = 0 ;
@ -98,6 +114,7 @@ bool VideoProcessor::processImage(const QImage& img,uint32_t size_hint)
bool VideoProcessor::nextEncodedPacket(RsVOIPDataChunk& chunk) bool VideoProcessor::nextEncodedPacket(RsVOIPDataChunk& chunk)
{ {
RS_STACK_MUTEX(vpMtx) ;
if(_encoded_out_queue.empty()) if(_encoded_out_queue.empty())
return false ; return false ;
@ -157,20 +174,23 @@ void VideoProcessor::receiveEncodedData(const RsVOIPDataChunk& chunk)
return ; return ;
} }
_total_encoded_size_in += chunk.size ;
time_t now = time(NULL) ;
if(now > _last_bw_estimate_in_TS)
{ {
_estimated_bandwidth_in = uint32_t(0.75*_estimated_bandwidth_in + 0.25 * (_total_encoded_size_in / (float)(now - _last_bw_estimate_in_TS))) ; RS_STACK_MUTEX(vpMtx) ;
_total_encoded_size_in += chunk.size ;
_total_encoded_size_in = 0 ; time_t now = time(NULL) ;
_last_bw_estimate_in_TS = now ;
if(now > _last_bw_estimate_in_TS)
{
_estimated_bandwidth_in = uint32_t(0.75*_estimated_bandwidth_in + 0.25 * (_total_encoded_size_in / (float)(now - _last_bw_estimate_in_TS))) ;
_total_encoded_size_in = 0 ;
_last_bw_estimate_in_TS = now ;
#ifdef DEBUG_VIDEO_OUTPUT_DEVICE #ifdef DEBUG_VIDEO_OUTPUT_DEVICE
std::cerr << "new bw estimate (in): " << _estimated_bandwidth_in << std::endl; std::cerr << "new bw estimate (in): " << _estimated_bandwidth_in << std::endl;
#endif #endif
}
} }
if(!codec->decodeData(chunk,img)) if(!codec->decodeData(chunk,img))
{ {
@ -794,7 +814,7 @@ bool FFmpegVideo::decodeData(const RsVOIPDataChunk& chunk,QImage& image)
buff = decoding_buffer.data ; buff = decoding_buffer.data ;
} }
memcpy(buff,chunk.data+HEADER_SIZE,chunk.size - HEADER_SIZE) ; memcpy(buff,&((unsigned char*)chunk.data)[HEADER_SIZE],chunk.size - HEADER_SIZE) ;
int got_frame = 0 ; int got_frame = 0 ;
int len = avcodec_decode_video2(decoding_context,decoding_frame_buffer,&got_frame,&decoding_buffer) ; int len = avcodec_decode_video2(decoding_context,decoding_frame_buffer,&got_frame,&decoding_buffer) ;

View File

@ -96,7 +96,7 @@ class VideoProcessor
{ {
public: public:
VideoProcessor() ; VideoProcessor() ;
virtual ~VideoProcessor() {} virtual ~VideoProcessor() ;
enum CodecId { enum CodecId {
VIDEO_PROCESSOR_CODEC_ID_UNKNOWN = 0x0000, VIDEO_PROCESSOR_CODEC_ID_UNKNOWN = 0x0000,
@ -167,5 +167,7 @@ class VideoProcessor
float _estimated_bandwidth_out ; float _estimated_bandwidth_out ;
float _target_bandwidth_out ; float _target_bandwidth_out ;
RsMutex vpMtx ;
}; };