OPAL  Version 3.18.8
msrp.h
Go to the documentation of this file.
1 /*
2  * msrp.h
3  *
4  * Support for RFC 4975 Message Session Relay Protocol (MSRP)
5  *
6  * Open Phone Abstraction Library (OPAL)
7  *
8  * Copyright (c) 2008 Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Open Phone Abstraction Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef OPAL_IM_MSRP_H
28 #define OPAL_IM_MSRP_H
29 
30 #include <ptlib.h>
31 #include <opal_config.h>
32 
33 #if OPAL_HAS_MSRP
34 
35 #include <opal/mediastrm.h>
36 #include <opal/mediasession.h>
37 #include <ptclib/url.h>
38 #include <ptclib/inetprot.h>
39 
40 
41 class OpalManager;
42 
43 
45 //
46 // Ancestor for all MSRP encoding types
47 //
48 
50 };
51 
52 
54 //
55 // Ancestor for MSRP protocol
56 //
57 
58 class MSRPProtocol : public PInternetProtocol
59 {
60  public:
61  enum Commands {
62  SEND,
65  };
66 
67  static const unsigned MaximumMessageLength = 1024;
68 
69  class Message
70  {
71  public:
72  struct Chunk {
73  Chunk(const PString & id, unsigned from, unsigned len)
74  : m_chunkId(id), m_rangeFrom(from + 1), m_rangeTo(from + len) { }
75 
76  PString m_chunkId;
77  unsigned m_rangeFrom;
78  unsigned m_rangeTo;
79  };
80  typedef std::vector<Chunk> ChunkList;
82 
83  PString m_id;
84  PURL m_fromURL;
85  PURL m_toURL;
86  PString m_contentType;
87  unsigned m_length;
88  };
89 
90  MSRPProtocol();
91 
92  bool SendSEND(
93  const PURL & from,
94  const PURL & to,
95  const PString & text,
96  const PString & contentType,
97  PString & messageId
98  );
99 
100  bool SendChunk(
101  const PString & transactionId,
102  const PString toUrl,
103  const PString fromUrl,
104  const PMIMEInfo & mime,
105  const PString & body
106  );
107 
108  bool SendResponse(const PString & chunkId,
109  unsigned response,
110  const PString & text,
111  const PString & toUrl,
112  const PString & fromUrl);
113 
114  bool SendREPORT(const PString & chunkId,
115  const PString & toUrl,
116  const PString & fromUrl,
117  const PMIMEInfo & mime);
118 
119  bool ReadMessage(
120  int & command,
121  PString & chunkId,
122  PMIMEInfo & mime,
123  PString & body
124  );
125 
126  //typedef std::map<std::string, Message> MessageMap;
127  //MessageMap m_messageMap;
128  PDECLARE_MUTEX(m_mutex);
129 };
130 
132 //
133 //
134 //
135 
136 class OpalMSRPManager : public PObject
137 {
138  public:
139  enum {
140  DefaultPort = 2855
141  };
142 
143  //
144  // Create an MSRP manager. This is a singleton class
145  //
146  OpalMSRPManager(OpalManager & opal, WORD port = DefaultPort);
148 
149  //
150  // Get the local port for the MSRP manager
151  //
152  bool GetLocalPort(WORD & port);
153 
154  //
155  // Information about a connection to another MSRP manager
156  //
157  class Connection : public PSafeObject {
158  public:
159  Connection(OpalMSRPManager & manager, const std::string & key, MSRPProtocol * protocol = NULL);
160  ~Connection();
161 
162  //
163  // Start handler thread for a connection
164  //
165  void StartHandler();
166 
167  //
168  // Handler thread for a connection
169  //
170  void HandlerThread();
171 
173  std::string m_key;
175  bool m_running;
176  PThread * m_handlerThread;
178  atomic<uint32_t> m_refCount;
179  };
180 
181  //
182  // Get the connection to use for communicating with a remote URL
183  //
184  PSafePtr<Connection> OpenConnection(
185  const PURL & localURL,
186  const PURL & remoteURL
187  );
188 
189  //
190  // close a connection
191  //
192  bool CloseConnection(
193  PSafePtr<OpalMSRPManager::Connection> & connection
194  );
195 
196  //
197  // Create a new MSRP session ID
198  //
199  std::string CreateSessionID();
200 
201  //
202  // return session ID as a path
203  //
204  PURL SessionIDToURL(const OpalTransportAddress & addr, const std::string & id);
205 
206  //
207  // Main listening thread for new connections
208  //
209  void ListenerThread();
210 
211  struct IncomingMSRP {
213  PString m_chunkId;
214  PMIMEInfo m_mime;
215  PString m_body;
216  PSafePtr<Connection> m_connection;
217  };
218 
219  //
220  // dispatch an incoming MSRP message to the correct callback
221  //
222  void DispatchMessage(
223  IncomingMSRP & incomingMsg
224  );
225 
226  typedef PNotifierTemplate<IncomingMSRP &> CallBack;
227 
228  void SetNotifier(
229  const PURL & localUrl,
230  const PURL & remoteURL,
231  const CallBack & notifier
232  );
233 
234  void RemoveNotifier(
235  const PURL & localUrl,
236  const PURL & remoteURL
237  );
238 
240 
241  protected:
244  PDECLARE_MUTEX(mutex);
245  PTCPSocket m_listenerSocket;
246  PThread * m_listenerThread;
247 
248  PDECLARE_MUTEX(m_connectionInfoMapAddMutex);
249  typedef std::map<PString, PSafePtr<Connection> > ConnectionInfoMapType;
251 
252  typedef std::map<PString, CallBack> CallBackMap;
254  PDECLARE_MUTEX(m_callBacksMutex);
255 
256  private:
257  static OpalMSRPManager * msrp;
258 };
259 
261 
265 {
267  public:
268  static const PCaselessString & TCP_MSRP();
269 
270  OpalMSRPMediaSession(const Init & init);
272 
273  virtual PObject * Clone() const { return new OpalMSRPMediaSession(*this); }
274 
275  virtual const PCaselessString & GetSessionType() const { return TCP_MSRP(); }
276  virtual bool Open(const PString & localInterface, const OpalTransportAddress & remoteAddress);
277  virtual bool Close();
278  virtual OpalTransportAddress GetLocalAddress(bool isMediaAddress = true) const;
279  virtual OpalTransportAddress GetRemoteAddress(bool isMediaAddress = true) const;
280  virtual bool SetRemoteAddress(const OpalTransportAddress & remoteAddress, bool isMediaAddress = true);
281 #if OPAL_SDP
282  virtual SDPMediaDescription * CreateSDPMediaDescription();
283 #endif
284 
285  PURL GetLocalURL() const { return m_localUrl; }
286  PURL GetRemoteURL() const { return m_remoteUrl; }
287  void SetRemoteURL(const PURL & url) { m_remoteUrl = url; }
288 
289  virtual bool WritePacket(
290  RTP_DataFrame & frame
291  );
292 
293  PBoolean ReadData(
294  BYTE * data,
295  PINDEX length,
296  PINDEX & read
297  );
298 
300  const OpalMediaFormat & mediaFormat,
301  unsigned sessionID,
302  PBoolean isSource
303  );
304 
306 
307  bool OpenMSRP(const PURL & remoteUrl);
308  void CloseMSRP();
309 
310  void SetConnection(PSafePtr<OpalMSRPManager::Connection> & conn);
311 
314  std::string m_localMSRPSessionId;
317  PSafePtr<OpalMSRPManager::Connection> m_connectionPtr;
319 
320  private:
322  : OpalMediaSession(Init(other.m_connection, other.m_sessionId, other.m_mediaType, false)), m_manager(other.m_manager) { }
323  void operator=(const OpalMSRPMediaSession &) { }
324 };
325 
326 
328 
330 {
331  public:
333  OpalConnection & conn,
334  const OpalMediaFormat & mediaFormat,
335  unsigned sessionID,
336  bool isSource,
337  OpalMSRPMediaSession & msrpSession
338 
339  );
340 
342 
343  virtual PBoolean IsSynchronous() const { return false; }
344  virtual PBoolean RequiresPatchThread() const { return IsSink(); }
345 
349  virtual PBoolean ReadPacket(
350  RTP_DataFrame & frame
351  );
352 
356  virtual PBoolean WritePacket(
357  RTP_DataFrame & frame
358  );
359 
360  virtual bool Open();
361 
362  PURL GetRemoteURL() const { return m_msrpSession.GetRemoteURL(); }
363  void SetRemoteURL(const PURL & url) { m_msrpSession.SetRemoteURL(url); }
364 
367 
368  protected:
369  virtual void InternalClose() { }
370 
372  PString m_remoteParty;
373 };
374 
375 
376 #endif // OPAL_HAS_MSRP
377 
378 #endif // OPAL_IM_MSRP_H
379 
bool m_originating
Definition: msrp.h:177
atomic< uint32_t > m_refCount
Definition: msrp.h:178
PDECLARE_MUTEX(m_mutex)
PURL m_localUrl
Definition: msrp.h:315
bool SendREPORT(const PString &chunkId, const PString &toUrl, const PString &fromUrl, const PMIMEInfo &mime)
Definition: manager.h:150
PDECLARE_NOTIFIER2(OpalMSRPManager, OpalMSRPMediaStream, OnReceiveMSRP, OpalMSRPManager::IncomingMSRP &)
OpalTransportAddress m_remoteAddress
Definition: msrp.h:318
virtual const PCaselessString & GetSessionType() const
Definition: msrp.h:275
OpalMSRPManager & GetManager()
Definition: msrp.h:305
PString m_remoteParty
Definition: msrp.h:372
Connection(OpalMSRPManager &manager, const std::string &key, MSRPProtocol *protocol=NULL)
Commands
Definition: msrp.h:61
Initialisation information for constructing a session.
Definition: mediasession.h:654
Definition: mediasession.h:647
OpalMSRPMediaSession & m_msrpSession
Definition: msrp.h:371
Definition: msrp.h:136
virtual void InternalClose()
Definition: msrp.h:369
std::string m_localMSRPSessionId
Definition: msrp.h:314
virtual bool Close()
virtual bool Open(const PString &localInterface, const OpalTransportAddress &remoteAddress)
PNotifierTemplate< IncomingMSRP & > CallBack
Definition: msrp.h:226
PURL GetRemoteURL() const
Definition: msrp.h:286
void SetConnection(PSafePtr< OpalMSRPManager::Connection > &conn)
Definition: msrp.h:49
OpalMSRPMediaStream(OpalConnection &conn, const OpalMediaFormat &mediaFormat, unsigned sessionID, bool isSource, OpalMSRPMediaSession &msrpSession)
bool ReadMessage(int &command, PString &chunkId, PMIMEInfo &mime, PString &body)
virtual PBoolean WritePacket(RTP_DataFrame &frame)
bool IsSink() const
Definition: mediastrm.h:359
unsigned m_sessionId
Definition: mediasession.h:846
Definition: msrp.h:64
bool SendResponse(const PString &chunkId, unsigned response, const PString &text, const PString &toUrl, const PString &fromUrl)
bool m_isOriginating
Definition: msrp.h:313
PURL m_remoteUrl
Definition: msrp.h:316
bool SendSEND(const PURL &from, const PURL &to, const PString &text, const PString &contentType, PString &messageId)
std::map< PString, PSafePtr< Connection > > ConnectionInfoMapType
Definition: msrp.h:249
PURL GetRemoteURL() const
Definition: msrp.h:362
PSafePtr< Connection > m_connection
Definition: msrp.h:216
PString m_id
Definition: msrp.h:83
virtual OpalMediaStream * CreateMediaStream(const OpalMediaFormat &mediaFormat, unsigned sessionID, PBoolean isSource)
OpalMSRPManager(OpalManager &opal, WORD port=DefaultPort)
Chunk(const PString &id, unsigned from, unsigned len)
Definition: msrp.h:73
static const PCaselessString & TCP_MSRP()
Definition: msrp.h:58
virtual PBoolean ReadPacket(RTP_DataFrame &frame)
virtual PBoolean IsSynchronous() const
Definition: msrp.h:343
PString m_body
Definition: msrp.h:215
Definition: msrp.h:140
Definition: msrp.h:264
PURL SessionIDToURL(const OpalTransportAddress &addr, const std::string &id)
Definition: msrp.h:63
ChunkList m_chunks
Definition: msrp.h:81
OpalMSRPMediaSession(const Init &init)
std::vector< Chunk > ChunkList
Definition: msrp.h:80
PString m_contentType
Definition: msrp.h:86
unsigned m_rangeFrom
Definition: msrp.h:77
PURL m_toURL
Definition: msrp.h:85
Definition: rtp.h:540
void SetRemoteURL(const PURL &url)
Definition: msrp.h:363
PURL GetLocalURL() const
Definition: msrp.h:285
Definition: mediafmt.h:806
Definition: msrp.h:69
OpalMSRPManager & m_manager
Definition: msrp.h:312
void RemoveNotifier(const PURL &localUrl, const PURL &remoteURL)
bool GetLocalPort(WORD &port)
PThread * m_listenerThread
Definition: msrp.h:246
OpalManager & GetOpalManager()
Definition: msrp.h:239
CallBackMap m_callBacks
Definition: msrp.h:253
Definition: msrp.h:211
unsigned m_rangeTo
Definition: msrp.h:78
int m_command
Definition: msrp.h:212
Definition: msrp.h:72
void DispatchMessage(IncomingMSRP &incomingMsg)
PTCPSocket m_listenerSocket
Definition: msrp.h:245
PDECLARE_MUTEX(mutex)
unsigned m_length
Definition: msrp.h:87
PSafePtr< OpalMSRPManager::Connection > m_connectionPtr
Definition: msrp.h:317
void SetNotifier(const PURL &localUrl, const PURL &remoteURL, const CallBack &notifier)
bool SendChunk(const PString &transactionId, const PString toUrl, const PString fromUrl, const PMIMEInfo &mime, const PString &body)
PSafePtr< Connection > OpenConnection(const PURL &localURL, const PURL &remoteURL)
std::string m_key
Definition: msrp.h:173
Definition: msrp.h:157
std::map< PString, CallBack > CallBackMap
Definition: msrp.h:252
PMIMEInfo m_mime
Definition: msrp.h:214
static const unsigned MaximumMessageLength
Definition: msrp.h:67
std::string CreateSessionID()
PBoolean ReadData(BYTE *data, PINDEX length, PINDEX &read)
void ListenerThread()
PString m_chunkId
Definition: msrp.h:213
virtual bool Open()
virtual OpalTransportAddress GetLocalAddress(bool isMediaAddress=true) const
Definition: mediastrm.h:110
WORD m_listenerPort
Definition: msrp.h:243
Definition: msrp.h:62
Definition: connection.h:415
virtual bool SetRemoteAddress(const OpalTransportAddress &remoteAddress, bool isMediaAddress=true)
Definition: msrp.h:329
OpalMSRPManager & m_manager
Definition: msrp.h:172
OpalMediaType m_mediaType
Definition: mediasession.h:847
virtual bool WritePacket(RTP_DataFrame &frame)
PString m_chunkId
Definition: msrp.h:76
bool OpenMSRP(const PURL &remoteUrl)
Definition: transports.h:151
MSRPProtocol * m_protocol
Definition: msrp.h:174
virtual PObject * Clone() const
Definition: msrp.h:273
OpalConnection & m_connection
Definition: mediasession.h:845
PURL m_fromURL
Definition: msrp.h:84
OpalManager & opalManager
Definition: msrp.h:242
PThread * m_handlerThread
Definition: msrp.h:176
bool m_running
Definition: msrp.h:175
ConnectionInfoMapType m_connectionInfoMap
Definition: msrp.h:250
void SetRemoteURL(const PURL &url)
Definition: msrp.h:287
bool CloseConnection(PSafePtr< OpalMSRPManager::Connection > &connection)
virtual PBoolean RequiresPatchThread() const
Definition: msrp.h:344
virtual OpalTransportAddress GetRemoteAddress(bool isMediaAddress=true) const