Debian Codegraph @

samples/qtvidcap/avicapwnd.cpp

raw
1
2 #include "avicapwnd.h"
3 #include "avicapwnd.moc"
4
5 #include "v4lwindow.h"
6 #include "ccap.h"
7 #include "capproc.h"
8
9 #include <avm_except.h>
10 #include <avm_cpuinfo.h>
11 #include <avm_fourcc.h>
12 #include <avm_output.h>
13 #include <utils.h>
14
15
16 #include <qpushbutton.h>
17 #include <qlabel.h>
18 #include <qtabwidget.h>
19 #include <qtimer.h>
20 #include <qframe.h>
21 #include <qpainter.h>
22 #include <qmessagebox.h>
23 #include <qdatetime.h>
24
25 #define DECLARE_REGISTRY_SHORTCUT
26 #include <configfile.h>
27 #undef DECLARE_REGISTRY_SHORTCUT
28
29 #include <math.h>
30 #include <stdio.h>
31 #include <stdio.h>
32 #include <libgen.h>
33 //#include <sys/vfs.h>
34
35 extern int free_diskspace(avm::string path);
36 extern QString find_best_dir();
37 extern int find_keepfree(QString dirname);
38
39
40 /*
41 * Constructs a AviCapDialog which is a child of 'parent', with the
42 * name 'name' and widget flags set to 'f'
43 */
44 AviCapDialog::AviCapDialog( V4LWindow* pWnd ,CaptureConfig *conf)
45 : AviCapDlg( 0 ),
46 m_pWnd(pWnd), m_pProgress(0), m_bStarted(false)
47 {
48 my_config=conf;
49 last_dropped_enc=0;
50 last_dropped_cap=0;
51 last_synchfix=0;
52 last_synchfix_time=0;
53 use_dirpool=false;
54 i_am_mini=false;
55
56 connect(m_pMiniButton, SIGNAL(clicked()), this, SLOT(mini()));
57 connect(MiniButton, SIGNAL(clicked()), this, SLOT(mini()));
58 connect(CloseButton, SIGNAL(clicked()), this, SLOT(close()));
59 connect(StartButton, SIGNAL(clicked()), this, SLOT(start()));
60 connect(SegmentButton, SIGNAL(clicked()), this, SLOT(segment()));
61 SegmentButton->setEnabled(false);
62 ProgressText->setText( tr( "Press START to begin capturing" ) );
63 m_pTimer = new QTimer;
64 m_pCapproc = 0;
65 m_pCC = pWnd->getcc();
66 if (m_pCC)
67 m_pCC->addref();
68 }
69
70 /*
71 * Destroys the object and frees any allocated resources
72 */
73 AviCapDialog::~AviCapDialog()
74 {
75 delete my_config;
76 delete miniwidget; // thats not a child of avicapdialog
77 }
78
79
80 QString make_dirpool_file(QString filename)
81 {
82
83 QDate cdate=QDate::currentDate();
84 QTime ctime=QTime::currentTime();
85 QString timestr=QString().sprintf("-%02d%02d%02d-%02d%02d%02d.avi", \
86 cdate.year()%1000,cdate.month(),cdate.day(), \
87 ctime.hour(),ctime.minute(),ctime.second());
88 if(filename==""){
89 filename="avicap"+timestr;
90 }
91 else if(filename.length()>4 && filename.right(4)!=".avi"){
92 //doesnt end on .avi
93 filename=filename+timestr;
94 }
95 else{
96 // no change in filename
97 }
98 QString fullname;
99 QString pathname=find_best_dir();
100 if(pathname==""){
101 fullname="";
102 }
103 else{
104 // make it easy for now
105 fullname=pathname+"/"+filename;
106 }
107
108 return fullname;
109 }
110 void AviCapDialog::start()
111 {
112 m_eOldMode = (int)m_pWnd->getMode();
113 if (m_eOldMode != V4LWindow::Overlay)
114 {
115 printf("setting overlay mode\n");
116 m_pWnd->setMode(V4LWindow::Overlay);
117 avm_usleep(50000);
118 }
119 disconnect(StartButton, SIGNAL(clicked()), this, SLOT(start()));
120 StartButton->setText( tr( "Stop" ) );
121 connect(StartButton, SIGNAL(clicked()), this, SLOT(stop()));
122
123 if (!my_config)
124 {
125 my_config = new CaptureConfig();
126 my_config->load();
127 }
128
129 if (my_config->filename[0]=='/' || my_config->filename[0]=='.'){
130 // relative or absolute path, no modifications
131 use_dirpool=false;
132 }
133 else{
134 use_dirpool=true;
135
136 QString filename=QString(my_config->filename.c_str());
137 QString fullname=make_dirpool_file(filename);
138 if (!fullname.length())
139 {
140 AVMOUT(AVML_WARN, "No more free space - not recording");
141 m_pCapproc=0;
142 return;
143 }
144
145 my_config->setFilename(fullname.latin1());
146 }//else of absolute/rel path
147
148 try
149 {
150 m_pCapproc = new CaptureProcess(m_pWnd->m_pDev, my_config, m_pCC);
151 AVMOUT(AVML_DEBUG, "capture process started");
152 }
153 catch (FatalError& e)
154 {
155 QMessageBox::critical(this, "Error", e.GetDesc());
156 disconnect(StartButton, SIGNAL(clicked()), this, SLOT(stop()));
157 connect(StartButton, SIGNAL(clicked()), this, SLOT(start()));
158 m_pCapproc = 0;
159 AVMOUT(AVML_ERROR, "Critical error when starting capture");
160 return;
161 }
162 connect(m_pTimer, SIGNAL(timeout()), this, SLOT(updateGraphs()));
163 m_pTimer->start(500);
164 if (RI("IsSegmented", 1))
165 SegmentButton->setEnabled(true);
166 m_pProgress=new CaptureProgress;
167 ProgressText->setAlignment( Qt::AlignVCenter );
168 m_pCapproc->setMessenger(m_pProgress);
169 m_bStarted=true;
170 }
171
172 void AviCapDialog::segment()
173 {
174 if (m_pCapproc){
175 AVMOUT(AVML_INFO, "Segmenting capture");
176 m_pCapproc->segment();
177 }
178 }
179
180 void AviCapDialog::stop()
181 {
182 m_pTimer->stop();
183 m_bStarted=false;
184 delete m_pCapproc;
185 delete m_pProgress;
186 m_pCapproc=0;
187 m_pProgress=0;
188 disconnect(StartButton, SIGNAL(clicked()), this, SLOT(stop()));
189 connect(StartButton, SIGNAL(clicked()), this, SLOT(start()));
190 SegmentButton->setEnabled( false );
191 StartButton->setText( tr( "Start" ) );
192 m_pWnd->setMode((V4LWindow::Modes)m_eOldMode);
193 AVMOUT(AVML_INFO, "Capture stopped");
194 }
195
196 void AviCapDialog::updateGraphs()
197 {
198 QPainter* qp;
199 int iHeight;
200 int iWidth;
201 int iMaxFrame=0;
202 int i;
203
204 m_pProgress->lock();
205
206 QString s = QString().
207 sprintf(tr("Capturing into file:\n %s\n") +
208 tr("Video frame rate: %.2f frames/s\n") +
209 tr("Video resolution: %dx%d\n"),
210 m_pProgress->filename,
211 m_pProgress->framerate,
212 m_pProgress->xdim, m_pProgress->ydim);
213
214 if (m_pProgress->audio_freq)
215 s += QString().sprintf(tr("Audio: %d Hz, %d bit, %s\n"),
216 m_pProgress->audio_freq,
217 m_pProgress->audio_ssize,
218 (m_pProgress->audio_channels==1)?"mono":"stereo");
219 else
220 s += QString().sprintf(tr("No audio\n"));
221
222 float elapsed_float=m_pProgress->enc_time/1000000.0;
223 QTime elapsed = QTime(0,0,0,0);
224 elapsed = elapsed.addSecs((int)elapsed_float);
225 #if QT_VERSION>=300
226 QString elapsedstr = elapsed.toString("hh:mm:ss");
227 #else
228 QString elapsedstr = elapsed.toString();
229 #endif
230
231 int free_space = free_diskspace(m_pProgress->filename);
232
233 if (use_dirpool && (((int)elapsed_float)%60==0)){
234 char buffer[100];
235 strcpy(buffer,m_pProgress->filename);
236 char *dname=dirname(buffer);
237 QString dirname=QString(dname);
238
239 strcpy(buffer,m_pProgress->filename);
240 char *bname=basename(buffer);
241 QString filename=QString(bname);
242
243 #if 0
244 printf("new segmenting name: %s\n",new_segmenting_name.latin1());
245 m_pCapproc->setSegmentName(new_segmenting_name.latin1());
246 //UserLog(QString().sprintf("new segmenting name %s\n",new_segmenting_name.latin1()));
247
248
249 int keepfree=find_keepfree(dirname);
250
251 printf("keepfree=%d freespace=%d\n",keepfree,free_space);
252
253 if(keepfree==0 || keepfree>free_space){
254 printf("out of free space - segmenting to another dir\n");
255 segment();
256 }
257 #else
258 QString fullname=make_dirpool_file(filename);
259 if(fullname==""){
260 AVMOUT(AVML_WARN,
261 "error - no more free space in dirpool\n"
262 "stopping recording\n");
263 //stop recording
264 stop();
265 return;
266 }
267 QString new_segmenting_name=fullname;
268
269 AVMOUT(AVML_DEBUG, "New segmenting name: %s",
270 new_segmenting_name.latin1());
271 m_pCapproc->setSegmentName(new_segmenting_name.latin1());
272 AVMOUT(AVML_DEBUG, "New segmenting name: %s",
273 new_segmenting_name.latin1());
274
275 int keepfree=find_keepfree(dirname);
276
277 AVMOUT(AVML_DEBUG, "keepfree=%d freespace=%d",
278 keepfree, free_space);
279
280 if (keepfree == 0 || keepfree > free_space) {
281 AVMOUT(AVML_WARN, "Out of free space - segmenting to another dir");
282 segment();
283 }
284 #endif
285 }
286
287 s+=QString().sprintf(
288 tr("\nElapsed: %s (%.2f s)\n")+
289 tr("Written video: \n")+
290 tr(" %d KB %d frames (%d kbps) %.2f s\n")+
291 tr("Written audio: \n")+
292 tr(" %d KB (%dkbps) %.2f s\n")+
293 tr("Synch fix: %d frames A:%.4f V:%.4f S:%.4f\n")+
294 tr("File size: %d KB (%.1f MB)\n")+
295 tr("Free space on device: %d MB\n")+
296 tr("Frame drop in capture thread: %d frames\n")+
297 tr("Frame drop in encoder: %d frames\n")+
298 tr("Capture buffer usage: %d %%\n"),
299 elapsedstr.latin1(),
300 elapsed_float,
301 (int)(m_pProgress->video_bytes/1024),
302 (int) m_pProgress->video_frames,
303 (int)(m_pProgress->video_bytes * 8 / m_pProgress->video_time / 1000),
304 (double)m_pProgress->video_time,
305 (int)(m_pProgress->audio_bytes/1024),
306 int(m_pProgress->audio_bytes * 8 / m_pProgress->audio_time / 1000),(float) m_pProgress->audio_time,
307 m_pProgress->synch_fix,
308 (float)m_pProgress->audio_error,
309 (float)m_pProgress->video_error,
310 (float)m_pProgress->timestamp_shift,
311 (int)(m_pProgress->file_size/1024),
312 (int)((float)m_pProgress->file_size)/(1024.0*1024.0),
313 free_space,
314 m_pProgress->dropped_cap,
315 m_pProgress->dropped_enc,
316 int(100.*m_pProgress->buffer_occupancies[(m_pProgress->buffer_head+m_pProgress->HISTORY_SIZE-1)%m_pProgress->HISTORY_SIZE]/m_pProgress->max_frames)
317 );
318 ProgressText->setText(s);
319
320 #if 0
321 struct rusage rus;
322
323 int res1=getrusage(RUSAGE_SELF,&rus);
324
325 long usertime=rus.ru_utime.tv_sec;
326 long systime=rus.ru_stime.tv_sec;
327 #endif
328
329
330 if ((((int)elapsed_float)%60)==0)
331 AVMOUT(AVML_DEBUG, "Capture is alive since %s", elapsedstr.latin1());
332
333 if (last_dropped_enc != m_pProgress->dropped_enc)
334 AVMOUT(AVML_DEBUG, "Dropped frames %s %s encoder=%3d (+%2d)\n",
335 m_pProgress->filename,elapsedstr.latin1(),
336 m_pProgress->dropped_enc,m_pProgress->dropped_enc-last_dropped_enc);
337
338 if (last_dropped_cap != m_pProgress->dropped_cap)
339 AVMOUT(AVML_DEBUG, "Dropped frames %s %s capture=%3d (+%2d)\n",
340 m_pProgress->filename, elapsedstr.latin1(),
341 m_pProgress->dropped_cap,m_pProgress->dropped_cap-last_dropped_cap);
342
343 if (last_synchfix!=m_pProgress->synch_fix)
344 {
345 long timediff=((long)elapsed_float)-last_synchfix_time;
346 QTime timed = QTime(0,0,0,0);
347 timed = timed.addSecs((int)timediff);
348 QString diffstr=timed.toString();
349
350 AVMOUT(AVML_DEBUG, "synchfix %s %s synchfix=%3d (+%2d +%s)\n",
351 m_pProgress->filename,elapsedstr.latin1(),
352 m_pProgress->synch_fix,
353 m_pProgress->synch_fix-last_synchfix,diffstr.latin1());
354 last_synchfix_time=(long)elapsed_float;
355 }
356 last_dropped_enc=m_pProgress->dropped_enc;
357 last_dropped_cap=m_pProgress->dropped_cap;
358 last_synchfix=m_pProgress->synch_fix;
359
360 QString shorttext = QString().
361 sprintf("%s %.fM %d+%d %d%%", elapsedstr.latin1(),
362 ((float)m_pProgress->file_size)/(1024.0*1024.0),
363 m_pProgress->dropped_cap, m_pProgress->dropped_enc,
364 int(100.*m_pProgress->buffer_occupancies[(m_pProgress->buffer_head+m_pProgress->HISTORY_SIZE-1)%m_pProgress->HISTORY_SIZE]/m_pProgress->max_frames));
365
366 m_pMiniButton->setText(shorttext);
367 miniwidget->setCaption(shorttext);
368
369 #if QT_VERSION > 220
370 if(CaptureTab->currentPageIndex()!=1)
371 goto finish;
372 #endif
373 qp = new QPainter(Frame3_2);
374 iHeight=Frame3_2->height();
375 iWidth=Frame3_2->width();
376 for(i=0; i<m_pProgress->video_frame_len; i++)
377 {
378 int index = m_pProgress->video_frame_head - i - 1;
379 while(index<0)
380 index += m_pProgress->HISTORY_SIZE;
381 index %= m_pProgress->HISTORY_SIZE;
382 if((m_pProgress->video_frame_sizes[index] & ~0x40000000)>iMaxFrame)
383 iMaxFrame=m_pProgress->video_frame_sizes[index] & ~0x40000000;
384 }
385 if (iMaxFrame<10)
386 iMaxFrame=10;
387 else if (iMaxFrame<100)
388 iMaxFrame=100;
389 else if (iMaxFrame<3000)
390 iMaxFrame=3000;
391 else if (iMaxFrame<10000)
392 iMaxFrame=10000;
393 else if (iMaxFrame<30000)
394 iMaxFrame=30000;
395 else if (iMaxFrame<100000)
396 iMaxFrame=100000;
397 else iMaxFrame=1000000;
398 if (iMaxFrame<1000)
399 s=QString().sprintf(tr("%d bytes"), iMaxFrame);
400 else if(iMaxFrame<1000000)
401 s=QString().sprintf(tr("%d KB"), iMaxFrame/1024);
402 else
403 s=QString().sprintf(tr("%d MB"), iMaxFrame/(1024*1024));
404 MaxFrameSizeText->setText(s);
405 for (i=iWidth; i>=0; i--)
406 {
407 int index = i-iWidth;
408 if(index<-m_pProgress->video_frame_len)
409 {
410 qp->setPen(QColor(0,0,0));
411 qp->moveTo(i, iHeight);
412 qp->lineTo(i, 0);
413 continue;
414 }
415 index += m_pProgress->video_frame_head;
416 while(index<0)
417 index += m_pProgress->HISTORY_SIZE;
418 index %= m_pProgress->HISTORY_SIZE;
419 float t=(m_pProgress->video_frame_sizes[index] & ~0x40000000)/(float)iMaxFrame;
420 if (t>1)t=1;
421 if (m_pProgress->video_frame_sizes[index] & 0x40000000){
422 //printf("avicapwnd keyframe\n");
423 qp->setPen(QColor(255, 0, 0));
424 }
425 else
426 qp->setPen(QColor(0, 255, 0));
427 qp->moveTo(i, iHeight);
428 qp->lineTo(i, (int) (iHeight*(1-t)));
429 if (t<1)
430 {
431 qp->setPen(QColor(0, 0, 0));
432 qp->lineTo(i, 0);
433 }
434 }
435 delete qp;
436 qp=new QPainter(Frame3);
437 iHeight=Frame3->height();
438 iWidth=Frame3->width();
439 for(i=0; i<iWidth; i++)
440 {
441 int index = i-iWidth;
442 if(index<-m_pProgress->buffer_len)
443 {
444 qp->setPen(QColor(0,0,0));
445 qp->moveTo(i, iHeight);
446 qp->lineTo(i, 0);
447 continue;
448 }
449 index += m_pProgress->buffer_head;
450 while(index<0)
451 index += m_pProgress->HISTORY_SIZE;
452 index %= m_pProgress->HISTORY_SIZE;
453 float t=m_pProgress->buffer_occupancies[index]/(double)m_pProgress->max_frames;
454 if(t>1)t=1;
455 qp->setPen(QColor(0, 255, 0));
456 qp->moveTo(i, iHeight);
457 qp->lineTo(i, (int) (iHeight*(1-t)));
458 if(t<1)
459 {
460 qp->setPen(QColor(0, 0, 0));
461 qp->lineTo(i, 0);
462 }
463 }
464 delete qp;
465 finish:
466 m_pProgress->unlock();
467 int finished;
468 avm::string error;
469 m_pCapproc->getState(finished, error);
470 if(finished)
471 {
472 printf("Finished\n");
473 stop();
474 if(error.size())
475 QMessageBox::critical(this, "Error", error.c_str());
476 }
477 }
478
479 bool AviCapDialog::close()
480 {
481 m_pWnd->unregisterCapDialog();
482 // no need to delete child widgets, Qt does it all for us
483 if (m_bStarted)
484 stop();
485 delete m_pTimer;
486 delete m_pCapproc;
487 delete m_pProgress;
488 if (m_pCC)
489 m_pCC->release();
490 return QWidget::close();
491 }
492
493 void AviCapDialog::mini()
494 {
495 i_am_mini=!i_am_mini;
496
497 if(i_am_mini){
498 this->hide();
499 miniwidget->show();
500 }
501 else{
502 this->show();
503 miniwidget->hide();
504 }
505 #if 0
506 setMaximumSize(80,40);
507 setBaseSize(80,40);
508 setGeometry(x(),y(),80,40);
509 //QSizePolicy pol(QSizePolicy::Minimum,QSizePolicy::Minimum,false);
510 //setSizePolicy(pol);
511 //m_pMiniButton->setSizePolicy(pol);
512 m_pMiniButton->setMaximumSize(50,20);
513 //miniwidget->setSizePolicy(pol);
514 miniwidget->setMaximumSize(60,30);
515 m_pMiniButton->adjustSize();
516 miniwidget->adjustSize();
517 adjustSize();
518 showNormal();
519 updateGeometry();
520 #endif
521 }