liveMedia/BasicUDPSink.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // A simple UDP sink (i.e., without RTP or other headers added); one frame per packet
00019 // Implementation
00020 
00021 #include "BasicUDPSink.hh"
00022 #include <GroupsockHelper.hh>
00023 
00024 BasicUDPSink* BasicUDPSink::createNew(UsageEnvironment& env, Groupsock* gs,
00025                                       unsigned maxPayloadSize) {
00026   return new BasicUDPSink(env, gs, maxPayloadSize);
00027 }
00028 
00029 BasicUDPSink::BasicUDPSink(UsageEnvironment& env, Groupsock* gs,
00030                            unsigned maxPayloadSize)
00031   : MediaSink(env),
00032     fGS(gs), fMaxPayloadSize(maxPayloadSize) {
00033   fOutputBuffer = new unsigned char[fMaxPayloadSize];
00034 }
00035 
00036 BasicUDPSink::~BasicUDPSink() {
00037   delete[] fOutputBuffer;
00038 }
00039 
00040 Boolean BasicUDPSink::continuePlaying() {
00041   // Record the fact that we're starting to play now:
00042   gettimeofday(&fNextSendTime, NULL);
00043 
00044   // Arrange to get and send the first payload.
00045   // (This will also schedule any future sends.)
00046   continuePlaying1();
00047   return True;
00048 }
00049 
00050 void BasicUDPSink::continuePlaying1() {
00051   if (fSource != NULL) {
00052     fSource->getNextFrame(fOutputBuffer, fMaxPayloadSize,
00053                           afterGettingFrame, this,
00054                           onSourceClosure, this);
00055   }
00056 }
00057 
00058 void BasicUDPSink::afterGettingFrame(void* clientData, unsigned frameSize,
00059                                      unsigned numTruncatedBytes,
00060                                      struct timeval /*presentationTime*/,
00061                                      unsigned durationInMicroseconds) {
00062   BasicUDPSink* sink = (BasicUDPSink*)clientData;
00063   sink->afterGettingFrame1(frameSize, numTruncatedBytes, durationInMicroseconds);
00064 }
00065 
00066 void BasicUDPSink::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
00067                                       unsigned durationInMicroseconds) {
00068   if (numTruncatedBytes > 0) {
00069     envir() << "BasicUDPSink::afterGettingFrame1(): The input frame data was too large for our spcified maximum payload size ("
00070             << fMaxPayloadSize << ").  "
00071             << numTruncatedBytes << " bytes of trailing data was dropped!\n";
00072   }
00073 
00074   // Send the packet:
00075   fGS->output(envir(), fGS->ttl(), fOutputBuffer, frameSize);
00076 
00077   // Figure out the time at which the next packet should be sent, based
00078   // on the duration of the payload that we just read:
00079   fNextSendTime.tv_usec += durationInMicroseconds;
00080   fNextSendTime.tv_sec += fNextSendTime.tv_usec/1000000;
00081   fNextSendTime.tv_usec %= 1000000;
00082 
00083   struct timeval timeNow;
00084   gettimeofday(&timeNow, NULL);
00085   int secsDiff = fNextSendTime.tv_sec - timeNow.tv_sec;
00086   int64_t uSecondsToGo = secsDiff*1000000 + (fNextSendTime.tv_usec - timeNow.tv_usec);
00087   if (uSecondsToGo < 0 || secsDiff < 0) { // sanity check: Make sure that the time-to-delay is non-negative:
00088     uSecondsToGo = 0;
00089   }
00090 
00091   // Delay this amount of time:
00092   nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecondsToGo,
00093                                                            (TaskFunc*)sendNext, this);
00094 }
00095 
00096 // The following is called after each delay between packet sends:
00097 void BasicUDPSink::sendNext(void* firstArg) {
00098   BasicUDPSink* sink = (BasicUDPSink*)firstArg;
00099   sink->continuePlaying1();
00100 }

Generated on Mon Apr 29 13:28:01 2013 for live by  doxygen 1.5.2