Media Controller
A GStreamer implementation using the VideoXpert SDK
MediaController::Rtsp::Commands Class Reference

Establishes and controls an RTSP stream. More...

#include <RtspCommands.h>

Public Member Functions

 Commands (const std::string &streamUri, bool isVideo)
 Constructor. More...
 
 ~Commands ()
 Destructor. More...
 
bool Options ()
 Send the OPTIONS method and read the server response. More...
 
bool GetParameter ()
 Send the GET_PARAMETER method and read the server response. More...
 
bool Describe (bool firstAttempt=false)
 Send the DESCRIBE method and read the server response. More...
 
bool Setup (bool firstAttempt=false)
 Send the SETUP method and read the server response. More...
 
void Pause ()
 Send the PAUSE method and read the server response. More...
 
bool SetupStream (GstWrapper *gstwrapper, float speed=0, unsigned int unixTime=0)
 Send the PLAY method with a Range header and read the server response. More...
 
void PlayStream (GstWrapper *gstwrapper)
 Create a pipe line More...
 
void Teardown ()
 Send the TEARDOWN method and read the server response. More...
 
void ResetPath (const std::string &streamUri)
 Reset the RTSP stream location. More...
 
std::string GetSessionId () const
 Get the current session ID for the video stream. More...
 

Private Member Functions

void GetSocket (const std::string &uriString, int bindPort)
 Get the existing socket connection or return a new socket. More...
 
void ClearSocket ()
 Clear any existing socket connections. More...
 

Private Attributes

std::string _sessionId
 
std::string _baseUri
 
std::string _controlUri
 
std::string _uuid
 
SdpParser _sdp
 
int _dataPort
 
int _rtcpPort
 
int _cSeqNum
 
boost::asio::io_service _ioService
 
boost::asio::ip::tcp::socket _pSocket
 
boost::asio::ip::tcp::socket _rSocket
 
bool _isVideo
 

Detailed Description

Establishes and controls an RTSP stream.

Definition at line 13 of file RtspCommands.h.

Constructor & Destructor Documentation

Commands::Commands ( const std::string &  streamUri,
bool  isVideo 
)

Constructor.

Parameters
streamUriThe location of the RTSP stream.
isVideoSpecifies wheather this command handles audio or video.

Definition at line 20 of file RtspCommands.cpp.

20  :
21  _baseUri(streamUri),
22  _controlUri(streamUri),
23  _dataPort(0),
24  _rtcpPort(0),
25  _cSeqNum(0),
26  _ioService(),
29  _isVideo(isVideo) { }
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
boost::asio::io_service _ioService
Definition: RtspCommands.h:96
boost::asio::ip::tcp::socket _rSocket
Definition: RtspCommands.h:98
MediaController::Rtsp::Commands::~Commands ( )
inline

Destructor.

Definition at line 26 of file RtspCommands.h.

26  {
27  ClearSocket();
28  }
void ClearSocket()
Clear any existing socket connections.

Member Function Documentation

void Commands::ClearSocket ( )
private

Clear any existing socket connections.

Definition at line 394 of file RtspCommands.cpp.

395 {
396  _pSocket.close();
397  _rSocket.close();
398 }
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
boost::asio::ip::tcp::socket _rSocket
Definition: RtspCommands.h:98
bool Commands::Describe ( bool  firstAttempt = false)

Send the DESCRIBE method and read the server response.

Definition at line 97 of file RtspCommands.cpp.

