コード例 #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 transmit audio to perself through " <<
			local_ip << "..." << endl;
		
		// ____Here comes the real RTP stuff____
		
		// Construct the RTP socket.
		socket = new RTPSession(local_ip,TRANSMITTER_BASE);
		
		// Set up connection
		socket->setSchedulingTimeout(10000);
		if( !socket->addDestination(local_ip,RECEIVER_BASE) )
			cerr << "I could not connect.";
		
		socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));

		socket->startRunning();
		cout << "The RTP queue service thread is ";
		if( socket->isActive() )
			cout << "active." << endl;
		else
			cerr << "not active." << endl;
		
		cout << "Transmitting " << PACKET_SIZE 
		     << " octects long packets "
		     << "every " << PERIOD << " milliseconds..." << endl;
		
		unsigned char buffer[PACKET_SIZE];
		int count=PACKET_SIZE;
		
		// This will be useful for periodic execution
		TimerPort::setTimer(PERIOD);
		
		setCancel(cancelImmediate);
		// This is the main loop, where packets are transmitted.
		for( int i = 0 ; (!sendingfile || count > 0) ; i++ ){
			
			count = ::read(audioinput,buffer,PACKET_SIZE);
			if( count > 0 ){				
				// send an RTP packet, providing timestamp,
				// payload type and payload.
				socket->putData(PACKET_SIZE*i,buffer,
						PACKET_SIZE);
			}
			cout << "." << flush;

			// Let's wait for the next cycle
			Thread::sleep(TimerPort::getTimer());
			TimerPort::incTimer(PERIOD);
		}
		cout << endl << "I have got no more data to send. " <<endl;
	}
コード例 #2
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