void MP3FromADUSource::doGetNextFrame() { if (fAreEnqueueingADU) insertDummyADUsIfNecessary(); fAreEnqueueingADU = False; if (needToGetAnADU()) { // Before returning a frame, we must enqueue at least one ADU: #ifdef TEST_LOSS NOTE: This code no longer works, because it uses synchronous reads, which are no longer supported. static unsigned const framesPerPacket = 10; static unsigned const frameCount = 0; static Boolean packetIsLost; while (1) { if ((frameCount++)%framesPerPacket == 0) { packetIsLost = (our_random()%10 == 0); // simulate 10% packet loss ##### } if (packetIsLost) { // Read and discard the next input frame (that would be part of // a lost packet): Segment dummySegment; unsigned numBytesRead; struct timeval presentationTime; // (this works only if the source can be read synchronously) fInputSource->syncGetNextFrame(dummySegment.buf, sizeof dummySegment.buf, numBytesRead, presentationTime); } else { break; // from while (1) } } #endif fAreEnqueueingADU = True; fSegments->enqueueNewSegment(fInputSource, this); } else {
void FileSink::addData(unsigned char const* data, unsigned dataSize, struct timeval presentationTime) { if (fPerFrameFileNameBuffer != NULL) { // Special case: Open a new file on-the-fly for this frame sprintf(fPerFrameFileNameBuffer, "%s-%lu.%06lu", fPerFrameFileNamePrefix, presentationTime.tv_sec, presentationTime.tv_usec); fOutFid = OpenOutputFile(envir(), fPerFrameFileNameBuffer); } // Write to our file: #ifdef TEST_LOSS static unsigned const framesPerPacket = 10; static unsigned const frameCount = 0; static Boolean const packetIsLost; if ((frameCount++)%framesPerPacket == 0) { packetIsLost = (our_random()%10 == 0); // simulate 10% packet loss ##### } if (!packetIsLost) #endif if (fOutFid != NULL && data != NULL) { fwrite(data, 1, dataSize, fOutFid); } }
void CMyRTSPClent::scheduleLivenessCommand() { //Delay a random time before sending another 'liveness' command. unsigned delayMax = sessionTimeoutParameter(); // if the server specified a maximum time between 'liveness' probes, then use that if (delayMax == 0) { delayMax = 60; } // Choose a random time from [delayMax/2,delayMax-1) seconds: unsigned const us_1stPart = delayMax*500000; unsigned uSecondsToDelay; if (us_1stPart <= 1000000) { uSecondsToDelay = us_1stPart; } else { unsigned const us_2ndPart = us_1stPart-1000000; uSecondsToDelay = us_1stPart + (us_2ndPart*our_random())%us_2ndPart; } fLivenessCommandTask = envir().taskScheduler().scheduleDelayedTask(uSecondsToDelay, sendLivenessCommand, this); }
void MultiFramedRTPSource::networkReadHandler1() { BufferedPacket* bPacket = fPacketReadInProgress; if (bPacket == NULL) { // Normal case: Get a free BufferedPacket descriptor to hold the new network packet: bPacket = fReorderingBuffer->getFreePacket(this); } // Read the network packet, and perform sanity checks on the RTP header: Boolean readSuccess = False; do { struct sockaddr_in fromAddress; Boolean packetReadWasIncomplete = fPacketReadInProgress != NULL; if (!bPacket->fillInData(fRTPInterface, fromAddress, packetReadWasIncomplete)) { if (bPacket->bytesAvailable() == 0) { // should not happen?? envir() << "MultiFramedRTPSource internal error: Hit limit when reading incoming packet over TCP\n"; } fPacketReadInProgress = NULL; break; } if (packetReadWasIncomplete) { // We need additional read(s) before we can process the incoming packet: fPacketReadInProgress = bPacket; return; } else { fPacketReadInProgress = NULL; } #ifdef TEST_LOSS setPacketReorderingThresholdTime(0); // don't wait for 'lost' packets to arrive out-of-order later if ((our_random()%10) == 0) break; // simulate 10% packet loss #endif // Check for the 12-byte RTP header: if (bPacket->dataSize() < 12) break; unsigned rtpHdr = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4); Boolean rtpMarkerBit = (rtpHdr&0x00800000) != 0; unsigned rtpTimestamp = ntohl(*(u_int32_t*)(bPacket->data()));ADVANCE(4); unsigned rtpSSRC = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4); // Check the RTP version number (it should be 2): if ((rtpHdr&0xC0000000) != 0x80000000) break; // Check the Payload Type. unsigned char rtpPayloadType = (unsigned char)((rtpHdr&0x007F0000)>>16); if (rtpPayloadType != rtpPayloadFormat()) { if (fRTCPInstanceForMultiplexedRTCPPackets != NULL && rtpPayloadType >= 64 && rtpPayloadType <= 95) { // This is a multiplexed RTCP packet, and we've been asked to deliver such packets. // Do so now: fRTCPInstanceForMultiplexedRTCPPackets ->injectReport(bPacket->data()-12, bPacket->dataSize()+12, fromAddress); } break; } // Skip over any CSRC identifiers in the header: unsigned cc = (rtpHdr>>24)&0x0F; if (bPacket->dataSize() < cc*4) break; ADVANCE(cc*4); // Check for (& ignore) any RTP header extension if (rtpHdr&0x10000000) { if (bPacket->dataSize() < 4) break; unsigned extHdr = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4); unsigned remExtSize = 4*(extHdr&0xFFFF); if (bPacket->dataSize() < remExtSize) break; ADVANCE(remExtSize); } // Discard any padding bytes: if (rtpHdr&0x20000000) { if (bPacket->dataSize() == 0) break; unsigned numPaddingBytes = (unsigned)(bPacket->data())[bPacket->dataSize()-1]; if (bPacket->dataSize() < numPaddingBytes) break; bPacket->removePadding(numPaddingBytes); } // The rest of the packet is the usable data. Record and save it: if (rtpSSRC != fLastReceivedSSRC) { // The SSRC of incoming packets has changed. Unfortunately we don't yet handle streams that contain multiple SSRCs, // but we can handle a single-SSRC stream where the SSRC changes occasionally: fLastReceivedSSRC = rtpSSRC; fReorderingBuffer->resetHaveSeenFirstPacket(); } unsigned short rtpSeqNo = (unsigned short)(rtpHdr&0xFFFF); Boolean usableInJitterCalculation = packetIsUsableInJitterCalculation((bPacket->data()), bPacket->dataSize()); struct timeval presentationTime; // computed by: Boolean hasBeenSyncedUsingRTCP; // computed by: receptionStatsDB() .noteIncomingPacket(rtpSSRC, rtpSeqNo, rtpTimestamp, timestampFrequency(), usableInJitterCalculation, presentationTime, hasBeenSyncedUsingRTCP, bPacket->dataSize()); // Fill in the rest of the packet descriptor, and store it: struct timeval timeNow; gettimeofday(&timeNow, NULL); bPacket->assignMiscParams(rtpSeqNo, rtpTimestamp, presentationTime, hasBeenSyncedUsingRTCP, rtpMarkerBit, timeNow); if (!fReorderingBuffer->storePacket(bPacket)) break; readSuccess = True; } while (0); if (!readSuccess) fReorderingBuffer->freePacket(bPacket); doGetNextFrame1(); // If we didn't get proper data this time, we'll get another chance }
extern "C" double drand30() { unsigned tmp = our_random()&0x3FFFFFFF; // a random 30-bit integer return tmp/(double)(1024*1024*1024); }
void ProxyRTSPClient::scheduleLivenessCommand() { // Delay a random time before sending "GET_PARAMETER": unsigned secondsToDelay = 30 + (our_random()&0x1F); // [30..61] seconds envir().taskScheduler().scheduleDelayedTask(secondsToDelay*1000000, sendLivenessCommand, this); }
int main(int argc, char** argv) { CroftonTessellation ctel; Rectangle win; config_droites stl; int nb_lines,nb_rep; unsigned int semence; if(parse_arguments(argc,argv,&nb_lines,&nb_rep,&semence)!=0) { usage(); return 1; } CGAL::Random our_random(semence); // Define window win = Rectangle(Point2(0,0),Point2(100,100)); for(int j=0;j<nb_rep;j++) { ctel.clear(); ctel.insert_window(win); stl.clear(); // Generate a random tessellation ctel.insert_random_lines(nb_lines,our_random); std::cout << ctel.number_of_lines() << " "; #ifdef BAVARD std::cout << "droites" << std::endl; #endif std::cout << ctel.number_of_vertices()-2*ctel.number_of_lines()-4 << " "; #ifdef BAVARD std::cout << "internal vertices" << std::endl; #endif std::cout << ctel.number_of_halfedges()/2-4-2*nb_lines << " "; #ifdef BAVARD std::cout << "internal edges" << std::endl; #endif std::cout << ctel.number_of_faces()-1 << " "; #ifdef BAVARD std::cout << "faces" << std::endl; #endif /* Get a void representation of a partial subtessellation. */ stl = ctel.init_partial_stl(); #ifdef BAVARD print_crossing_stl(stl); #endif int nb_stl = nb_subtessellations(stl,0); std::cout << nb_stl; #ifdef BAVARD std::cout << " sous-tessellations" << std::endl; #endif if(nb_stl==0) { std::cout << std::endl << "AUCUNE SOUS-TESSELLATION ! " << std::endl; print_crossing_stl(stl); break; } std::cout << std::endl; } return 0; }
void MultiFramedRTPSink::sendPacketIfNecessary() { if (fNumFramesUsedSoFar > 0) { // Send the packet: #ifdef TEST_LOSS if ((our_random()%10) != 0) // simulate 10% packet loss ##### #endif if(fSeqNo < 10) { Debug(ckite_log_message, "fSeqNo = %d\n", fSeqNo); } int sndIsSucess = fRTPInterface.sendPacket(fOutBuf->packet(), fOutBuf->curPacketSize()); Debug(ckite_log_message, "sndIsSucess = %d, fOwner = %x\n", sndIsSucess, fOwner); if (fOwner != NULL && sndIsSucess == 1) { Debug(ckite_log_message, "sendPacketIfNecessary.\n"); ((RTSPServer::RTSPClientSession*)fOwner)->noteLiveness(); } if(fOwner != NULL && sndIsSucess == -1) { ((RTSPServer::RTSPClientSession*)fOwner)->closeHttpSocketAndFreeResource(); } //Debug(ckite_log_message, "[wayde]MultiFramedRTPSink::sendPacketIfNecessary packetSize = %d\n",fOutBuf->curPacketSize()); ++fPacketCount; fTotalOctetCount += fOutBuf->curPacketSize(); fOctetCount += fOutBuf->curPacketSize() - rtpHeaderSize - fSpecialHeaderSize - fTotalFrameSpecificHeaderSizes; ++fSeqNo; // for next time } if (fOutBuf->haveOverflowData() && fOutBuf->totalBytesAvailable() > fOutBuf->totalBufferSize()/2) { // Efficiency hack: Reset the packet start pointer to just in front of // the overflow data (allowing for the RTP header and special headers), // so that we probably don't have to "memmove()" the overflow data // into place when building the next packet: unsigned newPacketStart = fOutBuf->curPacketSize() - (rtpHeaderSize + fSpecialHeaderSize + frameSpecificHeaderSize()); fOutBuf->adjustPacketStart(newPacketStart); } else { // Normal case: Reset the packet start pointer back to the start: fOutBuf->resetPacketStart(); } fOutBuf->resetOffset(); fNumFramesUsedSoFar = 0; if (fNoFramesLeft) { // We're done: onSourceClosure(this); } else { // We have more frames left to send. Figure out when the next frame // is due to start playing, then make sure that we wait this long before // sending the next packet. struct timeval timeNow; gettimeofday(&timeNow, NULL); int uSecondsToGo; if (fNextSendTime.tv_sec < timeNow.tv_sec || (fNextSendTime.tv_sec == timeNow.tv_sec && fNextSendTime.tv_usec < timeNow.tv_usec)) { uSecondsToGo = 0; // prevents integer underflow if too far behind } else { uSecondsToGo = (fNextSendTime.tv_sec - timeNow.tv_sec)*1000000 + (fNextSendTime.tv_usec - timeNow.tv_usec); } // Delay this amount of time: nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecondsToGo, (TaskFunc*)sendNext, this); } }
Boolean DarwinInjector ::setDestination(char const* remoteRTSPServerNameOrAddress, char const* remoteFileName, char const* sessionName, char const* sessionInfo, portNumBits remoteRTSPServerPortNumber, char const* remoteUserName, char const* remotePassword, char const* sessionAuthor, char const* sessionCopyright, int timeout) { char* sdp = NULL; char* url = NULL; Boolean success = False; // until we learn otherwise do { // Begin by creating our RTSP client object: fRTSPClient = RTSPClient::createNew(envir(), fVerbosityLevel, fApplicationName); if (fRTSPClient == NULL) break; // Get the remote RTSP server's IP address: struct in_addr addr; { NetAddressList addresses(remoteRTSPServerNameOrAddress); if (addresses.numAddresses() == 0) break; NetAddress const* address = addresses.firstAddress(); addr.s_addr = *(unsigned*)(address->data()); } char const* remoteRTSPServerAddressStr = our_inet_ntoa(addr); // Construct a SDP description for the session that we'll be streaming: char const* const sdpFmt = "v=0\r\n" "o=- %u %u IN IP4 127.0.0.1\r\n" "s=%s\r\n" "i=%s\r\n" "c=IN IP4 %s\r\n" "t=0 0\r\n" "a=x-qt-text-nam:%s\r\n" "a=x-qt-text-inf:%s\r\n" "a=x-qt-text-cmt:source application:%s\r\n" "a=x-qt-text-aut:%s\r\n" "a=x-qt-text-cpy:%s\r\n"; // plus, %s for each substream SDP unsigned sdpLen = strlen(sdpFmt) + 20 /* max int len */ + 20 /* max int len */ + strlen(sessionName) + strlen(sessionInfo) + strlen(remoteRTSPServerAddressStr) + strlen(sessionName) + strlen(sessionInfo) + strlen(fApplicationName) + strlen(sessionAuthor) + strlen(sessionCopyright) + fSubstreamSDPSizes; unsigned const sdpSessionId = our_random(); unsigned const sdpVersion = sdpSessionId; sdp = new char[sdpLen]; sprintf(sdp, sdpFmt, sdpSessionId, sdpVersion, // o= line sessionName, // s= line sessionInfo, // i= line remoteRTSPServerAddressStr, // c= line sessionName, // a=x-qt-text-nam: line sessionInfo, // a=x-qt-text-inf: line fApplicationName, // a=x-qt-text-cmt: line sessionAuthor, // a=x-qt-text-aut: line sessionCopyright // a=x-qt-text-cpy: line ); char* p = &sdp[strlen(sdp)]; SubstreamDescriptor* ss; for (ss = fHeadSubstream; ss != NULL; ss = ss->next()) { sprintf(p, "%s", ss->sdpLines()); p += strlen(p); } // Construct a RTSP URL for the remote stream: char const* const urlFmt = "rtsp://%s:%u/%s"; unsigned urlLen = strlen(urlFmt) + strlen(remoteRTSPServerNameOrAddress) + 5 /* max short len */ + strlen(remoteFileName); url = new char[urlLen]; sprintf(url, urlFmt, remoteRTSPServerNameOrAddress, remoteRTSPServerPortNumber, remoteFileName); // Do a RTSP "ANNOUNCE" with this SDP description: Boolean announceSuccess; if (remoteUserName[0] != '\0' || remotePassword[0] != '\0') { announceSuccess = fRTSPClient->announceWithPassword(url, sdp, remoteUserName, remotePassword, timeout); } else { announceSuccess = fRTSPClient->announceSDPDescription(url, sdp, NULL, timeout); } if (!announceSuccess) break; // Tell the remote server to start receiving the stream from us. // (To do this, we first create a "MediaSession" object from the SDP description.) fSession = MediaSession::createNew(envir(), sdp); if (fSession == NULL) break; ss = fHeadSubstream; MediaSubsessionIterator iter(*fSession); MediaSubsession* subsession; ss = fHeadSubstream; unsigned streamChannelId = 0; while ((subsession = iter.next()) != NULL) { if (!subsession->initiate()) break; if (!fRTSPClient->setupMediaSubsession(*subsession, True /*streamOutgoing*/, True /*streamUsingTCP*/)) { break; } // Tell this subsession's RTPSink and RTCPInstance to use // the RTSP TCP connection: ss->rtpSink()->setStreamSocket(fRTSPClient->socketNum(), streamChannelId++); if (ss->rtcpInstance() != NULL) { ss->rtcpInstance()->setStreamSocket(fRTSPClient->socketNum(), streamChannelId++); } ss = ss->next(); } if (subsession != NULL) break; // an error occurred above // Tell the RTSP server to start: if (!fRTSPClient->playMediaSession(*fSession)) break; // Finally, make sure that the output TCP buffer is a reasonable size: increaseSendBufferTo(envir(), fRTSPClient->socketNum(), 100*1024); success = True; } while (0); delete[] sdp; delete[] url; return success; }