97  {
98  // Increment the value for the CSeq field.
99  _cSeqNum++;
100 
101  // Send the DESCRIBE request in the following format:
102  // DESCRIBE {uri} RTSP/1.0
103  // CSeq: {_cSeqNum}
104  // User-Agent: Pelco VxSdk
105  // Accept: application/sdp
106  GetSocket(this->_controlUri, this->_dataPort);
107  boost::asio::streambuf request;
108  ostream requestStream(&request);
109  requestStream << kDescribe << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
110  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
111  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
112  requestStream << kHeaderAccept << kColonSpace << kSdpMimeType << kTwoNewLines;
113  try { write(_pSocket, request); }
114  catch (...) { return false; }
115 
116  // Parse the server response.
118  if (resp.statusCode != kStatusCode200) { return false; }
119  // Parse the session description information.
120  SdpParser& parser = this->_sdp;
121  parser.Parse(resp.content);
122  MediaDescription md;
123  if (_isVideo)
124  md = parser.GetFirstVideo();
125  else
126  md = parser.GetFirstAudio();
127 
128  if (!firstAttempt) {
129  // Set up the pipeline variables to use the correct values based on the transport protocol.
130  this->_dataPort = md.isMulticast ? md.port : _pSocket.local_endpoint().port();
131  this->_rtcpPort = md.isMulticast ? md.port + 1 : _rSocket.local_endpoint().port();
132  }
133 
134  if (!parser.sessionControlUri.empty() && parser.sessionControlUri != "*")
135  this->_controlUri = parser.sessionControlUri;
136 
137  return true;
138 }
std::string content
The body of the response.
Definition: RtspResponse.h:30
const MediaDescription & GetFirstVideo()
Get the first available video media description.
The parsed information from an RTSP response.
Definition: RtspResponse.h:10
static const unsigned short kStatusCode200
Definition: Constants.h:44
void Parse(const std::string &sdp)
Parses a raw SDP packet string.
static const char * kColonSpace
Definition: Constants.h:51
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const char * kHeaderAccept
Definition: Constants.h:25
static const char * kSdpMimeType
Definition: Constants.h:67
static const char * kActualUserAgent
Definition: Constants.h:59
Parses the SDP packets sent from an RTSP source.
Definition: RtspSdpParser.h:12
The parsed media information from an SDP packet.
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
unsigned short port
The transport port for media.
const MediaDescription & GetFirstAudio()
Get the first available audio media description.
std::string sessionControlUri
The session control URI.
Definition: RtspSdpParser.h:54
bool isMulticast
Specifies whether the transport type is multicast or unicast.
static const char * kWhitespace
Definition: Constants.h:49
unsigned int statusCode
The reponse status code.
Definition: RtspResponse.h:15
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
boost::asio::ip::tcp::socket _rSocket
Definition: RtspCommands.h:98
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
static const char * kDescribe
Definition: Constants.h:62
bool Commands::GetParameter ( )

Send the GET_PARAMETER method and read the server response.

Definition at line 71 of file RtspCommands.cpp.

71  {
72  // Increment the value for the CSeq field.
73  _cSeqNum++;
74 
75  // Send the GET_PARAMETER request in the following format:
76  // GET_PARAMETER {uri} RTSP/1.0
77  // CSeq: {_cSeqNum}
78  // User-Agent: Pelco VxSdk
79  // Session: {_session}
80  GetSocket(this->_controlUri, this->_dataPort);
81  boost::asio::streambuf request;
82  ostream requestStream(&request);
83  requestStream << kGetParameter << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
84  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
85  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
86  requestStream << kHeaderSession << kColonSpace << _sessionId << kTwoNewLines;
87  try { write(_pSocket, request); }
88  catch (...) { return false; }
89 
90  // Parse the server response.
92  if (resp.statusCode != kStatusCode200) { return false; }
93 
94  return true;
95 }
The parsed information from an RTSP response.
Definition: RtspResponse.h:10
static const unsigned short kStatusCode200
Definition: Constants.h:44
static const char * kHeaderSession
Definition: Constants.h:21
static const char * kColonSpace
Definition: Constants.h:51
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kGetParameter
Definition: Constants.h:61
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const char * kActualUserAgent
Definition: Constants.h:59
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
static const char * kWhitespace
Definition: Constants.h:49
unsigned int statusCode
The reponse status code.
Definition: RtspResponse.h:15
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
std::string MediaController::Rtsp::Commands::GetSessionId ( ) const
inline

Get the current session ID for the video stream.

Returns
The session ID.

Definition at line 83 of file RtspCommands.h.

83 { return _sessionId; }
void Commands::GetSocket ( const std::string &  uriString,
int  bindPort 
)
private

Get the existing socket connection or return a new socket.

