CtSaving.cpp 56.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
22
#include <cmath>
23
#include <sstream>
24 25
#include <sys/types.h>
#include <sys/stat.h>
26 27 28
#include <unistd.h>

#ifdef __linux__ 
Roberto Arturo Homs Regojo's avatar
.  
Roberto Arturo Homs Regojo committed
29
#include <dirent.h>
30
#include <sys/statvfs.h>
31 32
#else
#include <direct.h>
33
#endif
34

35
#include "lima/CtSaving.h"
seb's avatar
seb committed
36
#include "CtSaving_Edf.h"
37 38
#include "lima/CtAcquisition.h"
#include "lima/CtBuffer.h"
39 40 41 42

#ifdef WITH_NXS_SAVING
#include "CtSaving_Nxs.h"
#endif
seb's avatar
seb committed
43 44 45 46

#ifdef WITH_CBF_SAVING
#include "CtSaving_Cbf.h"
#endif
47

48 49 50 51
#ifdef WITH_FITS_SAVING
#include "CtSaving_Fits.h"
#endif

Teresa's avatar
Teresa committed
52 53 54 55
#ifdef WITH_TIFF_SAVING
#include "CtSaving_Tiff.h"
#endif

56 57 58 59
#ifdef WITH_HDF5_SAVING
#include "CtSaving_Hdf5.h"
#endif

60 61
#include "processlib/TaskMgr.h"
#include "processlib/SinkTask.h"
62 63 64 65 66 67 68

using namespace lima;

static const char DIR_SEPARATOR = '/';

/** @brief save task class
 */
69
class CtSaving::Stream::_SaveTask : public SinkTaskBase
70
{
71
    DEB_CLASS_NAMESPC(DebModControl,"CtSaving::Stream::_SaveTask","Control");
72
public:
73 74 75
  _SaveTask(CtSaving::Stream& stream) 
    : SinkTaskBase(), m_stream(stream) {}

76 77
  virtual void process(Data &aData)
  {
seb's avatar
seb committed
78 79 80
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);

81
    m_stream.writeFile(aData, m_header);
82 83 84 85
  }

  CtSaving::HeaderMap	 m_header;
private:
86
  CtSaving::Stream& m_stream;
87 88 89
};
/** @brief save callback
 */
90
class CtSaving::Stream::_SaveCBK : public TaskEventCallback
91
{
92
    DEB_CLASS_NAMESPC(DebModControl,"CtSaving::Stream::_SaveCBK","Control");
93
public:
94
  _SaveCBK(Stream& stream) : m_stream(stream) {}
seb's avatar
seb committed
95
  virtual void finished(Data &aData)
96
  {
seb's avatar
seb committed
97 98 99
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);

100
    m_stream.saveFinished(aData);
101 102
  }
private:
103
  Stream& m_stream;
104
};
105 106
/** @brief compression callback
 */
107
class CtSaving::Stream::_CompressionCBK : public TaskEventCallback
108
{
109
    DEB_CLASS_NAMESPC(DebModControl,"CtSaving::_CompressionCBK","Control");
110
public:
111
  _CompressionCBK(Stream& stream) : m_stream(stream) {}
112 113 114 115
  virtual void finished(Data &aData)
  {
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);
116

117
    m_stream.compressionFinished(aData);
118 119
  }
private:
120
  Stream &m_stream;
121
};
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/** @brief manual background saving
 */
class CtSaving::_ManualBackgroundSaveTask : public SinkTaskBase
{
public:
  _ManualBackgroundSaveTask(CtSaving& ct_saving,
			    HeaderMap &aHeader) :
    m_saving(ct_saving),
    m_header(aHeader)
  {
  }

  ~_ManualBackgroundSaveTask()
  {
    AutoMutex lock(m_saving.m_cond.mutex());
    m_saving.m_ready_flag = true;
    m_saving.m_cond.broadcast();
  }

  virtual void process(Data &aData)
  {
    m_saving._synchronousSaving(aData,m_header);
  }
private:
  CtSaving &m_saving;
  HeaderMap m_header;
};
149 150 151
/** @brief Parameters default constructor
 */
CtSaving::Parameters::Parameters()
152
  : imageType(Bpp8),nextNumber(0), fileFormat(RAW), savingMode(Manual), 
153
    overwritePolicy(Abort),
154 155
    indexFormat("%04d"),framesPerFile(1),
    nbframes(0)
156 157 158
{
}

159 160
void CtSaving::Parameters::checkValid() const
{
161
  DEB_MEMBER_FUNCT();
162 163 164 165 166
  switch(fileFormat)
    {
#ifdef WITH_CBF_SAVING
    case CBFFormat :
      if(framesPerFile > 1)
167 168
	THROW_CTL_ERROR(InvalidValue) << "CBF file format does not support "
			                 "multi frame per file";
169
      break;
170 171
#endif
#ifdef WITH_TIFF_SAVING
Teresa's avatar
Teresa committed
172 173 174 175 176
    case TIFFFormat :
      if(framesPerFile > 1)
	THROW_CTL_ERROR(InvalidValue) << "TIFF file format does not support "
			                 "multi frame per file";
      break;
177
#endif
178
#ifndef __unix
179
#pragma message ( "--- WARNING / TODO - no cases???" )
180
#endif
181 182 183 184 185
    default:
      break;
    }
}

186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
//@brief constructor
CtSaving::Stream::Stream(CtSaving& aCtSaving, int idx)
  : m_saving(aCtSaving), m_idx(idx),
    m_save_cnt(NULL),
    m_pars_dirty_flag(false),
    m_active(false),
    m_compression_cbk(NULL)
{
  DEB_CONSTRUCTOR();

  createSaveContainer();
  m_saving_cbk = new _SaveCBK(*this);
  m_compression_cbk = new _CompressionCBK(*this);
}

//@brief destructor
CtSaving::Stream::~Stream()
{
  DEB_DESTRUCTOR();

  delete m_save_cnt;
  m_saving_cbk->unref();
  m_compression_cbk->unref();
}

