liveMedia/HTTPSink.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 // HTTP sinks
00019 // Implementation
00020 
00021 #include "HTTPSink.hh"
00022 #include "GroupsockHelper.hh"
00023 
00024 #include <string.h>
00025 #if defined(__WIN32__) || defined(_WIN32)
00026 #define snprintf _snprintf
00027 #endif
00028 
00030 
00031 HTTPSink* HTTPSink::createNew(UsageEnvironment& env, Port ourPort) {
00032   int ourSocket = -1;
00033 
00034   do {
00035     int ourSocket = setUpOurSocket(env, ourPort);
00036     if (ourSocket == -1) break;
00037 
00038     HTTPSink* newSink = new HTTPSink(env, ourSocket);
00039     if (newSink == NULL) break;
00040 
00041     appendPortNum(env, ourPort);
00042 
00043     return newSink;
00044   } while (0);
00045 
00046   if (ourSocket != -1) ::closeSocket(ourSocket);
00047   return NULL;
00048 }
00049 
00050 int HTTPSink::setUpOurSocket(UsageEnvironment& env, Port& ourPort) {
00051   int ourSocket = -1;
00052 
00053   do {
00054     ourSocket = setupStreamSocket(env, ourPort);
00055     if (ourSocket < 0) break;
00056 
00057     // Make sure we have a big send buffer:
00058     if (!increaseSendBufferTo(env, ourSocket, 50*1024)) break;
00059 
00060     if (listen(ourSocket, 1) < 0) { // we allow only one connection
00061       env.setResultErrMsg("listen() failed: ");
00062       break;
00063     }
00064 
00065     if (ourPort.num() == 0) {
00066       // bind() will have chosen a port for us; return it also:
00067       if (!getSourcePort(env, ourSocket, ourPort)) break;
00068     }
00069 
00070     return ourSocket;
00071   } while (0);
00072 
00073   if (ourSocket != -1) ::closeSocket(ourSocket);
00074   return -1;
00075 }
00076 
00077 void HTTPSink::appendPortNum(UsageEnvironment& env,
00078                              Port const& port) {
00079   char tmpBuf[10]; // large enough to hold a port # string
00080   sprintf(tmpBuf, " %d", ntohs(port.num()));
00081   env.appendToResultMsg(tmpBuf);
00082 }
00083 
00084 
00085 HTTPSink::HTTPSink(UsageEnvironment& env, int ourSocket)
00086   : MediaSink(env), fSocket(ourSocket), fClientSocket(-1) {
00087 }
00088 
00089 HTTPSink::~HTTPSink() {
00090   ::closeSocket(fSocket);
00091 }
00092 
00093  Boolean HTTPSink::isUseableFrame(unsigned char* /*framePtr*/,
00094                                   unsigned /*frameSize*/) {
00095   // default implementation
00096   return True;
00097 }
00098 
00099 Boolean HTTPSink::continuePlaying() {
00100   if (fSource == NULL) return False;
00101 
00102   if (fClientSocket < 0) {
00103     // We're still waiting for a connection from a client.
00104     // Try making one now.  (Recall that we're non-blocking)
00105     struct sockaddr_in clientAddr;
00106     SOCKLEN_T clientAddrLen = sizeof clientAddr;
00107     fClientSocket = accept(fSocket, (struct sockaddr*)&clientAddr,
00108                            &clientAddrLen);
00109     if (fClientSocket < 0) {
00110       int err = envir().getErrno();
00111       if (err != EWOULDBLOCK) {
00112         envir().setResultErrMsg("accept() failed: ");
00113         return False;
00114       }
00115     } else {
00116       // We made a connection; so send back a HTTP "OK", followed by other
00117       // information (in particular, "Content-Type:") that will make
00118       // client player tools happy:
00119       char okResponse[400];
00120       const char* responseFmt = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Length: 2147483647\r\nContent-Type: %s\r\n\r\n";
00121 #if defined(IRIX) || defined(ALPHA) || defined(_QNX4) || defined(IMN_PIM) || defined(CRIS) || defined (VXWORKS)
00122       /* snprintf() isn't defined, so just use sprintf() - ugh! */
00123       sprintf(okResponse, responseFmt, fSource->MIMEtype());
00124 #else
00125       snprintf(okResponse, sizeof okResponse, responseFmt, fSource->MIMEtype());
00126 #endif
00127       send(fClientSocket, okResponse, strlen(okResponse), 0);
00128     }
00129   }
00130 
00131   fSource->getNextFrame(fBuffer, sizeof fBuffer,
00132                         afterGettingFrame, this,
00133                         ourOnSourceClosure, this);
00134 
00135   return True;
00136 }
00137 
00138 void HTTPSink::ourOnSourceClosure(void* clientData) {
00139   // No more input frames - we're done:
00140   HTTPSink* sink = (HTTPSink*) clientData;
00141   ::closeSocket(sink->fClientSocket);
00142   sink->fClientSocket = -1;
00143   onSourceClosure(sink);
00144 }
00145 
00146 void HTTPSink::afterGettingFrame(void* clientData, unsigned frameSize,
00147                                  unsigned /*numTruncatedBytes*/,
00148                                  struct timeval presentationTime,
00149                                  unsigned /*durationInMicroseconds*/) {
00150   HTTPSink* sink = (HTTPSink*)clientData;
00151   sink->afterGettingFrame1(frameSize, presentationTime);
00152 }
00153 
00154 void HTTPSink::afterGettingFrame1(unsigned frameSize,
00155                                  struct timeval /*presentationTime*/) {
00156   // Write the data back to our client socket (if we have one):
00157   if (fClientSocket >= 0 && isUseableFrame(fBuffer, frameSize)) {
00158     int sendResult
00159       = send(fClientSocket, (char*)(&fBuffer[0]), frameSize, 0);
00160     if (sendResult < 0) {
00161       int err = envir().getErrno();
00162       if (err != EWOULDBLOCK) {
00163         // The client appears to have gone; close him down,
00164         // and consider ourselves done:
00165         ourOnSourceClosure(this);
00166         return;
00167       }
00168     }
00169   }
00170 
00171   // Then try getting the next frame:
00172   continuePlaying();
00173 }

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