1 /*==================================================================
3 ! mardrone application AR-Drone for MeeGo
5 ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 ! Author:Kate Alhola kate.alhola@nokia.com
10 ! GNU Lesser General Public License Usage
11 ! This file may be used under the terms of the GNU Lesser
12 ! General Public License version 2.1 as published by the Free Software
13 ! Foundation and appearing in the file LICENSE.LGPL included in the
14 ! packaging of this file. Please review the following information to
15 ! ensure the GNU Lesser General Public License version 2.1 requirements
16 ! will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 *===================================================================*/
22 #include <QGraphicsView>
24 DroneVideo::DroneVideo()
26 droneHost.setAddress("192.168.1.1");
30 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
33 qDebug() << "videoThread::videoThread";
36 videoSock=new QUdpSocket();
37 videoSock->bind(QHostAddress::Any,5555);
43 void DroneVideo::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
46 if(!initialized) { // We need initialize QImage here because we don't know display depth before
49 depth=painter->device()->depth();
50 qDebug() << "depth=" << depth;
53 image=new QImage(320,240, QImage::Format_RGB32);
54 image->fill(0x555555);
57 image=new QImage(320,240, QImage::Format_RGB16);
61 p.drawLine(0,0,image->width(),image->height());
62 p.drawLine(image->width(),0,0,image->height());
63 update(boundingRect());
64 videoThread=new VideoThread(this,droneHost,image);
67 painter->drawImage(boundingRect(),*image,image->rect());
70 QRectF DroneVideo::boundingRect() const
72 return QRectF(0.0,0.0,size().width(),size().height());
76 void VideoThread::run()
79 #define ACQ_HEIGHT 240
81 memset(&controller,0,sizeof(controller));
82 memset(&picture,0,sizeof(picture));
83 pictureWidth= image->width();
84 pictureHeight=image->height();
85 int codec_type=UVLC_CODEC;
86 qDebug() << "videoThread::run()";
87 stateTimer=new QTimer();
88 connect(stateTimer,SIGNAL(timeout()),this,SLOT(timer()));
89 connect(videoSock,SIGNAL(readyRead()),this,SLOT(videoDataReady()));
90 qDebug() << "videoThread::run() 2";
92 num_picture_decoded=0;
93 /// Picture configuration
94 picture.format = PIX_FMT_YUV420P;
95 picture.width = pictureWidth;
96 picture.height = pictureHeight;
97 picture.framerate = 30;
98 picture.y_buf = (uint8_t*)(void*)vp_os_malloc((size_t) pictureWidth*pictureHeight );
99 picture.cr_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
100 picture.cb_buf = (uint8_t*)vp_os_malloc( pictureWidth*pictureHeight/4 );
101 picture.y_line_size = pictureWidth;
102 picture.cb_line_size = pictureWidth / 2;
103 picture.cr_line_size = pictureWidth / 2;
106 qDebug() << "videoThread::run() 3";
107 video_codec_open(&controller, (codec_type_t)UVLC_CODEC);
108 //stateTimer->start(1000);
109 qDebug() << "videoThread::run() initialized";
117 void VideoThread::timer()
119 // qDebug() << "thread Timer";
123 void VideoThread::sendVideoPort(QString cmd)
126 dgram=cmd.toLatin1();
127 qDebug() << "videoThread::sendCmd= " << cmd+"\n" << "to " << droneHost ;
128 videoSock->writeDatagram(dgram.data(),dgram.size(),droneHost,5555);
131 void VideoThread::videoDataReady()
134 QByteArray videoData;
138 videoData.resize(videoSock->pendingDatagramSize ());
139 l=videoSock->readDatagram(videoData.data(),videoData.size(),&host,&port);
140 qDebug() << "videoThread::videoDataReady" <<" l=" << l << "from" << host ;
141 decodeTransform(videoData);
144 void VideoThread::decodeTransform(QByteArray &videoData)
146 controller.in_stream.bytes = (uint32_t*)videoData.data();
147 controller.in_stream.used = videoData.size();
148 controller.in_stream.size = videoData.size();
149 controller.in_stream.index = 0;
150 controller.in_stream.length = 32;
151 controller.in_stream.code = 0;
153 bool_t got_image = FALSE;
154 //qDebug() <<"VideoThread::decodeTransform";
155 video_decode_blockline( &controller, &picture, &got_image );
156 //qDebug() <<"VideoThread::decodeTransform 2";
157 //video_decode_picture( &controller, &picture, &stream, &got_image );
160 qDebug() <<"VideoThread::decodeTransform got image" << picture.width << picture.height << image->byteCount() << image->bytesPerLine();
161 // we got one picture
163 picture.complete = 1;
164 num_picture_decoded++;
165 vp_stages_YUV420P_to_RGB565(NULL,&picture,image->bits(),image->bytesPerLine());
168 qDebug() << "pic " << num_picture_decoded;