const 
CtSaving::Parameters& CtSaving::Stream::getParameters(ParameterType type) const
{ 
  bool from_acq = (type == Acq) || ((type == Auto) && !m_pars_dirty_flag);
  return *(from_acq ? &m_acquisition_pars : &m_pars); 
}

CtSaving::Parameters& CtSaving::Stream::getParameters(ParameterType type)
{ 
  bool from_acq = (type == Acq) || ((type == Auto) && !m_pars_dirty_flag);
  return *(from_acq ? &m_acquisition_pars : &m_pars); 
}

void CtSaving::Stream::setParameters(const CtSaving::Parameters& pars)
{ 
  DEB_MEMBER_FUNCT();

229 230 231
  if (pars.nextNumber == m_acquisition_pars.nextNumber)
    m_pars.nextNumber = pars.nextNumber;

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  if (pars == m_pars)
    return;

  pars.checkValid(); 
  m_pars_dirty_flag = true; 
  m_pars = pars; 

  DEB_TRACE() << "pars changed";
}

void CtSaving::Stream::setActive(bool active)
{
  DEB_MEMBER_FUNCT();

  if (active == m_active)
    return;
  
  if (!active)
    m_save_cnt->close();

  m_active = active;
}

255
void CtSaving::Stream::prepare(CtControl& ct)
256 257 258 259
{
  DEB_MEMBER_FUNCT();

  if (hasAutoSaveMode())
260 261 262 263 264
    {
      m_save_cnt->close();
      updateParameters();
      checkWriteAccess();
    }
265
  m_save_cnt->prepare(ct);
266 267
}

268 269 270 271
void CtSaving::Stream::close()
{
  m_save_cnt->close();
}
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
void CtSaving::Stream::updateParameters()
{
  DEB_MEMBER_FUNCT();

  if (!m_pars_dirty_flag)
    return;

  if (m_pars.fileFormat != m_acquisition_pars.fileFormat)
    createSaveContainer();

  m_acquisition_pars = m_pars;
  m_pars_dirty_flag = false;
}

void CtSaving::Stream::createSaveContainer()
{
  DEB_MEMBER_FUNCT();

  switch (m_pars.fileFormat) {
  case CBFFormat :
#ifndef WITH_CBF_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the cbf "
                                     "saving option, not managed";  
#endif
296 297
    goto common;

298 299 300 301
  case NXS:
#ifndef WITH_NXS_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the nxs "
                                     "saving option, not managed";  
302
#endif        
303 304
    goto common;

305 306 307 308
  case FITS:
#ifndef WITH_FITS_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the fits "
                                     "saving option, not managed";  
309
#endif        
310
    goto common;
311 312 313 314
  case EDFGZ:
#ifndef WITH_EDFGZ_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the edf gzip "
                                     "saving option, not managed"; 
Teresa's avatar
Teresa committed
315 316 317 318 319 320
#endif
    goto common;
  case TIFFFormat:
#ifndef WITH_TIFF_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the tiff "
                                     "saving option, not managed";  
321 322 323 324 325 326
#endif
    goto common;
  case HDF5:
#ifndef WITH_HDF5_SAVING
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the hdf5 "
                                     "saving option, not managed";
327 328 329 330 331 332
#endif
    goto common;
  case EDFConcat:
#ifndef __unix
    THROW_CTL_ERROR(NotSupported) << "Lima is not compiled with the edf concat "
                                     "saving option, not managed";
333 334
#endif
    goto common;
335 336
  case RAW:
  case EDF:
337

338 339

  common:
340 341 342 343 344 345 346 347 348 349 350 351 352 353
    if (m_save_cnt) {
      m_save_cnt->close();
      delete m_save_cnt;
    }
    break;

  default:
    THROW_CTL_ERROR(NotSupported) << "File format not yet managed";
  }

  switch(m_pars.fileFormat)
  {
  case RAW:
  case EDF:
354
  case EDFGZ:
355
  case EDFConcat:
356
    m_save_cnt = new SaveContainerEdf(*this,m_pars.fileFormat);
357 358 359 360 361 362 363 364 365 366 367
    break;
#ifdef WITH_CBF_SAVING
  case CBFFormat:
    m_save_cnt = new SaveContainerCbf(*this);
    m_pars.framesPerFile = 1;
    break;
#endif
#ifdef WITH_NXS_SAVING
  case NXS:
    m_save_cnt = new SaveContainerNxs(*this);
    break;
368 369 370 371 372
#endif
#ifdef WITH_FITS_SAVING
  case FITS:
    m_save_cnt = new SaveContainerFits(*this);
    break;
Teresa's avatar
Teresa committed
373 374 375 376 377 378
#endif
#ifdef WITH_TIFF_SAVING
  case TIFFFormat:
    m_save_cnt = new SaveContainerTiff(*this);
    m_pars.framesPerFile = 1;
    break;
379 380 381 382 383
#endif
#ifdef WITH_HDF5_SAVING
  case HDF5:
    m_save_cnt = new SaveContainerHdf5(*this, m_pars.fileFormat);
    break;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#endif
  default:
    break;
  }
}

void CtSaving::Stream::writeFile(Data& data, HeaderMap& header)
{
  DEB_MEMBER_FUNCT();

  m_save_cnt->writeFile(data, header);
}


SinkTaskBase *CtSaving::Stream::getTask(TaskType type, const HeaderMap& header)
{
  DEB_MEMBER_FUNCT();

  SinkTaskBase *save_task;

  if (type == Compression) {
    if (!needCompression())
      return NULL;
    save_task = m_save_cnt->getCompressionTask(header);
    save_task->setEventCallback(m_compression_cbk);
  } else {
    _SaveTask *real_task = new _SaveTask(*this);
    real_task->m_header = header;
    save_task = real_task;
    save_task->setEventCallback(m_saving_cbk);
  }

  return save_task;
}

void CtSaving::Stream::compressionFinished(Data& data)
{
  DEB_MEMBER_FUNCT();
  m_saving._compressionFinished(data, *this);
}

void CtSaving::Stream::saveFinished(Data& data)
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR2(data, getIndex());

  m_saving._saveFinished(data, *this);
}

