Blame view

3rdparty/jrtp_export/jrtplib/include/jrtplib3/rtpexternaltransmitter.h 8.79 KB
3d2ab595   Hu Chunming   支持gb28181
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  /*
  
    This file is a part of JRTPLIB
    Copyright (c) 1999-2017 Jori Liesenborgs
  
    Contact: jori.liesenborgs@gmail.com
  
    This library was developed at the Expertise Centre for Digital Media
    (http://www.edm.uhasselt.be), a research center of the Hasselt University
    (http://www.uhasselt.be). The library is based upon work done for 
    my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
  
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:
  
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
  
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
  
  */
  
  /**
   * \file rtpexternaltransmitter.h
   */
  
  #ifndef RTPEXTERNALTRANSMITTER_H
  
  #define RTPEXTERNALTRANSMITTER_H
  
  #include "rtpconfig.h"
  #include "rtptransmitter.h"
  #include "rtpabortdescriptors.h"
  #include <list>
  
  #ifdef RTP_SUPPORT_THREAD
  	#include <jthread/jmutex.h>
  #endif // RTP_SUPPORT_THREAD
  
  namespace jrtplib
  {
  
  class RTPExternalTransmitter;
  
  /** Base class to specify a mechanism to transmit RTP packets outside of this library.
   *  Base class to specify a mechanism to transmit RTP packets outside of this library. When
   *  you want to use your own mechanism to transmit RTP packets, you need to specify that
   *  you'll be using the external transmission component, and derive a class from this base
   *  class. An instance should then be specified in the RTPExternalTransmissionParams object,
   *  so that the transmitter will call the \c SendRTP, \c SendRTCP and \c ComesFromThisSender
   *  methods of this instance when needed.
   */
  class JRTPLIB_IMPORTEXPORT RTPExternalSender
  {
  public:
  	RTPExternalSender()										{ }
  	virtual ~RTPExternalSender()									{ }
  
  	/** This member function will be called when RTP data needs to be transmitted. */
  	virtual bool SendRTP(const void *data, size_t len) = 0;
  
  	/** This member function will be called when an RTCP packet needs to be transmitted. */
  	virtual bool SendRTCP(const void *data, size_t len) = 0;
  
  	/** Used to identify if an RTPAddress instance originated from this sender (to be able to detect own packets). */
  	virtual bool ComesFromThisSender(const RTPAddress *a) = 0;
  };
  
  /** Interface to inject incoming RTP and RTCP packets into the library.
   *  Interface to inject incoming RTP and RTCP packets into the library. When you have your own
   *  mechanism to receive incoming RTP/RTCP data, you'll need to pass these packets to the library.
   *  By first retrieving the RTPExternalTransmissionInfo instance for the external transmitter you'll
   *  be using, you can obtain the associated RTPExternalPacketInjecter instance. By calling it's
   *  member functions, you can then inject RTP or RTCP data into the library for further processing.
   */
  class JRTPLIB_IMPORTEXPORT RTPExternalPacketInjecter
  {
  public:
  	RTPExternalPacketInjecter(RTPExternalTransmitter *trans)					{ transmitter = trans; }
  	~RTPExternalPacketInjecter()									{ }
  
  	/** This function can be called to insert an RTP packet into the transmission component. */
  	void InjectRTP(const void *data, size_t len, const RTPAddress &a);
  
  	/** This function can be called to insert an RTCP packet into the transmission component. */
  	void InjectRTCP(const void *data, size_t len, const RTPAddress &a);
  
  	/** Use this function to inject an RTP or RTCP packet and the transmitter will try to figure out which type of packet it is. */
  	void InjectRTPorRTCP(const void *data, size_t len, const RTPAddress &a);
  private:
  	RTPExternalTransmitter *transmitter;
  };
  
  /** Parameters to initialize a transmitter of type RTPExternalTransmitter. */
  class JRTPLIB_IMPORTEXPORT RTPExternalTransmissionParams : public RTPTransmissionParams
  {
  public:
  	/** Using this constructor you can specify which RTPExternalSender object you'll be using
  	 *  and how much the additional header overhead for each packet will be. */
  	RTPExternalTransmissionParams(RTPExternalSender *s, int headeroverhead):RTPTransmissionParams(RTPTransmitter::ExternalProto)	{ sender = s; headersize = headeroverhead; }
  
  	RTPExternalSender *GetSender() const								{ return sender; }
  	int GetAdditionalHeaderSize() const								{ return headersize; }
  private:
  	RTPExternalSender *sender;
  	int headersize;
  };
  
  /** Additional information about the external transmission component. */
  class JRTPLIB_IMPORTEXPORT RTPExternalTransmissionInfo : public RTPTransmissionInfo
  {
  public:
  	RTPExternalTransmissionInfo(RTPExternalPacketInjecter *p) : RTPTransmissionInfo(RTPTransmitter::ExternalProto) { packetinjector = p; }
  
  	/** Tells you which RTPExternalPacketInjecter you need to use to pass RTP or RTCP
  	 *  data on to the transmission component. */
  	RTPExternalPacketInjecter *GetPacketInjector() const						{ return packetinjector; }
  private:
  	RTPExternalPacketInjecter *packetinjector;
  };
  	
  /** A transmission component which will use user specified functions to transmit the data and
   *  which will expose functions to inject received RTP or RTCP data into this component.
   *  A transmission component which will use user specified functions to transmit the data and
   *  which will expose functions to inject received RTP or RTCP data into this component. Use
   *  a class derived from RTPExternalSender to specify the functions which need to be used for
   *  sending the data. Obtain the RTPExternalTransmissionInfo object associated with this
   *  transmitter to obtain the functions needed to pass RTP/RTCP packets on to the transmitter.
   */
  class JRTPLIB_IMPORTEXPORT RTPExternalTransmitter : public RTPTransmitter
  {
  	JRTPLIB_NO_COPY(RTPExternalTransmitter)
  public:
  	RTPExternalTransmitter(RTPMemoryManager *mgr);
  	~RTPExternalTransmitter();
  
  	int Init(bool treadsafe);
  	int Create(size_t maxpacksize, const RTPTransmissionParams *transparams);
  	void Destroy();
  	RTPTransmissionInfo *GetTransmissionInfo();
  	void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
  
  	int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
  	bool ComesFromThisTransmitter(const RTPAddress *addr);
  	size_t GetHeaderOverhead()									{ return headersize; }
  	
  	int Poll();
  	int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
  	int AbortWait();
  	
  	int SendRTPData(const void *data,size_t len);	
  	int SendRTCPData(const void *data,size_t len);
  
  	int AddDestination(const RTPAddress &addr);
  	int DeleteDestination(const RTPAddress &addr);
  	void ClearDestinations();
  
  	bool SupportsMulticasting();
  	int JoinMulticastGroup(const RTPAddress &addr);
  	int LeaveMulticastGroup(const RTPAddress &addr);
  	void LeaveAllMulticastGroups();
  
  	int SetReceiveMode(RTPTransmitter::ReceiveMode m);
  	int AddToIgnoreList(const RTPAddress &addr);
  	int DeleteFromIgnoreList(const RTPAddress &addr);
  	void ClearIgnoreList();
  	int AddToAcceptList(const RTPAddress &addr);
  	int DeleteFromAcceptList(const RTPAddress &addr);
  	void ClearAcceptList();
  	int SetMaximumPacketSize(size_t s);	
  	
  	bool NewDataAvailable();
  	RTPRawPacket *GetNextPacket();
  #ifdef RTPDEBUG
  	void Dump();
  #endif // RTPDEBUG
  
  	void InjectRTP(const void *data, size_t len, const RTPAddress &a);
  	void InjectRTCP(const void *data, size_t len, const RTPAddress &a);
  	void InjectRTPorRTCP(const void *data, size_t len, const RTPAddress &a);
  private:
  	void FlushPackets();
  	
  	bool init;
  	bool created;
  	bool waitingfordata;
  	RTPExternalSender *sender;
  	RTPExternalPacketInjecter packetinjector;
  
  	std::list<RTPRawPacket*> rawpacketlist;
  
  	uint8_t *localhostname;
  	size_t localhostnamelength;
  
  	size_t maxpacksize;
  	int headersize;
  
  	RTPAbortDescriptors m_abortDesc;
  	int m_abortCount;
  #ifdef RTP_SUPPORT_THREAD
  	jthread::JMutex mainmutex,waitmutex;
  	int threadsafe;
  #endif // RTP_SUPPORT_THREAD
  };
  
  inline void RTPExternalPacketInjecter::InjectRTP(const void *data, size_t len, const RTPAddress &a)
  {
  	transmitter->InjectRTP(data, len, a); 
  }
  
  inline void RTPExternalPacketInjecter::InjectRTCP(const void *data, size_t len, const RTPAddress &a)
  { 
  	transmitter->InjectRTCP(data, len, a); 
  }
  
  inline void RTPExternalPacketInjecter::InjectRTPorRTCP(const void *data, size_t len, const RTPAddress &a)
  { 
  	transmitter->InjectRTPorRTCP(data, len, a); 
  }
  
  } // end namespace
  
  #endif // RTPTCPSOCKETTRANSMITTER_H