Blame view

3rdparty/jrtplib-3.11.2/tests/testautoportbase.cpp 2.61 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
  #include "rtpsession.h"
  #include "rtpsessionparams.h"
  #include "rtpudpv4transmitter.h"
  #include "rtperrors.h"
  #include "rtpsourcedata.h"
  #include <stdlib.h>
  #include <stdio.h>
  #include <iostream>
  #include <string>
  
  using namespace jrtplib;
  
  void checkerror(int rtperr)
  {
  	if (rtperr < 0)
  	{
  		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
  		exit(-1);
  	}
  }
  
  class MyRTPSession : public RTPSession
  {
  protected:
  	void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled)
  	{
  		printf("Got packet in OnValidatedRTPPacket from source 0x%04x!\n", srcdat->GetSSRC());
  		DeletePacket(rtppack);
  		*ispackethandled = true;
  	}
  
  	void OnRTCPSDESItem(RTPSourceData *srcdat, RTCPSDESPacket::ItemType t, const void *itemdata, size_t itemlength)
  	{
  		char msg[1024];
  
  		memset(msg, 0, sizeof(msg));
  		if (itemlength >= sizeof(msg))
  			itemlength = sizeof(msg)-1;
  
  		memcpy(msg, itemdata, itemlength);
  		printf("Received SDES item (%d): %s", (int)t, msg);
  	}
  };
  
  int main(void)
  {
  #ifdef RTP_SOCKETTYPE_WINSOCK
  	WSADATA dat;
  	WSAStartup(MAKEWORD(2,2),&dat);
  #endif // RTP_SOCKETTYPE_WINSOCK
  	
  	MyRTPSession sess;
  
  	// Now, we'll create a RTP session, set the destination, send some
  	// packets and poll for incoming data.
  	
  	RTPUDPv4TransmissionParams transparams;
  	RTPSessionParams sessparams;
  	
  	sessparams.SetOwnTimestampUnit(1.0/10.0);
  	sessparams.SetAcceptOwnPackets(true);
  	transparams.SetPortbase(0);
  
  	//transparams.SetRTCPMultiplexing(true);
  	//transparams.SetAllowOddPortbase(true);
  
  	int status = sess.Create(sessparams,&transparams);	
  	checkerror(status);
  
  	RTPUDPv4TransmissionInfo *pInf = (RTPUDPv4TransmissionInfo *)sess.GetTransmissionInfo();
  	uint16_t rtpPort = pInf->GetRTPPort();
  	uint16_t rtcpPort = pInf->GetRTCPPort();	
  
  	printf("Using RTP port %d and RTCP port %d\n", (int)rtpPort, (int)rtcpPort);
  	
  	uint32_t destip = ntohl(inet_addr("127.0.0.1"));
  
  	// We're assuming that the destination is also using RTCP multiplexing 
  	// ('true' means that the same port will be used for RTCP)
  	RTPIPv4Address addr(destip,rtpPort,rtcpPort); 
  	
  	status = sess.AddDestination(addr);
  	checkerror(status);
  	
  	const int num = 10;
  	for (int i = 1 ; i <= num ; i++)
  	{
  		printf("\nSending packet %d/%d\n",i,num);
  		
  		// send the packet
  		status = sess.SendPacket((void *)"1234567890",10,0,false,10);
  		checkerror(status);
  		
  #ifndef RTP_SUPPORT_THREAD
  		status = sess.Poll();
  		checkerror(status);
  #endif // RTP_SUPPORT_THREAD
  		
  		RTPTime::Wait(RTPTime(1,0));
  	}
  	
  	sess.BYEDestroy(RTPTime(10,0),0,0);
  
  	sess.Destroy();
  
  #ifdef RTP_SOCKETTYPE_WINSOCK
  	WSACleanup();
  #endif // RTP_SOCKETTYPE_WINSOCK
  	return 0;
  }