Пример #1
0
	// This method does almost everything.
	void run(void){    
		// redefined from Thread.
		
		// Before using ccRTP you should learn something about other
		// CommonC++ classes. We need InetHostAddress...

		// Construct loopback address
		InetHostAddress local_ip;
		local_ip = "127.0.0.1";
		
		// Is that correct?
		if( ! local_ip ){  
		// this is equivalent to `! local_ip.isInetAddress()'
			cerr << ": IP address is not correct!" << endl;
			exit();
		}
		
		cout << local_ip.getHostname() << 
			" is going to listen to perself through " <<
			local_ip << "..." << endl;
		
		// ____Here comes the real RTP stuff____
		
		// Construct the RTP socket
		socket = new RTPSession(local_ip,RECEIVER_BASE,0);
		
		// Set up receiver's connection
		socket->setSchedulingTimeout(10000);  
		if( !socket->addDestination(local_ip,TRANSMITTER_BASE) )
			cerr << "The receiver could not connect.";
		
		// Let's check the queue (you should read the documentation
		// so that you know what the queue is for).
		socket->startRunning();
		cout << "The RTP queue is ";
		if( socket->isActive() )
			cout << "active." << endl;
		else
			cerr << "not active." << endl;
		
		cout << "Waiting for audio packets..." << endl;
		
		// This will be useful for periodic execution.
		TimerPort::setTimer(PERIOD);

		setCancel(cancelImmediate);
		// This is the main loop, where packets are sent and receipt.
		socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
		for( int i=0 ; true ; i++ ){
			const AppDataUnit* adu;
			do{
				adu = socket->getData(socket->getFirstTimestamp());
				if ( NULL == adu )
					Thread::sleep(5);
				else cout << ".";
			}while ( (NULL == adu) || (adu->getSize() <= 0) );

			
			// This is for buffering some packets at the
			// receiver side, since playing smoothly
			// without any reception buffer is almost
			// impossible.  Try commenting the two lines
			// below, or stop transmission and continue
			// later: you will probably hear noise or
			// cracks.  
			if (i==0)
				Thread::sleep(20);
			
			::write(audiooutput,adu->getData(),adu->getSize());

			cout << "." << flush;

			// Let's wait for the next cycle
			Thread::sleep(TimerPort::getTimer());
			TimerPort::incTimer(PERIOD);
		}

	} // end of run