www.pudn.com > CameraRead.zip > processImage.h, change:2015-12-16,size:9460b


#ifndef PROCESSIMAGE_H
#define PROCESSIMAGE_H

#include <QtGui>
#include <iostream>
#include <string>
#include <stdio.h>
#include "videodevice.h"
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include "rtpsession.h"
#include "rtpsessionparams.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtptimeutilities.h"
#include "rtppacket.h"
#include "rtcpsrpacket.h"
#include "rtpsourcedata.h"
#include <QMessageBox>
#include <QMap>
#include <QComboBox>
struct Packet {
    uint32_t magic1;
    uint32_t magic2;
    uint32_t sum;
    uint32_t lenght;
    uchar body[];
};
typedef struct Buffer{
    unsigned int capacity;
    unsigned int destlength;
    unsigned int curlength;
    uchar *data;
}Buffer;
class MyThread : public QThread{
      Q_OBJECT
 #define BROADCAST_PORT 13145
 public:
     MyThread(QObject *parent = 0):QThread(parent){
     }
     ~MyThread(){
        if(udpfd > 0)
            ::close(udpfd);
     }
 protected:
     void run(){
#define MAXLINE 1024
         struct sockaddr_in servaddr,cliaddr;
         udpfd = socket(AF_INET,SOCK_DGRAM,0);
         if(udpfd < 0){

             return ;
         }
         servaddr.sin_family = AF_INET;
         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
         servaddr.sin_port = htons(BROADCAST_PORT);
         if(bind(udpfd,(sockaddr *)&servaddr,sizeof(servaddr)) < 0){
             return;
         }
         int n;
         socklen_t len = sizeof(cliaddr);
         char mesg[MAXLINE];
         for(;;){
             n = recvfrom(udpfd,mesg,MAXLINE,0,(sockaddr *)&cliaddr,&len);
             if(n < 0 && n != EINTR){
                 ::close(udpfd);
                 return ;
             }

             QString str(mesg);
             int index = str.indexOf(":");
             if(index < 0){
                 qDebug("mesg format error");
                 continue;
              }
             QString portnmstr = str.left(index);
             QString portstr = str.right(str.length()-index-1);
            // qDebug("portnm = %s",QByteArray(portnmstr.toLatin1()).data());
            // qDebug("port   = %s",QByteArray(portstr.toLatin1()).data());
             if(portnmstr.compare("port") != 0){
                 qDebug("mesg format error");
                 continue;
             }
             bool ok;
             unsigned short port;
             port = portstr.toShort(&ok);
             if(!ok){
                 qDebug("mesg format error");
                 continue;
             }
             QString newIPStr(QString(inet_ntoa(cliaddr.sin_addr)) +QString(":") + portstr);
             QMap<QString, int>::iterator mi;
             mi = ip_map.find(newIPStr);
             if(mi == ip_map.end()){
                 ip_map.insert(newIPStr,1);
                 emit sendServIp(newIPStr);
             }
         }
     }

private slots:
     void deleteServIp(QString newIPStr){
         QMap<QString, int>::iterator mi;
         mi = ip_map.find(newIPStr);
         if(mi != ip_map.end())
            ip_map.erase(mi);
     }
signals:
     void sendServIp(QString);
 private:
     int udpfd;
     QMap<QString,int> ip_map;
};

class MyRTPSession : public QObject,public RTPSession
{
    Q_OBJECT
 protected:
       void OnPollThreadStep();
       void OnRTCPCompoundPacket(RTCPCompoundPacket *pack,\
                                 const RTPTime &receivetime,\
                                 const RTPAddress *senderaddress);
       void OnBYEPacket(RTPSourceData *dat){
           if(dat->IsOwnSSRC()){
               return;
           }
           uint32_t ip;
           uint16_t port;

           if (dat->GetRTPDataAddress() != 0)
           {
               const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTPDataAddress());
               ip = addr->GetIP();
               port = addr->GetPort();
           }
           else if (dat->GetRTCPDataAddress() != 0)
           {
               const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTCPDataAddress());
               ip = addr->GetIP();
               port = addr->GetPort()-1;
           }
           else
               return;

           RTPIPv4Address dest(ip,port);
           DeleteDestination(dest);

           struct in_addr inaddr;
           inaddr.s_addr = htonl(ip);
           QString str(QString(inet_ntoa(inaddr)) + QString(":") +  QString::number(port));
           emit ServExit(str);
       }
       void OnRemoveSource(RTPSourceData *dat){
           if(dat->IsOwnSSRC())
               return ;
           if(dat->ReceivedBYE())
               return ;
           uint32_t ip;
           uint16_t port;

           if (dat->GetRTPDataAddress() != 0)
           {
               const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTPDataAddress());
               ip = addr->GetIP();
               port = addr->GetPort();
           }
           else if (dat->GetRTCPDataAddress() != 0)
           {
               const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTCPDataAddress());
               ip = addr->GetIP();
               port = addr->GetPort()-1;
           }
           else
               return;

           RTPIPv4Address dest(ip,port);
           DeleteDestination(dest);

           struct in_addr inaddr;
           inaddr.s_addr = htonl(ip);
           QString str(QString(inet_ntoa(inaddr)) + QString(":") +  QString::number(port));
           emit ServExit(str);
       }
 signals:
       void Process();
       void ServExit(QString);
};

