1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "liveMedia"
17// Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
18// A simple UDP sink (i.e., without RTP or other headers added); one frame per packet
19// Implementation
20
21#include "BasicUDPSink.hh"
22#include <GroupsockHelper.hh>
23
24BasicUDPSink* BasicUDPSink::createNew(UsageEnvironment& env, Groupsock* gs,
25 unsigned maxPayloadSize) {
26 return new BasicUDPSink(env, gs, maxPayloadSize);
27}
28
29BasicUDPSink::BasicUDPSink(UsageEnvironment& env, Groupsock* gs,
30 unsigned maxPayloadSize)
31 : MediaSink(env),
32 fGS(gs), fMaxPayloadSize(maxPayloadSize) {
33 fOutputBuffer = new unsigned char[fMaxPayloadSize];
34}
35
36BasicUDPSink::~BasicUDPSink() {
37 delete[] fOutputBuffer;
38}
39
40Boolean BasicUDPSink::continuePlaying() {
41 // Record the fact that we're starting to play now:
42 gettimeofday(&fNextSendTime, NULL);
43
44 // Arrange to get and send the first payload.
45 // (This will also schedule any future sends.)
46 continuePlaying1();
47 return True;
48}
49
50void BasicUDPSink::continuePlaying1() {
51 nextTask() = NULL;
52 if (fSource != NULL) {
53 fSource->getNextFrame(fOutputBuffer, fMaxPayloadSize,
54 afterGettingFrame, this,
55 onSourceClosure, this);
56 }
57}
58
59void BasicUDPSink::afterGettingFrame(void* clientData, unsigned frameSize,
60 unsigned numTruncatedBytes,
61 struct timeval /*presentationTime*/,
62 unsigned durationInMicroseconds) {
63 BasicUDPSink* sink = (BasicUDPSink*)clientData;
64 sink->afterGettingFrame1(frameSize, numTruncatedBytes, durationInMicroseconds);
65}
66
67void BasicUDPSink::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
68 unsigned durationInMicroseconds) {
69 if (numTruncatedBytes > 0) {
70 envir() << "BasicUDPSink::afterGettingFrame1(): The input frame data was too large for our spcified maximum payload size ("
71 << fMaxPayloadSize << "). "
72 << numTruncatedBytes << " bytes of trailing data was dropped!\n";
73 }
74
75 // Send the packet:
76 fGS->output(envir(), fOutputBuffer, frameSize);
77
78 // Figure out the time at which the next packet should be sent, based
79 // on the duration of the payload that we just read:
80 fNextSendTime.tv_usec += durationInMicroseconds;
81 fNextSendTime.tv_sec += fNextSendTime.tv_usec/1000000;
82 fNextSendTime.tv_usec %= 1000000;
83
84 struct timeval timeNow;
85 gettimeofday(&timeNow, NULL);
86 int secsDiff = fNextSendTime.tv_sec - timeNow.tv_sec;
87 int64_t uSecondsToGo = secsDiff*1000000 + (fNextSendTime.tv_usec - timeNow.tv_usec);
88 if (uSecondsToGo < 0 || secsDiff < 0) { // sanity check: Make sure that the time-to-delay is non-negative:
89 uSecondsToGo = 0;
90 }
91
92 // Delay this amount of time:
93 nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecondsToGo,
94 (TaskFunc*)sendNext, this);
95}
96
97// The following is called after each delay between packet sends:
98void BasicUDPSink::sendNext(void* firstArg) {
99 BasicUDPSink* sink = (BasicUDPSink*)firstArg;
100 sink->continuePlaying1();
101}
102