Blame view

3rdparty/jrtp_export/jrtplib/include/jrtplib3/rtptcptransmitter.h 7.48 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
  /*
  
    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 rtptcptransmitter.h
   */
  
  #ifndef RTPTCPTRANSMITTER_H
  
  #define RTPTCPTRANSMITTER_H
  
  #include "rtpconfig.h"
  #include "rtptransmitter.h"
  #include "rtpsocketutil.h"
  #include "rtpabortdescriptors.h"
  #include <map>
  #include <list>
  #include <vector>
  
  #ifdef RTP_SUPPORT_THREAD
  	#include <jthread/jmutex.h>
  #endif // RTP_SUPPORT_THREAD
  
  namespace jrtplib
  {
  
  /** Parameters for the TCP transmitter. */
  class JRTPLIB_IMPORTEXPORT RTPTCPTransmissionParams : public RTPTransmissionParams
  {
  public:
  	RTPTCPTransmissionParams();
  
  	/** If non null, the specified abort descriptors will be used to cancel
  	 *  the function that's waiting for packets to arrive; set to null (the default)
  	 *  to let the transmitter create its own instance. */
  	void SetCreatedAbortDescriptors(RTPAbortDescriptors *desc) { m_pAbortDesc = desc; }
  
  	/** If non-null, this RTPAbortDescriptors instance will be used internally,
  	 *  which can be useful when creating your own poll thread for multiple
  	 *  sessions. */
  	RTPAbortDescriptors *GetCreatedAbortDescriptors() const		{ return m_pAbortDesc; }
  private:
  	RTPAbortDescriptors *m_pAbortDesc;
  };
  
  inline RTPTCPTransmissionParams::RTPTCPTransmissionParams() : RTPTransmissionParams(RTPTransmitter::TCPProto)	
  { 
  	m_pAbortDesc = 0;
  }
  
  /** Additional information about the TCP transmitter. */
  class JRTPLIB_IMPORTEXPORT RTPTCPTransmissionInfo : public RTPTransmissionInfo
  {
  public:
  	RTPTCPTransmissionInfo() : RTPTransmissionInfo(RTPTransmitter::TCPProto)	{ }
  	~RTPTCPTransmissionInfo()													{ }
  };
  	
  // TODO: this is for IPv4, and will only be valid if one  rtp packet is in one tcp frame
  #define RTPTCPTRANS_HEADERSIZE						(20+20+2) // 20 IP, 20 TCP, 2 for framing (RFC 4571)
  	
  /** A TCP transmission component.
   *
   *  This class inherits the RTPTransmitter interface and implements a transmission component 
   *  which uses TCP to send and receive RTP and RTCP data. The component's parameters 
   *  are described by the class RTPTCPTransmissionParams. The functions which have an RTPAddress 
   *  argument require an argument of RTPTCPAddress. The RTPTransmitter::GetTransmissionInfo member function
   *  returns an instance of type RTPTCPTransmissionInfo.
   *
   *  After this transmission component was created, no data will actually be sent or received
   *  yet. You can specify over which TCP connections (which must be established first) data
   *  should be transmitted by using the RTPTransmitter::AddDestination member function. This
   *  takes an argument of type RTPTCPAddress, with which relevant the socket descriptor can
   *  be passed to the transmitter. 
   *
   *  These sockets will also be used to check for incoming RTP or RTCP data. The RTPTCPAddress
   *  instance that's associated with a received packet, will contain the socket descriptor
   *  on which the data was received. This descriptor can be obtained using RTPTCPAddress::GetSocket.
   *
   *  To get notified of an error when sending over or receiving from a socket, override the
   *  RTPTCPTransmitter::OnSendError and RTPTCPTransmitter::OnReceiveError member functions.
   */
  class JRTPLIB_IMPORTEXPORT RTPTCPTransmitter : public RTPTransmitter
  {
  	JRTPLIB_NO_COPY(RTPTCPTransmitter)
  public:
  	RTPTCPTransmitter(RTPMemoryManager *mgr);
  	~RTPTCPTransmitter();
  
  	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 RTPTCPTRANS_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
  protected:
  	/** By overriding this function you can be notified of an error when sending over a socket. */
  	virtual void OnSendError(SocketType sock);
  	/** By overriding this function you can be notified of an error when receiving from a socket. */
  	virtual void OnReceiveError(SocketType sock);
  private:
  	class SocketData
  	{
  	public:
  		SocketData();
  		~SocketData();
  		void Reset();
  
  		uint8_t m_lengthBuffer[2];
  		int m_lengthBufferOffset;
  		int m_dataLength;
  		int m_dataBufferOffset;
  		uint8_t *m_pDataBuffer;
  
  		uint8_t *ExtractDataBuffer() { uint8_t *pTmp = m_pDataBuffer; m_pDataBuffer = 0; return pTmp; }
  		int ProcessAvailableBytes(SocketType sock, int availLen, bool &complete, RTPMemoryManager *pMgr);
  	};
  
  	int SendRTPRTCPData(const void *data,size_t len);	
  	void FlushPackets();
  	int PollSocket(SocketType sock, SocketData &sdata);
  	void ClearDestSockets();
  	int ValidateSocket(SocketType s);
  
  	bool m_init;
  	bool m_created;
  	bool m_waitingForData;
  
  	std::map<SocketType, SocketData> m_destSockets;
  	std::vector<SocketType> m_tmpSocks;
  	std::vector<int8_t> m_tmpFlags;
  	std::vector<uint8_t> m_localHostname;
  	size_t m_maxPackSize;
  	
  	std::list<RTPRawPacket*> m_rawpacketlist;
  
  	RTPAbortDescriptors m_abortDesc;
  	RTPAbortDescriptors *m_pAbortDesc; // in case an external one was specified
  
  #ifdef RTP_SUPPORT_THREAD
  	jthread::JMutex m_mainMutex, m_waitMutex;
  	bool m_threadsafe;
  #endif // RTP_SUPPORT_THREAD
  };
  
  inline void RTPTCPTransmitter::OnSendError(SocketType) { }
  inline void RTPTCPTransmitter::OnReceiveError(SocketType) { }
  
  } // end namespace
  
  #endif // RTPTCPTRANSMITTER_H