testProgs/sapWatch.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // Copyright (c) 1996-2012, Live Networks, Inc.  All rights reserved
00017 // A program that receives and prints SDP/SAP announcements
00018 // (on the default SDP/SAP directory: 224.2.127.254/9875)
00019 
00020 #include "Groupsock.hh"
00021 #include "GroupsockHelper.hh"
00022 #include "BasicUsageEnvironment.hh"
00023 #include <stdio.h>
00024 
00025 static unsigned const maxPacketSize = 65536;
00026 static unsigned char packet[maxPacketSize+1];
00027 
00028 int main(int argc, char** argv) {
00029   // Begin by setting up our usage environment:
00030   TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00031   UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
00032 
00033 
00034   // Create a 'groupsock' for the input multicast group,port:
00035   char const* sessionAddressStr = "224.2.127.254";
00036   struct in_addr sessionAddress;
00037   sessionAddress.s_addr = our_inet_addr(sessionAddressStr);
00038 
00039   const Port port(9875);
00040   const unsigned char ttl = 0; // we're only reading from this mcast group
00041 
00042   Groupsock inputGroupsock(*env, sessionAddress, port, ttl);
00043 
00044   // Start reading and printing incoming packets
00045   // (Because this is the only thing we do, we can just do this
00046   // synchronously, in a loop, so we don't need to set up an asynchronous
00047   // event handler like we do in most of the other test programs.)
00048   unsigned packetSize;
00049   struct sockaddr_in fromAddress;
00050   while (inputGroupsock.handleRead(packet, maxPacketSize,
00051                                    packetSize, fromAddress)) {
00052     printf("\n[packet from %s (%d bytes)]\n", AddressString(fromAddress).val(), packetSize);
00053 
00054     // Ignore the first 8 bytes (SAP header).
00055     if (packetSize < 8) {
00056       *env << "Ignoring short packet from " << AddressString(fromAddress).val() << "%s!\n";
00057       continue;
00058     }
00059 
00060     // convert "application/sdp\0" -> "application/sdp\0x20"
00061     // or all other nonprintable characters to blank, except new line
00062     unsigned idx = 8;
00063     while (idx < packetSize) {
00064       if (packet[idx] < 0x20 && packet[idx] != '\n') packet[idx] = 0x20;
00065       idx++;
00066     }
00067 
00068     packet[packetSize] = '\0'; // just in case
00069     printf((char*)(packet+8));
00070   }
00071 
00072   return 0; // only to prevent compiler warning
00073 }

Generated on Thu May 17 07:11:47 2012 for live by  doxygen 1.5.2