Parameters
uriStringThe location of the media.
bindPortThe initial port to bind to.

Definition at line 347 of file RtspCommands.cpp.

347  {
348  string ip, port;
349  Extract(uriString, ip, port);
350 
351  boost::system::error_code ec;
352  boost::asio::ip::tcp::endpoint endpoint = _pSocket.remote_endpoint(ec);
353  if (ec) {
354  _pSocket.open(boost::asio::ip::tcp::v4());
355  _pSocket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
356  _rSocket.open(boost::asio::ip::tcp::v4());
357  _rSocket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
358 
359  boost::asio::ip::tcp::resolver resolver(_ioService);
360  boost::asio::ip::tcp::resolver::query query(ip, port);
361  boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query);
362  boost::asio::ip::tcp::resolver::iterator end;
363 
364  boost::system::error_code error = boost::asio::error::host_not_found;
365  while (error && endpointIterator != end) {
366  _pSocket.connect(*endpointIterator++, error);
367  }
368  }
369  else if (endpoint.address().to_string() != ip || to_string(endpoint.port()) != port) {
370  _pSocket.shutdown(boost::asio::socket_base::shutdown_both);
371  _pSocket.close();
372  _rSocket.close();
373  _pSocket.open(boost::asio::ip::tcp::v4());
374  _pSocket.set_option(boost::asio::ip::tcp::socket::reuse_address(true));
375  _pSocket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), bindPort));
376  _rSocket.open(boost::asio::ip::tcp::v4());
377  _rSocket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
378 
379  boost::asio::ip::tcp::resolver resolver(_ioService);
380  boost::asio::ip::tcp::resolver::query query(ip, port);
381  boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query);
382  boost::asio::ip::tcp::resolver::iterator end;
383 
384  boost::system::error_code error = boost::asio::error::host_not_found;
385  while (error && endpointIterator != end) {
386  _pSocket.connect(*endpointIterator++, error);
387  }
388  }
389 }
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
void Extract(const std::string &ip, std::string &address, std::string &service)
boost::asio::io_service _ioService
Definition: RtspCommands.h:96
boost::asio::ip::tcp::socket _rSocket
Definition: RtspCommands.h:98
bool Commands::Options ( )

Send the OPTIONS method and read the server response.

Definition at line 31 of file RtspCommands.cpp.

31  {
32  // Reset the value for the CSeq field.
33  _cSeqNum = 0;
34 
35  // Send the OPTIONS request in the following format:
36  // OPTIONS {uri} RTSP/1.0
37  // CSeq: {_cSeqNum}
38  // User-Agent: Pelco VxSdk
39  GetSocket(this->_controlUri, this->_dataPort);
40  this->_dataPort = _pSocket.local_endpoint().port();
41  this->_rtcpPort = _rSocket.local_endpoint().port();
42  boost::asio::streambuf request;
43  ostream requestStream(&request);
44  requestStream << kOptions << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
45  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
47  try { write(_pSocket, request); }
48  catch (...) { return false; }
49 
50  // Parse the server response.
52  if (resp.statusCode == kStatusCode301 || resp.statusCode == kStatusCode302) {
53  // Set the playback URI to the redirect location.
54  typedef std::map<std::string, std::string>::iterator it_type;
55  for (it_type iterator = resp.headers.begin(); iterator != resp.headers.end(); ++iterator) {
56  string name = iterator->first;
57  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
58  if (name == kLowerCaseHeaderLocation) {
59  this->_controlUri = iterator->second;
60  Options();
61  break;
62  }
63  }
64  }
65 
66  if (resp.statusCode != kStatusCode200) { return false; }
67 
68  return true;
69 }
The parsed information from an RTSP response.
Definition: RtspResponse.h:10
static const unsigned short kStatusCode200
Definition: Constants.h:44
static const char * kColonSpace
Definition: Constants.h:51
std::map< std::string, std::string > headers
A list of response headers.
Definition: RtspResponse.h:25
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kLowerCaseHeaderLocation
Definition: Constants.h:32
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const unsigned short kStatusCode301
Definition: Constants.h:45
static const unsigned short kStatusCode302
Definition: Constants.h:46
bool Options()
Send the OPTIONS method and read the server response.
static const char * kActualUserAgent
Definition: Constants.h:59
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
static const char * kWhitespace
Definition: Constants.h:49
static const char * kOptions
Definition: Constants.h:60
unsigned int statusCode
The reponse status code.
Definition: RtspResponse.h:15
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
boost::asio::ip::tcp::socket _rSocket
Definition: RtspCommands.h:98
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
void Commands::Pause ( )