433 434 435 436 437 438 439
class CtSaving::_NewFrameSaveCBK : public HwSavingCtrlObj::Callback
{
public:
  _NewFrameSaveCBK(CtSaving &ct_saving) :
    m_saving(ct_saving)
  {
  }
440
  bool newFrameWritten(int frame_id)
441 442 443 444 445 446
  {
    return m_saving._newFrameWrite(frame_id);
  }
private:
  CtSaving&	m_saving;
};
447

448
#ifdef WITH_CONFIG
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
// --- Config
class CtSaving::_ConfigHandler : public CtConfig::ModuleTypeCallback
{
public:
  _ConfigHandler(CtSaving& saving) :
    CtConfig::ModuleTypeCallback("Saving"),
    m_saving(saving) {}
  virtual void store(Setting& saving_setting)
  {
    CtSaving::Parameters pars;
    m_saving.getParameters(pars);

    saving_setting.set("directory",pars.directory);
    saving_setting.set("prefix",pars.prefix);
    saving_setting.set("suffix",pars.suffix);
464
    saving_setting.set("options",pars.options);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    saving_setting.set("imageType",convert_2_string(pars.imageType));
    saving_setting.set("nextNumber",pars.nextNumber);
    saving_setting.set("fileFormat",convert_2_string(pars.fileFormat));
    saving_setting.set("savingMode",convert_2_string(pars.savingMode));
    saving_setting.set("overwritePolicy",convert_2_string(pars.overwritePolicy));
    saving_setting.set("indexFormat",pars.indexFormat);
    saving_setting.set("framesPerFile",pars.framesPerFile);
    saving_setting.set("nbframes",pars.nbframes);

    CtSaving::ManagedMode managedmode;
    m_saving.getManagedMode(managedmode);
    saving_setting.set("managedmode",convert_2_string(managedmode));
  }
  virtual void restore(const Setting& saving_setting)
  {
    CtSaving::Parameters pars;
    m_saving.getParameters(pars);

    saving_setting.get("directory",pars.directory);
    saving_setting.get("prefix",pars.prefix);
    saving_setting.get("suffix",pars.suffix);
486
    saving_setting.get("options",pars.options);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

    std::string strimageType;
    if(saving_setting.get("imageType",strimageType))
      convert_from_string(strimageType,pars.imageType);

    int nextNumber;
    if(saving_setting.get("nextNumber",nextNumber))
      pars.nextNumber = nextNumber;

    std::string strfileFormat;
    if(saving_setting.get("fileFormat",strfileFormat))
      convert_from_string(strfileFormat,pars.fileFormat);

    std::string strsavingMode;
    if(saving_setting.get("savingMode",strsavingMode))
      convert_from_string(strsavingMode,pars.savingMode);

    std::string stroverwritePolicy;
    if(saving_setting.get("overwritePolicy",stroverwritePolicy))
      convert_from_string(stroverwritePolicy,pars.overwritePolicy);

    saving_setting.get("indexFormat",pars.indexFormat);

    int framesPerFile;
    if(saving_setting.get("framesPerFile",framesPerFile))
      pars.framesPerFile = framesPerFile;

    int nbframes;
    if(saving_setting.get("nbframes",nbframes))
      pars.nbframes = nbframes;

    std::string strmanagedmode;
    if(saving_setting.get("managedmode",strmanagedmode))
      {
	CtSaving::ManagedMode managedmode;
	convert_from_string(strmanagedmode,managedmode);
	m_saving.getManagedMode(managedmode);
      }

    m_saving.setParameters(pars);
  }
private:
  CtSaving& m_saving;
};
531
#endif //WITH_CONFIG
532

533 534 535
//@brief constructor
CtSaving::CtSaving(CtControl &aCtrl) :
  m_ctrl(aCtrl),
536
  m_stream(NULL),
537
  m_ready_flag(true),
538 539
  m_need_compression(false),
  m_nb_save_cbk(0),
540 541
  m_end_cbk(NULL),
  m_managed_mode(Software)
542
{
seb's avatar
seb committed
543 544
  DEB_CONSTRUCTOR();

545 546 547 548 549 550 551
  m_nb_stream = 5;
  m_stream = new Stream *[m_nb_stream];
  for (int s = 0; s < m_nb_stream; ++s)
    m_stream[s] = new Stream(*this, s);

  m_stream[0]->setActive(true);

552
  resetLastFrameNb();
553 554

  HwInterface *hw = aCtrl.hwInterface();
555
#ifdef __linux__
556 557 558 559 560 561 562 563
  m_has_hwsaving = hw->getHwCtrlObj(m_hwsaving);
  if(m_has_hwsaving)
    {
      m_new_frame_save_cbk = new _NewFrameSaveCBK(*this);
      m_hwsaving->registerCallback(m_new_frame_save_cbk);
    }
  else
    m_new_frame_save_cbk = NULL;
564 565 566 567
#else
  m_has_hwsaving = false;
  m_new_frame_save_cbk = NULL;
#endif
568 569 570 571 572
}

//@brief destructor
CtSaving::~CtSaving()
{
seb's avatar
seb committed
573 574
  DEB_DESTRUCTOR();

575 576 577 578
  for (int s = 0; s < m_nb_stream; ++s)
    delete m_stream[s];
  delete [] m_stream;

579
  setEndCallback(NULL);
580 581 582 583 584
  if(m_has_hwsaving)
    {
      m_hwsaving->unregisterCallback(m_new_frame_save_cbk);
      delete m_new_frame_save_cbk;
    }
585 586
}

587 588 589 590 591 592
CtSaving::Stream& CtSaving::getStreamExc(int stream_idx) const
{
  DEB_MEMBER_FUNCT();
  THROW_CTL_ERROR(InvalidValue) << "Invalid " << DEB_VAR1(stream_idx);
}

593 594 595 596 597
/** @brief set saving parameter for a saving stream

    @param pars parameters for the saving stream
    @param stream_idx the id of the saving stream
 */
