UsTK : Ultrasound ToolKit  version 2.0.1 under development (2023-12-07)
usVirtualServer.h
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ustk with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Marc Pouliquen
30  *
31  *****************************************************************************/
32 
37 #ifndef US_VIRTUAL_SERVER_H
38 #define US_VIRTUAL_SERVER_H
39 
40 #include <QApplication>
41 #include <QtCore/QDataStream>
42 #include <QtCore/QDateTime>
43 #include <QtCore/QFile>
44 #include <QtCore/QString>
45 #include <QtCore/QStringList>
46 #include <QtNetwork/QTcpServer>
47 #include <QtNetwork/QTcpSocket>
48 
49 #include <cmath>
50 #include <ctime>
51 #include <iostream>
52 #include <vector>
53 //#include <unistd.h>
54 
55 // USTK inclues
56 #include <visp3/ustk_core/usConfig.h>
57 #include <visp3/ustk_core/usMHDSequenceReader.h>
58 #include <visp3/ustk_core/usSequenceReader.h>
59 
60 #include "usConsoleListener.h"
61 
67 class usVirtualServer : public QObject
68 {
69  Q_OBJECT
70 
71 public:
72  // Following headers must be the same in the client grabber !
73 
74  // header sent by the client to init porta
76  usInitHeaderIncomming() : headerId(1) {} // set header Id to 1 by default
77  int headerId; // to differenciate usInitHeaderIncomming (=1) / usAcquisitionParameters (=2) /
78  // usRunControlHeaderSent(=3)
79 
80  // probe / slot selection
81  int probeId; // available probes : 4DC7 (id = 15) , C5-2 (id = 10)
82  int slotId; // 3 slots on ultrasonix station (0 : top, 2 : middle, 1 : botom)
83 
84  int imagingMode; // see ImagingModes.h
85  };
86 
87  // header sent by the client to update porta config
89  usUpdateHeaderIncomming() : headerId(2) {} // set header Id to 1 by default
90  int headerId; // to differenciate usInitHeaderIncomming (=1) / usUpdateHeaderIncomming (=2)
91 
99  int sector;
104  };
105 
106  // header sent to the client to confirm porta is initialized
108  usInitHeaderConfirmation() : headerId(1) {} // set header Id to 1 by default
109  int headerId; // to differenciate usInitHeaderConfirmation (=1) / usImageHeader (=2)
110 
111  int initOk; // 1 if init ok, 0 otherwise
112  int probeId; // unique code for each probe (4DC7 = 15, C 50-62 = 10)
113  };
114 
115  //
116  struct usImageHeader {
117  usImageHeader() : headerId(2) {} // set header Id to 2 by default
118  int headerId; // to differenciate usInitHeaderConfirmation (=1) / usImageHeader (=2)
119 
120  quint32 frameCount;
121  quint64 timeStamp;
122 
123  double dataRate; // fps
124 
125  int dataLength; // image size in bytes
126  int ss;
127 
129 
132 
133  double pixelWidth; // width of a pixel of a post-scan frame (meters)
134  double pixelHeight; // height of a pixel of a post-scan frame (meters)
135 
138 
141  unsigned int scanLineNumber;
143 
144  double degPerFrame;
146  double motorRadius;
148  };
149 
150  explicit usVirtualServer(std::string sequencePath, QObject *parent = 0);
152 
153 signals:
154 
155  void runAcquisitionSignal(bool run);
156 
158 
159 private slots:
160  // Called automatically when a client attempts to connect
161  void acceptTheConnection();
162 
163  // Called automatically when client has closed the connection
164  void connectionAboutToClose();
165 
166  // Called when user decide to quit the pause (see usConsoleListener)
167  void quitPause();
168 
169  // Called automatically when data sent by a client is fully available to the server
170  void readIncomingData();
171 
172  void runAcquisition(bool run);
173 
174  void startSendingLoop();
175 
176 private:
177  void initServer(usInitHeaderIncomming header);
178 
179  void invertRowsColsOnPreScan();
180 
181  void setSequencePath(const std::string sequencePath);
182 
183  void sendingLoopSequenceXml();
184  void sendingLoopSequenceMHD();
185 
186  bool updateServer(usUpdateHeaderIncomming header);
187 
188  void writeInitAcquisitionParameters(QDataStream &out, int imagingMode);
189 
190  // Variable(socket) to store listening tcpserver
191  QTcpServer m_tcpServer;
192 
193  // Variable(socket) to store newly established connection with the client
194  QTcpSocket *connectionSoc;
195  usInitHeaderConfirmation confirmHeader;
196 
197  bool initWithoutUpdate;
198 
199 #ifdef VISP_HAVE_XML2
200  usSequenceReader<usImagePostScan2D<unsigned char> > m_sequenceReaderPostScan;
201  usSequenceReader<usImagePreScan2D<unsigned char> > m_sequenceReaderPreScan;
202 #endif
203  usMHDSequenceReader m_MHDSequenceReader;
204 
205  usImageHeader imageHeader;
206 
207  // 2D images
208  usImagePostScan2D<unsigned char> m_postScanImage2d;
209  usImagePreScan2D<unsigned char> m_preScanImage2d;
210  usImageRF2D<short int> m_rfImage2d;
211 
212  // 3D images
213  usImagePreScan3D<unsigned char> m_preScanImage3d;
214  usImageRF3D<short int> m_rfImage3d;
215 
216  us::ImageType m_imageType;
217 
218  bool m_isMHDSequence; // mhd sequence if true, xml otherwise
219 
220  std::vector<uint64_t>
221  m_timestamps; // vector of timestamps of the current volume (only for 3D case : RF and pre-scan volumes)
222  uint64_t m_nextImageTimestamp;
223 
224  bool m_serverIsSendingImages;
225 
226  std::string m_sequencePath;
227 
228  // pause option
229  bool m_usePause;
230  bool m_pauseOn;
231  unsigned int m_pauseImageNumber;
232  uint64_t m_pauseDurationOffset;
233  unsigned int m_pauseIndexOffset;
234  // memory for pause on volumes (sending V, V+1, V, V+1 ...)
235  usImagePreScan3D<unsigned char> m_preScanImage3dTemp;
236  usImageRF3D<short int> m_rfImage3dTemp;
237  std::vector<uint64_t> m_timestampsTemp;
238  bool m_volumePauseTmp;
239 
240  // rewind option
241  bool m_useRewind;
242 
243  // For user inputs in console
244  usConsoleListener m_consoleListener;
245 };
246 
247 #endif // US_VIRTUAL_SERVER_H
Reader for a sequence of images stored as mhd/raw files in a directory Image sequence files order hav...
Reading of sequences of ultrasound images.
Class to simulate a server sending frames from an ultrasound station. Permits to replay a sequence of...
void startSendingLoopSignal()
void runAcquisitionSignal(bool run)
usVirtualServer(std::string sequencePath, QObject *parent=0)
ImageType
Definition: us.h:65