Send the PAUSE method and read the server response.

Definition at line 279 of file RtspCommands.cpp.

279  {
280  // Increment the value for the CSeq field.
281  _cSeqNum++;
282 
283  // Send the PAUSE request in the following format:
284  // PAUSE {uri} RTSP/1.0
285  // CSeq: {_cSeqNum}
286  // User-Agent: Pelco VxSdk
287  // Session: {_sessionId}
288  GetSocket(this->_controlUri, this->_dataPort);
289  boost::asio::streambuf request;
290  ostream requestStream(&request);
291  requestStream << kPause << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
292  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
293  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
294  requestStream << kHeaderSession << kColonSpace << this->_sessionId << kTwoNewLines;
295  try { write(_pSocket, request); }
296  catch (...) { return; }
297 
298  // Parse the server response.
300 }
static const char * kHeaderSession
Definition: Constants.h:21
static const char * kColonSpace
Definition: Constants.h:51
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kPause
Definition: Constants.h:65
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const char * kActualUserAgent
Definition: Constants.h:59
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
static const char * kWhitespace
Definition: Constants.h:49
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
void Commands::PlayStream ( MediaController::GstWrapper gstwrapper)

Create a pipe line

Definition at line 252 of file RtspCommands.cpp.

252  {
253  if (!gstwrapper->IsPipelineActive()) {
254  // Update the pipeline using the description generated above.
255  gstwrapper->SetPorts(this->_dataPort, this->_rtcpPort);
256  gstwrapper->SetRtcpHostIP(this->_pSocket.remote_endpoint().address().to_string());
257 
258  if (_isVideo) {
259  MediaDescription md = this->_sdp.GetFirstVideo();
260  if (md.isMulticast)
261  gstwrapper->SetMulticastAddress(md.ip);
262  if (md.payload == 26)
263  md.encoding = "JPEG";
264 
265  gstwrapper->SetCaps("video" + BuildGstCaps(md));
266  gstwrapper->CreateVideoRtspPipeline(md.encoding);
267  }
268  else {
269  MediaDescription md = this->_sdp.GetFirstAudio();
270  if (md.isMulticast)
271  gstwrapper->SetMulticastAddress(md.ip);
272 
273  gstwrapper->SetCaps("audio" + BuildGstCaps(md));
274  gstwrapper->CreateAudioRtspPipeline();
275  }
276  }
277 }
const MediaDescription & GetFirstVideo()
Get the first available video media description.
string BuildGstCaps(MediaDescription description)
Build the GStreamers caps based on the values contained in the SDP response.
unsigned short payload
RTP payload type number.
std::string ip
The address for media.
void SetMulticastAddress(std::string multicastAddress)
Set the multicast group address.
Definition: GstWrapper.cpp:297
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
void CreateVideoRtspPipeline(std::string encoding)
Create the pipeline for an RTSP video stream.
Definition: GstWrapper.cpp:412
bool IsPipelineActive() const
Get the status of the pipeline.
Definition: GstWrapper.cpp:324
void CreateAudioRtspPipeline()
Create the pipeline for an RTSP audio stream.
Definition: GstWrapper.cpp:458
void SetRtcpHostIP(std::string hostIp)
Set the RTCP host IP.
Definition: GstWrapper.cpp:293
The parsed media information from an SDP packet.
const MediaDescription & GetFirstAudio()
Get the first available audio media description.
void SetPorts(int rtpPort, int rtcpPort)
Set the receiver ports.
Definition: GstWrapper.cpp:278
bool isMulticast
Specifies whether the transport type is multicast or unicast.
void SetCaps(std::string caps, bool isMjpeg=false)
Set the stream capabilities.
Definition: GstWrapper.cpp:284
std::string encoding
The encoding name denoting the payload format to be used.
void Commands::ResetPath ( const std::string &  streamUri)

