JRTPLIB  3.10.0
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 
80 class JRTPLIB_IMPORTEXPORT RTPSession : public RTPMemoryObject
81 {
82 public:
92  RTPSession(RTPRandom *rnd = 0, RTPMemoryManager *mgr = 0);
93  virtual ~RTPSession();
94 
101  int Create(const RTPSessionParams &sessparams,const RTPTransmissionParams *transparams = 0, RTPTransmitter::TransmissionProtocol proto = RTPTransmitter::IPv4UDPProto);
102 
110  int Create(const RTPSessionParams &sessparams,RTPTransmitter *transmitter);
111 
113  void Destroy();
114 
120  void BYEDestroy(const RTPTime &maxwaittime,const void *reason,size_t reasonlength);
121 
123  bool IsActive();
124 
126  uint32_t GetLocalSSRC();
127 
129  int AddDestination(const RTPAddress &addr);
130 
132  int DeleteDestination(const RTPAddress &addr);
133 
135  void ClearDestinations();
136 
138  bool SupportsMulticasting();
139 
141  int JoinMulticastGroup(const RTPAddress &addr);
142 
144  int LeaveMulticastGroup(const RTPAddress &addr);
145 
147  void LeaveAllMulticastGroups();
148 
154  int SendPacket(const void *data,size_t len);
155 
160  int SendPacket(const void *data,size_t len,
161  uint8_t pt,bool mark,uint32_t timestampinc);
162 
169  int SendPacketEx(const void *data,size_t len,
170  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
171 
178  int SendPacketEx(const void *data,size_t len,
179  uint8_t pt,bool mark,uint32_t timestampinc,
180  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
181 #ifdef RTP_SUPPORT_SENDAPP
182 
189  int SendRTCPAPPPacket(uint8_t subtype, const uint8_t name[4], const void *appdata, size_t appdatalen);
190 #endif // RTP_SUPPORT_SENDAPP
191 
192 #ifdef RTP_SUPPORT_RTCPUNKNOWN
193 
198  int SendUnknownPacket(bool sr, uint8_t payload_type, uint8_t subtype, const void *data, size_t len);
199 #endif // RTP_SUPPORT_RTCPUNKNOWN
200 
204  int SendRawData(const void *data, size_t len, bool usertpchannel);
205 
207  int SetDefaultPayloadType(uint8_t pt);
208 
210  int SetDefaultMark(bool m);
211 
213  int SetDefaultTimestampIncrement(uint32_t timestampinc);
214 
221  int IncrementTimestamp(uint32_t inc);
222 
230  int IncrementTimestampDefault();
231 
238  int SetPreTransmissionDelay(const RTPTime &delay);
239 
247  RTPTransmissionInfo *GetTransmissionInfo();
248 
250  void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
251 
255  int Poll();
256 
262  int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
263 
267  int AbortWait();
268 
272  RTPTime GetRTCPDelay();
273 
280  int BeginDataAccess();
281 
286  bool GotoFirstSource();
287 
292  bool GotoNextSource();
293 
298  bool GotoPreviousSource();
299 
306  bool GotoFirstSourceWithData();
307 
314  bool GotoNextSourceWithData();
315 
322  bool GotoPreviousSourceWithData();
323 
325  RTPSourceData *GetCurrentSourceInfo();
326 
330  RTPSourceData *GetSourceInfo(uint32_t ssrc);
331 
338  RTPPacket *GetNextPacket();
339 
341  uint16_t GetNextSequenceNumber() const;
342 
344  void DeletePacket(RTPPacket *p);
345 
347  int EndDataAccess();
348 
353  int SetReceiveMode(RTPTransmitter::ReceiveMode m);
354 
356  int AddToIgnoreList(const RTPAddress &addr);
357 
359  int DeleteFromIgnoreList(const RTPAddress &addr);
360 
362  void ClearIgnoreList();
363 
365  int AddToAcceptList(const RTPAddress &addr);
366 
368  int DeleteFromAcceptList(const RTPAddress &addr);
369 
371  void ClearAcceptList();
372 
374  int SetMaximumPacketSize(size_t s);
375 
377  int SetSessionBandwidth(double bw);
378 
385  int SetTimestampUnit(double u);
386 
393  void SetNameInterval(int count);
394 
401  void SetEMailInterval(int count);
402 
409  void SetLocationInterval(int count);
410 
417  void SetPhoneInterval(int count);
418 
425  void SetToolInterval(int count);
426 
433  void SetNoteInterval(int count);
434 
436  int SetLocalName(const void *s,size_t len);
437 
439  int SetLocalEMail(const void *s,size_t len);
440 
442  int SetLocalLocation(const void *s,size_t len);
443 
445  int SetLocalPhone(const void *s,size_t len);
446 
448  int SetLocalTool(const void *s,size_t len);
449 
451  int SetLocalNote(const void *s,size_t len);
452 
453 #ifdef RTPDEBUG
454  void DumpSources();
455  void DumpTransmitter();
456 #endif // RTPDEBUG
457 protected:
465  virtual RTPTransmitter *NewUserDefinedTransmitter() { return 0; }
466 
473  virtual void OnRTPPacket(RTPPacket *pack,const RTPTime &receivetime,
474  const RTPAddress *senderaddress) { }
475 
477  virtual void OnRTCPCompoundPacket(RTCPCompoundPacket *pack,const RTPTime &receivetime,
478  const RTPAddress *senderaddress) { }
479 
485  virtual void OnSSRCCollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,bool isrtp) { }
486 
488  virtual void OnCNAMECollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,
489  const uint8_t *cname,size_t cnamelength) { }
490 
492  virtual void OnNewSource(RTPSourceData *srcdat) { }
493 
495  virtual void OnRemoveSource(RTPSourceData *srcdat) { }
496 
498  virtual void OnTimeout(RTPSourceData *srcdat) { }
499 
501  virtual void OnBYETimeout(RTPSourceData *srcdat) { }
502 
506  virtual void OnAPPPacket(RTCPAPPPacket *apppacket,const RTPTime &receivetime,
507  const RTPAddress *senderaddress) { }
508 
510  virtual void OnUnknownPacketType(RTCPPacket *rtcppack,const RTPTime &receivetime,
511  const RTPAddress *senderaddress) { }
512 
514  virtual void OnUnknownPacketFormat(RTCPPacket *rtcppack,const RTPTime &receivetime,
515  const RTPAddress *senderaddress) { }
516 
518  virtual void OnNoteTimeout(RTPSourceData *srcdat) { }
519 
521  virtual void OnRTCPSenderReport(RTPSourceData *srcdat) { }
522 
524  virtual void OnRTCPReceiverReport(RTPSourceData *srcdat) { }
525 
528  const void *itemdata, size_t itemlength) { }
529 #ifdef RTP_SUPPORT_SDESPRIV
530 
531  virtual void OnRTCPSDESPrivateItem(RTPSourceData *srcdat, const void *prefixdata, size_t prefixlen,
532  const void *valuedata, size_t valuelen) { }
533 #endif // RTP_SUPPORT_SDESPRIV
534 
536  virtual void OnBYEPacket(RTPSourceData *srcdat) { }
537 
540 #ifdef RTP_SUPPORT_THREAD
541 
542  virtual void OnPollThreadError(int errcode) { }
543 
548  virtual void OnPollThreadStep() { }
549 
555  virtual void OnPollThreadStart(bool &stop) { }
556 
560  virtual void OnPollThreadStop() { }
561 #endif // RTP_SUPPORT_THREAD
562 
565  void SetChangeOutgoingData(bool change) { m_changeOutgoingData = change; }
566 
569  void SetChangeIncomingData(bool change) { m_changeIncomingData = change; }
570 
580  virtual int OnChangeRTPOrRTCPData(const void *origdata, size_t origlen, bool isrtp, void **senddata, size_t *sendlen)
581  { return ERR_RTP_RTPSESSION_CHANGEREQUESTEDBUTNOTIMPLEMENTED; }
582 
586  virtual void OnSentRTPOrRTCPData(void *senddata, size_t sendlen, bool isrtp) { }
587 
594  virtual bool OnChangeIncomingData(RTPRawPacket *rawpack) { return true; }
595 
606  virtual void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled) { }
607 private:
608  int InternalCreate(const RTPSessionParams &sessparams);
609  int CreateCNAME(uint8_t *buffer,size_t *bufferlength,bool resolve);
610  int ProcessPolledData();
611  int ProcessRTCPCompoundPacket(RTCPCompoundPacket &rtcpcomppack,RTPRawPacket *pack);
612  RTPRandom *GetRandomNumberGenerator(RTPRandom *r);
613  int SendRTPData(const void *data, size_t len);
614  int SendRTCPData(const void *data, size_t len);
615 
616  RTPRandom *rtprnd;
617  bool deletertprnd;
618 
619  RTPTransmitter *rtptrans;
620  bool created;
621  bool deletetransmitter;
622  bool usingpollthread;
623  bool acceptownpackets;
624  bool useSR_BYEifpossible;
625  size_t maxpacksize;
626  double sessionbandwidth;
627  double controlfragment;
628  double sendermultiplier;
629  double byemultiplier;
630  double membermultiplier;
631  double collisionmultiplier;
632  double notemultiplier;
633  bool sentpackets;
634 
635  bool m_changeIncomingData, m_changeOutgoingData;
636 
637  RTPSessionSources sources;
638  RTPPacketBuilder packetbuilder;
639  RTCPScheduler rtcpsched;
640  RTCPPacketBuilder rtcpbuilder;
641  RTPCollisionList collisionlist;
642 
643  std::list<RTCPCompoundPacket *> byepackets;
644 
645 #ifdef RTP_SUPPORT_THREAD
646  RTPPollThread *pollthread;
647  jthread::JMutex sourcesmutex,buildermutex,schedmutex,packsentmutex;
648 
649  friend class RTPPollThread;
650 #endif // RTP_SUPPORT_THREAD
651  friend class RTPSessionSources;
652  friend class RTCPSessionPacketBuilder;
653 };
654 
655 } // end namespace
656 
657 #endif // RTPSESSION_H
658 
High level class for using RTP.
Definition: rtpsession.h:80
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:536
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:72
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:506
Base class for transmission parameters.
Definition: rtptransmitter.h:228
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
void SetChangeIncomingData(bool change)
If this is set to true, incoming data will be passed through RTPSession::OnChangeIncomingData, allowing you to modify the data (e.g.
Definition: rtpsession.h:569
virtual void OnNoteTimeout(RTPSourceData *srcdat)
Is called when the SDES NOTE item for source srcdat has been timed out.
Definition: rtpsession.h:518
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:477
virtual bool OnChangeIncomingData(RTPRawPacket *rawpack)
By overriding this function, the raw incoming data can be inspected and modified (e.g.
Definition: rtpsession.h:594
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:473
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:465
virtual void OnSSRCCollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, bool isrtp)
Is called when an SSRC collision was detected.
Definition: rtpsession.h:485
ItemType
Identifies the type of an SDES item.
Definition: rtcpsdespacket.h:59
virtual void OnPollThreadError(int errcode)
Is called when error errcode was detected in the poll thread.
Definition: rtpsession.h:542
virtual void OnBYETimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed after having sent a BYE packet.
Definition: rtpsession.h:501
Describes an entry in the RTPSources source table.
Definition: rtpsourcedata.h:166
virtual int OnChangeRTPOrRTCPData(const void *origdata, size_t origlen, bool isrtp, void **senddata, size_t *sendlen)
If RTPSession::SetChangeOutgoingData was sent to true, overriding this you can change the data packet...
Definition: rtpsession.h:580
virtual void OnRTCPSDESPrivateItem(RTPSourceData *srcdat, const void *prefixdata, size_t prefixlen, const void *valuedata, size_t valuelen)
Is called when a specific SDES item of 'private' type was received for this source.
Definition: rtpsession.h:531
virtual void OnRTCPReceiverReport(RTPSourceData *srcdat)
Is called when an RTCP receiver report has been processed for this source.
Definition: rtpsession.h:524
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:514
Abstract class from which actual transmission components should be derived.
Definition: rtptransmitter.h:62
ReceiveMode
Three kind of receive modes can be specified.
Definition: rtptransmitter.h:79
virtual void OnSentRTPOrRTCPData(void *senddata, size_t sendlen, bool isrtp)
This function is called when an RTP or RTCP packet was sent, it can be helpful when data was allocate...
Definition: rtpsession.h:586
virtual void OnRemoveSource(RTPSourceData *srcdat)
Is called when the entry srcdat is about to be deleted from the source table.
Definition: rtpsession.h:495
virtual void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled)
Allows you to use an RTP packet from the specified source directly.
Definition: rtpsession.h:606
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:488
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:84
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:539
A memory manager.
Definition: rtpmemorymanager.h:150
virtual void OnPollThreadStep()
Is called each time the poll thread loops.
Definition: rtpsession.h:548
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:510
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:492
Base class for specific types of RTCP packets.
Definition: rtcppacket.h:50
Base class for additional information about the transmitter.
Definition: rtptransmitter.h:247
virtual void OnPollThreadStart(bool &stop)
Is called when the poll thread is started.
Definition: rtpsession.h:555
void SetChangeOutgoingData(bool change)
If this is set to true, outgoing data will be passed through RTPSession::OnChangeRTPOrRTCPData and RT...
Definition: rtpsession.h:565
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
virtual void OnRTCPSenderReport(RTPSourceData *srcdat)
Is called when an RTCP sender report has been processed for this source.
Definition: rtpsession.h:521
virtual void OnRTCPSDESItem(RTPSourceData *srcdat, RTCPSDESPacket::ItemType t, const void *itemdata, size_t itemlength)
Is called when a specific SDES item was received for this source.
Definition: rtpsession.h:527
TransmissionProtocol
Used to identify a specific transmitter.
Definition: rtptransmitter.h:70
virtual void OnTimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed out.
Definition: rtpsession.h:498
virtual void OnPollThreadStop()
Is called when the poll thread is going to stop.
Definition: rtpsession.h:560