C++ Samples
Demonstrates how to create a C++ application using the VideoXpert SDK
CppSamples::LiveStreaming::DataSourceLiveStreaming Class Reference

This plugin sample streams the video in live More...

#include <DataSourceLiveStreaming.h>

Public Member Functions

 DataSourceLiveStreaming (const std::string description, bool isPlayBack)
 
 ~DataSourceLiveStreaming ()
 
CppSamples::Common::PluginRun (CppSamples::Common::DataModel *dataModel) override
 Streams a video in live. More...
 
- Public Member Functions inherited from CppSamples::Common::Plugin
 Plugin (const std::string description)
 
virtual ~Plugin ()
 
std::string GetDescription () const
 Gets the description of this plugin. More...
 
PluginGetParent () const
 Gets the reference to the parent of this plugin. More...
 
void SetParent (Plugin *parent)
 Sets the reference to the parent of this plugin. More...
 

Protected Member Functions

void DoLiveStreaming (VxSdk::VxCollection< VxSdk::IVxDataSource ** > dataSources)
 Streams a video in live. More...
 
bool StartStreamingForDataSource (VxSdk::IVxDataSource *dataSource, VxSdk::IVxDataInterface *dataInterface, time_t seekTime)
 Initiate streaming for selected datasource. This method checks the parameter isMjpegEnabled to determine whether streaming is RTSP or MJPEG Also, it will automatically invoke playback method, if seekTime is given a value; otherwise live streaming will be invoked More...
 

Static Protected Member Functions

static void DisplayPlayerOptionsToConsole (bool isPtzEnabled, bool isLive, bool isRecording)
 Display the options that can be entered by user when a stream is playing More...
 
static MediaController::IControllerGetController (VxSdk::IVxDataSource *dataSource, VxSdk::IVxDataInterface *dataInterface)
 Get an instance of IController for the given data source and data interface. More...
 
static VxSdk::VxCollection< VxSdk::IVxDataSource ** > GetDataSources (VxSdk::IVxSystem *vxSystem)
 Get a collection of data source from the given VideoExpert system. More...
 
static void ShowDataSourceDetails (VxSdk::IVxDataSource *dataSource)
 Print the details of the given data source to the screen. More...
 
static void TimestampCallback (MediaController::TimestampEvent *timeEvent)
 Callback fired from MediaController class for updating the timestamp of currently playing stream More...
 

Private Attributes

bool _isPlayBack
 

Detailed Description

This plugin sample streams the video in live

Definition at line 16 of file DataSourceLiveStreaming.h.

Inherits CppSamples::Common::Plugin.

Constructor & Destructor Documentation

CppSamples::LiveStreaming::DataSourceLiveStreaming::DataSourceLiveStreaming ( const std::string  description,
bool  isPlayBack 
)
inline

Definition at line 21 of file DataSourceLiveStreaming.h.

21 : Plugin(description), _isPlayBack(isPlayBack) {}
Plugin(const std::string description)
Definition: Plugin.h:35
CppSamples::LiveStreaming::DataSourceLiveStreaming::~DataSourceLiveStreaming ( )
inline

Definition at line 22 of file DataSourceLiveStreaming.h.

22 {}

Member Function Documentation

void CppSamples::LiveStreaming::DataSourceLiveStreaming::DisplayPlayerOptionsToConsole ( bool  isPtzEnabled,
bool  isLive,
bool  isRecording 
)
staticprotected

Display the options that can be entered by user when a stream is playing

Parameters
isPtzEnabledTrue indicates PTZ options to be displayed else player controls only
isLiveTrue indicates to show live options only else include playback options also
isRecordingTrue indicates the camera is in recording state

Definition at line 53 of file DataSourceLiveStreaming.cpp.