Reset the RTSP stream location.

Parameters
streamUriThe location of the RTSP stream.

Definition at line 332 of file RtspCommands.cpp.

332  {
333  this->_baseUri.clear();
334  this->_controlUri.clear();
335  this->_baseUri = this->_controlUri = streamUri;
336 }
bool Commands::Setup ( bool  firstAttempt = false)

Send the SETUP method and read the server response.

Definition at line 140 of file RtspCommands.cpp.

140  {
141  // Increment the value for the CSeq field.
142  _cSeqNum++;
143  MediaDescription md;
144  if (_isVideo)
145  md = this->_sdp.GetFirstVideo();
146  else
147  md = this->_sdp.GetFirstAudio();
148 
149  if (md.controlUri == "video" || md.controlUri == "audio")
150  md.controlUri = _controlUri;
151 
152  // Send the SETUP request in the following format:
153  // SETUP {uri} RTSP/1.0
154  // CSeq: {_cSeqNum}
155  // User-Agent: Pelco VxSdk
156  // Transport: {md.protocol};unicast;client_port={_port}-{_port+1}
157  GetSocket(md.controlUri, this->_dataPort);
158  boost::asio::streambuf request;
159  ostream requestStream(&request);
160  requestStream << kSetup << kWhitespace << md.controlUri << kWhitespace << kRtspVersion << kOneNewLine;
161  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
162  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
163  if (!firstAttempt)
164  requestStream << kHeaderTransport << kColonSpace << md.protocol << kSemicolon << (md.isMulticast ? "multicast" : "unicast");
165  else
166  requestStream << kHeaderTransport << kColonSpace << md.protocol << kSemicolon << "unicast";
167 
168  requestStream << ";client_port=" <<this->_dataPort << "-" <<this->_rtcpPort << kTwoNewLines;
169  try { write(_pSocket, request); }
170  catch (...) { return false; }
171 
172  // Parse the server response.
174  if (resp.statusCode != kStatusCode200) { return false; }
175  // Set the session ID using the UUID obtained from the server response.
176  this->_sessionId = GetSessionUuid(resp.session);
177 
178  return true;
179 }
static const char * kSemicolon
Definition: Constants.h:52
const MediaDescription & GetFirstVideo()
Get the first available video media description.
std::string protocol
The transport protocol.
The parsed information from an RTSP response.
Definition: RtspResponse.h:10
std::string session
The session identifier.
Definition: RtspResponse.h:20
static const unsigned short kStatusCode200
Definition: Constants.h:44
static const char * kSetup
Definition: Constants.h:63
static const char * kColonSpace
Definition: Constants.h:51
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kHeaderUserAgent
Definition: Constants.h:22
std::string controlUri
The media control URI.
static const char * kActualUserAgent
Definition: Constants.h:59
The parsed media information from an SDP packet.
static const char * kTwoNewLines
Definition: Constants.h:57
string GetSessionUuid(const string &session)
Parse the session ID contained within an RTSP response.
static const char * kRtspVersion
Definition: Constants.h:58
const MediaDescription & GetFirstAudio()
Get the first available audio media description.
bool isMulticast
Specifies whether the transport type is multicast or unicast.
static const char * kWhitespace
Definition: Constants.h:49
unsigned int statusCode
The reponse status code.
Definition: RtspResponse.h:15
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
static const char * kHeaderTransport
Definition: Constants.h:23
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
bool Commands::SetupStream ( MediaController::GstWrapper gstwrapper,
float  speed = 0,
unsigned int  unixTime = 0 
)

Send the PLAY method with a Range header and read the server response.

Parameters
speedThe playback speed. Negative values can be used for reverse playback. A value of 0 will resume a paused stream.
unixTimeThe start time for playback. A value of 0 will start a live stream.

Definition at line 181 of file RtspCommands.cpp.

