Untitled

By Buff Wigeon, 11 Years ago, written in Plain Text, viewed 756 times.
URL http://pb.stoleyour.com/view/96867ffd 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. #include "filefinder.h"
  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.         filefinder();
  118.  
  119.         /* Display program banner */
  120.         printf("------------------------------------------------------\n");
  121.         printf(" AudioStreamSrvr - multicast audio server\n");
  122.         printf("------------------------------------------------------\n");
  123.  
  124.         /* Init WinSock */
  125.         nRet = WSAStartup(0x0202, &stWSAData);
  126.         if (nRet) {
  127.                 printf ("WSAStartup failed: %d\r\n", nRet);
  128.                 exit (1);
  129.         }
  130.  
  131.         /* Display current settings */
  132.         printf ("Multicast Address:%s, Port:%d, IP TTL:%d, Interval:%d\n",
  133.                 achMCAddr, nPort, lTTL, nInterval);
  134.  
  135.         /* Get a datagram socket */
  136.         hSocket = socket(AF_INET,
  137.                 SOCK_DGRAM,
  138.                 0);
  139.         if (hSocket == INVALID_SOCKET) {
  140.                 printf ("socket() failed, Err: %d\n", WSAGetLastError());
  141.                 exit(1);
  142.         }
  143.  
  144.         /* Bind the socket
  145.         *
  146.         * NOTE: Normally, we wouldn't need to call bind unless we were
  147.         *  assigning a local port number explicitly (naming the socket),
  148.         *  however Microsoft requires that a socket be bound before it
  149.         *  can join a multicast group with setsockopt() IP_ADD_MEMBERSHIP
  150.         *  (or fails w/ WSAEINVAL).
  151.         */
  152.         stLclAddr.sin_family      = AF_INET;
  153.         stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* any interface */
  154.         stLclAddr.sin_port        = 0;                 /* any port */
  155.         nRet = bind(hSocket,
  156.                 (struct sockaddr*) &stLclAddr,
  157.                 sizeof(stLclAddr));
  158.         if (nRet == SOCKET_ERROR) {
  159.                 printf ("bind() port: %d failed, Err: %d\n", nPort,
  160.                         WSAGetLastError());
  161.         }
  162.  
  163.         /* Join the multicast group
  164.         *
  165.         * NOTE: According to RFC 1112, a sender does not need to join the
  166.         *  group, however Microsoft requires a socket to join a group in
  167.         *  order to use setsockopt() IP_MULTICAST_TTL (or fails with error
  168.         *  WSAEINVAL).
  169.         */
  170.         stMreq.imr_multiaddr.s_addr = inet_addr(achMCAddr);
  171.         stMreq.imr_interface.s_addr = INADDR_ANY;
  172.         nRet = setsockopt(hSocket,
  173.                 IPPROTO_IP,
  174.                 IP_ADD_MEMBERSHIP,
  175.                 (char *)&stMreq,
  176.                 sizeof(stMreq));
  177.         if (nRet == SOCKET_ERROR) {
  178.                 printf (
  179.                         "setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d\n",
  180.                         achMCAddr, WSAGetLastError());
  181.         }
  182.  
  183.         /* Set IP TTL to traverse up to multiple routers */
  184.         nRet = setsockopt(hSocket,
  185.                 IPPROTO_IP,
  186.                 IP_MULTICAST_TTL,
  187.                 (char *)&lTTL,
  188.                 sizeof(lTTL));
  189.         if (nRet == SOCKET_ERROR) {
  190.                 printf ("setsockopt() IP_MULTICAST_TTL failed, Err: %d\n",
  191.                         WSAGetLastError());
  192.         }
  193.  
  194.         /* Disable loopback */
  195.         fFlag = FALSE;
  196.         nRet = setsockopt(hSocket,
  197.                 IPPROTO_IP,
  198.                 IP_MULTICAST_LOOP,
  199.                 (char *)&fFlag,
  200.                 sizeof(fFlag));
  201.         if (nRet == SOCKET_ERROR) {
  202.                 printf ("setsockopt() IP_MULTICAST_LOOP failed, Err: %d\n",
  203.                         WSAGetLastError());
  204.         }
  205.  
  206.         /* Assign our destination address */
  207.         stDstAddr.sin_family =      AF_INET;
  208.         stDstAddr.sin_addr.s_addr = inet_addr(achMCAddr);
  209.         stDstAddr.sin_port =        htons(nPort);
  210.  
  211.         int count = 0;
  212.     // load an audio buffer from a sound file
  213.     sf::SoundBuffer buffer;
  214.         const sf::Int16* pSample;
  215.         sf::Int16 * pSound;
  216.         sf::Int16 * temp;
  217.         size_t sampleCount, sampleRate, channelCount;
  218.     buffer.loadFromFile("test.wav");
  219.  
  220.         sampleRate   = buffer.getSampleRate();
  221.         channelCount = buffer.getChannelCount();
  222.         pSample      = buffer.getSamples();
  223.         sampleCount  = buffer.getSampleCount();
  224.  
  225.  
  226.         pSound = const_cast<sf::Int16*>(pSample);
  227.         temp   = const_cast<sf::Int16*>(pSample);
  228.  
  229.         char * test;
  230.         MyStream stream;
  231.  
  232.         //stream.load(pSound, 250000, sampleRate, channelCount);
  233.     //stream.play();
  234.         //stream.setVolume(10);
  235.  
  236.         Sleep(1000);
  237.     // initialize and play our custom stream
  238.         for (;;) {
  239.                 test = (char *)&temp[count * 30000];
  240.                 printf("%d\n", &temp[count * 30000]);
  241.                 Sleep(10);
  242.                 /*---------------------------------------
  243.  
  244.  
  245.                 Read File
  246.  
  247.  
  248.                 ----------------------------------------*/
  249.                 /* Send the time to our multicast group! */
  250.                 nRet = sendto(hSocket,
  251.                         test,
  252.                         60000,
  253.                         0,
  254.                         (struct sockaddr*)&stDstAddr,
  255.                         sizeof(stDstAddr));
  256.                 if (nRet < 0) {
  257.                         printf ("sendto() failed, Error: %d\n", WSAGetLastError());
  258.                         exit(1);
  259.                 }
  260.                 sf::Int16 * derp = reinterpret_cast<sf::Int16*>(test);
  261.                
  262.                 //stream.add(derp, sampleRate);
  263.                 printf("Sent...\n");
  264.                 count++;
  265.                 //*temp = count*44100; 
  266.                 /* Wait for the specified interval */
  267.                 //Sleep(nInterval*1000);
  268.                 if(count * sampleRate >= sampleCount - (sampleRate*2))
  269.                 {
  270.                         while(true)
  271.                         {
  272.  
  273.                         }
  274.                         count = 0;
  275.                         Sleep(1000);
  276.                         printf("count reset");
  277.                 }
  278.  
  279.         } /* end for(;;) */
  280.  
  281.         /* Close the socket */
  282.         closesocket(hSocket);
  283.  
  284.         /* Tell WinSock we're leaving */
  285.         WSACleanup();
  286.  
  287.         return (0);
  288. } /* end main() */

Reply to "Untitled"

Here you can reply to the paste above