53  {
54  cout << "\nUSAGE: Choose one of the following options, then press enter:\n";
55  if (isPtzEnabled) {
56  // Menu for PTZ enabled devices
57  cout <<
58  " '7' to send PTZ command: UpLeft\n"
59  " '8' to send PTZ command: Up\n"
60  " '9' to send PTZ command: UpRight\n"
61  " '4' to send PTZ command: Left\n"
62  " '6' to send PTZ command: Right\n"
63  " '1' to send PTZ command: DownLeft\n"
64  " '2' to send PTZ command: Down\n"
65  " '3' to send PTZ command: DownRight\n"
66  " '+' to send PTZ command: ZoomIn\n"
67  " '-' to send PTZ command: ZoomOut\n"
68  " '0' to show presets\n"
69  " '.' to show patterns\n";
70  }
71  if (isRecording && !isLive) {
72  cout <<
73  " 'P' to toggle between PAUSE and PLAY\n"
74  " 'z' to increase playback speed\n"
75  " 'x' to decrease playback speed\n";
76  }
77  cout << " 'Q' to quit\n";
78 }
void CppSamples::LiveStreaming::DataSourceLiveStreaming::DoLiveStreaming ( VxSdk::VxCollection< VxSdk::IVxDataSource ** >  dataSources)
protected

Streams a video in live.

Parameters
dataSourcescollection of data source.

Definition at line 81 of file DataSourceLiveStreaming.cpp.

81  {
82  // Select a Data source
83  cout << "\n" << "Enter data source number [1-" << dataSources.collectionSize << "] : ";
84  int camNum = Utility::ReadInt();
85 
86  // Verify input
87  if (camNum < 1 || camNum > dataSources.collectionSize)
88  return;
89 
90  IVxDataSource* dataSource = dataSources.collection[camNum - 1];
91 
92  // Proceed with online device only
93  if (dataSource->type != VxDataSourceType::kVideo) {
94  cout << "\n" << "Please select a video data source.\n";
95  return;
96  }
97 
98  // Proceed with online device only
99  if (dataSource->state != VxDeviceState::kOnline) {
100  cout << "\nDevice is offline.";
101  return;
102  }
103 
104  // Choose MJPEG or RTSP
105  bool isMjpegEnabled = false;
106  std::cout << "\nDo you want to play MJPEG (Y/N) [Default is RTSP]: ";
107  string mJPEGOption = Utility::ReadString();
108  if (mJPEGOption == "Y" || mJPEGOption == "y") { isMjpegEnabled = true; }
109 
110  // Read date and time for play back streaming
111  time_t timeSinceEpoch = 0;
112  if (_isPlayBack) {
113  cout << "\n\n" << "Input date and time to start playback(yyyy-mm-dd hh:mm:ss)";
114  struct tm playBackTime = Utility::GetDateAndTimeFromUser();
115  timeSinceEpoch = mktime(&playBackTime);
116  }
117 
118  // Print the details of selected data source
119  ShowDataSourceDetails(dataSource);
120 
121  // Traverse through the data interfaces and choose the first one with given protocol will be chosen.
122  for (int i = 0; i < dataSource->dataInterfaceSize; i++) {
123 
124  IVxDataInterface* dataInterface = dataSource->dataInterfaces[i];
125  if (isMjpegEnabled) {
126  // MJPEG Protocol
127  if (dataInterface->protocol == VxStreamProtocol::kMjpegPull) {
128  std::cout << "\nFound MJPEG DataInterface.\n";
129  if (!StartStreamingForDataSource(dataSource, dataInterface, timeSinceEpoch)) {
130  std::cout << "\nStream failed to start. Checking for another MJPEG DataInterface.\n";
131  continue;
132  }
133  break;
134  }
135  }
136  else {
137  // RTSP Protocol
138  if (dataInterface->protocol == VxStreamProtocol::kRtspRtp) {
139  std::cout << "Found RTSP DataInterface.\n";
140  if (!StartStreamingForDataSource(dataSource, dataInterface, timeSinceEpoch)) {
141  std::cout << "\nStream failed to start. Checking for another RTSP DataInterface.\n";
142  continue;
143  }
144  break;
145  }
146  }
147  }
148 }
static void ShowDataSourceDetails(VxSdk::IVxDataSource *dataSource)
Print the details of the given data source to the screen.
IVxDataInterface ** dataInterfaces
bool StartStreamingForDataSource(VxSdk::IVxDataSource *dataSource, VxSdk::IVxDataInterface *dataInterface, time_t seekTime)
Initiate streaming for selected datasource. This method checks the parameter isMjpegEnabled to determ...
VxDeviceState::Value state
VxStreamProtocol::Value protocol
VxDataSourceType::Value type
IController * CppSamples::LiveStreaming::DataSourceLiveStreaming::GetController ( VxSdk::IVxDataSource dataSource,
VxSdk::IVxDataInterface dataInterface 
)
staticprotected

