RTPInterface.hh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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-2020 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 RTPInterface {
  37. public:
  38. RTPInterface(Medium* owner, Groupsock* gs);
  39. virtual ~RTPInterface();
  40. Groupsock* gs() const { return fGS; }
  41. void setStreamSocket(int sockNum, unsigned char streamChannelId);
  42. void addStreamSocket(int sockNum, unsigned char streamChannelId);
  43. void removeStreamSocket(int sockNum, unsigned char streamChannelId);
  44. static void setServerRequestAlternativeByteHandler(UsageEnvironment& env, int socketNum,
  45. ServerRequestAlternativeByteHandler* handler, void* clientData);
  46. static void clearServerRequestAlternativeByteHandler(UsageEnvironment& env, int socketNum);
  47. Boolean sendPacket(unsigned char* packet, unsigned packetSize);
  48. void startNetworkReading(TaskScheduler::BackgroundHandlerProc*
  49. handlerProc);
  50. Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
  51. // out parameters:
  52. unsigned& bytesRead, struct sockaddr_in& fromAddress,
  53. int& tcpSocketNum, unsigned char& tcpStreamChannelId,
  54. Boolean& packetReadWasIncomplete);
  55. // Note: If "tcpSocketNum" < 0, then the packet was received over UDP, and "tcpStreamChannelId"
  56. // is undefined (and irrelevant).
  57. // Otherwise (if "tcpSocketNum" >= 0), the packet was received (interleaved) over TCP, and
  58. // "tcpStreamChannelId" will return the channel id.
  59. void stopNetworkReading();
  60. UsageEnvironment& envir() const { return fOwner->envir(); }
  61. void setAuxilliaryReadHandler(AuxHandlerFunc* handlerFunc,
  62. void* handlerClientData) {
  63. fAuxReadHandlerFunc = handlerFunc;
  64. fAuxReadHandlerClientData = handlerClientData;
  65. }
  66. void forgetOurGroupsock() { fGS = NULL; }
  67. // This may be called - *only immediately prior* to deleting this - to prevent our destructor
  68. // from turning off background reading on the 'groupsock'. (This is in case the 'groupsock'
  69. // is also being read from elsewhere.)
  70. private:
  71. // Helper functions for sending a RTP or RTCP packet over a TCP connection:
  72. Boolean sendRTPorRTCPPacketOverTCP(unsigned char* packet, unsigned packetSize,
  73. int socketNum, unsigned char streamChannelId);
  74. Boolean sendDataOverTCP(int socketNum, u_int8_t const* data, unsigned dataSize, Boolean forceSendToSucceed);
  75. private:
  76. friend class SocketDescriptor;
  77. Medium* fOwner;
  78. Groupsock* fGS;
  79. class tcpStreamRecord* fTCPStreams; // optional, for RTP-over-TCP streaming/receiving
  80. unsigned short fNextTCPReadSize;
  81. // how much data (if any) is available to be read from the TCP stream
  82. int fNextTCPReadStreamSocketNum;
  83. unsigned char fNextTCPReadStreamChannelId;
  84. TaskScheduler::BackgroundHandlerProc* fReadHandlerProc; // if any
  85. AuxHandlerFunc* fAuxReadHandlerFunc;
  86. void* fAuxReadHandlerClientData;
  87. };
  88. #endif