Untitled

By Soiled Agouti, 11 Years ago, written in C++, viewed 739 times.
URL http://pb.stoleyour.com/view/d5c4d1fb Embed
Download Paste or View RawExpand paste to full width of browser
  1. /*
  2. * TimeCastSrvr.c              (c) Bob Quinn              3/15/97
  3. *
  4. * Released to the public domain
  5. *
  6. * Description:
  7. *  Sample multicast server (sender) application that multicasts the
  8. *  system time for clients (receivers) to use to set their clocks.
  9. */
  10. #include <winsock2.h>
  11. #include <ws2tcpip.h>
  12. #include <stdio.h>
  13. #include <SFML/Audio.hpp>
  14.  
  15.  
  16. #define BUFSIZE     1024
  17. #define MAXADDRSTR  16
  18.  
  19. #define TIMECAST_ADDR   "234.5.6.7"
  20. #define TIMECAST_PORT   8910
  21. #define TIMECAST_TTL    2
  22. #define TIMECAST_INTRVL 0
  23.  
  24. char achMCAddr[MAXADDRSTR] = TIMECAST_ADDR;
  25. u_long lMCAddr;
  26. u_short nPort              = TIMECAST_PORT;
  27. u_long  lTTL               = TIMECAST_TTL;
  28. u_short nInterval          = TIMECAST_INTRVL;
  29. SYSTEMTIME stSysTime;
  30.  
  31. class MyStream : public sf::SoundStream
  32. {
  33. public:
  34.  
  35.        
  36.     std::vector<sf::Int16> m_samples;
  37.     std::size_t m_currentSample;
  38.         HANDLE sem;
  39.  
  40.     void load(sf::Int16* buffer, size_t sampleCount, size_t sampleRate, size_t channelCount)
  41.     {
  42.                 sem = CreateSemaphore(NULL, 1, 1, NULL);
  43.  
  44.         // extract the audio samples from the sound buffer to our own container
  45.         m_samples.assign(buffer, buffer + sampleCount);
  46.  
  47.         // reset the current playing position
  48.         m_currentSample = 0;
  49.  
  50.         // initialize the base class
  51.         initialize(channelCount, sampleRate);
  52.     }
  53.  
  54.         void add(sf::Int16* buffer, size_t size)
  55.         {
  56.                 // check semaphore
  57.                 WaitForSingleObject(sem, INFINITE);
  58.  
  59.                 for (int i = 0; i < size; i++)
  60.                 {
  61.                         m_samples.push_back(buffer[i]);
  62.                 }
  63.                 ReleaseSemaphore(sem, 1, NULL);
  64.         }
  65.  
  66. private:
  67.  
  68.     virtual bool onGetData(Chunk& data)
  69.         {
  70.                 // number of samples to stream every time the function is called;
  71.                 // in a more robust implementation, it would rather be a fixed
  72.                 // amount of time rather than an arbitrary number of samples
  73.                 const int samplesToStream = 50000;
  74.  
  75.                 //check semaphore
  76.                 WaitForSingleObject(sem, INFINITE);
  77.  
  78.                 // set the pointer to the next audio samples to be played
  79.                 data.samples = &m_samples[m_currentSample];
  80.  
  81.                 // have we reached the end of the sound?
  82.                 if (m_currentSample + samplesToStream <= m_samples.size())
  83.                 {
  84.                         // end not reached: stream the samples and continue
  85.                         data.sampleCount = samplesToStream;
  86.                         m_currentSample += samplesToStream;
  87.                         ReleaseSemaphore(sem, 1, NULL);
  88.                         return true;
  89.                 }
  90.                 else
  91.                 {
  92.                         // end of stream reached: stream the remaining samples and stop playback
  93.                         data.sampleCount = m_samples.size() - m_currentSample;
  94.                         m_currentSample = m_samples.size();
  95.                         ReleaseSemaphore(sem, 1, NULL);
  96.                         return false;
  97.                 }
  98.  
  99.         }
  100.  
  101.     virtual void onSeek(sf::Time timeOffset)
  102.     {
  103.         // compute the corresponding sample index according to the sample rate and channel count
  104.         m_currentSample = static_cast<std::size_t>(timeOffset.asSeconds() * getSampleRate() * getChannelCount());
  105.     }
  106.  
  107. };
  108.  
  109. int main(int argc, char *argv[]) {
  110.         int nRet, i;
  111.         BOOL  fFlag;
  112.         SOCKADDR_IN stLclAddr, stDstAddr;
  113.         struct ip_mreq stMreq;        /* Multicast interface structure */
  114.         SOCKET hSocket;
  115.         WSADATA stWSAData;
  116.  
  117.         /* Display program banner */
  118.         printf("------------------------------------------------------\n");
  119.         printf(" AudioStreamSrvr - multicast audio server\n");
  120.         printf("------------------------------------------------------\n");
  121.  
  122.         /* Init WinSock */
  123.         nRet = WSAStartup(0x0202, &stWSAData);
  124.         if (nRet) {
  125.                 printf ("WSAStartup failed: %d\r\n", nRet);
  126.                 exit (1);
  127.         }
  128.  
  129.         /* Display current settings */
  130.         printf ("Multicast Address:%s, Port:%d, IP TTL:%d, Interval:%d\n",
  131.                 achMCAddr, nPort, lTTL, nInterval);
  132.  
  133.         /* Get a datagram socket */
  134.         hSocket = socket(AF_INET,
  135.                 SOCK_DGRAM,
  136.                 0);
  137.         if (hSocket == INVALID_SOCKET) {
  138.                 printf ("socket() failed, Err: %d\n", WSAGetLastError());
  139.                 exit(1);
  140.         }
  141.  
  142.         /* Bind the socket
  143.         *
  144.         * NOTE: Normally, we wouldn't need to call bind unless we were
  145.         *  assigning a local port number explicitly (naming the socket),
  146.         *  however Microsoft requires that a socket be bound before it
  147.         *  can join a multicast group with setsockopt() IP_ADD_MEMBERSHIP
  148.         *  (or fails w/ WSAEINVAL).
  149.         */
  150.         stLclAddr.sin_family      = AF_INET;
  151.         stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* any interface */
  152.         stLclAddr.sin_port        = 0;                 /* any port */
  153.         nRet = bind(hSocket,
  154.                 (struct sockaddr*) &stLclAddr,
  155.                 sizeof(stLclAddr));
  156.         if (nRet == SOCKET_ERROR) {
  157.                 printf ("bind() port: %d failed, Err: %d\n", nPort,
  158.                         WSAGetLastError());
  159.         }
  160.  
  161.         /* Join the multicast group
  162.         *
  163.         * NOTE: According to RFC 1112, a sender does not need to join the
  164.         *  group, however Microsoft requires a socket to join a group in
  165.         *  order to use setsockopt() IP_MULTICAST_TTL (or fails with error
  166.         *  WSAEINVAL).
  167.         */
  168.         stMreq.imr_multiaddr.s_addr = inet_addr(achMCAddr);
  169.         stMreq.imr_interface.s_addr = INADDR_ANY;
  170.         nRet = setsockopt(hSocket,
  171.                 IPPROTO_IP,
  172.                 IP_ADD_MEMBERSHIP,
  173.                 (char *)&stMreq,
  174.                 sizeof(stMreq));
  175.         if (nRet == SOCKET_ERROR) {
  176.                 printf (
  177.                         "setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d\n",
  178.                         achMCAddr, WSAGetLastError());
  179.         }
  180.  
  181.         /* Set IP TTL to traverse up to multiple routers */
  182.         nRet = setsockopt(hSocket,
  183.                 IPPROTO_IP,
  184.                 IP_MULTICAST_TTL,
  185.                 (char *)&lTTL,
  186.                 sizeof(lTTL));
  187.         if (nRet == SOCKET_ERROR) {
  188.                 printf ("setsockopt() IP_MULTICAST_TTL failed, Err: %d\n",
  189.                         WSAGetLastError());
  190.         }
  191.  
  192.         /* Disable loopback */
  193.         fFlag = FALSE;
  194.         nRet = setsockopt(hSocket,
  195.                 IPPROTO_IP,
  196.                 IP_MULTICAST_LOOP,
  197.                 (char *)&fFlag,
  198.                 sizeof(fFlag));
  199.         if (nRet == SOCKET_ERROR) {
  200.                 printf ("setsockopt() IP_MULTICAST_LOOP failed, Err: %d\n",
  201.                         WSAGetLastError());
  202.         }
  203.  
  204.         /* Assign our destination address */
  205.         stDstAddr.sin_family =      AF_INET;
  206.         stDstAddr.sin_addr.s_addr = inet_addr(achMCAddr);
  207.         stDstAddr.sin_port =        htons(nPort);
  208.  
  209.         int count = 0;
  210.     // load an audio buffer from a sound file
  211.     sf::SoundBuffer buffer;
  212.         const sf::Int16* pSample;
  213.         sf::Int16 * pSound;
  214.         sf::Int16 * temp;
  215.         size_t sampleCount, sampleRate, channelCount;
  216.     buffer.loadFromFile("test.wav");
  217.  
  218.         sampleRate   = buffer.getSampleRate();
  219.         channelCount = buffer.getChannelCount();
  220.         pSample      = buffer.getSamples();
  221.         sampleCount  = buffer.getSampleCount();
  222.  
  223.  
  224.         pSound = const_cast<sf::Int16*>(pSample);
  225.         temp   = const_cast<sf::Int16*>(pSample);
  226.  
  227.         char * test;
  228.         MyStream stream;
  229.  
  230.         //stream.load(pSound, 250000, sampleRate, channelCount);
  231.    // stream.play();
  232.         //stream.setVolume(10);
  233.  
  234.         Sleep(1000);
  235.     // initialize and play our custom stream
  236.         for (;;) {
  237.                 test = (char *)&temp[count * sampleRate];
  238.                 printf("%d\n", &temp[count * sampleRate]);
  239.                 Sleep(100);
  240.                 /*---------------------------------------
  241.  
  242.  
  243.                 Read File
  244.  
  245.  
  246.                 ----------------------------------------*/
  247.                 /* Send the time to our multicast group! */
  248.                 nRet = sendto(hSocket,
  249.                         test,
  250.                         sizeof(sampleRate),
  251.                         0,
  252.                         (struct sockaddr*)&stDstAddr,
  253.                         sizeof(stDstAddr));
  254.                 if (nRet < 0) {
  255.                         printf ("sendto() failed, Error: %d\n", WSAGetLastError());
  256.                         exit(1);
  257.                 }
  258.                 //sf::Int16 * derp = reinterpret_cast<sf::Int16*>(test);
  259.                
  260.                 //stream.add(derp, sampleRate);
  261.                 printf("Sent...\n");
  262.                 count++;
  263.                 //*temp = count*44100; 
  264.                 /* Wait for the specified interval */
  265.                 //Sleep(nInterval*1000);
  266.                 if(count * sampleRate >= sampleCount)
  267.                 {
  268.                         count = 0;
  269.                         Sleep(1000);
  270.                         printf("count reset");
  271.                 }
  272.  
  273.         } /* end for(;;) */
  274.  
  275.         /* Close the socket */
  276.         closesocket(hSocket);
  277.  
  278.         /* Tell WinSock we're leaving */
  279.         WSACleanup();
  280.  
  281.         return (0);
  282. } /* end main() */

Reply to "Untitled"

Here you can reply to the paste above