181  {
182  // Increment the value for the CSeq field.
183  _cSeqNum++;
184  bool ret = false;
185 
186  // Send the PLAY request in the following format:
187  // PLAY {uri} RTSP/1.0
188  // CSeq: {_cSeqNum}
189  // User-Agent: Pelco VxSdk
190  // Session: {_sessionId}
191  // Scale: {speed}
192  // Range: clock={timeStr}
193  string timeStr = Utilities::UnixTimeToRfc3339(unixTime);
194  GetSocket(this->_controlUri, this->_dataPort);
195  boost::asio::streambuf request;
196  ostream requestStream(&request);
197  requestStream << kPlay << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
198  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
199  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
200  if (speed != 0)
201  if (speed < 1.0f && speed > -1.0f)
202  requestStream << kHeaderScale << kColonSpace << setprecision(1) << fixed << speed << kOneNewLine;
203  else
204  requestStream << kHeaderScale << kColonSpace << static_cast<int>(speed) << kOneNewLine;
205  else
206  requestStream << kHeaderRange << kColonSpace << "npt=0-" << kOneNewLine;
207 
208  if (unixTime != 0)
209  requestStream << kHeaderRange << kColonSpace << "clock=" << timeStr.c_str() << kOneNewLine;
210 
211  requestStream << kHeaderSession << kColonSpace << this->_sessionId << kTwoNewLines;
212 
213  try { write(_pSocket, request); }
214  catch (...) { return false; }
215 
216  // Parse the server response.
218 
219  // If a redirect code was returned we need to tear down the current session and start a new one using the redirect location.
220  if (resp.statusCode == kStatusCode301 || resp.statusCode == kStatusCode302) {
221  // Set the playback URI to the redirect location.
222  typedef std::map<std::string, std::string>::iterator it_type;
223  for (it_type iterator = resp.headers.begin(); iterator != resp.headers.end(); ++iterator) {
224  string name = iterator->first;
225  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
226  if (name == kLowerCaseHeaderLocation) {
227  this->_controlUri = iterator->second;
228  break;
229  }
230  }
231 
232  if (int(speed) != 0)
234  else
236 
237  // Send the sequence of RTSP commands needed to start a new stream using the new location.
238 #ifndef WIN32
239  ClearSocket();
240 #endif
241  Options();
242  Describe();
243  Setup();
244  ret = SetupStream(gstwrapper, speed, unixTime);
245  }
246  else if (resp.statusCode == kStatusCode200)
247  return true;
248 
249  return ret;
250 }
void SetMode(Controller::Mode mode)
Set the playback mode.
Definition: GstWrapper.cpp:319
bool Setup(bool firstAttempt=false)
Send the SETUP method and read the server response.
static const char * kHeaderRange
Definition: Constants.h:26
The stream is playing live video.
Definition: IStream.h:21
void ClearSocket()
Clear any existing socket connections.
The parsed information from an RTSP response.
Definition: RtspResponse.h:10
static const unsigned short kStatusCode200
Definition: Constants.h:44
static const char * kHeaderSession
Definition: Constants.h:21
static const char * kColonSpace
Definition: Constants.h:51
std::map< std::string, std::string > headers
A list of response headers.
Definition: RtspResponse.h:25
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
bool SetupStream(GstWrapper *gstwrapper, float speed=0, unsigned int unixTime=0)
Send the PLAY method with a Range header and read the server response.
The stream is playing recorded video.
Definition: IStream.h:24
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kLowerCaseHeaderLocation
Definition: Constants.h:32
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const unsigned short kStatusCode301
Definition: Constants.h:45
static const unsigned short kStatusCode302
Definition: Constants.h:46
bool Options()
Send the OPTIONS method and read the server response.
static const char * kActualUserAgent
Definition: Constants.h:59
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
static const char * kWhitespace
Definition: Constants.h:49
unsigned int statusCode
The reponse status code.
Definition: RtspResponse.h:15
static const char * kPlay
Definition: Constants.h:64
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
bool Describe(bool firstAttempt=false)
Send the DESCRIBE method and read the server response.
static const char * kHeaderCSeq
Definition: Constants.h:24
string UnixTimeToRfc3339(unsigned int unixTime)
Convert a unix timestamp to an RFC 3339 formatted string.
Definition: Utilities.cpp:8
static const char * kHeaderScale
Definition: Constants.h:27
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.
void Commands::Teardown ( )