Get an instance of IController for the given data source and data interface.

Parameters
dataSourceInstance of VxSdk::IVxDataSource.
dataInterfaceInstance of VxSdk::IVxDataInterface.
Returns
Instance of IController.

Definition at line 151 of file DataSourceLiveStreaming.cpp.

151  {
152  // Create new instance of MediaRequest
153  MediaController::MediaRequest mediaRequest;
154  mediaRequest.dataSource = dataSource;
155  mediaRequest.dataInterface = *dataInterface;
156  mediaRequest.audioDataSource = nullptr;
157 
158  // Get controller for the given MediaRequest
159  MediaController::IController* control = nullptr;
160  MediaController::GetController(&mediaRequest, &control);
161  return control;
162 }
VxSdk::IVxDataSource * dataSource
VxSdk::IVxDataInterface dataInterface
void GetController(MediaRequest *request, IController **control)
VxSdk::IVxDataSource * audioDataSource
VxCollection< IVxDataSource ** > CppSamples::LiveStreaming::DataSourceLiveStreaming::GetDataSources ( VxSdk::IVxSystem vxSystem)
staticprotected

Get a collection of data source from the given VideoExpert system.

Parameters
vxSystemPointer to the VideoExpert system.
Returns
A collection of data source.

Definition at line 165 of file DataSourceLiveStreaming.cpp.

165  {
166  cout << "Fetching datasources from system, nPlease wait...\n";
167  // Read the size of collection from system.
168  VxCollection<IVxDataSource**> dataSources;
169  VxResult::Value result = vxSystem->GetDataSources(dataSources);
170  if (result == VxResult::kInsufficientSize) {
171  // Allocate memory for the requried collection.
172  dataSources.collection = new IVxDataSource*[dataSources.collectionSize];
173  // Read the collection from system.
174  vxSystem->GetDataSources(dataSources);
175  }
176  // Print the number of data source items read from the system.
177  cout << dataSources.collectionSize << " datasources found.\n";
178  return dataSources;
179 }
virtual VxResult::Value GetDataSources(VxCollection< IVxDataSource ** > &dataSourceCollection) const =0
Plugin * CppSamples::LiveStreaming::DataSourceLiveStreaming::Run ( CppSamples::Common::DataModel dataModel)
overridevirtual

Streams a video in live.

Parameters
dataModelInstance of data model.

Implements CppSamples::Common::Plugin.

Definition at line 27 of file DataSourceLiveStreaming.cpp.

27  {
28  cout << "\n\n";
29  VxCollection<IVxDataSource**> dataSources = GetDataSources(dataModel->VxSystem);
30 
31  try {
32  // Start the live stream
33  DoLiveStreaming(dataSources);
34  } catch (...) {
35  cout << "\nStream is not working.\n";
36  }
37 
38  // Wait for user response before going back to parent menu.
39  Utility::Pause();
40 
41  // Remove the memory allocated to the collection.
42  delete[] dataSources.collection;
43  // Return reference of parent plugin to move back to parent menu.
44  return GetParent();
45 }
void DoLiveStreaming(VxSdk::VxCollection< VxSdk::IVxDataSource ** > dataSources)
Streams a video in live.
VxSdk::IVxSystem * VxSystem
Represents a VideoXpert system and allows the user to manage the system and devices.
Definition: Plugin.h:17
static VxSdk::VxCollection< VxSdk::IVxDataSource ** > GetDataSources(VxSdk::IVxSystem *vxSystem)
Get a collection of data source from the given VideoExpert system.
Plugin * GetParent() const
Gets the reference to the parent of this plugin.
Definition: Plugin.h:46
void CppSamples::LiveStreaming::DataSourceLiveStreaming::ShowDataSourceDetails ( VxSdk::IVxDataSource dataSource)
staticprotected

Print the details of the given data source to the screen.

Parameters
dataSourceThe data source to be printed.

