bool UdpConnection::initTwoWay(const std::string &remoteIp, uint16_t remotePort,
                                   uint16_t localPort,
                                   uint32_t packetSize) {
        if (hasInitialized) {
            freeResources();
        }

        bool worked = true;
        worked &= initSdlNet();
        worked &= openPort(localPort);
        worked &= setUpPort(remoteIp, remotePort);
        worked &= allocPacket(packetSize);

        hasInitialized = worked;
        return worked;
    }
Beispiel #2
0
OMXPacket* OMXReader::Read()
{
    AVPacket  pkt;
    OMXPacket* omxPacket = NULL;
    int       result = -1;
    
    if(!avFormatContext)
        return NULL;
    
    lock();
    
    // assume we are not eof
    if(avFormatContext->pb)
        avFormatContext->pb->eof_reached = 0;
    
    // keep track if ffmpeg doesn't always set these
    pkt.size = 0;
    pkt.data = NULL;
    pkt.stream_index = MAX_OMX_STREAMS;
    
    result = av_read_frame(avFormatContext, &pkt);
    if (result < 0)
    {
        isEOF = true;
        unlock();
        return NULL;
    }
    else if (pkt.size < 0 || pkt.stream_index >= MAX_OMX_STREAMS)
    {
        // XXX, in some cases ffmpeg returns a negative packet size
        if(avFormatContext->pb && !avFormatContext->pb->eof_reached)
        {
            ofLog(OF_LOG_ERROR, "OMXReader::Read no valid packet");
            //FlushRead();
        }
        
        av_free_packet(&pkt);
        
        isEOF = true;
        unlock();
        return NULL;
    }
    
    AVStream *pStream = avFormatContext->streams[pkt.stream_index];
    
    /* only read packets for active streams */
    /*
     if(!isActive(pkt.stream_index))
     {
     av_free_packet(&pkt);
     unlock();
     return NULL;
     }
     */
    
    // lavf sometimes bugs out and gives 0 dts/pts instead of no dts/pts
    // since this could only happens on initial frame under normal
    // circomstances, let's assume it is wrong all the time
#if 0
    if(pkt.dts == 0)
        pkt.dts = AV_NOPTS_VALUE;
    if(pkt.pts == 0)
        pkt.pts = AV_NOPTS_VALUE;
#endif
    if(isMatroska && pStream->codec && pStream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
    { // matroska can store different timestamps
        // for different formats, for native stored
        // stuff it is pts, but for ms compatibility
        // tracks, it is really dts. sadly ffmpeg
        // sets these two timestamps equal all the
        // time, so we select it here instead
        if(pStream->codec->codec_tag == 0)
            pkt.dts = AV_NOPTS_VALUE;
        else
            pkt.pts = AV_NOPTS_VALUE;
    }
    // we need to get duration slightly different for matroska embedded text subtitels
    if(isMatroska && pStream->codec->codec_id == AV_CODEC_ID_SUBRIP && pkt.convergence_duration != 0)
        pkt.duration = pkt.convergence_duration;
    
    if(isAVI && pStream->codec && pStream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
    {
        // AVI's always have borked pts, specially if avFormatContext->flags includes
        // AVFMT_FLAG_GENPTS so always use dts
        pkt.pts = AV_NOPTS_VALUE;
    }
    
    omxPacket = allocPacket(pkt.size);
    /* oom error allocation av packet */
    if(!omxPacket)
    {
        isEOF = true;
        av_free_packet(&pkt);
        unlock();
        return NULL;
    }
    
    omxPacket->codec_type = pStream->codec->codec_type;
    
    /* copy content into our own packet */
    omxPacket->size = pkt.size;
    
    if (pkt.data)
        memcpy(omxPacket->data, pkt.data, omxPacket->size);
    
    omxPacket->stream_index = pkt.stream_index;
    getHints(pStream, &omxPacket->hints);
    
    omxPacket->dts = ConvertTimestamp(pkt.dts, pStream->time_base.den, pStream->time_base.num);
    omxPacket->pts = ConvertTimestamp(pkt.pts, pStream->time_base.den, pStream->time_base.num);
    omxPacket->duration = DVD_SEC_TO_TIME((double)pkt.duration * pStream->time_base.num / pStream->time_base.den);
    
    // used to guess streamlength
    if (omxPacket->dts != DVD_NOPTS_VALUE && (omxPacket->dts > currentPTS || currentPTS == DVD_NOPTS_VALUE))
        currentPTS = omxPacket->dts;
    
    // check if stream has passed full duration, needed for live streams
    if(pkt.dts != (int64_t)AV_NOPTS_VALUE)
    {
        int64_t duration;
        duration = pkt.dts;
        if(pStream->start_time != (int64_t)AV_NOPTS_VALUE)
            duration -= pStream->start_time;
        
        if(duration > pStream->duration)
        {
            pStream->duration = duration;
            duration = av_rescale_rnd(pStream->duration, (int64_t)pStream->time_base.num * AV_TIME_BASE, 
                                      pStream->time_base.den, AV_ROUND_NEAR_INF);
            if ((avFormatContext->duration == (int64_t)AV_NOPTS_VALUE)
                ||  (avFormatContext->duration != (int64_t)AV_NOPTS_VALUE && duration > avFormatContext->duration))
                avFormatContext->duration = duration;
        }
    }
    
    av_free_packet(&pkt);
    
    unlock();
    return omxPacket;
}