598
void CtSaving::setParameters(const CtSaving::Parameters &pars, int stream_idx)
599
{
seb's avatar
seb committed
600
  DEB_MEMBER_FUNCT();
601
  DEB_PARAM() << DEB_VAR2(pars, stream_idx);
seb's avatar
seb committed
602

603
  AutoMutex aLock(m_cond.mutex());
604 605
  Stream& stream = getStream(stream_idx);
  stream.setParameters(pars);
606 607
}

608 609 610 611 612
/** @brief get the saving stream parameters

    @param pars the return parameters
    @param stream_idx the stream id
 */
613
void CtSaving::getParameters(CtSaving::Parameters &pars, int stream_idx) const
614
{
seb's avatar
seb committed
615
  DEB_MEMBER_FUNCT();
616
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
617

618
  AutoMutex aLock(m_cond.mutex());
619 620
  const Stream& stream = getStream(stream_idx);
  pars = stream.getParameters(Auto);
seb's avatar
seb committed
621 622

  DEB_RETURN() << DEB_VAR1(pars);
623
}
624 625
/** @brief set the saving directory for a saving stream
 */
626
void CtSaving::setDirectory(const std::string &directory, int stream_idx)
627
{
seb's avatar
seb committed
628
  DEB_MEMBER_FUNCT();
629
  DEB_PARAM() << DEB_VAR2(directory, stream_idx);
seb's avatar
seb committed
630

631
  AutoMutex aLock(m_cond.mutex());
632 633
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
634
  stream.checkDirectoryAccess(directory);
635 636
  pars.directory = directory;
  stream.setParameters(pars);
637
}
638 639
/** @brief get the saving directory for a saving stream
 */
640
void CtSaving::getDirectory(std::string& directory, int stream_idx) const
641
{
seb's avatar
seb committed
642
  DEB_MEMBER_FUNCT();
643
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
644

645
  AutoMutex aLock(m_cond.mutex());
646 647 648
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  directory = pars.directory;
seb's avatar
seb committed
649 650

  DEB_RETURN() << DEB_VAR1(directory);
651
}
652 653
/** @brief set the filename prefix for a saving stream
 */
654
void CtSaving::setPrefix(const std::string &prefix, int stream_idx)
655
{
seb's avatar
seb committed
656
  DEB_MEMBER_FUNCT();
657
  DEB_PARAM() << DEB_VAR2(prefix, stream_idx);
seb's avatar
seb committed
658

659
  AutoMutex aLock(m_cond.mutex());
660 661 662 663
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.prefix = prefix;
  stream.setParameters(pars);
664
}
665 666
/** @brief get the filename prefix for a saving stream
 */
667
void CtSaving::getPrefix(std::string& prefix, int stream_idx) const
668
{
seb's avatar
seb committed
669
  DEB_MEMBER_FUNCT();
670
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
671

672
  AutoMutex aLock(m_cond.mutex());
673 674 675
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  prefix = pars.prefix;
seb's avatar
seb committed
676 677

  DEB_RETURN() << DEB_VAR1(prefix);
678
}
679 680
/** @brief set the filename suffix for a saving stream
 */
681
void CtSaving::setSuffix(const std::string &suffix, int stream_idx)
682
{
seb's avatar
seb committed
683
  DEB_MEMBER_FUNCT();
684
  DEB_PARAM() << DEB_VAR2(suffix, stream_idx);
seb's avatar
seb committed
685

686
  AutoMutex aLock(m_cond.mutex());
687 688 689 690
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.suffix = suffix;
  stream.setParameters(pars);
691
}
692 693
/** @brief get the filename suffix for a saving stream
 */
694
void CtSaving::getSuffix(std::string& suffix, int stream_idx) const
695
{
seb's avatar
seb committed
696
  DEB_MEMBER_FUNCT();
697
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
698

699
  AutoMutex aLock(m_cond.mutex());
700 701 702
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  suffix = pars.suffix;
seb's avatar
seb committed
703 704

  DEB_RETURN() << DEB_VAR1(suffix);
705
}
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

/** @brief set the additional options for a saving stream
 */
void CtSaving::setOptions(const std::string &options, int stream_idx)
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR2(options, stream_idx);

  AutoMutex aLock(m_cond.mutex());
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.options = options;
  stream.setParameters(pars);
}
/** @brief get the additional options for a saving stream
 */
void CtSaving::getOptions(std::string& options, int stream_idx) const
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(stream_idx);

  AutoMutex aLock(m_cond.mutex());
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  options = pars.options;

  DEB_RETURN() << DEB_VAR1(options);
}

735 736
/** @brief set the next number for the filename for a saving stream
 */
737
void CtSaving::setNextNumber(long number, int stream_idx)
738
{
seb's avatar
seb committed
739
  DEB_MEMBER_FUNCT();
740
  DEB_PARAM() << DEB_VAR2(number, stream_idx);
seb's avatar
seb committed
741

742
  AutoMutex aLock(m_cond.mutex());
743 744
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
745

746 747
  pars.nextNumber = number;
  stream.setParameters(pars);
748
}
749 750
/** @brief get the next number for the filename for a saving stream
 */
751
void CtSaving::getNextNumber(long& number, int stream_idx) const
752
{
seb's avatar
seb committed
753
  DEB_MEMBER_FUNCT();
754
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
755
  
756
  AutoMutex aLock(m_cond.mutex());
757 758 759
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  number = pars.nextNumber;
seb's avatar
seb committed
760 761

  DEB_RETURN() << DEB_VAR1(number);
762
}
763 764
/** @brief set the saving format for a saving stream
 */
765
void CtSaving::setFormat(FileFormat format, int stream_idx)
766
{
seb's avatar
seb committed
767 768
  DEB_MEMBER_FUNCT();

769
  AutoMutex aLock(m_cond.mutex());
770 771 772 773
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.fileFormat = format;
  stream.setParameters(pars);
seb's avatar
seb committed
774
}
775 776
/** @brief get the saving format for a saving stream
 */
777
void CtSaving::getFormat(FileFormat& format, int stream_idx) const
778
{
seb's avatar
seb committed
779
  DEB_MEMBER_FUNCT();
780
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
781

782
  AutoMutex aLock(m_cond.mutex());
783 784 785
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  format = pars.fileFormat;
seb's avatar
seb committed
786 787

  DEB_RETURN() << DEB_VAR1(format);
788
}
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
/** @brief return a list of hardware possible saving format
 */