class ProcessImage : public QWidget
{
    Q_OBJECT
public:
    ProcessImage(QWidget *parent=0);
    ~ProcessImage();

private:
    //QPainter *painter;
    MyThread *thread;
    QLabel *label;
    QLabel *servip;
    QLabel *setip;
    //QLineEdit *lineEdit;
    QComboBox *comboBox;
    QImage *frame;
    QPushButton *save;
    QPushButton *setting;
    QTimer *timer;
    int rs;
    char *ipaddr;
    char *port;
    unsigned int len;
    unsigned char takeflag;
    uchar *p;
    Buffer     *buffer;
    MyRTPSession session;
    uint8_t ServIpNum[4];
    uint16_t ServPortNum;
    uint8_t ServFlag;
    int convert_yuv_to_rgb_pixel(int y, int u, int v);
    int convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
    //VideoDevice *vd;
    void save_mjpeg();
    Buffer *BufferCreate(unsigned int capacity);
signals:
    void ServExitToThread(QString);
private slots:
    void paintEvent(QPaintEvent *);
    void display_error(QString err);
    void take_a_photo();
    void ServExit(QString str){
         label->clear();
         label->setText(tr("no vedio signal"));
         servip->clear();
         int index = comboBox->findText(str);
         comboBox->removeItem(index);
         emit ServExitToThread(str);
    }
    void enableSettingButton(const QString &text)
    {
        setting->setEnabled(!text.isEmpty());
    }
    void addServIP(QString newIPStr){
         comboBox->addItem(newIPStr);
    }
    void set_servip(){
         QString str = comboBox->currentText();
         char *ipstr;
         uint8_t DestIp[4];
         uint16_t portnum;
         int index = str.indexOf(":");
         if(index < 0){
             QMessageBox::warning(this,tr("error"),tr("ip:port format error(eg:192.168.0.1:8000)"),QMessageBox::Yes);
             return;
         }
         QString ip = str.left(index);
         QString port = str.right(str.length()-index-1);

         QByteArray ipbyte(ip.toLatin1());
         ipstr = ipbyte.data();
         if(inet_pton(AF_INET,ipstr,DestIp) != 1){
             QMessageBox::warning(this,tr("error"),tr("ip:port format error(eg:192.168.0.1:8000)"),QMessageBox::Yes);
             return ;
         }

         bool ok;
         portnum = port.toShort(&ok);
         if(!ok){
             QMessageBox::warning(this,tr("error"),tr("ip:port format error(eg:192.168.0.1:8000)"),QMessageBox::Yes);
             return ;
         }
         if(ServFlag == 1){
             //RTPIPv4Address Deleteaddr(ServIpNum,ServPortNum);
             //session.DeleteDestination(Deleteaddr);
             session.BYEDestroy(RTPTime(3.0),"servip exchange",strlen("servip exchange"));
             label->clear();
         }
         RTPSessionParams sessionparams;
         RTPUDPv4TransmissionParams transparams;
         sessionparams.SetOwnTimestampUnit(1.0/90000.0);
         sessionparams.SetAcceptOwnPackets(false);
         transparams.SetPortbase(8000);
         sessionparams.SetUsePollThread(true);
         int status = session.Create(sessionparams,&transparams);
         if(status < 0){
             std::cerr << RTPGetErrorString(status) << std::endl;
              QMessageBox::warning(this,tr("error"),tr("session.Create failed"),QMessageBox::Yes);
         }
         session.SetDefaultPayloadType(25);
         session.SetDefaultMark(false);
         session.SetDefaultTimestampIncrement(3000);
         session.SetReceiveMode(RTPTransmitter::AcceptSome);
         RTPIPv4Address Destaddr(DestIp,portnum);
         RTPIPv4Address DestRTCPaddr(DestIp,portnum+1);
         session.AddDestination(Destaddr);
         session.AddToAcceptList(Destaddr);
         session.AddToAcceptList(DestRTCPaddr);
         servip->setText(tr("ServIP:")+ str);
         label->setText(tr("no vedio signal"));
         memcpy(ServIpNum,DestIp,4);
         ServPortNum = portnum;
         ServFlag = 1;
    }
};

#endif