CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxVideoRecorder.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 /*
14  * sscVideoRecorder.cpp
15  *
16  * Created on: Dec 17, 2010
17  * Author: christiana
18  */
19 
20 #include "cxVideoRecorder.h"
21 #include "vtkImageData.h"
22 #include "cxTime.h"
23 #include "cxLogger.h"
24 
25 namespace cx
26 {
27 
29  mSource(source)
30 {
31  mSynced = !sync;
32  mSyncShift = 0;
33 }
34 
36 {
37 // std::cout << "deleting VideoRecorder " << mData.size() << std::endl;
38 //
39 // for (DataType::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
40 // {
41 // if (iter->second->GetReferenceCount()>1)
42 // {
43 // std::cout << "WARNING found ref count " << iter->second->GetReferenceCount() << std::endl;
44 // }
45 // }
46 }
47 
49 {
50  connect(mSource.get(), &VideoSource::newFrame, this, &VideoRecorder::newFrameSlot);
51 }
52 
54 {
55  disconnect(mSource.get(), &VideoSource::newFrame, this, &VideoRecorder::newFrameSlot);
56 }
57 
58 void VideoRecorder::newFrameSlot()
59 {
60  if (!mSource->validData())
61  return;
62 
63  double timestamp = mSource->getTimestamp();
64 
65  if (!mSynced)
66  {
67  mSyncShift = getMilliSecondsSinceEpoch() - timestamp;
68  report(QString("VideoRecorder found and corrected sync shift: %1 s").arg(double(mSyncShift)/1000, 6, 'f', 3));
69 // std::cout << "RealTimeStreamSourceRecorder SyncShift: " << mSyncShift << std::endl;
70  mSynced = true;
71  }
72 
73 // double diff = 0;
74 // if (!mData.empty())
75 // diff = timestamp - mData.rbegin()->first;
76 // std::cout << "timestamp " << timestamp << ", " << diff << std::endl;
77 
78  vtkImageDataPtr frame = vtkImageDataPtr::New();
79 // std::cout << " RC after construct " << frame->GetReferenceCount() << std::endl;
80  frame->DeepCopy(mSource->getVtkImageData());
81 // std::cout << " RC after fill " << frame->GetReferenceCount() << std::endl;
82  mData[timestamp] = frame;
83 // std::cout << " RC after assign " << frame->GetReferenceCount() << std::endl;
84 // if (frame->GetReferenceCount()>2)
85 // frame->SetReferenceCount(2);
86 // std::cout << " RC after explicit set " << frame->GetReferenceCount() << std::endl;
87 // mData.clear();
88 // std::cout << " RC after clear map " << frame->GetReferenceCount() << std::endl;
89  frame = NULL;
90 // std::cout << " RC after construct+clear temp " << mData.find(timestamp)->second->GetReferenceCount() << std::endl;
91 // std::cout << "============================" << std::endl << std::endl;
92 }
93 
95 {
96  start -= mSyncShift;
97  stop -= mSyncShift;
98  VideoRecorder::DataType retval = mData;
99 
100 // std::cout << std::endl;
101 // RealTimeStreamSourceRecorder::DataType::iterator iter;
102 // for (iter=retval.begin(); iter!=retval.end(); ++iter)
103 // {
104 // std::cout << "timestamp pre: " << (iter->first - start) << std::endl;
105 // }
106 
107  retval.erase(retval.begin(), retval.lower_bound(start)); // erase all data earlier than start
108  retval.erase(retval.upper_bound(stop), retval.end()); // erase all data earlier than start
109 
110 // std::cout << std::endl;
111 
112 // for (iter=retval.begin(); iter!=retval.end(); ++iter)
113 // {
114 // std::cout << "timestamp post: " << (iter->first - start) << std::endl;
115 // }
116 // std::cout << std::endl;
117 // std::cout << "recording " << retval.size() << "/" << mData.size() << " start/stop " << start << ", " << stop << std::endl;
118 // std::cout << "length " << stop - start << std::endl;
119  return retval;
120 }
121 
122 
123 } // ssc
VideoRecorder(VideoSourcePtr source, bool sync=true)
double getMilliSecondsSinceEpoch()
Definition: cxTime.cpp:44
std::map< double, vtkImageDataPtr > DataType
<timestamp, frame>
virtual void startRecord()
virtual DataType getRecording(double start, double stop)
virtual void stopRecord()
boost::shared_ptr< class VideoSource > VideoSourcePtr
void report(QString msg)
Definition: cxLogger.cpp:69
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
Namespace for all CustusX production code.