void CtSaving::getHardwareFormatList(std::list<std::string> &format_list) const
{
  DEB_MEMBER_FUNCT();

  if(!m_has_hwsaving)
    THROW_CTL_ERROR(NotSupported) << "No hardware saving for this camera";
  
  m_hwsaving->getPossibleSaveFormat(format_list);
}

void CtSaving::setHardwareFormat(const std::string &format)
{
  DEB_MEMBER_FUNCT();

  if(!m_has_hwsaving)
    THROW_CTL_ERROR(NotSupported) << "No hardware saving for this camera";

  bool found = _checkHwFileFormat(format);
  
  if(!found)
    {
      THROW_CTL_ERROR(NotSupported) << 
	"Hardware does not support" << DEB_VAR1(format);
    }

  m_specific_hardware_format = format;
}

bool CtSaving::_checkHwFileFormat(const std::string &format) const
{
  std::list<std::string> format_list;
  m_hwsaving->getPossibleSaveFormat(format_list);
  bool found = false;
  for(std::list<std::string>::const_iterator i = format_list.begin();
      !found && i != format_list.end();++i)
    found = *i == format;
  return found;
}

void CtSaving::_ReadImage(Data &image,int frameNumber)
{
  DEB_MEMBER_FUNCT();

  if(m_hwsaving->getCapabilities() & HwSavingCtrlObj::MANUAL_READ)
    {
      HwFrameInfoType frame;
      m_hwsaving->readFrame(frame,frameNumber);
      CtBuffer::transformHwFrameInfoToData(image,frame);
    }
  else
    THROW_CTL_ERROR(NotSupported) << "Image read is not supported for this hardware";
}
void CtSaving::getHardwareFormat(std::string &format) const
{
  DEB_MEMBER_FUNCT();

  if(!m_has_hwsaving)
    THROW_CTL_ERROR(NotSupported) << "No hardware saving for this camera";
  
  format = m_specific_hardware_format;
}

853 854
/** @brief set the saving mode for a saving stream
 */
855 856
void CtSaving::setSavingMode(SavingMode mode)
{
seb's avatar
seb committed
857 858 859
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(mode);

860
  AutoMutex aLock(m_cond.mutex());
861 862 863 864 865 866
  for (int s = 0; s < m_nb_stream; ++s) {
    Stream& stream = getStream(s);
    Parameters pars = stream.getParameters(Auto);
    pars.savingMode = mode;
    stream.setParameters(pars);
  }
867
}
868 869
/** @brief get the saving mode for a saving stream
 */
870 871
void CtSaving::getSavingMode(SavingMode& mode) const
{ 
seb's avatar
seb committed
872 873
  DEB_MEMBER_FUNCT();
  
874
  AutoMutex aLock(m_cond.mutex());
875 876 877
  const Stream& stream = getStream(0);
  const Parameters& pars = stream.getParameters(Auto);
  mode = pars.savingMode;
seb's avatar
seb committed
878 879
  
  DEB_RETURN() << DEB_VAR1(mode);
880
}
881 882
/** @brief set the overwrite policy for a saving stream
 */
883
void CtSaving::setOverwritePolicy(OverwritePolicy policy, int stream_idx)
884
{
seb's avatar
seb committed
885
  DEB_MEMBER_FUNCT();
886
  DEB_PARAM() << DEB_VAR2(policy, stream_idx);
seb's avatar
seb committed
887

888
  AutoMutex aLock(m_cond.mutex());
889 890 891
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.overwritePolicy = policy;
892
 
893
  stream.setParameters(pars);
894
}
895 896
/** @brief get the overwrite policy for a saving stream
 */
897 898
void CtSaving::getOverwritePolicy(OverwritePolicy& policy, 
				  int stream_idx) const
899
{
seb's avatar
seb committed
900
  DEB_MEMBER_FUNCT();
901
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
902
  
903
  AutoMutex aLock(m_cond.mutex());
904 905 906
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  policy = pars.overwritePolicy;
seb's avatar
seb committed
907 908

  DEB_RETURN() << DEB_VAR1(policy);
909
}
910 911
/** @brief set the number of frame saved per file for a saving stream
 */
912
void CtSaving::setFramesPerFile(unsigned long frames_per_file, int stream_idx)
913
{
seb's avatar
seb committed
914
  DEB_MEMBER_FUNCT();
915
  DEB_PARAM() << DEB_VAR2(frames_per_file, stream_idx);
seb's avatar
seb committed
916

917
  AutoMutex aLock(m_cond.mutex());
918 919 920 921
  Stream& stream = getStream(stream_idx);
  Parameters pars = stream.getParameters(Auto);
  pars.framesPerFile = frames_per_file;
  stream.setParameters(pars);
922
}
923 924
/** @brief get the number of frame saved per file for a saving stream
 */
925
void CtSaving::getFramesPerFile(unsigned long& frames_per_file, 
926
			       int stream_idx) const
927
{
seb's avatar
seb committed
928
  DEB_MEMBER_FUNCT();
929
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
930

931
  AutoMutex aLock(m_cond.mutex());
932 933 934
  const Stream& stream = getStream(stream_idx);
  const Parameters& pars = stream.getParameters(Auto);
  frames_per_file = pars.framesPerFile;
seb's avatar
seb committed
935 936

  DEB_RETURN() << DEB_VAR1(frames_per_file);
937 938
}

