RTPInterface.hh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // An abstraction of a network interface used for RTP (or RTCP).
  17. // (This allows the RTP-over-TCP hack (RFC 2326, section 10.12) to
  18. // be implemented transparently.)
  19. // C++ header
  20. #ifndef _RTP_INTERFACE_HH
  21. #define _RTP_INTERFACE_HH
  22. #ifndef _MEDIA_HH
  23. #include <Media.hh>
  24. #endif
  25. #ifndef _GROUPSOCK_HH
  26. #include "Groupsock.hh"
  27. #endif
  28. // Typedef for an optional auxilliary handler function, to be called
  29. // when each new packet is read:
  30. typedef void AuxHandlerFunc(void* clientData, unsigned char* packet,
  31. unsigned& packetSize);
  32. typedef void ServerRequestAlternativeByteHandler(void* instance, u_int8_t requestByte);
  33. // A hack that allows a handler for RTP/RTCP packets received over TCP to process RTSP commands that may also appear within
  34. // the same TCP connection. A RTSP server implementation would supply a function like this - as a parameter to
  35. // "ServerMediaSubsession::startStream()".
  36. class tcpStreamRecord {
  37. public:
  38. tcpStreamRecord(int streamSocketNum, unsigned char streamChannelId,
  39. tcpStreamRecord* next);
  40. virtual ~tcpStreamRecord();
  41. public:
  42. tcpStreamRecord* fNext;
  43. int fStreamSocketNum;
  44. unsigned char fStreamChannelId;
  45. };
  46. class RTPInterface {
  47. public:
  48. RTPInterface(Medium* owner, Groupsock* gs);
  49. virtual ~RTPInterface();
  50. Groupsock* gs() const { return fGS; }
  51. void setStreamSocket(int sockNum, unsigned char streamChannelId);
  52. void addStreamSocket(int sockNum, unsigned char streamChannelId);
  53. void removeStreamSocket(int sockNum, unsigned char streamChannelId);
  54. static void setServerRequestAlternativeByteHandler(UsageEnvironment& env, int socketNum,
  55. ServerRequestAlternativeByteHandler* handler, void* clientData);
  56. static void clearServerRequestAlternativeByteHandler(UsageEnvironment& env, int socketNum);
  57. Boolean sendPacket(unsigned char* packet, unsigned packetSize);
  58. void startNetworkReading(TaskScheduler::BackgroundHandlerProc*
  59. handlerProc);
  60. Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
  61. // out parameters:
  62. unsigned& bytesRead, struct sockaddr_in& fromAddress,
  63. int& tcpSocketNum, unsigned char& tcpStreamChannelId,
  64. Boolean& packetReadWasIncomplete);
  65. // Note: If "tcpSocketNum" < 0, then the packet was received over UDP, and "tcpStreamChannelId"
  66. // is undefined (and irrelevant).
  67. // Otherwise (if "tcpSocketNum" >= 0), the packet was received (interleaved) over TCP, and
  68. // "tcpStreamChannelId" will return the channel id.
  69. void stopNetworkReading();
  70. UsageEnvironment& envir() const { return fOwner->envir(); }
  71. void setAuxilliaryReadHandler(AuxHandlerFunc* handlerFunc,
  72. void* handlerClientData) {
  73. fAuxReadHandlerFunc = handlerFunc;
  74. fAuxReadHandlerClientData = handlerClientData;
  75. }
  76. void forgetOurGroupsock() { fGS = NULL; }
  77. // This may be called - *only immediately prior* to deleting this - to prevent our destructor
  78. // from turning off background reading on the 'groupsock'. (This is in case the 'groupsock'
  79. // is also being read from elsewhere.)
  80. private:
  81. // Helper functions for sending a RTP or RTCP packet over a TCP connection:
  82. Boolean sendRTPorRTCPPacketOverTCP(unsigned char* packet, unsigned packetSize,
  83. int socketNum, unsigned char streamChannelId);
  84. Boolean sendDataOverTCP(int socketNum, u_int8_t const* data, unsigned dataSize, Boolean forceSendToSucceed);
  85. private:
  86. friend class SocketDescriptor;
  87. Medium* fOwner;
  88. Groupsock* fGS;
  89. tcpStreamRecord* fTCPStreams; // optional, for RTP-over-TCP streaming/receiving
  90. unsigned short fNextTCPReadSize;
  91. // how much data (if any) is available to be read from the TCP stream
  92. int fNextTCPReadStreamSocketNum;
  93. unsigned char fNextTCPReadStreamChannelId;
  94. TaskScheduler::BackgroundHandlerProc* fReadHandlerProc; // if any
  95. AuxHandlerFunc* fAuxReadHandlerFunc;
  96. void* fAuxReadHandlerClientData;
  97. };
  98. #endif