Send the TEARDOWN method and read the server response.

Definition at line 302 of file RtspCommands.cpp.

302  {
303  // Increment the value for the CSeq field.
304  _cSeqNum++;
305 
306  // Send the TEARDOWN request in the following format:
307  // TEARDOWN {uri} RTSP/1.0
308  // CSeq: {_cSeqNum}
309  // User-Agent: Pelco VxSdk
310  // Session: {_sessionId}
311  GetSocket(this->_controlUri, this->_dataPort);
312  boost::asio::streambuf request;
313  ostream requestStream(&request);
314  requestStream << kTeardown << kWhitespace << this->_controlUri << kWhitespace << kRtspVersion << kOneNewLine;
315  requestStream << kHeaderCSeq << kColonSpace << _cSeqNum << kOneNewLine;
316  requestStream << kHeaderUserAgent << kColonSpace << kActualUserAgent << kOneNewLine;
317  requestStream << kHeaderSession << kColonSpace << this->_sessionId << kTwoNewLines;
318 
319  try { write(_pSocket, request); }
320  catch (...) { return; }
321 
322  // Parse the server response.
324 
325  // Reset the value for the CSeq field.
326  _cSeqNum = 0;
327  // Reset the control URI
328  this->_controlUri = _baseUri;
329  this->_dataPort = this->_rtcpPort = 0;
330 }
static const char * kTeardown
Definition: Constants.h:66
static const char * kHeaderSession
Definition: Constants.h:21
static const char * kColonSpace
Definition: Constants.h:51
boost::asio::ip::tcp::socket _pSocket
Definition: RtspCommands.h:97
static const char * kOneNewLine
Definition: Constants.h:56
static const char * kHeaderUserAgent
Definition: Constants.h:22
static const char * kActualUserAgent
Definition: Constants.h:59
static const char * kTwoNewLines
Definition: Constants.h:57
static const char * kRtspVersion
Definition: Constants.h:58
static const char * kWhitespace
Definition: Constants.h:49
Response ProcessResponse(boost::asio::ip::tcp::socket &socket)
Process an RTSP response.
static const char * kHeaderCSeq
Definition: Constants.h:24
void GetSocket(const std::string &uriString, int bindPort)
Get the existing socket connection or return a new socket.

Member Data Documentation

std::string MediaController::Rtsp::Commands::_baseUri
private

Definition at line 89 of file RtspCommands.h.

std::string MediaController::Rtsp::Commands::_controlUri
private

Definition at line 90 of file RtspCommands.h.

int MediaController::Rtsp::Commands::_cSeqNum
private

Definition at line 95 of file RtspCommands.h.

int MediaController::Rtsp::Commands::_dataPort
private

Definition at line 93 of file RtspCommands.h.

boost::asio::io_service MediaController::Rtsp::Commands::_ioService
private

Definition at line 96 of file RtspCommands.h.

bool MediaController::Rtsp::Commands::_isVideo
private

Definition at line 99 of file RtspCommands.h.

boost::asio::ip::tcp::socket MediaController::Rtsp::Commands::_pSocket
private

Definition at line 97 of file RtspCommands.h.

boost::asio::ip::tcp::socket MediaController::Rtsp::Commands::_rSocket
private

Definition at line 98 of file RtspCommands.h.

int MediaController::Rtsp::Commands::_rtcpPort
private

Definition at line 94 of file RtspCommands.h.

SdpParser MediaController::Rtsp::Commands::_sdp
private

Definition at line 92 of file RtspCommands.h.

std::string MediaController::Rtsp::Commands::_sessionId
private

Definition at line 88 of file RtspCommands.h.

std::string MediaController::Rtsp::Commands::_uuid
private

Definition at line 91 of file RtspCommands.h.


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