939 940 941 942 943 944 945 946 947 948 949 950 951 952
/** @brief set who will manage the saving.
 *
 *  with this methode you can choose who will do the saving
 *   - if mode is set to Software, the saving will be managed by Lima core
 *   - if mode is set to Hardware then it's the sdk or the hardware of the camera that will manage the saving.
 *  @param mode can be either Software or Hardware
*/
void CtSaving::setManagedMode(CtSaving::ManagedMode mode)
{
  DEB_MEMBER_FUNCT();
  if(mode == Hardware && !m_has_hwsaving)
    THROW_CTL_ERROR(InvalidValue) << DEB_VAR1(mode) << "Not supported";

  AutoMutex aLock(m_cond.mutex());
953
  if(mode == Hardware)
954
    {
955 956 957 958 959 960 961 962 963 964 965
      if(!m_has_hwsaving)
	THROW_CTL_ERROR(NotSupported) << "Hardware saving is not supported";

      int hw_cap = m_hwsaving->getCapabilities();
      if(hw_cap & HwSavingCtrlObj::COMMON_HEADER)
	m_hwsaving->setCommonHeader(m_common_header);
      else if(!m_common_header.empty())
	{
	  THROW_CTL_ERROR(Error) << "Hardware saving do not manage common header"
				 << ", clear it first";
	}
966
    }
967
  m_managed_mode = mode;
968 969 970 971 972 973 974
}

void CtSaving::getManagedMode(CtSaving::ManagedMode &mode) const
{
  DEB_MEMBER_FUNCT();

  AutoMutex aLock(m_cond.mutex());
975
  mode = m_managed_mode;
976 977
}

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
void CtSaving::_getTaskList(TaskType type, long frame_nr, 
			    const HeaderMap& header, TaskList& task_list)
{
  DEB_MEMBER_FUNCT();

  task_list.clear();
  for (int s = 0; s < m_nb_stream; ++s) {
    Stream& stream = getStream(s);
    if (stream.isActive()) {
      SinkTaskBase *save_task = stream.getTask(type, header);
      if (save_task)
	task_list.push_back(save_task);
    }
  }
  int nb_cbk = task_list.size();
  DEB_TRACE() << DEB_VAR1(nb_cbk);
  if (type == Compression) {
    FrameCbkCountMap::value_type map_pair(frame_nr, nb_cbk);
    m_nb_compression_cbk.insert(map_pair);
  } else
    m_nb_save_cbk = nb_cbk;
}
1000 1001
/** @brief clear the common header
 */
1002 1003
void CtSaving::resetCommonHeader()
{
seb's avatar
seb committed
1004 1005
  DEB_MEMBER_FUNCT();

1006
  AutoMutex aLock(m_cond.mutex());
1007
  ManagedMode managed_mode = getManagedMode();
1008
  if(managed_mode == Hardware)
1009 1010 1011 1012 1013 1014 1015
    {
      int hw_cap = m_hwsaving->getCapabilities();
      if(hw_cap & HwSavingCtrlObj::COMMON_HEADER)
	m_hwsaving->resetCommonHeader();
      else
	THROW_CTL_ERROR(NotSupported) << "Common header is not supported";
    }
1016
  m_common_header.clear();
1017
}
1018 1019 1020
/** @brief set the common header.
    This is the header which will be write for all frame for this acquisition
 */
1021 1022
void CtSaving::setCommonHeader(const HeaderMap &header)
{
seb's avatar
seb committed
1023 1024 1025
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(header);

1026
  AutoMutex aLock(m_cond.mutex());
1027
  ManagedMode managed_mode = getManagedMode();
1028
  if(managed_mode == Hardware)
1029 1030 1031 1032 1033 1034 1035
    {
      int hw_cap = m_hwsaving->getCapabilities();
      if(hw_cap & HwSavingCtrlObj::COMMON_HEADER)
	m_hwsaving->setCommonHeader(header);
      else
	THROW_CTL_ERROR(NotSupported) << "Common header is not supported";
    }
1036
  m_common_header = header;
1037
}
1038 1039
/** @brief replace/add field in the common header
 */
1040 1041
void CtSaving::updateCommonHeader(const HeaderMap &header)
{
seb's avatar
seb committed
1042 1043 1044
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(header);

1045
  AutoMutex aLock(m_cond.mutex());
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  //Update
  for(HeaderMap::const_iterator i = header.begin();
      i != header.end();++i)
    {
      std::pair<HeaderMap::iterator,bool> result= 
	m_common_header.insert(HeaderMap::value_type(i->first,i->second));
      //if it exist, update
      if(!result.second)
	result.first->second = i->second;
    }
1056
}
1057 1058
/** @brief get the current common header
 */
1059 1060
void CtSaving::getCommonHeader(HeaderMap& header) const
{
seb's avatar
seb committed
1061 1062
  DEB_MEMBER_FUNCT();

1063 1064
  AutoMutex aLock(m_cond.mutex());
  header = HeaderMap(m_common_header);
seb's avatar
seb committed
1065 1066

  DEB_RETURN() << DEB_VAR1(header);
1067
}
1068 1069
/** @brief add/replace a header value in the current common header
 */
1070 1071
void CtSaving::addToCommonHeader(const HeaderValue &value)
{
seb's avatar
seb committed
1072 1073 1074
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(value);

1075 1076 1077
  AutoMutex aLock(m_cond.mutex());
  m_common_header.insert(value);
}
1078 1079
/** @brief add/replace a header value in the current frame header
 */
1080 1081
void CtSaving::addToFrameHeader(long frame_nr,const HeaderValue &value)
{
seb's avatar
seb committed
1082 1083 1084
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR2(frame_nr,value);

1085 1086 1087
  AutoMutex aLock(m_cond.mutex());
  m_frame_headers[frame_nr].insert(value);
}
1088 1089
/** @brief add/replace several value in the current frame header
 */
1090 1091
void CtSaving::updateFrameHeader(long frame_nr,const HeaderMap &header)
{
seb's avatar
seb committed
1092 1093 1094
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR2(frame_nr,header);

1095 1096
  AutoMutex aLock(m_cond.mutex());
  HeaderMap &frameHeader = m_frame_headers[frame_nr];
1097 1098 1099 1100 1101 1102 1103 1104 1105
  for(HeaderMap::const_iterator i = header.begin();
      i != header.end();++i)
    {
      std::pair<HeaderMap::iterator,bool> result= 
	frameHeader.insert(HeaderMap::value_type(i->first,i->second));
      //if it exist, update
      if(!result.second)
	result.first->second = i->second;
    }
1106
  _validateFrameHeader(frame_nr,aLock);
1107
}
1108 1109 1110 1111
/** @brief validate a header for a frame.
    this mean that the header is ready and can now be save.
    If you are in AutoHeader this will trigger the saving if the data frame is available
 */
