jrtplib  3.8.0
rtpsession.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2010 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 <jmutex.h>
56 #endif // RTP_SUPPORT_THREAD
57 
58 class RTPTransmitter;
59 class RTPSessionParams;
61 class RTPAddress;
62 class RTPSourceData;
63 class RTPPacket;
64 class RTPPollThread;
66 class RTCPCompoundPacket;
67 class RTCPPacket;
68 class RTCPAPPPacket;
69 
76 class RTPSession : public RTPMemoryObject
77 {
78 public:
88  RTPSession(RTPRandom *rnd = 0, RTPMemoryManager *mgr = 0);
89  virtual ~RTPSession();
90 
98 
106  int Create(const RTPSessionParams &sessparams,RTPTransmitter *transmitter);
107 
109  void Destroy();
110 
116  void BYEDestroy(const RTPTime &maxwaittime,const void *reason,size_t reasonlength);
117 
119  bool IsActive();
120 
122  uint32_t GetLocalSSRC();
123 
125  int AddDestination(const RTPAddress &addr);
126 
128  int DeleteDestination(const RTPAddress &addr);
129 
131  void ClearDestinations();
132 
134  bool SupportsMulticasting();
135 
137  int JoinMulticastGroup(const RTPAddress &addr);
138 
140  int LeaveMulticastGroup(const RTPAddress &addr);
141 
144 
150  int SendPacket(const void *data,size_t len);
151 
156  int SendPacket(const void *data,size_t len,
157  uint8_t pt,bool mark,uint32_t timestampinc);
158 
165  int SendPacketEx(const void *data,size_t len,
166  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
167 
174  int SendPacketEx(const void *data,size_t len,
175  uint8_t pt,bool mark,uint32_t timestampinc,
176  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
177 #ifdef RTP_SUPPORT_SENDAPP
178 
185  int SendRTCPAPPPacket(uint8_t subtype, const uint8_t name[4], const void *appdata, size_t appdatalen);
186 #endif // RTP_SUPPORT_SENDAPP
187 
188 #ifdef RTP_SUPPORT_RTCPUNKNOWN
189 
194  int SendUnknownPacket(bool sr, uint8_t payload_type, uint8_t subtype, const void *data, size_t len);
195 #endif // RTP_SUPPORT_RTCPUNKNOWN
196 
197  int SetDefaultPayloadType(uint8_t pt);
198 
200  int SetDefaultMark(bool m);
201 
203  int SetDefaultTimestampIncrement(uint32_t timestampinc);
204 
211  int IncrementTimestamp(uint32_t inc);
212 
221 
228  int SetPreTransmissionDelay(const RTPTime &delay);
229 
238 
241 
245  int Poll();
246 
252  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
253 
257  int AbortWait();
258 
263 
270  int BeginDataAccess();
271 
276  bool GotoFirstSource();
277 
282  bool GotoNextSource();
283 
288  bool GotoPreviousSource();
289 
297 
304  bool GotoNextSourceWithData();
305 
313 
316 
320  RTPSourceData *GetSourceInfo(uint32_t ssrc);
321 
329 
331  void DeletePacket(RTPPacket *p);
332 
334  int EndDataAccess();
335 
341 
343  int AddToIgnoreList(const RTPAddress &addr);
344 
346  int DeleteFromIgnoreList(const RTPAddress &addr);
347 
349  void ClearIgnoreList();
350 
352  int AddToAcceptList(const RTPAddress &addr);
353 
355  int DeleteFromAcceptList(const RTPAddress &addr);
356 
358  void ClearAcceptList();
359 
361  int SetMaximumPacketSize(size_t s);
362 
364  int SetSessionBandwidth(double bw);
365 
372  int SetTimestampUnit(double u);
373 
380  void SetNameInterval(int count);
381 
388  void SetEMailInterval(int count);
389 
396  void SetLocationInterval(int count);
397 
404  void SetPhoneInterval(int count);
405 
412  void SetToolInterval(int count);
413 
420  void SetNoteInterval(int count);
421 
423  int SetLocalName(const void *s,size_t len);
424 
426  int SetLocalEMail(const void *s,size_t len);
427 
429  int SetLocalLocation(const void *s,size_t len);
430 
432  int SetLocalPhone(const void *s,size_t len);
433 
435  int SetLocalTool(const void *s,size_t len);
436 
438  int SetLocalNote(const void *s,size_t len);
439 
440 #ifdef RTPDEBUG
441  void DumpSources();
442  void DumpTransmitter();
443 #endif // RTPDEBUG
444 protected:
452  virtual RTPTransmitter *NewUserDefinedTransmitter() { return 0; }
453 
455  virtual void OnRTPPacket(RTPPacket *pack,const RTPTime &receivetime,
456  const RTPAddress *senderaddress) { }
457 
459  virtual void OnRTCPCompoundPacket(RTCPCompoundPacket *pack,const RTPTime &receivetime,
460  const RTPAddress *senderaddress) { }
461 
467  virtual void OnSSRCCollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,bool isrtp) { }
468 
470  virtual void OnCNAMECollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,
471  const uint8_t *cname,size_t cnamelength) { }
472 
474  virtual void OnNewSource(RTPSourceData *srcdat) { }
475 
477  virtual void OnRemoveSource(RTPSourceData *srcdat) { }
478 
480  virtual void OnTimeout(RTPSourceData *srcdat) { }
481 
483  virtual void OnBYETimeout(RTPSourceData *srcdat) { }
484 
488  virtual void OnAPPPacket(RTCPAPPPacket *apppacket,const RTPTime &receivetime,
489  const RTPAddress *senderaddress) { }
490 
492  virtual void OnUnknownPacketType(RTCPPacket *rtcppack,const RTPTime &receivetime,
493  const RTPAddress *senderaddress) { }
494 
496  virtual void OnUnknownPacketFormat(RTCPPacket *rtcppack,const RTPTime &receivetime,
497  const RTPAddress *senderaddress) { }
498 
500  virtual void OnNoteTimeout(RTPSourceData *srcdat) { }
501 
503  virtual void OnBYEPacket(RTPSourceData *srcdat) { }
504 
507 #ifdef RTP_SUPPORT_THREAD
508 
509  virtual void OnPollThreadError(int errcode) { }
510 
515  virtual void OnPollThreadStep() { }
516 
522  virtual void OnPollThreadStart(bool &stop) { }
523 
527  virtual void OnPollThreadStop() { }
528 
529 #endif // RTP_SUPPORT_THREAD
530 private:
531  int InternalCreate(const RTPSessionParams &sessparams);
532  int CreateCNAME(uint8_t *buffer,size_t *bufferlength,bool resolve);
533  int ProcessPolledData();
534  int ProcessRTCPCompoundPacket(RTCPCompoundPacket &rtcpcomppack,RTPRawPacket *pack);
535  RTPRandom *GetRandomNumberGenerator(RTPRandom *r);
536 
537  RTPRandom *rtprnd;
538  bool deletertprnd;
539 
540  RTPTransmitter *rtptrans;
541  bool created;
542  bool deletetransmitter;
543  bool usingpollthread;
544  bool acceptownpackets;
545  bool useSR_BYEifpossible;
546  size_t maxpacksize;
547  double sessionbandwidth;
548  double controlfragment;
549  double sendermultiplier;
550  double byemultiplier;
551  double membermultiplier;
552  double collisionmultiplier;
553  double notemultiplier;
554  bool sentpackets;
555 
556  RTPSessionSources sources;
557  RTPPacketBuilder packetbuilder;
558  RTCPScheduler rtcpsched;
559  RTCPPacketBuilder rtcpbuilder;
560  RTPCollisionList collisionlist;
561 
562  std::list<RTCPCompoundPacket *> byepackets;
563 
564 #ifdef RTP_SUPPORT_THREAD
565  RTPPollThread *pollthread;
566  JMutex sourcesmutex,buildermutex,schedmutex,packsentmutex;
567 
568  friend class RTPPollThread;
569 #endif // RTP_SUPPORT_THREAD
570  friend class RTPSessionSources;
571  friend class RTCPSessionPacketBuilder;
572 };
573 
574 #endif // RTPSESSION_H
575 
virtual void OnSSRCCollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, bool isrtp)
Is called when an SSRC collision was detected.
Definition: rtpsession.h:467
int BeginDataAccess()
The following member functions (till EndDataAccess}) need to be accessed between a call to BeginDataA...
void SetLocationInterval(int count)
Sets the RTCP interval for the SDES location item.
RTPSession(RTPRandom *rnd=0, RTPMemoryManager *mgr=0)
Constructs an RTPSession instance, optionally using a specific instance of a random number generator...
bool GotoNextSourceWithData()
Sets the current source to be the next source in the table which has RTPPacket instances that we have...
int EndDataAccess()
See BeginDataAccess.
void SetPhoneInterval(int count)
Sets the RTCP interval for the SDES phone item.
High level class for using RTP.
Definition: rtpsession.h:76
int AddToIgnoreList(const RTPAddress &addr)
Adds addr to the list of addresses to ignore.
Represents an RTP Packet.
Definition: rtppacket.h:53
TransmissionProtocol
Used to identify a specific transmitter.
Definition: rtptransmitter.h:65
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:459
void ClearIgnoreList()
Clears the list of addresses to ignore.
uint32_t GetLocalSSRC()
Returns our own SSRC.
RTPSourceData * GetSourceInfo(uint32_t ssrc)
Returns the RTPSourceData instance for the participant identified by ssrc, or NULL if no such entry e...
bool GotoPreviousSource()
Sets the current source to be the previous source in the table.
A memory manager.
Definition: rtpmemorymanager.h:144
virtual void OnPollThreadStep()
Is called each time the poll thread loops.
Definition: rtpsession.h:515
void ClearAcceptList()
Clears the list of addresses to accept.
void Destroy()
Leaves the session without sending a BYE packet.
virtual void OnNewSource(RTPSourceData *srcdat)
Is called when a new entry srcdat is added to the source table.
Definition: rtpsession.h:474
int WaitForIncomingData(const RTPTime &delay, bool *dataavailable=0)
Waits at most a time delay until incoming data has been detected.
virtual RTPTransmitter * NewUserDefinedTransmitter()
Allocate a user defined transmitter.
Definition: rtpsession.h:452
virtual void OnTimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed out.
Definition: rtpsession.h:480
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:77
Describes an RTCP APP packet.
Definition: rtcpapppacket.h:51
Base class for specific types of RTCP packets.
Definition: rtcppacket.h:47
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:470
int SetLocalTool(const void *s, size_t len)
Sets the SDES tool item for the local participant to the value s with length len. ...
void SetNameInterval(int count)
Sets the RTCP interval for the SDES name item.
int SetLocalEMail(const void *s, size_t len)
Sets the SDES e-mail item for the local participant to the value s with length len.
Represents an RTCP compound packet.
Definition: rtcpcompoundpacket.h:50
int SendPacket(const void *data, size_t len)
Sends the RTP packet with payload data which has length len.
int SetLocalLocation(const void *s, size_t len)
Sets the SDES location item for the local participant to the value s with length len.
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:236
virtual void OnUnknownPacketType(RTCPPacket *rtcppack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an unknown RTCP packet type was detected.
Definition: rtpsession.h:492
bool GotoNextSource()
Sets the current source to be the next source in the table.
virtual void OnBYETimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed after having sent a BYE packet.
Definition: rtpsession.h:483
int AddToAcceptList(const RTPAddress &addr)
Adds addr to the list of addresses to accept.
int Poll()
If you're not using the poll thread, this function must be called regularly to process incoming data ...
int SendRTCPAPPPacket(uint8_t subtype, const uint8_t name[4], const void *appdata, size_t appdatalen)
If sending of RTCP APP packets was enabled at compile time, this function creates a compound packet c...
virtual void OnBYEPacket(RTPSourceData *srcdat)
Is called when a BYE packet has been processed for source srcdat.
Definition: rtpsession.h:503
void SetNoteInterval(int count)
Sets the RTCP interval for the SDES note item.
int JoinMulticastGroup(const RTPAddress &addr)
Joins the multicast group specified by addr.
bool GotoFirstSourceWithData()
Sets the current source to be the first source in the table which has RTPPacket instances that we hav...
int IncrementTimestamp(uint32_t inc)
This function increments the timestamp with the amount given by inc.
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:506
void LeaveAllMulticastGroups()
Leaves all multicast groups.
Specifies the internal UDP over IPv4 transmitter.
Definition: rtptransmitter.h:67
This class is used by the transmission component to store the incoming RTP and RTCP data in...
Definition: rtprawpacket.h:48
bool IsActive()
Returns whether the session has been created or not.
This class can be used to build RTCP compound packets, on a higher level than the RTCPCompoundPacketB...
Definition: rtcppacketbuilder.h:60
int AbortWait()
If the previous function has been called, this one aborts the waiting (only works when you're not usi...
int DeleteFromIgnoreList(const RTPAddress &addr)
Deletes addr from the list of addresses to ignore.
void DeletePacket(RTPPacket *p)
Frees the memory used by p.
int SetSessionBandwidth(double bw)
Sets the session bandwidth to bw, which is specified in bytes per second.
virtual void OnPollThreadError(int errcode)
Is called when error errcode was detected in the poll thread.
Definition: rtpsession.h:509
void BYEDestroy(const RTPTime &maxwaittime, const void *reason, size_t reasonlength)
Sends a BYE packet and leaves the session.
int Create(const RTPSessionParams &sessparams, const RTPTransmissionParams *transparams=0, RTPTransmitter::TransmissionProtocol proto=RTPTransmitter::IPv4UDPProto)
Creates an RTP session.
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:73
RTPTransmissionInfo * GetTransmissionInfo()
This function returns an instance of a subclass of RTPTransmissionInfo which will give some additiona...
Describes the parameters for to be used by an RTPSession instance.
Definition: rtpsessionparams.h:51
virtual void OnRemoveSource(RTPSourceData *srcdat)
Is called when the entry srcdat is about to be deleted from the source table.
Definition: rtpsession.h:477
int SendUnknownPacket(bool sr, uint8_t payload_type, uint8_t subtype, const void *data, size_t len)
Tries to send an Unknown packet immediately.
RTPTime GetRTCPDelay()
Returns the time interval after which an RTCP compound packet may have to be sent (only works when yo...
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:488
RTPSourceData * GetCurrentSourceInfo()
Returns the RTPSourceData instance for the currently selected participant.
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:57
int SetLocalPhone(const void *s, size_t len)
Sets the SDES phone item for the local participant to the value s with length len.
int AddDestination(const RTPAddress &addr)
Adds addr to the list of destinations.
int SetMaximumPacketSize(size_t s)
Sets the maximum allowed packet size to s.
void ClearDestinations()
Clears the list of destinations.
This class is an abstract class which is used to specify destinations, multicast groups etc...
Definition: rtpaddress.h:47
int IncrementTimestampDefault()
This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement ...
virtual void OnNoteTimeout(RTPSourceData *srcdat)
Is called when the SDES NOTE item for source srcdat has been timed out.
Definition: rtpsession.h:500
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:455
int SetPreTransmissionDelay(const RTPTime &delay)
This function allows you to inform the library about the delay between sampling the first sample of a...
virtual void OnPollThreadStart(bool &stop)
Is called when the poll thread is started.
Definition: rtpsession.h:522
virtual void OnPollThreadStop()
Is called when the poll thread is going to stop.
Definition: rtpsession.h:527
int DeleteDestination(const RTPAddress &addr)
Deletes addr from the list of destinations.
bool SupportsMulticasting()
Returns true if multicasting is supported.
int SetDefaultPayloadType(uint8_t pt)
Sets the default payload type for RTP packets to pt.
This class represents a list of addresses from which SSRC collisions were detected.
Definition: rtpcollisionlist.h:50
void DeleteTransmissionInfo(RTPTransmissionInfo *inf)
Frees the memory used by the transmission information inf.
This class can be used to build RTP packets and is a bit more high-level than the RTPPacket class: it...
Definition: rtppacketbuilder.h:54
int SetLocalName(const void *s, size_t len)
Sets the SDES name item for the local participant to the value s with length len. ...
int SetTimestampUnit(double u)
Sets the timestamp unit for our own data.
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:496
void SetToolInterval(int count)
Sets the RTCP interval for the SDES tool item.
int SetReceiveMode(RTPTransmitter::ReceiveMode m)
Sets the receive mode to m.
int LeaveMulticastGroup(const RTPAddress &addr)
Leaves the multicast group specified by addr.
int DeleteFromAcceptList(const RTPAddress &addr)
Deletes addr from the list of addresses to accept.
bool GotoPreviousSourceWithData()
Sets the current source to be the previous source in the table which has RTPPacket instances that we ...
int SetDefaultMark(bool m)
Sets the default marker for RTP packets to m.
Base class for transmission parameters.
Definition: rtptransmitter.h:217
This class determines when RTCP compound packets should be sent.
Definition: rtcpscheduler.h:98
int SetLocalNote(const void *s, size_t len)
Sets the SDES note item for the local participant to the value s with length len. ...
void SetEMailInterval(int count)
Sets the RTCP interval for the SDES e-mail item.
Interface for generating random numbers.
Definition: rtprandom.h:48
int SetDefaultTimestampIncrement(uint32_t timestampinc)
Sets the default value to increment the timestamp with to timestampinc.
Describes an entry in the RTPSources source table.
Definition: rtpsourcedata.h:164
int SendPacketEx(const void *data, size_t len, uint16_t hdrextID, const void *hdrextdata, size_t numhdrextwords)
Sends the RTP packet with payload data which has length len.
bool GotoFirstSource()
Starts the iteration over the participants by going to the first member in the table.
RTPPacket * GetNextPacket()
Extracts the next packet from the received packets queue of the current participant, or NULL if no more packets are available.