示例#1
0
// Create a new RTP session.  If multicast is not being used then multicastIP
// should be set to an empty string.
static RTPSession createRtpSession(const std::string& multicastIP, 
                                   unsigned int port)
{
    RTPSession session;

    //if (setenv("LOGNAME", "video", 0) != 0) {
    //    throw std::runtime_error("Error setting LOGNAME environment variable");
    //}

    // Set up session params
    RTPSessionParams sessionparams;
    sessionparams.SetUsePollThread(false);
    sessionparams.SetMaximumPacketSize(1400);
    sessionparams.SetAcceptOwnPackets(true);
    sessionparams.SetOwnTimestampUnit(1.0f/900000.0f);
    sessionparams.SetResolveLocalHostname(false); 
    sessionparams.SetSessionBandwidth(9000000);
    sessionparams.SetReceiveMode(RTPTransmitter::AcceptAll);

    RTPUDPv4TransmissionParams transparams;
    transparams.SetPortbase(port);

    int status = session.Create(sessionparams, &transparams);
    if (status < 0) {
        throw std::runtime_error("Error creating RTP session");
    }

    // Join multicast groups if they are specified
    if (multicastIP.size() > 0) {
        if (!session.SupportsMulticasting()) {
            throw std::runtime_error("Multicast not supported!");
        } else {
            int joinip = ntohl(inet_addr(multicastIP.c_str())); 

            RTPIPv4Address joinaddr(joinip, port);
            int jstatus = session.JoinMulticastGroup(joinaddr);

            if (jstatus < 0) {
                throw std::runtime_error("Unable to join multicast group");
            }
        }
    }

    return session;
}
bool MediaStream::start(QHostAddress ip, int port, int localPort, int codecPayload )
{
    if ( isRunning() )
        stop();

    printf("%s %d | %d | %d\n",ip.toString().toAscii().data(), port, localPort, codecPayload);

    if(d->processThread == NULL)
	d->processThread = new MediaThread(this);
    d->outBufferPos = 0;
    d->outBufferTime = 0;

//    int localPort = 3000;
printf("getFactory(%d)\n", codecPayload);
    VoiceCodecFactory *factory = CodecsManager::instance()->codecFactory(codecPayload);
    if ( !factory ) {
        printf("VoiceCodecFactory not found!\n");
        return true;
    }

    d->codecPayload = codecPayload;
    d->decoder =  factory->decoder();
    d->encoder =  factory->encoder();

printf("transparams\n");
    // Now, we'll create a RTP session and set the destination
//     d->transparams.mediaChannel = mediaChannel;
//     d->transparams.incomingPackets = incomingPackets;
    d->transparams.SetPortbase(localPort);
	
    RTPSessionParams sessparams;
    sessparams.SetReceiveMode(RTPTransmitter::AcceptAll);

    sessparams.SetOwnTimestampUnit(1.0/8000.0); // 8KHz
	sessparams.SetAcceptOwnPackets(true);

printf("session.Create()\n");
    int status = d->session.Create( sessparams, &d->transparams, RTPTransmitter::SynapseProto );

    if ( status<0 ) {
        qDebug("can't create RTP session, %s", RTPGetErrorString(status).c_str() );
        d->session.Destroy(); 
        return false;
    }

printf("session.AddDestination()\n");
    RTPIPv4Address addr(ip.toIPv4Address(),port);
	status = d->session.AddDestination(addr);

    if ( status<0 ) {
         qDebug("can't add rtp destination, %s", RTPGetErrorString(status).c_str() );
        d->session.Destroy(); 
        return false;
    }

    d->session.SetDefaultPayloadType(codecPayload);
    d->session.SetDefaultMark(false);
    d->session.SetDefaultTimestampIncrement(160);

    //initialise audio

    status = Pa_Initialize();
//////////////////// FOR TESTING
    if( status != paNoError ) {
        qDebug( "PortAudio error: %s", Pa_GetErrorText(status) );
//        stop();
 //       return true;
    }

if(status == paNoError) {

    status = Pa_OpenDefaultStream(
        &d->audioStream,/* passes back stream pointer */
        1,              /* 1 input channel */
        1,              /* mono output */
        paInt16,        /* 16 bit fixed point output */
        8000,           /* sample rate */
        240,            /* frames per buffer */
        16,             /* number of buffers, if zero then use default minimum */
        audioCallback,  /* specify our custom callback */
        d );            /* pass our data through to callback */

    status = Pa_StartStream( d->audioStream );
    if( status != paNoError ) {
         qDebug( "PortAudio error: %s", Pa_GetErrorText(status) );
//        stop();
//        return true;
    }
}
    

    // put something to dsp buffer
    /*
    char emptyData[160*8];
    memset(  emptyData, 1, sizeof(emptyData) );
    d->dspBuffer->lock();
    d->dspBuffer->put( emptyData, sizeof(emptyData) );
    d->dspBuffer->unlock();
    */



    //d->timer.start(1,false);
    d->isRunning = true;
    d->processThread->start();
    
//    qDebug("mediastream started");
    printf("mediastream started\n");
    return true;
} // }}}