Fraxinus  17.12-rc2
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 /*
35  * sscVideoRecorder.cpp
36  *
37  * Created on: Dec 17, 2010
38  * Author: christiana
39  */
40 
41 #include "cxVideoRecorder.h"
42 #include "vtkImageData.h"
43 #include "cxTime.h"
44 #include "cxLogger.h"
45 
46 namespace cx
47 {
48 
50  mSource(source)
51 {
52  mSynced = !sync;
53  mSyncShift = 0;
54 }
55 
57 {
58 // std::cout << "deleting VideoRecorder " << mData.size() << std::endl;
59 //
60 // for (DataType::iterator iter=mData.begin(); iter!=mData.end(); ++iter)
61 // {
62 // if (iter->second->GetReferenceCount()>1)
63 // {
64 // std::cout << "WARNING found ref count " << iter->second->GetReferenceCount() << std::endl;
65 // }
66 // }
67 }
68 
70 {
71  connect(mSource.get(), &VideoSource::newFrame, this, &VideoRecorder::newFrameSlot);
72 }
73 
75 {
76  disconnect(mSource.get(), &VideoSource::newFrame, this, &VideoRecorder::newFrameSlot);
77 }
78 
79 void VideoRecorder::newFrameSlot()
80 {
81  if (!mSource->validData())
82  return;
83 
84  double timestamp = mSource->getTimestamp();
85 
86  if (!mSynced)
87  {
88  mSyncShift = getMilliSecondsSinceEpoch() - timestamp;
89  report(QString("VideoRecorder found and corrected sync shift: %1 s").arg(double(mSyncShift)/1000, 6, 'f', 3));
90 // std::cout << "RealTimeStreamSourceRecorder SyncShift: " << mSyncShift << std::endl;
91  mSynced = true;
92  }
93 
94 // double diff = 0;
95 // if (!mData.empty())
96 // diff = timestamp - mData.rbegin()->first;
97 // std::cout << "timestamp " << timestamp << ", " << diff << std::endl;
98 
99  vtkImageDataPtr frame = vtkImageDataPtr::New();
100 // std::cout << " RC after construct " << frame->GetReferenceCount() << std::endl;
101  frame->DeepCopy(mSource->getVtkImageData());
102 // std::cout << " RC after fill " << frame->GetReferenceCount() << std::endl;
103  mData[timestamp] = frame;
104 // std::cout << " RC after assign " << frame->GetReferenceCount() << std::endl;
105 // if (frame->GetReferenceCount()>2)
106 // frame->SetReferenceCount(2);
107 // std::cout << " RC after explicit set " << frame->GetReferenceCount() << std::endl;
108 // mData.clear();
109 // std::cout << " RC after clear map " << frame->GetReferenceCount() << std::endl;
110  frame = NULL;
111 // std::cout << " RC after construct+clear temp " << mData.find(timestamp)->second->GetReferenceCount() << std::endl;
112 // std::cout << "============================" << std::endl << std::endl;
113 }
114 
116 {
117  start -= mSyncShift;
118  stop -= mSyncShift;
119  VideoRecorder::DataType retval = mData;
120 
121 // std::cout << std::endl;
122 // RealTimeStreamSourceRecorder::DataType::iterator iter;
123 // for (iter=retval.begin(); iter!=retval.end(); ++iter)
124 // {
125 // std::cout << "timestamp pre: " << (iter->first - start) << std::endl;
126 // }
127 
128  retval.erase(retval.begin(), retval.lower_bound(start)); // erase all data earlier than start
129  retval.erase(retval.upper_bound(stop), retval.end()); // erase all data earlier than start
130 
131 // std::cout << std::endl;
132 
133 // for (iter=retval.begin(); iter!=retval.end(); ++iter)
134 // {
135 // std::cout << "timestamp post: " << (iter->first - start) << std::endl;
136 // }
137 // std::cout << std::endl;
138 // std::cout << "recording " << retval.size() << "/" << mData.size() << " start/stop " << start << ", " << stop << std::endl;
139 // std::cout << "length " << stop - start << std::endl;
140  return retval;
141 }
142 
143 
144 } // ssc
VideoRecorder(VideoSourcePtr source, bool sync=true)
double getMilliSecondsSinceEpoch()
Definition: cxTime.cpp:65
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:90
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.