1112
void CtSaving::validateFrameHeader(long frame_nr)
1113
{
seb's avatar
seb committed
1114 1115 1116
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(frame_nr);

1117
  AutoMutex aLock(m_cond.mutex());
1118 1119 1120 1121 1122 1123
  _validateFrameHeader(frame_nr,aLock);
}

void CtSaving::_validateFrameHeader(long frame_nr,
				    AutoMutex& aLock)
{
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  SavingMode saving_mode = getAcqSavingMode();
  if (saving_mode != CtSaving::AutoHeader)
    return;

  FrameMap::iterator frame_iter = m_frame_datas.find(frame_nr);
  bool data_available = (frame_iter != m_frame_datas.end());
  bool can_save = (m_ready_flag && (m_last_frameid_saved == frame_nr - 1));
  if (!data_available || !(m_need_compression || can_save))
    return;
  Data aData = frame_iter->second;

  HeaderMap task_header;
  FrameHeaderMap::iterator aHeaderIter;
  aHeaderIter = m_frame_headers.find(frame_nr);
  bool keep_header = m_need_compression;
  _takeHeader(aHeaderIter, task_header, keep_header);

  TaskType task_type = m_need_compression ? Compression : Save;
  TaskList task_list;
  _getTaskList(task_type, frame_nr, task_header, task_list);
  if (!m_need_compression) {
    m_frame_datas.erase(frame_iter);
    m_ready_flag = false, m_last_frameid_saved = frame_nr;
  }
  aLock.unlock();
  _postTaskList(aData, task_list);
1150
}
1151
/** @brief get the frame header.
1152

1153 1154 1155
    @param frame_nr the frame id
    @param header the current frame header
 */
1156 1157
void CtSaving::getFrameHeader(long frame_nr, HeaderMap& header) const
{
seb's avatar
seb committed
1158 1159 1160
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(frame_nr);

1161
  AutoMutex aLock(m_cond.mutex());
1162
  FrameHeaderMap::const_iterator i = m_frame_headers.find(frame_nr);
1163 1164
  if(i != m_frame_headers.end())
    header.insert(i->second.begin(),i->second.end());
seb's avatar
seb committed
1165 1166

  DEB_RETURN() << DEB_VAR1(header);
1167 1168
}

1169 1170
/** @brief get the frame header and remove it from the container
 */
1171 1172
void CtSaving::takeFrameHeader(long frame_nr, HeaderMap& header)
{
seb's avatar
seb committed
1173 1174 1175
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(frame_nr);

1176
  AutoMutex aLock(m_cond.mutex());
1177
  FrameHeaderMap::iterator i = m_frame_headers.find(frame_nr);
1178 1179 1180 1181 1182
  if(i != m_frame_headers.end())
    {
      header = i->second;
      m_frame_headers.erase(i);
    }
seb's avatar
seb committed
1183 1184
  
  DEB_RETURN() << DEB_VAR1(header);
1185
}
1186
/** @brief remove a frame header
1187

1188 1189
    @param frame_nr the frame id
 */
1190 1191
void CtSaving::removeFrameHeader(long frame_nr)
{
seb's avatar
seb committed
1192 1193 1194
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(frame_nr);

1195 1196 1197
  AutoMutex aLock(m_cond.mutex());
  m_frame_headers.erase(frame_nr);
}
1198 1199
/** @brief remove all frame header
 */
1200 1201
void CtSaving::removeAllFrameHeaders()
{
seb's avatar
seb committed
1202 1203
  DEB_MEMBER_FUNCT();

1204 1205 1206 1207 1208 1209
  AutoMutex aLock(m_cond.mutex());
  m_frame_headers.clear();
}

// Private methodes

1210
void CtSaving::_getCommonHeader(HeaderMap &header)
1211
{
1212 1213
  header.insert(m_internal_common_header.begin(),
		m_internal_common_header.end());
1214 1215
  header.insert(m_common_header.begin(),m_common_header.end());
}
1216 1217
void CtSaving::_takeHeader(FrameHeaderMap::iterator& headerIter, 
			   HeaderMap& header, bool keep_in_map)
1218
{
1219 1220 1221 1222 1223
  _getCommonHeader(header);

  if (headerIter == m_frame_headers.end())
    return;

1224
  HeaderMap &aFrameHeaderMap = headerIter->second;
1225 1226 1227 1228 1229 1230 1231 1232
  for(HeaderMap::iterator i = aFrameHeaderMap.begin();
      i != aFrameHeaderMap.end();++i)
    {
      std::pair<HeaderMap::iterator,bool> result = 
	header.insert(HeaderMap::value_type(i->first,i->second));
      if(!result.second)
	result.first->second = i->second;
    }
1233 1234 1235

  if(!keep_in_map)
    m_frame_headers.erase(headerIter);
1236 1237
}

1238 1239
void CtSaving::resetLastFrameNb()
{
seb's avatar
seb committed
1240 1241
  DEB_MEMBER_FUNCT();

1242 1243 1244 1245 1246
  AutoMutex aLock(m_cond.mutex());

  while(!m_ready_flag)
    m_cond.wait();

1247 1248
  m_last_frameid_saved = -1;
}
1249

1250 1251
void CtSaving::setEndCallback(TaskEventCallback *aCbkPt)
{
seb's avatar
seb committed
1252 1253
  DEB_MEMBER_FUNCT();

1254 1255 1256 1257 1258 1259 1260
  AutoMutex aLock(m_cond.mutex());
  if(m_end_cbk)
    m_end_cbk->unref();
  m_end_cbk = aCbkPt;
  if(m_end_cbk)
    m_end_cbk->ref();
}
seb's avatar
seb committed
1261

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
void CtSaving::resetInternalCommonHeader()
{
  AutoMutex aLock(m_cond.mutex());
  m_internal_common_header.clear();
}

void CtSaving::addToInternalCommonHeader(const HeaderValue& value)
{
  AutoMutex aLock(m_cond.mutex());
  m_internal_common_header.insert(value);
}