Definition at line 182 of file DataSourceLiveStreaming.cpp.

182  {
183  // Read PTZ info
184  bool canPtz;
185  dataSource->CanPtz(canPtz);
186 
187  // Convert the state to its corresponding string
188  string deviceState = "Unknown";
189  if (dataSource->state == VxDeviceState::kOffline)
190  deviceState = "Offline";
191  else if (dataSource->state == VxDeviceState::kOnline)
192  deviceState = "Online";
193 
194  // Print the details to the console screen.
195  cout << "-----------------------------------------------------------------";
196  cout << "\nName : " << dataSource->name;
197  cout << "\nID : " << dataSource->id;
198  cout << "\nIP : " << dataSource->ip;
199  cout << "\nPTZ : " << (canPtz ? "Yes" : "No");
200  cout << "\nState : " << deviceState;
201  cout << "\nLive Stream : " << (dataSource->hasLive ? "Yes" : "No");
202  cout << "\nPlayback Stream : " << (dataSource->hasRecorded ? "Yes" : "No");
203  cout << "\nCapturing : " << (dataSource->isCapturing ? "Yes" : "No");
204  cout << "\nRecording : " << (dataSource->isRecording ? "Yes" : "No");
205  cout << "\nDataInterfaces : " << dataSource->dataInterfaceSize;
206  cout << "\n-----------------------------------------------------------------\n";
207 }
virtual VxResult::Value CanPtz(bool &canPtz) const =0
VxDeviceState::Value state
bool CppSamples::LiveStreaming::DataSourceLiveStreaming::StartStreamingForDataSource ( VxSdk::IVxDataSource dataSource,
VxSdk::IVxDataInterface dataInterface,
time_t  seekTime 
)
protected

Initiate streaming for selected datasource. This method checks the parameter isMjpegEnabled to determine whether streaming is RTSP or MJPEG Also, it will automatically invoke playback method, if seekTime is given a value; otherwise live streaming will be invoked

Parameters
dataSourceSelected datasource
dataInterfaceSelected dataInterface
seekTimenullptr for live; otherwise pass time in "yyyymmddThhmmssZ-" format
Returns
True if streaming was successfull. False otherwise.

Definition at line 218 of file DataSourceLiveStreaming.cpp.

