#include "stream.hpp"
|
|
extern "C"{
|
#include <libavcodec/avcodec.h>
|
}
|
|
#include "../ffmpeg/format/FormatIn.hpp"
|
#include "../ffmpeg/data/CodedData.hpp"
|
#include "../ffmpeg/log/log.hpp"
|
using namespace logif;
|
|
namespace cffmpeg_wrap{
|
stream::stream(ffwrapper::FormatIn *in, const int maxSize)
|
:streamRef_(in)
|
,max_size_(maxSize)
|
,audio_(false)
|
{}
|
|
stream::~stream(){
|
std::lock_guard<std::mutex> locker(mutex_avpkt_);
|
list_pkt_.clear();
|
}
|
|
int stream::SetPacket(const CPacket &pkt){
|
if (pkt.data){
|
// 如果包是音频包,但是不使用音频,直接返回
|
if (!audio_ && streamRef_->isAudioPkt(&pkt.data->getAVPacket())){
|
return 0;
|
}
|
|
std::lock_guard<std::mutex> locker(mutex_avpkt_);
|
list_pkt_.push_back(pkt);
|
while(list_pkt_.size() > max_size_/2*3){
|
CPacket &tmpkt = list_pkt_.front();
|
if (tmpkt.data->getAVPacket().flags & AV_PKT_FLAG_KEY){
|
break;
|
}
|
list_pkt_.pop_front();
|
}
|
|
return list_pkt_.size();
|
}
|
return 0;
|
}
|
|
void stream::GetPacket(unsigned char **pktData, int *size, int *key){
|
std::lock_guard<std::mutex> l(mutex_avpkt_);
|
if(list_pkt_.empty()){
|
return;
|
}
|
|
auto data = list_pkt_.front();
|
list_pkt_.pop_front();
|
|
auto pkt = data.data->getAVPacket();
|
*key = pkt.flags & AV_PKT_FLAG_KEY;
|
*size = pkt.size;
|
*pktData = (unsigned char *)malloc(*size);
|
memcpy(*pktData, pkt.data, pkt.size);
|
}
|
}
|