1274
bool CtSaving::_controlIsFault()
1275
{
seb's avatar
seb committed
1276
  DEB_MEMBER_FUNCT();
1277 1278
  CtControl::Status status;
  m_ctrl.getStatus(status);
1279 1280 1281 1282 1283
  bool fault = (status.AcquisitionStatus == AcqFault);
  DEB_RETURN() << DEB_VAR1(fault);
  return fault;
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
bool CtSaving::_newFrameWrite(int frame_id)
{
  if(m_end_cbk)
    {
      Data aData;
      aData.frameNumber = frame_id;
      m_end_cbk->finished(aData);
    }
  return !!m_end_cbk;
}

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
void CtSaving::frameReady(Data &aData)
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aData);

  if (_controlIsFault()) {
    DEB_WARNING() << "Skip saving data: " << aData;
    return;
  }

1305
  AutoMutex aLock(m_cond.mutex());
1306

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
  SavingMode saving_mode = getAcqSavingMode();
  bool auto_header = (saving_mode == AutoHeader);
  long frame_nr = aData.frameNumber;
  FrameHeaderMap::iterator aHeaderIter;
  aHeaderIter = m_frame_headers.find(frame_nr);
  bool header_available = (aHeaderIter != m_frame_headers.end());
  bool can_save = (m_ready_flag && (m_last_frameid_saved == frame_nr - 1));
  DEB_TRACE() << DEB_VAR5(saving_mode, m_need_compression, can_save,
			  auto_header, header_available);
  if (!(m_need_compression || can_save) || 
      (auto_header && !header_available) || (saving_mode == Manual)) {
    FrameMap::value_type map_pair(frame_nr, aData);
    m_frame_datas.insert(map_pair);
    return;
  }
1322

1323 1324 1325
  HeaderMap task_header;
  bool keep_header = m_need_compression;
  _takeHeader(aHeaderIter, task_header, keep_header);
1326

1327 1328 1329 1330 1331
  TaskType task_type = m_need_compression ? Compression : Save;
  TaskList task_list;
  _getTaskList(task_type, frame_nr, task_header, task_list);
  if (!m_need_compression)
    m_ready_flag = false, m_last_frameid_saved = frame_nr;
1332

1333 1334
  aLock.unlock();
  _postTaskList(aData, task_list);
1335
}
1336 1337 1338
/** @brief get write statistic
    this is the last write time
 */
1339 1340
void CtSaving::getWriteTimeStatistic(std::list<double> &aReturnList,
				     int stream_idx) const
seb's avatar
seb committed
1341
{
seb's avatar
seb committed
1342
  DEB_MEMBER_FUNCT();
1343
  DEB_PARAM() << DEB_VAR1(stream_idx);
seb's avatar
seb committed
1344

1345 1346
  const Stream& stream = getStream(stream_idx);
  stream.getStatistic(aReturnList);
seb's avatar
seb committed
1347
}
1348 1349
/** @brief set the size of the write time static list
 */
1350
void CtSaving::setStatisticHistorySize(int aSize, int stream_idx)
seb's avatar
seb committed
1351
{
seb's avatar
seb committed
1352
  DEB_MEMBER_FUNCT();
1353
  DEB_PARAM() << DEB_VAR2(aSize, stream_idx);
seb's avatar
seb committed
1354

1355 1356 1357
  Stream& stream = getStream(stream_idx);
  stream.setStatisticSize(aSize);
}
1358 1359
/** @brief activate/desactivate a stream
 */
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
void CtSaving::setStreamActive(int stream_idx, bool  active)
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR2(stream_idx, active);

  if ((stream_idx == 0) && !active)
    THROW_CTL_ERROR(InvalidValue) << "Cannot deactivate file stream 0!";

  Stream& stream = getStream(stream_idx);
  stream.setActive(active);
}
1371 1372
/** @brief get if stream is active
 */
1373 1374 1375 1376 1377 1378 1379
void CtSaving::getStreamActive(int stream_idx, bool& active) const
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(stream_idx);
  const Stream& stream = getStream(stream_idx);
  active = stream.isActive();
  DEB_RETURN() << DEB_VAR1(active);
seb's avatar
seb committed
1380
}
1381 1382 1383 1384 1385
/** @brief clear everything.
    - all header
    - all waiting data to be saved
    - close all stream
*/
seb's avatar
seb committed
1386 1387
void CtSaving::clear()
{
seb's avatar
seb committed
1388 1389
  DEB_MEMBER_FUNCT();

1390 1391
  resetLastFrameNb();

1392 1393 1394 1395 1396
  for (int s = 0; s < m_nb_stream; ++s) {
    Stream& stream = getStream(s);
    stream.clear();
  }

seb's avatar
seb committed
1397 1398 1399 1400
  AutoMutex aLock(m_cond.mutex());
  m_frame_headers.clear();
  m_common_header.clear();	// @fix Should we clear common header???
  m_frame_datas.clear();
1401
  
seb's avatar
seb committed
1402
}
1403 1404 1405 1406 1407 1408 1409 1410

void CtSaving::close()
{
  DEB_MEMBER_FUNCT();
  AutoMutex aLock(m_cond.mutex());
  _close();
}

1411
/** @brief write manually a frame
seb's avatar
seb committed
1412

1413 1414 1415
    @param aFrameNumber the frame id you want to save
    @param aNbFrames the number of frames you want to concatenate
 */
1416
void CtSaving::writeFrame(int aFrameNumber, int aNbFrames,bool synchronous)
1417 1418 1419 1420
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aFrameNumber);

1421 1422 1423
  class WaitAndCleanupReadyFlag
  {
  public:
1424 1425 1426
    WaitAndCleanupReadyFlag(bool &ready_flag,Cond& aCond,bool cleanupReadyFlag) :
      m_lock(aCond.mutex()),m_cond(aCond),m_ready_flag(ready_flag),
      m_clean_ready_flag(cleanupReadyFlag){}
1427

1428 1429 1430
    ~WaitAndCleanupReadyFlag() 
    {
      m_lock.lock();
1431 1432 1433 1434 1435
      if(m_clean_ready_flag)
	{
	  m_ready_flag = true;
	  m_cond.broadcast();
	}