TCPStreamSink.hh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "liveMedia"
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // A sink representing a TCP output stream
  17. // C++ header
  18. #ifndef _TCP_STREAM_SINK_HH
  19. #define _TCP_STREAM_SINK_HH
  20. #ifndef _MEDIA_SINK_HH
  21. #include "MediaSink.hh"
  22. #endif
  23. #define TCP_STREAM_SINK_BUFFER_SIZE 10000
  24. class TCPStreamSink: public MediaSink {
  25. public:
  26. static TCPStreamSink* createNew(UsageEnvironment& env, int socketNum);
  27. // "socketNum" is the socket number of an existing, writable TCP socket (which should be non-blocking).
  28. // The caller is responsible for closing this socket later (when this object no longer exists).
  29. protected:
  30. TCPStreamSink(UsageEnvironment& env, int socketNum); // called only by "createNew()"
  31. virtual ~TCPStreamSink();
  32. protected:
  33. // Redefined virtual functions:
  34. virtual Boolean continuePlaying();
  35. private:
  36. void processBuffer(); // common routine, called from both the 'socket writable' and 'incoming data' handlers below
  37. static void socketWritableHandler(void* clientData, int mask);
  38. void socketWritableHandler1();
  39. static void afterGettingFrame(void* clientData, unsigned frameSize, unsigned numTruncatedBytes,
  40. struct timeval /*presentationTime*/, unsigned /*durationInMicroseconds*/);
  41. void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes);
  42. static void ourOnSourceClosure(void* clientData);
  43. void ourOnSourceClosure1();
  44. unsigned numUnwrittenBytes() const { return fUnwrittenBytesEnd - fUnwrittenBytesStart; }
  45. unsigned freeBufferSpace() const { return TCP_STREAM_SINK_BUFFER_SIZE - fUnwrittenBytesEnd; }
  46. private:
  47. unsigned char fBuffer[TCP_STREAM_SINK_BUFFER_SIZE];
  48. unsigned fUnwrittenBytesStart, fUnwrittenBytesEnd;
  49. Boolean fInputSourceIsOpen, fOutputSocketIsWritable;
  50. int fOutputSocketNum;
  51. };
  52. #endif