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 sink representing a TCP output stream
19// C++ header
20
21#ifndef _TCP_STREAM_SINK_HH
22#define _TCP_STREAM_SINK_HH
23
24#ifndef _MEDIA_SINK_HH
25#include "MediaSink.hh"
26#endif
27
28#define TCP_STREAM_SINK_BUFFER_SIZE 10000
29
30class TCPStreamSink: public MediaSink {
31public:
32 static TCPStreamSink* createNew(UsageEnvironment& env, int socketNum);
33 // "socketNum" is the socket number of an existing, writable TCP socket (which should be non-blocking).
34 // The caller is responsible for closing this socket later (when this object no longer exists).
35
36protected:
37 TCPStreamSink(UsageEnvironment& env, int socketNum); // called only by "createNew()"
38 virtual ~TCPStreamSink();
39
40protected:
41 // Redefined virtual functions:
42 virtual Boolean continuePlaying();
43
44private:
45 void processBuffer(); // common routine, called from both the 'socket writable' and 'incoming data' handlers below
46
47 static void socketWritableHandler(void* clientData, int mask);
48 void socketWritableHandler1();
49
50 static void afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes,
51 struct timeval /*presentationTime*/, unsigned /*durationInMicroseconds*/);
52 void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes);
53
54 static void ourOnSourceClosure(void* clientData);
55 void ourOnSourceClosure1();
56
57 unsigned numUnwrittenBytes() const { return fUnwrittenBytesEnd - fUnwrittenBytesStart; }
58 unsigned freeBufferSpace() const { return TCP_STREAM_SINK_BUFFER_SIZE - fUnwrittenBytesEnd; }
59
60private:
61 unsigned char fBuffer[TCP_STREAM_SINK_BUFFER_SIZE];
62 unsigned fUnwrittenBytesStart, fUnwrittenBytesEnd;
63 Boolean fInputSourceIsOpen, fOutputSocketIsWritable;
64 int fOutputSocketNum;
65};
66
67#endif
68