You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.5KB

  1. #ifndef STK_INETWVIN_H
  2. #define STK_INETWVIN_H
  3. #include "WvIn.h"
  4. #include "TcpServer.h"
  5. #include "UdpSocket.h"
  6. #include "Thread.h"
  7. #include "Mutex.h"
  8. namespace stk {
  9. /***************************************************/
  10. /*! \class InetWvIn
  11. \brief STK internet streaming input class.
  12. This Wvin subclass reads streamed audio data over a network via a
  13. TCP or UDP socket connection. The data is assumed in big-endian,
  14. or network, byte order. Only a single socket connection is
  15. supported.
  16. InetWvIn supports multi-channel data. It is important to
  17. distinguish the tick() method that computes a single frame (and
  18. returns only the specified sample of a multi-channel frame) from
  19. the overloaded one that takes an StkFrames object for
  20. multi-channel and/or multi-frame data.
  21. This class implements a socket server. When using the TCP
  22. protocol, the server "listens" for a single remote connection
  23. within the InetWvIn::start() function. For the UDP protocol, no
  24. attempt is made to verify packet delivery or order. The default
  25. data type for the incoming stream is signed 16-bit integers,
  26. though any of the defined StkFormats are permissible.
  27. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  28. */
  29. /***************************************************/
  30. typedef struct {
  31. bool finished;
  32. void *object;
  33. } ThreadInfo;
  34. class InetWvIn : public WvIn
  35. {
  36. public:
  37. //! Default constructor.
  38. /*!
  39. An StkError will be thrown if an error occurs while initializing the input thread.
  40. */
  41. InetWvIn( unsigned long bufferFrames = 1024, unsigned int nBuffers = 8 );
  42. //! Class destructor.
  43. ~InetWvIn();
  44. //! Wait for a (new) socket connection with specified protocol, port, data channels and format.
  45. /*!
  46. For the UDP protocol, this function will create a socket
  47. instance and return. For the TCP protocol, this function will
  48. block until a connection is established. An StkError will be
  49. thrown if a socket error occurs or an invalid function argument is
  50. provided.
  51. */
  52. void listen( int port = 2006, unsigned int nChannels = 1,
  53. Stk::StkFormat format = STK_SINT16,
  54. Socket::ProtocolType protocol = Socket::PROTO_TCP );
  55. //! Returns true is an input connection exists or input data remains in the queue.
  56. /*!
  57. This method will not return false after an input connection has been closed until
  58. all buffered input data has been read out.
  59. */
  60. bool isConnected( void );
  61. //! Return the specified channel value of the last computed frame.
  62. /*!
  63. For multi-channel files, use the lastFrame() function to get
  64. all values from the last computed frame. If no connection exists,
  65. the returned value is 0.0. The \c channel argument must be less
  66. than the number of channels in the data stream (the first channel
  67. is specified by 0). However, range checking is only performed if
  68. _STK_DEBUG_ is defined during compilation, in which case an
  69. out-of-range value will trigger an StkError exception.
  70. */
  71. StkFloat lastOut( unsigned int channel = 0 );
  72. //! Compute a sample frame and return the specified \c channel value.
  73. /*!
  74. For multi-channel files, use the lastFrame() function to get
  75. all values from the computed frame. If no connection exists, the
  76. returned value is 0.0 (and a warning will be issued if _STK_DEBUG_
  77. is defined during compilation). The \c channel argument must be
  78. less than the number of channels in the data stream (the first
  79. channel is specified by 0). However, range checking is only
  80. performed if _STK_DEBUG_ is defined during compilation, in which
  81. case an out-of-range value will trigger an StkError exception.
  82. */
  83. StkFloat tick( unsigned int channel = 0 );
  84. //! Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.
  85. /*!
  86. The \c channel argument plus the number of channels specified
  87. in the listen() function must be less than the number of channels
  88. in the StkFrames argument (the first channel is specified by 0).
  89. However, this is only checked if _STK_DEBUG_ is defined during
  90. compilation, in which case an incompatibility will trigger an
  91. StkError exception. If no connection exists, the function does
  92. nothing (a warning will be issued if _STK_DEBUG_ is defined during
  93. compilation).
  94. */
  95. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  96. // Called by the thread routine to receive data via the socket connection
  97. // and fill the socket buffer. This is not intended for general use but
  98. // must be public for access from the thread.
  99. void receive( void );
  100. protected:
  101. // Read buffered socket data into the data buffer ... will block if none available.
  102. int readData( void );
  103. Socket *soket_;
  104. Thread thread_;
  105. Mutex mutex_;
  106. char *buffer_;
  107. unsigned long bufferFrames_;
  108. unsigned long bufferBytes_;
  109. unsigned long bytesFilled_;
  110. unsigned int nBuffers_;
  111. unsigned long writePoint_;
  112. unsigned long readPoint_;
  113. long bufferCounter_;
  114. int dataBytes_;
  115. bool connected_;
  116. int fd_;
  117. ThreadInfo threadInfo_;
  118. Stk::StkFormat dataType_;
  119. };
  120. inline StkFloat InetWvIn :: lastOut( unsigned int channel )
  121. {
  122. #if defined(_STK_DEBUG_)
  123. if ( channel >= data_.channels() ) {
  124. oStream_ << "InetWvIn::lastOut(): channel argument and data stream are incompatible!";
  125. handleError( StkError::FUNCTION_ARGUMENT );
  126. }
  127. #endif
  128. // If no connection and we've output all samples in the queue, return.
  129. if ( !connected_ && bytesFilled_ == 0 && bufferCounter_ == 0 ) return 0.0;
  130. return lastFrame_[channel];
  131. }
  132. } // stk namespace
  133. #endif