219  {
220 
221  // Get Controlller
222  MediaController::IController* mediaControl = GetController(dataSource, dataInterface);
223 
224 #ifdef VxSdkInLinux
225  mediaControl->SetWindow(nullptr);
226 #else
227  _currentlyPlayingCam = string(dataSource->name);
228 
229  // Create Window
230  HWND gstWindowHandle = ::CreateWindowA("Gstreamer", "VideoPlayer", WS_VISIBLE, 0, 0, 200, 200, NULL, NULL, NULL, NULL);
231  SetWindowTextA(gstWindowHandle, _currentlyPlayingCam.c_str());
232 
233  // Set the window handle to the controller
234  mediaControl->SetWindow(gstWindowHandle);
235 #endif
236 
237  if (seekTime == 0) {
238  // Start LIVE streaming
239  cout << "\nStarting Live stream.\n";
240  if (!mediaControl->Play(1)) {
241  cout << "\nError starting stream.";
242  delete mediaControl;
243  return false;
244  }
245  }
246  else {
247  // Start playback streaming
248  cout << "\nStarting Playback stream.";
249  if (!mediaControl->Play(1, static_cast<unsigned int>(seekTime))) {
250  cout << "\nError starting stream.";
251  delete mediaControl;
252  return false;
253  }
254  }
255 
256  // Register method for time stamp events
258 
259  // Get PTZ Controller
260  bool isPtz = false;
261  dataSource->CanPtz(isPtz);
262  bool isRecording = dataSource->isRecording;
263  IVxPtzController* ptzControl = nullptr;
264  if (isPtz) {
265  dataSource->GetPtzController(ptzControl);
266  }
267 
268  bool isLive = seekTime == 0;
269  char option;
270  PtzOperationsHandler ptzOperationsHandler(ptzControl);
271  MediaOperationshandler mediaOperationshandler(mediaControl);
272  bool isPtzCommandProcessed = true;
273  do {
274  if (!isPtzCommandProcessed)
275  cout << "Invalid Option !!\n";
276 
277  // Display the user options while streaming
278  DisplayPlayerOptionsToConsole(isPtz, isLive, isRecording);
279 
280  // Read option from user
281  cin >> option;
282  option = tolower(option);
283 
284  // Process PTZ Operations
285  isPtzCommandProcessed = false;
286  if (isPtz)
287  isPtzCommandProcessed = ptzOperationsHandler.DoOperation(option);
288  if (!isPtzCommandProcessed && isRecording && !isLive) {
289  // Process media operations
290  isPtzCommandProcessed = mediaOperationshandler.DoOperation(option);
291  }
292  } while (option != 'q');
293 
294  // Stop Streaming
295  mediaControl->Stop();
297  _gotPlayerHandle = false;
298 
299  // Release all memory created here
300  if (ptzControl != nullptr)
301  delete ptzControl;
302 
303  delete mediaControl;
304  return true;
305 }
virtual void SetWindow(void *handle)=0
virtual bool Play(float speed=0, unsigned int unixTime=0)=0
virtual void Stop()=0
virtual VxResult::Value CanPtz(bool &canPtz) const =0
string _currentlyPlayingCam
virtual VxResult::Value GetPtzController(IVxPtzController *&ptzController) const =0
bool _gotPlayerHandle
static void DisplayPlayerOptionsToConsole(bool isPtzEnabled, bool isLive, bool isRecording)
Display the options that can be entered by user when a stream is playing
static MediaController::IController * GetController(VxSdk::IVxDataSource *dataSource, VxSdk::IVxDataInterface *dataInterface)
Get an instance of IController for the given data source and data interface.
virtual void AddObserver(TimestampEventCallback observer)=0
static void TimestampCallback(MediaController::TimestampEvent *timeEvent)
Callback fired from MediaController class for updating the timestamp of currently playing stream ...
void CppSamples::LiveStreaming::DataSourceLiveStreaming::TimestampCallback ( MediaController::TimestampEvent timeEvent)
staticprotected

Callback fired from MediaController class for updating the timestamp of currently playing stream

Parameters
timeEventContains time info

Definition at line 311 of file DataSourceLiveStreaming.cpp.

311  {
312  // Convert the time to string format
313  time_t timestamp = timeEvent->unixTime;
314  struct tm timeinfo;
315  stringstream fmt;
316  localtime_s(&timeinfo, &timestamp);
317  fmt << (timeinfo.tm_year + 1900) << "-"
318  << setfill('0') << setw(2) << (timeinfo.tm_mon + 1) << "-"
319  << setfill('0') << setw(2) << timeinfo.tm_mday << " "
320  << setfill('0') << setw(2) << timeinfo.tm_hour << ":"
321  << setfill('0') << setw(2) << timeinfo.tm_min << ":"
322  << setfill('0') << setw(2) << timeinfo.tm_sec;
323 
324  // Normally when gstreamer player is invoked it comes with a predefined window title as given below.
325  // So use that text to get the window handle.
326 
327 #ifndef VxSdkInLinux
328 #ifndef NO_MEDIA
329  if (!_gotPlayerHandle) {
330  _gstreamerPlayerWindow = FindWindow(nullptr, _T("GStreamer D3D video sink (internal window)"));
331  if (_gstreamerPlayerWindow != nullptr)
332  {
333  _gotPlayerHandle = true;
334  }
335  }
336 #endif
337  // Once window handle is available, update the title text with currently streaming camera name and time.
338  if (_gotPlayerHandle) {
339  string textToDisplayIntitle = _currentlyPlayingCam + " (" + fmt.str() + ")";
340  ::SetWindowTextA(_gstreamerPlayerWindow, textToDisplayIntitle.c_str());
341  }
342 #endif // #ifndef VxSdkInLinux
343 }
HWND _gstreamerPlayerWindow
#define localtime_s(time, result)
string _currentlyPlayingCam
bool _gotPlayerHandle

Member Data Documentation

bool CppSamples::LiveStreaming::DataSourceLiveStreaming::_isPlayBack
private

Definition at line 18 of file DataSourceLiveStreaming.h.


The documentation for this class was generated from the following files: