JRTPLIB  3.9.2
rtpsession.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2016 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31 */
32 
37 #ifndef RTPSESSION_H
38 
39 #define RTPSESSION_H
40 
41 #include "rtpconfig.h"
42 #include "rtplibraryversion.h"
43 #include "rtppacketbuilder.h"
44 #include "rtpsessionsources.h"
45 #include "rtptransmitter.h"
46 #include "rtpcollisionlist.h"
47 #include "rtcpscheduler.h"
48 #include "rtcppacketbuilder.h"
49 #include "rtptimeutilities.h"
51 #include "rtpmemoryobject.h"
52 #include <list>
53 
54 #ifdef RTP_SUPPORT_THREAD
55  #include <jthread/jmutex.h>
56 #endif // RTP_SUPPORT_THREAD
57 
58 namespace jrtplib
59 {
60 
61 class RTPTransmitter;
62 class RTPSessionParams;
63 class RTPTransmissionParams;
64 class RTPAddress;
65 class RTPSourceData;
66 class RTPPacket;
67 class RTPPollThread;
68 class RTPTransmissionInfo;
69 class RTCPCompoundPacket;
70 class RTCPPacket;
71 class RTCPAPPPacket;
72 
79 class JRTPLIB_IMPORTEXPORT RTPSession : public RTPMemoryObject
80 {
81 public:
91  RTPSession(RTPRandom *rnd = 0, RTPMemoryManager *mgr = 0);
92  virtual ~RTPSession();
93 
100  int Create(const RTPSessionParams &sessparams,const RTPTransmissionParams *transparams = 0, RTPTransmitter::TransmissionProtocol proto = RTPTransmitter::IPv4UDPProto);
101 
109  int Create(const RTPSessionParams &sessparams,RTPTransmitter *transmitter);
110 
112  void Destroy();
113 
119  void BYEDestroy(const RTPTime &maxwaittime,const void *reason,size_t reasonlength);
120 
122  bool IsActive();
123 
125  uint32_t GetLocalSSRC();
126 
128  int AddDestination(const RTPAddress &addr);
129 
131  int DeleteDestination(const RTPAddress &addr);
132 
134  void ClearDestinations();
135 
137  bool SupportsMulticasting();
138 
140  int JoinMulticastGroup(const RTPAddress &addr);
141 
143  int LeaveMulticastGroup(const RTPAddress &addr);
144 
146  void LeaveAllMulticastGroups();
147 
153  int SendPacket(const void *data,size_t len);
154 
159  int SendPacket(const void *data,size_t len,
160  uint8_t pt,bool mark,uint32_t timestampinc);
161 
168  int SendPacketEx(const void *data,size_t len,
169  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
170 
177  int SendPacketEx(const void *data,size_t len,
178  uint8_t pt,bool mark,uint32_t timestampinc,
179  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
180 #ifdef RTP_SUPPORT_SENDAPP
181 
188  int SendRTCPAPPPacket(uint8_t subtype, const uint8_t name[4], const void *appdata, size_t appdatalen);
189 #endif // RTP_SUPPORT_SENDAPP
190 
191 #ifdef RTP_SUPPORT_RTCPUNKNOWN
192 
197  int SendUnknownPacket(bool sr, uint8_t payload_type, uint8_t subtype, const void *data, size_t len);
198 #endif // RTP_SUPPORT_RTCPUNKNOWN
199 
200  int SetDefaultPayloadType(uint8_t pt);
201 
203  int SetDefaultMark(bool m);
204 
206  int SetDefaultTimestampIncrement(uint32_t timestampinc);
207 
214  int IncrementTimestamp(uint32_t inc);
215 
223  int IncrementTimestampDefault();
224 
231  int SetPreTransmissionDelay(const RTPTime &delay);
232 
240  RTPTransmissionInfo *GetTransmissionInfo();
241 
243  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
244 
248  int Poll();
249 
255  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
256 
260  int AbortWait();
261 
265  RTPTime GetRTCPDelay();
266 
273  int BeginDataAccess();
274 
279  bool GotoFirstSource();
280 
285  bool GotoNextSource();
286 
291  bool GotoPreviousSource();
292 
299  bool GotoFirstSourceWithData();
300 
307  bool GotoNextSourceWithData();
308 
315  bool GotoPreviousSourceWithData();
316 
318  RTPSourceData *GetCurrentSourceInfo();
319 
323  RTPSourceData *GetSourceInfo(uint32_t ssrc);
324 
331  RTPPacket *GetNextPacket();
332 
334  void DeletePacket(RTPPacket *p);
335 
337  int EndDataAccess();
338 
343  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
344 
346  int AddToIgnoreList(const RTPAddress &addr);
347 
349  int DeleteFromIgnoreList(const RTPAddress &addr);
350 
352  void ClearIgnoreList();
353 
355  int AddToAcceptList(const RTPAddress &addr);
356 
358  int DeleteFromAcceptList(const RTPAddress &addr);
359 
361  void ClearAcceptList();
362 
364  int SetMaximumPacketSize(size_t s);
365 
367  int SetSessionBandwidth(double bw);
368 
375  int SetTimestampUnit(double u);
376 
383  void SetNameInterval(int count);
384 
391  void SetEMailInterval(int count);
392 
399  void SetLocationInterval(int count);
400 
407  void SetPhoneInterval(int count);
408 
415  void SetToolInterval(int count);
416 
423  void SetNoteInterval(int count);
424 
426  int SetLocalName(const void *s,size_t len);
427 
429  int SetLocalEMail(const void *s,size_t len);
430 
432  int SetLocalLocation(const void *s,size_t len);
433 
435  int SetLocalPhone(const void *s,size_t len);
436 
438  int SetLocalTool(const void *s,size_t len);
439 
441  int SetLocalNote(const void *s,size_t len);
442 
443 #ifdef RTPDEBUG
444  void DumpSources();
445  void DumpTransmitter();
446 #endif // RTPDEBUG
447 protected:
455  virtual RTPTransmitter *NewUserDefinedTransmitter() { return 0; }
456 
458  virtual void OnRTPPacket(RTPPacket *pack,const RTPTime &receivetime,
459  const RTPAddress *senderaddress) { }
460 
462  virtual void OnRTCPCompoundPacket(RTCPCompoundPacket *pack,const RTPTime &receivetime,
463  const RTPAddress *senderaddress) { }
464 
470  virtual void OnSSRCCollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,bool isrtp) { }
471 
473  virtual void OnCNAMECollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,
474  const uint8_t *cname,size_t cnamelength) { }
475 
477  virtual void OnNewSource(RTPSourceData *srcdat) { }
478 
480  virtual void OnRemoveSource(RTPSourceData *srcdat) { }
481 
483  virtual void OnTimeout(RTPSourceData *srcdat) { }
484 
486  virtual void OnBYETimeout(RTPSourceData *srcdat) { }
487 
491  virtual void OnAPPPacket(RTCPAPPPacket *apppacket,const RTPTime &receivetime,
492  const RTPAddress *senderaddress) { }
493 
495  virtual void OnUnknownPacketType(RTCPPacket *rtcppack,const RTPTime &receivetime,
496  const RTPAddress *senderaddress) { }
497 
499  virtual void OnUnknownPacketFormat(RTCPPacket *rtcppack,const RTPTime &receivetime,
500  const RTPAddress *senderaddress) { }
501 
503  virtual void OnNoteTimeout(RTPSourceData *srcdat) { }
504 
506  virtual void OnBYEPacket(RTPSourceData *srcdat) { }
507 
510 #ifdef RTP_SUPPORT_THREAD
511 
512  virtual void OnPollThreadError(int errcode) { }
513 
518  virtual void OnPollThreadStep() { }
519 
525  virtual void OnPollThreadStart(bool &stop) { }
526 
530  virtual void OnPollThreadStop() { }
531 
532 #endif // RTP_SUPPORT_THREAD
533 private:
534  int InternalCreate(const RTPSessionParams &sessparams);
535  int CreateCNAME(uint8_t *buffer,size_t *bufferlength,bool resolve);
536  int ProcessPolledData();
537  int ProcessRTCPCompoundPacket(RTCPCompoundPacket &rtcpcomppack,RTPRawPacket *pack);
538  RTPRandom *GetRandomNumberGenerator(RTPRandom *r);
539 
540  RTPRandom *rtprnd;
541  bool deletertprnd;
542 
543  RTPTransmitter *rtptrans;
544  bool created;
545  bool deletetransmitter;
546  bool usingpollthread;
547  bool acceptownpackets;
548  bool useSR_BYEifpossible;
549  size_t maxpacksize;
550  double sessionbandwidth;
551  double controlfragment;
552  double sendermultiplier;
553  double byemultiplier;
554  double membermultiplier;
555  double collisionmultiplier;
556  double notemultiplier;
557  bool sentpackets;
558 
559  RTPSessionSources sources;
560  RTPPacketBuilder packetbuilder;
561  RTCPScheduler rtcpsched;
562  RTCPPacketBuilder rtcpbuilder;
563  RTPCollisionList collisionlist;
564 
565  std::list<RTCPCompoundPacket *> byepackets;
566 
567 #ifdef RTP_SUPPORT_THREAD
568  RTPPollThread *pollthread;
569  jthread::JMutex sourcesmutex,buildermutex,schedmutex,packsentmutex;
570 
571  friend class RTPPollThread;
572 #endif // RTP_SUPPORT_THREAD
573  friend class RTPSessionSources;
574  friend class RTCPSessionPacketBuilder;
575 };
576 
577 } // end namespace
578 
579 #endif // RTPSESSION_H
580 
High level class for using RTP.
Definition: rtpsession.h:79
Describes an RTCP APP packet.
Definition: rtcpapppacket.h:54
virtual void OnBYEPacket(RTPSourceData *srcdat)
Is called when a BYE packet has been processed for source srcdat.
Definition: rtpsession.h:506
This class is used by the transmission component to store the incoming RTP and RTCP data in...
Definition: rtprawpacket.h:51
Specifies the internal UDP over IPv4 transmitter.
Definition: rtptransmitter.h:71
virtual void OnAPPPacket(RTCPAPPPacket *apppacket, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an RTCP APP packet apppacket has been received at time receivetime from address sender...
Definition: rtpsession.h:491
Base class for transmission parameters.
Definition: rtptransmitter.h:227
This class can be used to build RTP packets and is a bit more high-level than the RTPPacket class: it...
Definition: rtppacketbuilder.h:57
virtual void OnNoteTimeout(RTPSourceData *srcdat)
Is called when the SDES NOTE item for source srcdat has been timed out.
Definition: rtpsession.h:503
This class determines when RTCP compound packets should be sent.
Definition: rtcpscheduler.h:101
Interface for generating random numbers.
Definition: rtprandom.h:51
Represents an RTCP compound packet.
Definition: rtcpcompoundpacket.h:53
virtual void OnRTCPCompoundPacket(RTCPCompoundPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an incoming RTCP packet is about to be processed.
Definition: rtpsession.h:462
virtual void OnRTPPacket(RTPPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an incoming RTP packet is about to be processed.
Definition: rtpsession.h:458
This class is an abstract class which is used to specify destinations, multicast groups etc...
Definition: rtpaddress.h:50
virtual RTPTransmitter * NewUserDefinedTransmitter()
Allocate a user defined transmitter.
Definition: rtpsession.h:455
virtual void OnSSRCCollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, bool isrtp)
Is called when an SSRC collision was detected.
Definition: rtpsession.h:470
virtual void OnPollThreadError(int errcode)
Is called when error errcode was detected in the poll thread.
Definition: rtpsession.h:512
virtual void OnBYETimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed after having sent a BYE packet.
Definition: rtpsession.h:486
Describes an entry in the RTPSources source table.
Definition: rtpsourcedata.h:166
virtual void OnUnknownPacketFormat(RTCPPacket *rtcppack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an unknown packet format for a known packet type was detected.
Definition: rtpsession.h:499
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:61
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:78
virtual void OnRemoveSource(RTPSourceData *srcdat)
Is called when the entry srcdat is about to be deleted from the source table.
Definition: rtpsession.h:480
virtual void OnCNAMECollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, const uint8_t *cname, size_t cnamelength)
Is called when another CNAME was received than the one already present for source srcdat...
Definition: rtpsession.h:473
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:80
virtual void OnSendRTCPCompoundPacket(RTCPCompoundPacket *pack)
Is called when an RTCP compound packet has just been sent (useful to inspect outgoing RTCP data)...
Definition: rtpsession.h:509
A memory manager.
Definition: rtpmemorymanager.h:147
virtual void OnPollThreadStep()
Is called each time the poll thread loops.
Definition: rtpsession.h:518
This class can be used to build RTCP compound packets, on a higher level than the RTCPCompoundPacketB...
Definition: rtcppacketbuilder.h:63
virtual void OnUnknownPacketType(RTCPPacket *rtcppack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an unknown RTCP packet type was detected.
Definition: rtpsession.h:495
Represents an RTP Packet.
Definition: rtppacket.h:56
virtual void OnNewSource(RTPSourceData *srcdat)
Is called when a new entry srcdat is added to the source table.
Definition: rtpsession.h:477
Base class for specific types of RTCP packets.
Definition: rtcppacket.h:50
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:246
virtual void OnPollThreadStart(bool &stop)
Is called when the poll thread is started.
Definition: rtpsession.h:525
This class represents a list of addresses from which SSRC collisions were detected.
Definition: rtpcollisionlist.h:53
Describes the parameters for to be used by an RTPSession instance.
Definition: rtpsessionparams.h:54
TransmissionProtocol
Used to identify a specific transmitter.
Definition: rtptransmitter.h:69
virtual void OnTimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed out.
Definition: rtpsession.h:483
virtual void OnPollThreadStop()
Is called when the poll thread is going to stop.
Definition: rtpsession.h:530