Commit 2d31d743 by lenz su

传输优化

parent 2f2bd13a
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.2, 2020-01-05T19:03:06. -->
<!-- Written by QtCreator 4.10.2, 2020-01-07T23:03:46. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -343,17 +343,19 @@ bool VideoDecoder::decode(uint8_t *inputbuf, int frame_size, uint8_t *&outBuf, i
{
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = inputbuf;
pkt.size = frame_size;
if (avcodec_send_packet(pCodecCtx, &pkt) != 0)
int resSend=avcodec_send_packet(pCodecCtx, &pkt);
if (resSend!= 0)
{
fprintf(stderr, "input AVPacket to decoder failed!\n");
cout<<"fangle fangle send "<<resSend<<" "<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
av_packet_unref(&pkt);
return 0;
}
// while (0 == avcodec_receive_frame(pCodecCtx, pFrame))
while(1)
{
int ret = avcodec_receive_frame(pCodecCtx, pFrame);
......@@ -365,6 +367,7 @@ bool VideoDecoder::decode(uint8_t *inputbuf, int frame_size, uint8_t *&outBuf, i
break;
}
cout<<"fangle fangle receive"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
//前面初始化解码器的时候 并没有设置视频的宽高信息,
//因为h264的每一帧数据都带有编码的信息,当然也包括这些宽高信息了,因此解码完之后,便可以知道视频的宽高是多少
//这就是为什么 初始化编码器的时候 需要初始化高度,而初始化解码器却不需要。
......
......@@ -25,6 +25,7 @@ NALUParsing::NALUParsing()
int NALUParsing::inputH264Data(uint8_t *buf, int len)
{
lock=true;
memcpy(mH264Buffer + mBufferSize, buf, len);
mBufferSize += len;
......@@ -35,6 +36,10 @@ int NALUParsing::inputH264Data(uint8_t *buf, int len)
T_NALU *NALUParsing::getNextFrame()
{
QDateTime current_date_time =QDateTime::currentDateTime();
/*根据h264文件的特性 逐个字节搜索 直到遇到h264的帧头 视为获取到了完整的一帧h264视频数据*/
/// 关于起始码startcode的两种形式:3字节的0x000001和4字节的0x00000001
......@@ -63,13 +68,14 @@ T_NALU *NALUParsing::getNextFrame()
lock=false;
return NULL;
}
cout<<"get frame head"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl;
///然后查找下一个起始码查找第一个起始码
int pos_2 = pos + StartCode; //记录当前处理的数据偏移量
int StartCode_2 = 0;
while((mBufferSize - pos_2)>4)
{
Buf = mH264Buffer + pos_2;
///查找起始码(0x00000001)
if(Buf[0]==0 && Buf[1]==0 && Buf[2] ==0 && Buf[3] ==1)
......@@ -84,35 +90,27 @@ T_NALU *NALUParsing::getNextFrame()
lock=false;
return NULL;
}
cout<<"fangle fangle "<<endl;
/// 现在 pos和pos_2之间的数据就是一帧数据了
/// 把他取出来
cout<<"time get frame:"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
cout<<"get frame foot"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
///由于传递给ffmpeg解码的数据 需要带上起始码 因此这里的nalu带上了起始码
Buf = mH264Buffer + pos; //这帧数据的起始数据(包含起始码)
int naluSize = pos_2 - pos; //nalu数据大小 包含起始码
T_NALU * nalu = AllocNALU(naluSize, mVideoType);//分配nal 资源
if (mVideoType == T_NALU_H264){
T_H264_NALU_HEADER *nalu_header = (T_H264_NALU_HEADER *)(Buf + StartCode);
nalu->nalu.h264Nalu.startcodeprefix_len = StartCode; //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
nalu->nalu.h264Nalu.len = naluSize; //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
nalu->nalu.h264Nalu.forbidden_bit = 0; //! should be always FALSE
nalu->nalu.h264Nalu.nal_reference_idc = nalu_header->NRI; //! NALU_PRIORITY_xxxx
nalu->nalu.h264Nalu.nal_unit_type = nalu_header->TYPE; //! NALU_TYPE_xxxx
nalu->nalu.h264Nalu.lost_packets = false; //! true, if packet loss is detected
memcpy(nalu->nalu.h264Nalu.buf, Buf, naluSize); //! contains the first byte followed by the EBSP
}
else{
T_H265_NALU_HEADER *nalu_header = (T_H265_NALU_HEADER *)(Buf + StartCode);
T_H264_NALU_HEADER *nalu_header = (T_H264_NALU_HEADER *)(Buf + StartCode);
nalu->nalu.h264Nalu.startcodeprefix_len = StartCode; //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
nalu->nalu.h264Nalu.len = naluSize; //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
nalu->nalu.h264Nalu.forbidden_bit = 0; //! should be always FALSE
nalu->nalu.h264Nalu.nal_reference_idc = nalu_header->NRI; //! NALU_PRIORITY_xxxx
nalu->nalu.h264Nalu.nal_unit_type = nalu_header->TYPE; //! NALU_TYPE_xxxx
nalu->nalu.h264Nalu.lost_packets = false; //! true, if packet loss is detected
memcpy(nalu->nalu.h264Nalu.buf, Buf, naluSize); //! contains the first byte followed by the EBSP
nalu->nalu.h265Nalu.startCodeLen = StartCode; //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
nalu->nalu.h265Nalu.len = naluSize; //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
nalu->nalu.h265Nalu.h265NaluHeader = *nalu_header;
memcpy(nalu->nalu.h265Nalu.buf, Buf, naluSize); //! contains the first byte followed by the EBSP
}
/// 将这一帧数据去掉
/// 把后一帧数据覆盖上来
......@@ -120,6 +118,9 @@ T_NALU *NALUParsing::getNextFrame()
memmove(mH264Buffer, mH264Buffer + pos_2, leftSize);
mBufferSize = leftSize;
lock=false;
current_date_time =QDateTime::currentDateTime();
cout<<"start get frame end"<<current_date_time.toString("yyyyMMddhhmmsszzz").toStdString()<<endl;
return nalu;
}
......@@ -161,6 +162,8 @@ void NALUParsing::FreeNALU(T_NALU *n)
}
void NALUParsing::bufferCopy(uchar* start,uchar* end,size_t lenght){
}
......@@ -7,10 +7,10 @@
#include <thread>
#include "usb/hidapi.h"
#include <QDebug>
#include "QDateTime"
#include "ReadVideoFileThread.h"
#include "hid_thread.h"
#include "iostream"
#if defined(WIN32)
#include <WinSock2.h>
#include <Windows.h>
......@@ -28,7 +28,7 @@ void Sleep(long mSeconds)
#endif
static FILE * fpOut = fopen("haha", "wb");
void ReadVideoFileThread::run()
{
mVideoDecoder->openDecoder(id);
......@@ -38,14 +38,16 @@ void ReadVideoFileThread::run()
{
//从前面读到的数据中获取一个nalu
if(mNaluParsing->lock){
msleep(10);
msleep(5);
continue;
}
T_NALU* nalu = mNaluParsing->getNextFrame();
if (nalu == nullptr){
msleep(10);
msleep(5);
continue;
}
// fwrite(nalu->nalu.h264Nalu.buf, 1, nalu->nalu.h264Nalu.len, fpOut);
cout<<"time thread get frame:"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
uint8_t *bufferYUV = nullptr;
int width;
......@@ -56,21 +58,23 @@ void ReadVideoFileThread::run()
if (bufferYUV != nullptr)
{
//然后传给主线程显示
cout<<"time thread get do display:"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
doDisplayVideo(bufferYUV, width, height, frameNum++);
}
NALUParsing::FreeNALU(nalu);
msleep(10);
}
fflush(fpOut);
fclose(fpOut);
mVideoDecoder->closeDecoder();
}
///显示视频数据,此函数不宜做耗时操作,否则会影响播放的流畅性。
void ReadVideoFileThread::doDisplayVideo(const uint8_t *yuv420Buffer, const int &width, const int &height, const int &frameNum)
{
// fprintf(stderr, "%s \n", __FUNCTION__);
if (mVideoCallBack != nullptr)
{
VideoFramePtr videoFrame = std::make_shared<VideoFrame>();
......
......@@ -350,8 +350,6 @@ Exit:
void hid_thread::run()
{
int bufferlength=0;
HRESULT hr;
REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC;
......@@ -420,17 +418,17 @@ void hid_thread::run()
memset(buffer, 0, 2048);
res = hid_read(hid_fd,buffer, 2048);
if(res!=2048){
msleep(100);
if(res<=0){
msleep(10);
continue;
}
cout<<"pUsbFram1:"<<pUsbFram1->id<<endl;
cout<<"pUsbFram1->head"<<pUsbFram1->head<<endl;
cout<<"pUsbFram1->length"<<pUsbFram1->length<<endl;
//cout<<"pUsbFram1:"<<pUsbFram1->id<<endl;
// cout<<"pUsbFram1->head"<<pUsbFram1->head<<endl;
// cout<<"pUsbFram1->length"<<pUsbFram1->length<<endl;
cout<<"pUsbFram2:"<<pUsbFram2->id<<endl;
cout<<"pUsbFram2->head"<<pUsbFram2->head<<endl;
cout<<"pUsbFram2->length"<<pUsbFram2->length<<endl;
// cout<<"pUsbFram2:"<<pUsbFram2->id<<endl;
// cout<<"pUsbFram2->head"<<pUsbFram2->head<<endl;
// cout<<"pUsbFram2->length"<<pUsbFram2->length<<endl;
if(0x44332222==pUsbFram1->head){
//查询角色返回
......@@ -504,7 +502,7 @@ void hid_thread::run()
}
if(isPlay==false){
cout<<"bu bo fang"<<endl;
cout<<"bu bo fang"<<endl;
mcb(SCREEN_SHOT_STOP);
continue;
}
......@@ -512,29 +510,22 @@ void hid_thread::run()
if(pUsbFram1->head == 0x44332211) {
while (pNaluParsing->lock) {
qDebug("delay 1 ");
msleep(10);
msleep(2);
}
cout<<"time send decode:"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
int nCount = pNaluParsing->inputH264Data(pUsbFram1->buff,pUsbFram1->length);
qDebug("fram id:%d %d ", pUsbFram1->id, pUsbFram1->length);
if((frameId+1)!=pUsbFram1->id){
qDebug("fram id error current id %d real id %d",frameId, pUsbFram1->id);
}
frameId=pUsbFram1->id;
}
if(pUsbFram2->head == 0x44332211) {
while (pNaluParsing->lock) {
qDebug("delay 2 ");
msleep(10);
msleep(2);
}
cout<<"time send decode:"<<QDateTime::currentDateTime().toString("hhmmsszzz").toStdString()<<endl;
int nCount = pNaluParsing->inputH264Data(pUsbFram2->buff,pUsbFram2->length);
if((frameId+1)!=pUsbFram2->id){
qDebug("fram id error current id %d real id %d",frameId, pUsbFram2->id);
}
qDebug("fram id:%d %d ", pUsbFram2->id, pUsbFram2->length);
frameId=pUsbFram2->id;
}
......
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -27,8 +27,7 @@ MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent)
av_register_all();
avformat_network_init();
avdevice_register_all();
mNaluParsing = new NALUParsing();
mNaluParsing->setVideoType(T_NALU_H264);
usb=new USBOperator;
......@@ -88,15 +87,20 @@ void MainWindow::slotBtnClick(bool isChecked)
}
else if (QObject::sender() == ui->pushButton_play)
{
mNaluParsing = new NALUParsing();
mNaluParsing->setVideoType(T_NALU_H264);
mReadVideoFileThread = new ReadVideoFileThread(mNaluParsing);
hidThread=new hid_thread(mNaluParsing,usb->getHidDevice());
hidThread->setCallback(onRole);
hidThread->start();
mReadVideoFileThread = new ReadVideoFileThread(mNaluParsing);
hidThread->isPlay=true;
mReadVideoFileThread->setVideoCallBack(this);
mReadVideoFileThread->start();
cout<<"kaishi"<<endl;
hidThread->start();
hidThread->isPlay=true;
/*if (isChecked) {
qDebug("start decode!");
mReadVideoFileThread->start();
......
......@@ -16,10 +16,12 @@ USBOperator::USBOperator(QObject *parent) : QThread(parent){
qDebug("usb hid open failed!");
}
if (hid_set_nonblocking(hid_fd, 1) < 0) {
qDebug("hid_set_nonblocking error");
}
QDateTime current_date_time =QDateTime::currentDateTime();
ssid ="pair-"+current_date_time.toString("yyyyMMddhhmmsszzz");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment