liveMedia/MP3HTTPSource.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 // "liveMedia"
00017 // Copyright (c) 1996-2008 Live Networks, Inc.  All rights reserved.
00018 // MP3 HTTP Sources
00019 // Implementation
00020 
00021 #include "MP3HTTPSource.hh"
00022 #include "GroupsockHelper.hh"
00023 #include "MP3StreamState.hh"
00024 
00025 MP3HTTPSource* MP3HTTPSource::createNew(UsageEnvironment& env,
00026                                         NetAddress const& remoteAddress,
00027                                         Port remotePort,
00028                                         char const* remoteHostName,
00029                                         char const* fileName) {
00030   int ourSocket = -1;
00031   MP3HTTPSource* newSource = NULL;
00032 
00033   do {
00034     // Create a stream socket for this source.
00035     // Note: We don't make the socket non-blocking, because we want
00036     // to read from it synchronously (as we do with real file sources)
00037     ourSocket = setupStreamSocket(env, 0, False);
00038     if (ourSocket < 0) break;
00039 
00040     // Connect to the remote endpoint:
00041     MAKE_SOCKADDR_IN(remoteName, *(unsigned*)(remoteAddress.data()), remotePort.num());
00042     if (connect(ourSocket, (struct sockaddr*)&remoteName, sizeof remoteName)
00043         != 0) {
00044       env.setResultErrMsg("connect() failed: ");
00045       break;
00046     }
00047 
00048     // Make sure we have a big receive buffer:
00049     if (!increaseReceiveBufferTo(env, ourSocket, 100*1024)) break;
00050 
00051     // Try to make the new socket into a FILE*:
00052     unsigned streamLength = 0; //#####
00053     FILE* fid = NULL;
00054 #if !defined(IMN_PIM) && !defined(CRIS) && !defined(_WIN32_WCE)
00055     fid = fdopen(ourSocket, "r+b");
00056 #endif
00057     if (fid == NULL) {
00058       // HACK HACK HACK #####
00059       // We couldn't convert the socket to a FILE*; perhaps this is Windoze?
00060       // Instead, tell the low-level to read it directly as a socket:
00061       long ourSocket_long = (long)ourSocket;
00062       fid = (FILE*)ourSocket_long;
00063       streamLength = (unsigned)(-1);
00064     }
00065 
00066     newSource = new MP3HTTPSource(env, fid);
00067     if (newSource == NULL) break;
00068 
00069     newSource->assignStream(fid, streamLength);
00070 
00071     // Write the HTTP 'GET' command:
00072     newSource->writeGetCmd(remoteHostName, ntohs(remotePort.num()),
00073                            fileName);
00074 
00075     // Now read the first frame header, to finish initializing the stream:
00076     if (!newSource->initializeStream()) break;
00077 
00078     return newSource;
00079   } while (0);
00080 
00081   if (ourSocket != -1) ::closeSocket(ourSocket);
00082   Medium::close(newSource);
00083   return NULL;
00084 }
00085 
00086 MP3HTTPSource::MP3HTTPSource(UsageEnvironment& env, FILE* fid)
00087   : MP3FileSource(env, fid) {
00088 }
00089 
00090 MP3HTTPSource::~MP3HTTPSource() {
00091 }
00092 
00093 void MP3HTTPSource::writeGetCmd(char const* hostName, unsigned portNum,
00094                                 char const* fileName) {
00095   streamState()->writeGetCmd(hostName, portNum, fileName);
00096 }

Generated on Tue Oct 7 15:38:09 2008 for live by  doxygen 1.5.2