Made src directory structure more sane - all binaries are now built in src but sources are divided amongst subdirs of src
This commit is contained in:
		
							parent
							
								
									3b98ac6547
								
							
						
					
					
						commit
						1762ae9724
					
				
					 29 changed files with 40 additions and 42 deletions
				
			
		
							
								
								
									
										510
									
								
								src/connectors/conn_http.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										510
									
								
								src/connectors/conn_http.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,510 @@
 | 
			
		|||
/// \file conn_http.cpp
 | 
			
		||||
/// Contains the main code for the HTTP Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/http_parser.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
#include <mist/auth.h>
 | 
			
		||||
#include "tinythread.h"
 | 
			
		||||
#include "embed.js.h"
 | 
			
		||||
 | 
			
		||||
/// Holds everything unique to HTTP Connector.
 | 
			
		||||
namespace Connector_HTTP {
 | 
			
		||||
 | 
			
		||||
  /// Class for keeping track of connections to connectors.
 | 
			
		||||
  class ConnConn{
 | 
			
		||||
    public:
 | 
			
		||||
      Socket::Connection * conn; ///< The socket of this connection
 | 
			
		||||
      unsigned int lastuse; ///< Seconds since last use of this connection.
 | 
			
		||||
      tthread::mutex in_use; ///< Mutex for this connection.
 | 
			
		||||
      /// Constructor that sets the socket and lastuse to 0.
 | 
			
		||||
      ConnConn(){
 | 
			
		||||
        conn = 0;
 | 
			
		||||
        lastuse = 0;
 | 
			
		||||
      }
 | 
			
		||||
      ;
 | 
			
		||||
      /// Constructor that sets lastuse to 0, but socket to s.
 | 
			
		||||
      ConnConn(Socket::Connection * s){
 | 
			
		||||
        conn = s;
 | 
			
		||||
        lastuse = 0;
 | 
			
		||||
      }
 | 
			
		||||
      ;
 | 
			
		||||
      /// Destructor that deletes the socket if non-null.
 | 
			
		||||
      ~ConnConn(){
 | 
			
		||||
        if (conn){
 | 
			
		||||
          conn->close();
 | 
			
		||||
          delete conn;
 | 
			
		||||
        }
 | 
			
		||||
        conn = 0;
 | 
			
		||||
      }
 | 
			
		||||
      ;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  std::map<std::string, ConnConn *> connconn; ///< Connections to connectors
 | 
			
		||||
  std::set<tthread::thread *> active_threads; ///< Holds currently active threads
 | 
			
		||||
  std::set<tthread::thread *> done_threads; ///< Holds threads that are done and ready to be joined.
 | 
			
		||||
  tthread::mutex thread_mutex; ///< Mutex for adding/removing threads.
 | 
			
		||||
  tthread::mutex conn_mutex; ///< Mutex for adding/removing connector connections.
 | 
			
		||||
  tthread::mutex timeout_mutex; ///< Mutex for timeout thread.
 | 
			
		||||
  tthread::thread * timeouter = 0; ///< Thread that times out connections to connectors.
 | 
			
		||||
 | 
			
		||||
  void Timeout_Thread(void * n){
 | 
			
		||||
    n = 0; //prevent unused variable warning
 | 
			
		||||
    tthread::lock_guard<tthread::mutex> guard(timeout_mutex);
 | 
			
		||||
    while (true){
 | 
			
		||||
      {
 | 
			
		||||
        tthread::lock_guard<tthread::mutex> guard(conn_mutex);
 | 
			
		||||
        if (connconn.empty()){
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        std::map<std::string, ConnConn*>::iterator it;
 | 
			
		||||
        for (it = connconn.begin(); it != connconn.end(); it++){
 | 
			
		||||
          if ( !it->second->conn->connected() || it->second->lastuse++ > 15){
 | 
			
		||||
            if (it->second->in_use.try_lock()){
 | 
			
		||||
              it->second->in_use.unlock();
 | 
			
		||||
              delete it->second;
 | 
			
		||||
              connconn.erase(it);
 | 
			
		||||
              it = connconn.begin(); //get a valid iterator
 | 
			
		||||
              if (it == connconn.end()){
 | 
			
		||||
                return;
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        conn_mutex.unlock();
 | 
			
		||||
      }
 | 
			
		||||
      usleep(1000000); //sleep 1 second and re-check
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Handles requests without associated handler, displaying a nice friendly error message.
 | 
			
		||||
  long long int Handle_None(HTTP::Parser & H, Socket::Connection * conn){
 | 
			
		||||
    H.Clean();
 | 
			
		||||
    H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
    H.SetBody(
 | 
			
		||||
        "<!DOCTYPE html><html><head><title>Unsupported Media Type</title></head><body><h1>Unsupported Media Type</h1>The server isn't quite sure what you wanted to receive from it.</body></html>");
 | 
			
		||||
    long long int ret = Util::getMS();
 | 
			
		||||
    conn->SendNow(H.BuildResponse("415", "Unsupported Media Type"));
 | 
			
		||||
    return ret;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  long long int Handle_Timeout(HTTP::Parser & H, Socket::Connection * conn){
 | 
			
		||||
    H.Clean();
 | 
			
		||||
    H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
    H.SetBody(
 | 
			
		||||
        "<!DOCTYPE html><html><head><title>Gateway timeout</title></head><body><h1>Gateway timeout</h1>Though the server understood your request and attempted to handle it, somehow handling it took longer than it should. Your request has been cancelled - please try again later.</body></html>");
 | 
			
		||||
    long long int ret = Util::getMS();
 | 
			
		||||
    conn->SendNow(H.BuildResponse("504", "Gateway Timeout"));
 | 
			
		||||
    return ret;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Handles internal requests.
 | 
			
		||||
  long long int Handle_Internal(HTTP::Parser & H, Socket::Connection * conn){
 | 
			
		||||
 | 
			
		||||
    std::string url = H.getUrl();
 | 
			
		||||
 | 
			
		||||
    if (url == "/crossdomain.xml"){
 | 
			
		||||
      H.Clean();
 | 
			
		||||
      H.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
      H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
      H.SetBody(
 | 
			
		||||
          "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" /><site-control permitted-cross-domain-policies=\"all\"/></cross-domain-policy>");
 | 
			
		||||
      long long int ret = Util::getMS();
 | 
			
		||||
      conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
      return ret;
 | 
			
		||||
    } //crossdomain.xml
 | 
			
		||||
 | 
			
		||||
    if (url == "/clientaccesspolicy.xml"){
 | 
			
		||||
      H.Clean();
 | 
			
		||||
      H.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
      H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
      H.SetBody(
 | 
			
		||||
          "<?xml version=\"1.0\" encoding=\"utf-8\"?><access-policy><cross-domain-access><policy><allow-from http-methods=\"*\" http-request-headers=\"*\"><domain uri=\"*\"/></allow-from><grant-to><resource path=\"/\" include-subpaths=\"true\"/></grant-to></policy></cross-domain-access></access-policy>");
 | 
			
		||||
      long long int ret = Util::getMS();
 | 
			
		||||
      conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
      return ret;
 | 
			
		||||
    } //clientaccesspolicy.xml
 | 
			
		||||
    
 | 
			
		||||
    // send logo icon
 | 
			
		||||
    if (url.length() > 4 && url.substr(url.length() - 4, 4) == ".ico"){
 | 
			
		||||
      H.Clean();
 | 
			
		||||
#include "icon.h"
 | 
			
		||||
      H.SetHeader("Content-Type", "image/x-icon");
 | 
			
		||||
      H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
      H.SetHeader("Content-Length", icon_len);
 | 
			
		||||
      H.SetBody("");
 | 
			
		||||
      long long int ret = Util::getMS();
 | 
			
		||||
      conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
      conn->SendNow((const char*)icon_data, icon_len);
 | 
			
		||||
      return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((url.length() > 9 && url.substr(0, 6) == "/info_" && url.substr(url.length() - 3, 3) == ".js")
 | 
			
		||||
        || (url.length() > 10 && url.substr(0, 7) == "/embed_" && url.substr(url.length() - 3, 3) == ".js")){
 | 
			
		||||
      std::string streamname;
 | 
			
		||||
      if (url.substr(0, 6) == "/info_"){
 | 
			
		||||
        streamname = url.substr(6, url.length() - 9);
 | 
			
		||||
      }else{
 | 
			
		||||
        streamname = url.substr(7, url.length() - 10);
 | 
			
		||||
      }
 | 
			
		||||
      Util::Stream::sanitizeName(streamname);
 | 
			
		||||
      JSON::Value ServConf = JSON::fromFile("/tmp/mist/streamlist");
 | 
			
		||||
      std::string response;
 | 
			
		||||
      std::string host = H.GetHeader("Host");
 | 
			
		||||
      if (host.find(':')){
 | 
			
		||||
        host.resize(host.find(':'));
 | 
			
		||||
      }
 | 
			
		||||
      H.Clean();
 | 
			
		||||
      H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
      H.SetHeader("Content-Type", "application/javascript");
 | 
			
		||||
      response = "// Generating info code for stream " + streamname + "\n\nif (!mistvideo){var mistvideo = {};}\n";
 | 
			
		||||
      JSON::Value json_resp;
 | 
			
		||||
      if (ServConf["streams"].isMember(streamname) && ServConf["config"]["protocols"].size() > 0){
 | 
			
		||||
        json_resp["width"] = ServConf["streams"][streamname]["meta"]["video"]["width"].asInt();
 | 
			
		||||
        json_resp["height"] = ServConf["streams"][streamname]["meta"]["video"]["height"].asInt();
 | 
			
		||||
        //first, see if we have RTMP working and output all the RTMP.
 | 
			
		||||
        for (JSON::ArrIter it = ServConf["config"]["protocols"].ArrBegin(); it != ServConf["config"]["protocols"].ArrEnd(); it++){
 | 
			
		||||
          if (( *it)["connector"].asString() == "RTMP"){
 | 
			
		||||
            JSON::Value tmp;
 | 
			
		||||
            tmp["type"] = "rtmp";
 | 
			
		||||
            tmp["url"] = "rtmp://" + host + ":" + ( *it)["port"].asString() + "/play/" + streamname;
 | 
			
		||||
            json_resp["source"].append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        //then, see if we have HTTP working and output all the dynamic.
 | 
			
		||||
        for (JSON::ArrIter it = ServConf["config"]["protocols"].ArrBegin(); it != ServConf["config"]["protocols"].ArrEnd(); it++){
 | 
			
		||||
          if (( *it)["connector"].asString() == "HTTP"){
 | 
			
		||||
            JSON::Value tmp;
 | 
			
		||||
            tmp["type"] = "f4v";
 | 
			
		||||
            tmp["url"] = "http://" + host + ":" + ( *it)["port"].asString() + "/" + streamname + "/manifest.f4m";
 | 
			
		||||
            json_resp["source"].append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        //and all the progressive.
 | 
			
		||||
        for (JSON::ArrIter it = ServConf["config"]["protocols"].ArrBegin(); it != ServConf["config"]["protocols"].ArrEnd(); it++){
 | 
			
		||||
          if (( *it)["connector"].asString() == "HTTP"){
 | 
			
		||||
            JSON::Value tmp;
 | 
			
		||||
            tmp["type"] = "flv";
 | 
			
		||||
            tmp["url"] = "http://" + host + ":" + ( *it)["port"].asString() + "/" + streamname + ".flv";
 | 
			
		||||
            json_resp["source"].append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        json_resp["error"] = "The specified stream is not available on this server.";
 | 
			
		||||
        json_resp["bbq"] = "sauce"; //for legacy purposes ^_^
 | 
			
		||||
      }
 | 
			
		||||
      response += "mistvideo['" + streamname + "'] = " + json_resp.toString() + ";\n";
 | 
			
		||||
      if (url.substr(0, 6) != "/info_" && !json_resp.isMember("error")){
 | 
			
		||||
        response.append("\n(");
 | 
			
		||||
        response.append((char*)embed_js, (size_t)embed_js_len - 2); //remove trailing ";\n" from xxd conversion
 | 
			
		||||
        response.append("(\"" + streamname + "\"));\n");
 | 
			
		||||
      }
 | 
			
		||||
      H.SetBody(response);
 | 
			
		||||
      long long int ret = Util::getMS();
 | 
			
		||||
      conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
      return ret;
 | 
			
		||||
    } //embed code generator
 | 
			
		||||
 | 
			
		||||
    return Handle_None(H, conn); //anything else doesn't get handled
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Handles requests without associated handler, displaying a nice friendly error message.
 | 
			
		||||
  long long int Handle_Through_Connector(HTTP::Parser & H, Socket::Connection * conn, std::string & connector){
 | 
			
		||||
    //create a unique ID based on a hash of the user agent and host, followed by the stream name and connector
 | 
			
		||||
    std::string uid = Secure::md5(H.GetHeader("User-Agent") + conn->getHost()) + "_" + H.GetVar("stream") + "_" + connector;
 | 
			
		||||
    H.SetHeader("X-Stream", H.GetVar("stream"));
 | 
			
		||||
    H.SetHeader("X-UID", uid); //add the UID to the headers before copying
 | 
			
		||||
    H.SetHeader("X-Origin", conn->getHost()); //add the UID to the headers before copying
 | 
			
		||||
    std::string request = H.BuildRequest(); //copy the request for later forwarding to the connector
 | 
			
		||||
    std::string orig_url = H.getUrl();
 | 
			
		||||
    H.Clean();
 | 
			
		||||
 | 
			
		||||
    //check if a connection exists, and if not create one
 | 
			
		||||
    conn_mutex.lock();
 | 
			
		||||
    if ( !connconn.count(uid) || !connconn[uid]->conn->connected()){
 | 
			
		||||
      if (connconn.count(uid)){
 | 
			
		||||
        connconn.erase(uid);
 | 
			
		||||
      }
 | 
			
		||||
      connconn[uid] = new ConnConn(new Socket::Connection("/tmp/mist/http_" + connector));
 | 
			
		||||
      connconn[uid]->conn->setBlocking(false); //do not block on spool() with no data
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
      std::cout << "Created new connection " << uid << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    }else{
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
      std::cout << "Re-using connection " << uid << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    //start a new timeout thread, if neccesary
 | 
			
		||||
    if (timeout_mutex.try_lock()){
 | 
			
		||||
      if (timeouter){
 | 
			
		||||
        timeouter->join();
 | 
			
		||||
        delete timeouter;
 | 
			
		||||
      }
 | 
			
		||||
      timeouter = new tthread::thread(Connector_HTTP::Timeout_Thread, 0);
 | 
			
		||||
      timeout_mutex.unlock();
 | 
			
		||||
    }
 | 
			
		||||
    conn_mutex.unlock();
 | 
			
		||||
 | 
			
		||||
    //lock the mutex for this connection, and handle the request
 | 
			
		||||
    tthread::lock_guard<tthread::mutex> guard(connconn[uid]->in_use);
 | 
			
		||||
    //if the server connection is dead, handle as timeout.
 | 
			
		||||
    if ( !connconn.count(uid) || !connconn[uid]->conn->connected()){
 | 
			
		||||
      return Handle_Timeout(H, conn);
 | 
			
		||||
    }
 | 
			
		||||
    //forward the original request
 | 
			
		||||
    connconn[uid]->conn->SendNow(request);
 | 
			
		||||
    connconn[uid]->lastuse = 0;
 | 
			
		||||
    unsigned int timeout = 0;
 | 
			
		||||
    //wait for a response
 | 
			
		||||
    while (connconn.count(uid) && connconn[uid]->conn->connected() && conn->connected()){
 | 
			
		||||
      conn->spool();
 | 
			
		||||
      if (connconn[uid]->conn->Received().size() || connconn[uid]->conn->spool()){
 | 
			
		||||
        //make sure we end in a \n
 | 
			
		||||
        if ( *(connconn[uid]->conn->Received().get().rbegin()) != '\n'){
 | 
			
		||||
          std::string tmp = connconn[uid]->conn->Received().get();
 | 
			
		||||
          connconn[uid]->conn->Received().get().clear();
 | 
			
		||||
          if (connconn[uid]->conn->Received().size()){
 | 
			
		||||
            connconn[uid]->conn->Received().get().insert(0, tmp);
 | 
			
		||||
          }else{
 | 
			
		||||
            connconn[uid]->conn->Received().append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        //check if the whole response was received
 | 
			
		||||
        if (H.Read(connconn[uid]->conn->Received().get())){
 | 
			
		||||
          break; //continue down below this while loop
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        //keep trying unless the timeout triggers
 | 
			
		||||
        if (timeout++ > 4000){
 | 
			
		||||
          std::cout << "[20s timeout triggered]" << std::endl;
 | 
			
		||||
          return Handle_Timeout(H, conn);
 | 
			
		||||
        }else{
 | 
			
		||||
          Util::sleep(5);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if ( !connconn.count(uid) || !connconn[uid]->conn->connected() || !conn->connected()){
 | 
			
		||||
      //failure, disconnect and sent error to user
 | 
			
		||||
      return Handle_Timeout(H, conn);
 | 
			
		||||
    }else{
 | 
			
		||||
      long long int ret = Util::getMS();
 | 
			
		||||
      //success, check type of response
 | 
			
		||||
      if (H.GetHeader("Content-Length") != ""){
 | 
			
		||||
        //known length - simply re-send the request with added headers and continue
 | 
			
		||||
        H.SetHeader("X-UID", uid);
 | 
			
		||||
        H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
        conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
      }else{
 | 
			
		||||
        //unknown length
 | 
			
		||||
        H.SetHeader("X-UID", uid);
 | 
			
		||||
        H.SetHeader("Server", "mistserver/" PACKAGE_VERSION "/" + Util::Config::libver);
 | 
			
		||||
        conn->SendNow(H.BuildResponse("200", "OK"));
 | 
			
		||||
        //switch out the connection for an empty one - it makes no sense to keep these globally
 | 
			
		||||
        Socket::Connection * myConn = connconn[uid]->conn;
 | 
			
		||||
        connconn[uid]->conn = new Socket::Connection();
 | 
			
		||||
        connconn[uid]->in_use.unlock();
 | 
			
		||||
        //continue sending data from this socket and keep it permanently in use
 | 
			
		||||
        while (myConn->connected() && conn->connected()){
 | 
			
		||||
          if (myConn->Received().size() || myConn->spool()){
 | 
			
		||||
            //forward any and all incoming data directly without parsing
 | 
			
		||||
            conn->SendNow(myConn->Received().get());
 | 
			
		||||
            myConn->Received().get().clear();
 | 
			
		||||
          }else{
 | 
			
		||||
            Util::sleep(30);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        myConn->close();
 | 
			
		||||
        delete myConn;
 | 
			
		||||
        conn->close();
 | 
			
		||||
      }
 | 
			
		||||
      return ret;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Returns the name of the HTTP connector the given request should be served by.
 | 
			
		||||
  /// Can currently return:
 | 
			
		||||
  /// - none (request not supported)
 | 
			
		||||
  /// - internal (request fed from information internal to this connector)
 | 
			
		||||
  /// - dynamic (request fed from http_dynamic connector)
 | 
			
		||||
  /// - progressive (request fed from http_progressive connector)
 | 
			
		||||
  std::string getHTTPType(HTTP::Parser & H){
 | 
			
		||||
    std::string url = H.getUrl();
 | 
			
		||||
    if (url.find("/dynamic/") != std::string::npos){
 | 
			
		||||
      std::string streamname = url.substr(9, url.find("/", 9) - 9);
 | 
			
		||||
      Util::Stream::sanitizeName(streamname);
 | 
			
		||||
      H.SetVar("stream", streamname);
 | 
			
		||||
      return "dynamic";
 | 
			
		||||
    }
 | 
			
		||||
    if (url.find("/smooth/") != std::string::npos && url.find(".ism") != std::string::npos){
 | 
			
		||||
      std::string streamname = url.substr(8, url.find("/", 8) - 12);
 | 
			
		||||
      Util::Stream::sanitizeName(streamname);
 | 
			
		||||
      H.SetVar("stream", streamname);
 | 
			
		||||
      return "smooth";
 | 
			
		||||
    }
 | 
			
		||||
    if (url.find("/hls/") != std::string::npos && (url.find(".m3u") != std::string::npos || url.find(".ts") != std::string::npos)){
 | 
			
		||||
      std::string streamname = url.substr(5, url.find("/", 5) - 5);
 | 
			
		||||
      Util::Stream::sanitizeName(streamname);
 | 
			
		||||
      H.SetVar("stream", streamname);
 | 
			
		||||
      return "live";
 | 
			
		||||
    }
 | 
			
		||||
    if (url.length() > 4){
 | 
			
		||||
      std::string ext = url.substr(url.length() - 4, 4);
 | 
			
		||||
      if (ext == ".flv" || ext == ".mp3"){
 | 
			
		||||
        std::string streamname = url.substr(1, url.length() - 5);
 | 
			
		||||
        Util::Stream::sanitizeName(streamname);
 | 
			
		||||
        H.SetVar("stream", streamname);
 | 
			
		||||
        return "progressive";
 | 
			
		||||
      }
 | 
			
		||||
      if (ext == ".ico"){
 | 
			
		||||
        return "internal";
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if (url == "/crossdomain.xml"){
 | 
			
		||||
      return "internal";
 | 
			
		||||
    }
 | 
			
		||||
    if (url == "/clientaccesspolicy.xml"){
 | 
			
		||||
      return "internal";
 | 
			
		||||
    }
 | 
			
		||||
    if (url.length() > 10 && url.substr(0, 7) == "/embed_" && url.substr(url.length() - 3, 3) == ".js"){
 | 
			
		||||
      return "internal";
 | 
			
		||||
    }
 | 
			
		||||
    if (url.length() > 9 && url.substr(0, 6) == "/info_" && url.substr(url.length() - 3, 3) == ".js"){
 | 
			
		||||
      return "internal";
 | 
			
		||||
    }
 | 
			
		||||
    return "none";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Thread for handling a single HTTP connection
 | 
			
		||||
  void Handle_HTTP_Connection(void * pointer){
 | 
			
		||||
    Socket::Connection * conn = (Socket::Connection *)pointer;
 | 
			
		||||
    conn->setBlocking(false); //do not block on conn.spool() when no data is available
 | 
			
		||||
    HTTP::Parser Client;
 | 
			
		||||
    while (conn->connected()){
 | 
			
		||||
      if (conn->spool() || conn->Received().size()){
 | 
			
		||||
        //make sure it ends in a \n
 | 
			
		||||
        if ( *(conn->Received().get().rbegin()) != '\n'){
 | 
			
		||||
          std::string tmp = conn->Received().get();
 | 
			
		||||
          conn->Received().get().clear();
 | 
			
		||||
          if (conn->Received().size()){
 | 
			
		||||
            conn->Received().get().insert(0, tmp);
 | 
			
		||||
          }else{
 | 
			
		||||
            conn->Received().append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (Client.Read(conn->Received().get())){
 | 
			
		||||
          std::string handler = getHTTPType(Client);
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
          std::cout << "Received request: " << Client.getUrl() << " (" << conn->getSocket() << ") => " << handler << " (" << Client.GetVar("stream")
 | 
			
		||||
              << ")" << std::endl;
 | 
			
		||||
          long long int startms = Util::getMS();
 | 
			
		||||
#endif
 | 
			
		||||
          long long int midms = 0;
 | 
			
		||||
          bool closeConnection = false;
 | 
			
		||||
          if (Client.GetHeader("Connection") == "close"){
 | 
			
		||||
            closeConnection = true;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          if (handler == "none" || handler == "internal"){
 | 
			
		||||
            if (handler == "internal"){
 | 
			
		||||
              midms = Handle_Internal(Client, conn);
 | 
			
		||||
            }else{
 | 
			
		||||
              midms = Handle_None(Client, conn);
 | 
			
		||||
            }
 | 
			
		||||
          }else{
 | 
			
		||||
            midms = Handle_Through_Connector(Client, conn, handler);
 | 
			
		||||
          }
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
          long long int nowms = Util::getMS();
 | 
			
		||||
          std::cout << "Completed request " << conn->getSocket() << " " << handler << " in " << (midms - startms) << " ms (processing) / " << (nowms - midms) << " ms (transfer)" << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
          if (closeConnection){
 | 
			
		||||
            break;
 | 
			
		||||
          }
 | 
			
		||||
          Client.Clean(); //clean for any possible next requests
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        Util::sleep(10); //sleep 10ms
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    //close and remove the connection
 | 
			
		||||
    conn->close();
 | 
			
		||||
    delete conn;
 | 
			
		||||
    //remove this thread from active_threads and add it to done_threads.
 | 
			
		||||
    thread_mutex.lock();
 | 
			
		||||
    for (std::set<tthread::thread *>::iterator it = active_threads.begin(); it != active_threads.end(); it++){
 | 
			
		||||
      if (( *it)->get_id() == tthread::this_thread::get_id()){
 | 
			
		||||
        tthread::thread * T = ( *it);
 | 
			
		||||
        active_threads.erase(T);
 | 
			
		||||
        done_threads.insert(T);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    thread_mutex.unlock();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
} //Connector_HTTP namespace
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(8080);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server(conf.getInteger("listen_port"), conf.getString("listen_interface"));
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      //lock the thread mutex and spawn a new thread for this connection
 | 
			
		||||
      Connector_HTTP::thread_mutex.lock();
 | 
			
		||||
      tthread::thread * T = new tthread::thread(Connector_HTTP::Handle_HTTP_Connection, (void *)(new Socket::Connection(S)));
 | 
			
		||||
      Connector_HTTP::active_threads.insert(T);
 | 
			
		||||
      //clean up any threads that may have finished
 | 
			
		||||
      while ( !Connector_HTTP::done_threads.empty()){
 | 
			
		||||
        T = *Connector_HTTP::done_threads.begin();
 | 
			
		||||
        T->join();
 | 
			
		||||
        Connector_HTTP::done_threads.erase(T);
 | 
			
		||||
        delete T;
 | 
			
		||||
      }
 | 
			
		||||
      Connector_HTTP::thread_mutex.unlock();
 | 
			
		||||
    }else{
 | 
			
		||||
      Util::sleep(10); //sleep 10ms
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected and not requested to stop
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
 | 
			
		||||
  //wait for existing connections to drop
 | 
			
		||||
  bool repeat = true;
 | 
			
		||||
  while (repeat){
 | 
			
		||||
    Connector_HTTP::thread_mutex.lock();
 | 
			
		||||
    repeat = !Connector_HTTP::active_threads.empty();
 | 
			
		||||
    //clean up any threads that may have finished
 | 
			
		||||
    while ( !Connector_HTTP::done_threads.empty()){
 | 
			
		||||
      tthread::thread * T = *Connector_HTTP::done_threads.begin();
 | 
			
		||||
      T->join();
 | 
			
		||||
      Connector_HTTP::done_threads.erase(T);
 | 
			
		||||
      delete T;
 | 
			
		||||
    }
 | 
			
		||||
    Connector_HTTP::thread_mutex.unlock();
 | 
			
		||||
    if (repeat){
 | 
			
		||||
      Util::sleep(100); //sleep 100ms
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										455
									
								
								src/connectors/conn_http_dynamic.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										455
									
								
								src/connectors/conn_http_dynamic.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,455 @@
 | 
			
		|||
/// \file conn_http_dynamic.cpp
 | 
			
		||||
/// Contains the main code for the HTTP Dynamic Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/http_parser.h>
 | 
			
		||||
#include <mist/json.h>
 | 
			
		||||
#include <mist/dtsc.h>
 | 
			
		||||
#include <mist/flv_tag.h>
 | 
			
		||||
#include <mist/base64.h>
 | 
			
		||||
#include <mist/amf.h>
 | 
			
		||||
#include <mist/mp4.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
 | 
			
		||||
/// Holds everything unique to HTTP Dynamic Connector.
 | 
			
		||||
namespace Connector_HTTP {
 | 
			
		||||
 | 
			
		||||
  std::string GenerateBootstrap(std::string & MovieId, JSON::Value & metadata, int fragnum, int starttime, int endtime){
 | 
			
		||||
    std::string empty;
 | 
			
		||||
 | 
			
		||||
    MP4::ASRT asrt;
 | 
			
		||||
    if (starttime == 0 && metadata.isMember("vod")){
 | 
			
		||||
      asrt.setUpdate(false);
 | 
			
		||||
    }else{
 | 
			
		||||
      asrt.setUpdate(true);
 | 
			
		||||
    }
 | 
			
		||||
    asrt.setVersion(1);
 | 
			
		||||
    //asrt.setQualityEntry(empty, 0);
 | 
			
		||||
    if (metadata.isMember("live")){
 | 
			
		||||
      asrt.setSegmentRun(1, 4294967295, 0);
 | 
			
		||||
    }else{
 | 
			
		||||
      asrt.setSegmentRun(1, metadata["keytime"].size(), 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    MP4::AFRT afrt;
 | 
			
		||||
    if (starttime == 0 && metadata.isMember("vod")){
 | 
			
		||||
      afrt.setUpdate(false);
 | 
			
		||||
    }else{
 | 
			
		||||
      afrt.setUpdate(true);
 | 
			
		||||
    }
 | 
			
		||||
    afrt.setVersion(1);
 | 
			
		||||
    afrt.setTimeScale(1000);
 | 
			
		||||
    //afrt.setQualityEntry(empty, 0);
 | 
			
		||||
    MP4::afrt_runtable afrtrun;
 | 
			
		||||
    if (metadata.isMember("live")){
 | 
			
		||||
      for (int i = 0; i < metadata["keynum"].size(); i++){
 | 
			
		||||
        afrtrun.firstFragment = metadata["keynum"][i].asInt();
 | 
			
		||||
        afrtrun.firstTimestamp = metadata["keytime"][i].asInt();
 | 
			
		||||
        afrtrun.duration = metadata["keylen"][i].asInt();
 | 
			
		||||
        afrt.setFragmentRun(afrtrun, i);
 | 
			
		||||
      }
 | 
			
		||||
    }else{
 | 
			
		||||
      for (int i = 0; i < metadata["keytime"].size(); i++){
 | 
			
		||||
        afrtrun.firstFragment = i + 1;
 | 
			
		||||
        afrtrun.firstTimestamp = metadata["keytime"][i].asInt();
 | 
			
		||||
        if (i + 1 < metadata["keytime"].size()){
 | 
			
		||||
          afrtrun.duration = metadata["keytime"][i + 1].asInt() - metadata["keytime"][i].asInt();
 | 
			
		||||
        }else{
 | 
			
		||||
          if (metadata["lastms"].asInt()){
 | 
			
		||||
            afrtrun.duration = metadata["lastms"].asInt() - metadata["keytime"][i].asInt();
 | 
			
		||||
          }else{
 | 
			
		||||
            afrtrun.duration = 3000; //guess 3 seconds if unknown
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        afrt.setFragmentRun(afrtrun, i);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    MP4::ABST abst;
 | 
			
		||||
    abst.setVersion(1);
 | 
			
		||||
    if (metadata.isMember("live")){
 | 
			
		||||
      abst.setBootstrapinfoVersion(metadata["keynum"][metadata["keynum"].size() - 2].asInt());
 | 
			
		||||
    }else{
 | 
			
		||||
      abst.setBootstrapinfoVersion(1);
 | 
			
		||||
    }
 | 
			
		||||
    abst.setProfile(0);
 | 
			
		||||
    if (starttime == 0){
 | 
			
		||||
      abst.setUpdate(false);
 | 
			
		||||
    }else{
 | 
			
		||||
      abst.setUpdate(true);
 | 
			
		||||
    }
 | 
			
		||||
    abst.setTimeScale(1000);
 | 
			
		||||
    if (metadata.isMember("vod")){
 | 
			
		||||
      abst.setLive(false);
 | 
			
		||||
      if (metadata["lastms"].asInt()){
 | 
			
		||||
        abst.setCurrentMediaTime(metadata["lastms"].asInt());
 | 
			
		||||
      }else{
 | 
			
		||||
        abst.setCurrentMediaTime(1000 * metadata["length"].asInt());
 | 
			
		||||
      }
 | 
			
		||||
    }else{
 | 
			
		||||
      abst.setLive(false);
 | 
			
		||||
      abst.setCurrentMediaTime(metadata["lastms"].asInt());
 | 
			
		||||
    }
 | 
			
		||||
    abst.setSmpteTimeCodeOffset(0);
 | 
			
		||||
    abst.setMovieIdentifier(MovieId);
 | 
			
		||||
    //abst.setServerEntry(empty, 0);
 | 
			
		||||
    //abst.setQualityEntry(empty, 0);
 | 
			
		||||
    //abst.setDrmData(empty);
 | 
			
		||||
    //abst.setMetaData(empty);
 | 
			
		||||
    abst.setSegmentRunTable(asrt, 0);
 | 
			
		||||
    abst.setFragmentRunTable(afrt, 0);
 | 
			
		||||
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
    std::cout << "Sending bootstrap:" << std::endl << abst.toPrettyString(0) << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    return std::string((char*)abst.asBox(), (int)abst.boxedSize());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Returns a F4M-format manifest file
 | 
			
		||||
  std::string BuildManifest(std::string & MovieId, JSON::Value & metadata){
 | 
			
		||||
    std::string Result;
 | 
			
		||||
    if (metadata.isMember("vod")){
 | 
			
		||||
      Result =
 | 
			
		||||
          "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
 | 
			
		||||
              "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n"
 | 
			
		||||
              "<id>" + MovieId + "</id>\n"
 | 
			
		||||
              "<width>" + metadata["video"]["width"].asString() + "</width>\n"
 | 
			
		||||
              "<height>" + metadata["video"]["height"].asString() + "</height>\n"
 | 
			
		||||
              "<duration>" + metadata["length"].asString() + ".000</duration>\n"
 | 
			
		||||
              "<mimeType>video/mp4</mimeType>\n"
 | 
			
		||||
              "<streamType>recorded</streamType>\n"
 | 
			
		||||
              "<deliveryType>streaming</deliveryType>\n"
 | 
			
		||||
              "<bootstrapInfo profile=\"named\" id=\"bootstrap1\">" + Base64::encode(GenerateBootstrap(MovieId, metadata, 1, 0, 0)) + "</bootstrapInfo>\n"
 | 
			
		||||
              "<media streamId=\"1\" bootstrapInfoId=\"bootstrap1\" url=\"" + MovieId + "/\">\n"
 | 
			
		||||
              "<metadata>AgAKb25NZXRhRGF0YQMAAAk=</metadata>\n"
 | 
			
		||||
              "</media>\n"
 | 
			
		||||
              "</manifest>\n";
 | 
			
		||||
    }else{
 | 
			
		||||
      Result =
 | 
			
		||||
          "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
 | 
			
		||||
              "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n"
 | 
			
		||||
              "<id>" + MovieId + "</id>\n"
 | 
			
		||||
              "<dvrInfo windowDuration=\"" + metadata["buffer_window"].asString().substr(0, metadata["buffer_window"].asString().size() - 3) + "\"></dvrInfo>"
 | 
			
		||||
              "<mimeType>video/mp4</mimeType>\n"
 | 
			
		||||
              "<streamType>live</streamType>\n"
 | 
			
		||||
              "<deliveryType>streaming</deliveryType>\n"
 | 
			
		||||
              "<media url=\"" + MovieId + "/\">\n"
 | 
			
		||||
              "<metadata>AgAKb25NZXRhRGF0YQMAAAk=</metadata>\n"
 | 
			
		||||
              "</media>\n"
 | 
			
		||||
              "<bootstrapInfo profile=\"named\" url=\"" + MovieId + ".abst\" />\n"
 | 
			
		||||
              "</manifest>\n";
 | 
			
		||||
    }
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
    std::cerr << "Sending this manifest:" << std::endl << Result << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    return Result;
 | 
			
		||||
  } //BuildManifest
 | 
			
		||||
 | 
			
		||||
  /// Main function for Connector_HTTP_Dynamic
 | 
			
		||||
  int Connector_HTTP_Dynamic(Socket::Connection conn){
 | 
			
		||||
    std::deque<std::string> FlashBuf;
 | 
			
		||||
    int FlashBufSize = 0;
 | 
			
		||||
    long long int FlashBufTime = 0;
 | 
			
		||||
    FLV::Tag tmp; //temporary tag
 | 
			
		||||
 | 
			
		||||
    DTSC::Stream Strm; //Incoming stream buffer.
 | 
			
		||||
    HTTP::Parser HTTP_R, HTTP_S; //HTTP Receiver en HTTP Sender.
 | 
			
		||||
 | 
			
		||||
    bool ready4data = false; //Set to true when streaming is to begin.
 | 
			
		||||
    bool pending_manifest = false;
 | 
			
		||||
    bool receive_marks = false; //when set to true, this stream will ignore keyframes and instead use pause marks
 | 
			
		||||
    bool inited = false;
 | 
			
		||||
    Socket::Connection ss( -1);
 | 
			
		||||
    std::string streamname;
 | 
			
		||||
    std::string recBuffer = "";
 | 
			
		||||
 | 
			
		||||
    std::string Quality;
 | 
			
		||||
    int Segment = -1;
 | 
			
		||||
    int ReqFragment = -1;
 | 
			
		||||
    int temp;
 | 
			
		||||
    int Flash_RequestPending = 0;
 | 
			
		||||
    unsigned int lastStats = 0;
 | 
			
		||||
    conn.setBlocking(false); //do not block on conn.spool() when no data is available
 | 
			
		||||
 | 
			
		||||
    while (conn.connected()){
 | 
			
		||||
      if (conn.spool() || conn.Received().size()){
 | 
			
		||||
        //make sure it ends in a \n
 | 
			
		||||
        if ( *(conn.Received().get().rbegin()) != '\n'){
 | 
			
		||||
          std::string tmp = conn.Received().get();
 | 
			
		||||
          conn.Received().get().clear();
 | 
			
		||||
          if (conn.Received().size()){
 | 
			
		||||
            conn.Received().get().insert(0, tmp);
 | 
			
		||||
          }else{
 | 
			
		||||
            conn.Received().append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (HTTP_R.Read(conn.Received().get())){
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
          std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
          conn.setHost(HTTP_R.GetHeader("X-Origin"));
 | 
			
		||||
          streamname = HTTP_R.GetHeader("X-Stream");
 | 
			
		||||
          if ( !ss){
 | 
			
		||||
            ss = Util::Stream::getStream(streamname);
 | 
			
		||||
            if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
              fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
              ss.close();
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
              ready4data = false;
 | 
			
		||||
              HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
              continue;
 | 
			
		||||
            }
 | 
			
		||||
            ss.setBlocking(false);
 | 
			
		||||
            inited = true;
 | 
			
		||||
            while ( !ss.spool()){
 | 
			
		||||
            }
 | 
			
		||||
            Strm.parsePacket(ss.Received());
 | 
			
		||||
          }
 | 
			
		||||
          if (HTTP_R.url.find(".abst") != std::string::npos){
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetBody(GenerateBootstrap(streamname, Strm.metadata, 1, 0, 0));
 | 
			
		||||
            HTTP_S.SetHeader("Content-Type", "binary/octet");
 | 
			
		||||
            HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
            HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
          if (HTTP_R.url.find("f4m") == std::string::npos){
 | 
			
		||||
            Quality = HTTP_R.url.substr(HTTP_R.url.find("/", 10) + 1);
 | 
			
		||||
            Quality = Quality.substr(0, Quality.find("Seg"));
 | 
			
		||||
            temp = HTTP_R.url.find("Seg") + 3;
 | 
			
		||||
            Segment = atoi(HTTP_R.url.substr(temp, HTTP_R.url.find("-", temp) - temp).c_str());
 | 
			
		||||
            temp = HTTP_R.url.find("Frag") + 4;
 | 
			
		||||
            ReqFragment = atoi(HTTP_R.url.substr(temp).c_str());
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
            printf("Quality: %s, Seg %d Frag %d\n", Quality.c_str(), Segment, ReqFragment);
 | 
			
		||||
#endif
 | 
			
		||||
            std::stringstream sstream;
 | 
			
		||||
            sstream << "f " << ReqFragment << "\no \n";
 | 
			
		||||
            ss.SendNow(sstream.str().c_str());
 | 
			
		||||
            Flash_RequestPending++;
 | 
			
		||||
          }else{
 | 
			
		||||
            if ( !Strm.metadata.isNull()){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              if (Strm.metadata.isMember("length")){
 | 
			
		||||
                receive_marks = true;
 | 
			
		||||
              }
 | 
			
		||||
              std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
              printf("Sent manifest\n");
 | 
			
		||||
#endif
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }else{
 | 
			
		||||
              pending_manifest = true;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          ready4data = true;
 | 
			
		||||
          HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        if (Flash_RequestPending){
 | 
			
		||||
          usleep(1000); //sleep 1ms
 | 
			
		||||
        }else{
 | 
			
		||||
          usleep(10000); //sleep 10ms
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (ready4data){
 | 
			
		||||
        if ( !inited){
 | 
			
		||||
          //we are ready, connect the socket!
 | 
			
		||||
          ss = Util::Stream::getStream(streamname);
 | 
			
		||||
          if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
            fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
            ss.close();
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
            ready4data = false;
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
          ss.setBlocking(false);
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
          fprintf(stderr, "Everything connected, starting to send video data...\n");
 | 
			
		||||
#endif
 | 
			
		||||
          inited = true;
 | 
			
		||||
        }
 | 
			
		||||
        unsigned int now = Util::epoch();
 | 
			
		||||
        if (now != lastStats){
 | 
			
		||||
          lastStats = now;
 | 
			
		||||
          ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
 | 
			
		||||
        }
 | 
			
		||||
        if (ss.spool()){
 | 
			
		||||
          while (Strm.parsePacket(ss.Received())){
 | 
			
		||||
            /*
 | 
			
		||||
             if (Strm.getPacket(0).isMember("time")){
 | 
			
		||||
             if ( !Strm.metadata.isMember("firsttime")){
 | 
			
		||||
             Strm.metadata["firsttime"] = Strm.getPacket(0)["time"];
 | 
			
		||||
             }else{
 | 
			
		||||
             if ( !Strm.metadata.isMember("length") || Strm.metadata["length"].asInt() == 0){
 | 
			
		||||
             Strm.getPacket(0)["time"] = Strm.getPacket(0)["time"].asInt() - Strm.metadata["firsttime"].asInt();
 | 
			
		||||
             }
 | 
			
		||||
             }
 | 
			
		||||
             Strm.metadata["lasttime"] = Strm.getPacket(0)["time"];
 | 
			
		||||
             }
 | 
			
		||||
             */
 | 
			
		||||
            if (pending_manifest){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              if (Strm.metadata.isMember("length")){
 | 
			
		||||
                receive_marks = true;
 | 
			
		||||
              }
 | 
			
		||||
              std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
              printf("Sent manifest\n");
 | 
			
		||||
#endif
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }
 | 
			
		||||
            if ( !receive_marks && Strm.metadata.isMember("length")){
 | 
			
		||||
              receive_marks = true;
 | 
			
		||||
            }
 | 
			
		||||
            if ((Strm.getPacket(0).isMember("keyframe") && !receive_marks) || Strm.lastType() == DTSC::PAUSEMARK){
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
              fprintf(stderr, "Received a %s fragment of %i bytes.\n", Strm.getPacket(0)["datatype"].asString().c_str(), FlashBufSize);
 | 
			
		||||
#endif
 | 
			
		||||
              if (Flash_RequestPending > 0 && FlashBufSize){
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
                fprintf(stderr, "Sending a fragment...");
 | 
			
		||||
#endif
 | 
			
		||||
                //static std::string btstrp;
 | 
			
		||||
                //btstrp = GenerateBootstrap(streamname, Strm.metadata, ReqFragment, FlashBufTime, Strm.getPacket(0)["time"]);
 | 
			
		||||
                HTTP_S.Clean();
 | 
			
		||||
                HTTP_S.SetHeader("Content-Type", "video/mp4");
 | 
			
		||||
                HTTP_S.SetBody("");
 | 
			
		||||
                std::string new_strap = GenerateBootstrap(streamname, Strm.metadata, 1, 0, 0);
 | 
			
		||||
                HTTP_S.SetHeader("Content-Length", FlashBufSize + 8 + new_strap.size()); //32+33+btstrp.size());
 | 
			
		||||
                conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
                //conn.SendNow("\x00\x00\x00\x21" "afra\x00\x00\x00\x00\x00\x00\x00\x03\xE8\x00\x00\x00\x01", 21);
 | 
			
		||||
                //unsigned long tmptime = htonl(FlashBufTime << 32);
 | 
			
		||||
                //conn.SendNow((char*)&tmptime, 4);
 | 
			
		||||
                //tmptime = htonl(FlashBufTime & 0xFFFFFFFF);
 | 
			
		||||
                //conn.SendNow((char*)&tmptime, 4);
 | 
			
		||||
                //tmptime = htonl(65);
 | 
			
		||||
                //conn.SendNow((char*)&tmptime, 4);
 | 
			
		||||
 | 
			
		||||
                //conn.SendNow(btstrp);
 | 
			
		||||
 | 
			
		||||
                //conn.SendNow("\x00\x00\x00\x18moof\x00\x00\x00\x10mfhd\x00\x00\x00\x00", 20);
 | 
			
		||||
                //unsigned long fragno = htonl(ReqFragment);
 | 
			
		||||
                //conn.SendNow((char*)&fragno, 4);
 | 
			
		||||
                conn.SendNow(new_strap);
 | 
			
		||||
                unsigned long size = htonl(FlashBufSize+8);
 | 
			
		||||
                conn.SendNow((char*) &size, 4);
 | 
			
		||||
                conn.SendNow("mdat", 4);
 | 
			
		||||
                while (FlashBuf.size() > 0){
 | 
			
		||||
                  conn.SendNow(FlashBuf.front());
 | 
			
		||||
                  FlashBuf.pop_front();
 | 
			
		||||
                }
 | 
			
		||||
                Flash_RequestPending--;
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
                fprintf(stderr, "Done\n");
 | 
			
		||||
#endif
 | 
			
		||||
              }
 | 
			
		||||
              FlashBuf.clear();
 | 
			
		||||
              FlashBufSize = 0;
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){
 | 
			
		||||
              if (FlashBufSize == 0){
 | 
			
		||||
                //fill buffer with init data, if needed.
 | 
			
		||||
                if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
 | 
			
		||||
                  tmp.DTSCAudioInit(Strm);
 | 
			
		||||
                  tmp.tagTime(Strm.getPacket(0)["time"].asInt());
 | 
			
		||||
                  FlashBuf.push_back(std::string(tmp.data, tmp.len));
 | 
			
		||||
                  FlashBufSize += tmp.len;
 | 
			
		||||
                }
 | 
			
		||||
                if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
 | 
			
		||||
                  tmp.DTSCVideoInit(Strm);
 | 
			
		||||
                  tmp.tagTime(Strm.getPacket(0)["time"].asInt());
 | 
			
		||||
                  FlashBuf.push_back(std::string(tmp.data, tmp.len));
 | 
			
		||||
                  FlashBufSize += tmp.len;
 | 
			
		||||
                }
 | 
			
		||||
                FlashBufTime = Strm.getPacket(0)["time"].asInt();
 | 
			
		||||
              }
 | 
			
		||||
              tmp.DTSCLoader(Strm);
 | 
			
		||||
              FlashBuf.push_back(std::string(tmp.data, tmp.len));
 | 
			
		||||
              FlashBufSize += tmp.len;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          if (pending_manifest && !Strm.metadata.isNull()){
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
            HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
            if (Strm.metadata.isMember("length")){
 | 
			
		||||
              receive_marks = true;
 | 
			
		||||
            }
 | 
			
		||||
            std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
            HTTP_S.SetBody(manifest);
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
            printf("Sent manifest\n");
 | 
			
		||||
#endif
 | 
			
		||||
            pending_manifest = false;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if ( !ss.connected()){
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    conn.close();
 | 
			
		||||
    ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
 | 
			
		||||
    ss.close();
 | 
			
		||||
    return 0;
 | 
			
		||||
  } //Connector_HTTP_Dynamic main function
 | 
			
		||||
 | 
			
		||||
} //Connector_HTTP_Dynamic namespace
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(1935);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server("/tmp/mist/http_dynamic");
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return Connector_HTTP::Connector_HTTP_Dynamic(S);
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 3
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										404
									
								
								src/connectors/conn_http_live.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										404
									
								
								src/connectors/conn_http_live.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,404 @@
 | 
			
		|||
/// \file conn_http_dynamic.cpp
 | 
			
		||||
/// Contains the main code for the HTTP Dynamic Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/http_parser.h>
 | 
			
		||||
#include <mist/json.h>
 | 
			
		||||
#include <mist/dtsc.h>
 | 
			
		||||
#include <mist/mp4.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
#include <mist/ts_packet.h>
 | 
			
		||||
 | 
			
		||||
/// Holds everything unique to HTTP Connectors.
 | 
			
		||||
namespace Connector_HTTP {
 | 
			
		||||
  /// Parses the list of keyframes into 10 second fragments
 | 
			
		||||
  std::vector<int> keyframesToFragments(JSON::Value & metadata){
 | 
			
		||||
    std::vector<int> result;
 | 
			
		||||
    if (metadata.isNull()){
 | 
			
		||||
      return result;
 | 
			
		||||
    }
 | 
			
		||||
    if (metadata.isMember("keynum")){
 | 
			
		||||
      for (int i = 0; i < metadata["keynum"].size(); i++){
 | 
			
		||||
        result.push_back(metadata["keynum"][i].asInt());
 | 
			
		||||
      }
 | 
			
		||||
    }else{
 | 
			
		||||
      result.push_back(0);
 | 
			
		||||
      int currentBase = metadata["keytime"][0u].asInt();
 | 
			
		||||
      for (int i = 0; i < metadata["keytime"].size(); i++){
 | 
			
		||||
        if ((metadata["keytime"][i].asInt() - currentBase) > 10000){
 | 
			
		||||
          currentBase = metadata["keytime"][i].asInt();
 | 
			
		||||
          result.push_back(i);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Returns a m3u or m3u8 index file
 | 
			
		||||
  std::string BuildIndex(std::string & MovieId, JSON::Value & metadata){
 | 
			
		||||
    std::stringstream Result;
 | 
			
		||||
    if ( !metadata.isMember("live")){
 | 
			
		||||
      std::vector<int> fragIndices = keyframesToFragments(metadata);
 | 
			
		||||
      int longestFragment = 0;
 | 
			
		||||
      for (int i = 1; i < fragIndices.size(); i++){
 | 
			
		||||
        int fragDuration = metadata["keytime"][fragIndices[i]].asInt() - metadata["keytime"][fragIndices[i - 1]].asInt();
 | 
			
		||||
        if (fragDuration > longestFragment){
 | 
			
		||||
          longestFragment = fragDuration;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      Result << "#EXTM3U\r\n"
 | 
			
		||||
      //"#EXT-X-VERSION:1\r\n"
 | 
			
		||||
      //"#EXT-X-ALLOW-CACHE:YES\r\n"
 | 
			
		||||
              "#EXT-X-TARGETDURATION:" << (longestFragment / 1000) + 1 << "\r\n"
 | 
			
		||||
          "#EXT-X-MEDIA-SEQUENCE:0\r\n";
 | 
			
		||||
      //"#EXT-X-PLAYLIST-TYPE:VOD\r\n";
 | 
			
		||||
      int lastDuration = 0;
 | 
			
		||||
      for (int i = 0; i < fragIndices.size() - 1; i++){
 | 
			
		||||
        Result << "#EXTINF:" << (metadata["keytime"][fragIndices[i + 1]].asInt() - lastDuration) / 1000 << ", no desc\r\n" << fragIndices[i] + 1
 | 
			
		||||
            << "_" << fragIndices[i + 1] - fragIndices[i] << ".ts\r\n";
 | 
			
		||||
        lastDuration = metadata["keytime"][fragIndices[i + 1]].asInt();
 | 
			
		||||
      }
 | 
			
		||||
      Result << "#EXT-X-ENDLIST";
 | 
			
		||||
    }else{
 | 
			
		||||
      if (metadata["missed_frags"].asInt() < 0){
 | 
			
		||||
        metadata["missed_frags"] = 0ll;
 | 
			
		||||
      }
 | 
			
		||||
      Result << "#EXTM3U\r\n"
 | 
			
		||||
          "#EXT-X-MEDIA-SEQUENCE:" << metadata["missed_frags"].asInt() <<"\r\n"
 | 
			
		||||
          "#EXT-X-TARGETDURATION:30\r\n";
 | 
			
		||||
      for (JSON::ArrIter ai = metadata["frags"].ArrBegin(); ai != metadata["frags"].ArrEnd(); ai++){
 | 
			
		||||
        Result << "#EXTINF:" << (*ai)["dur"].asInt() / 1000 << ", no desc\r\n" << (*ai)["num"].asInt() << "_" << (*ai)["len"].asInt() << ".ts\r\n";
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
    std::cerr << "Sending this index:" << std::endl << Result.str() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    return Result.str();
 | 
			
		||||
  } //BuildIndex
 | 
			
		||||
 | 
			
		||||
  /// Main function for Connector_HTTP_Live
 | 
			
		||||
  int Connector_HTTP_Live(Socket::Connection conn){
 | 
			
		||||
    std::stringstream TSBuf;
 | 
			
		||||
    long long int TSBufTime = 0;
 | 
			
		||||
 | 
			
		||||
    DTSC::Stream Strm; //Incoming stream buffer.
 | 
			
		||||
    HTTP::Parser HTTP_R, HTTP_S; //HTTP Receiver en HTTP Sender.
 | 
			
		||||
 | 
			
		||||
    bool ready4data = false; //Set to true when streaming is to begin.
 | 
			
		||||
    bool pending_manifest = false;
 | 
			
		||||
    bool receive_marks = false; //when set to true, this stream will ignore keyframes and instead use pause marks
 | 
			
		||||
    bool inited = false;
 | 
			
		||||
    Socket::Connection ss( -1);
 | 
			
		||||
    std::string streamname;
 | 
			
		||||
    std::string recBuffer = "";
 | 
			
		||||
 | 
			
		||||
    TS::Packet PackData;
 | 
			
		||||
    int PacketNumber = 0;
 | 
			
		||||
    long long unsigned int TimeStamp = 0;
 | 
			
		||||
    int ThisNaluSize;
 | 
			
		||||
    char VideoCounter = 0;
 | 
			
		||||
    char AudioCounter = 0;
 | 
			
		||||
    bool IsKeyFrame;
 | 
			
		||||
    MP4::AVCC avccbox;
 | 
			
		||||
    bool haveAvcc = false;
 | 
			
		||||
 | 
			
		||||
    std::vector<int> fragIndices;
 | 
			
		||||
 | 
			
		||||
    std::string manifestType;
 | 
			
		||||
 | 
			
		||||
    int Segment = -1;
 | 
			
		||||
    int temp;
 | 
			
		||||
    int Flash_RequestPending = 0;
 | 
			
		||||
    unsigned int lastStats = 0;
 | 
			
		||||
    conn.setBlocking(false); //do not block on conn.spool() when no data is available
 | 
			
		||||
 | 
			
		||||
    while (conn.connected()){
 | 
			
		||||
      if (conn.spool() || conn.Received().size()){
 | 
			
		||||
        //make sure it ends in a \n
 | 
			
		||||
        if ( *(conn.Received().get().rbegin()) != '\n'){
 | 
			
		||||
          std::string tmp = conn.Received().get();
 | 
			
		||||
          conn.Received().get().clear();
 | 
			
		||||
          if (conn.Received().size()){
 | 
			
		||||
            conn.Received().get().insert(0, tmp);
 | 
			
		||||
          }else{
 | 
			
		||||
            conn.Received().append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (HTTP_R.Read(conn.Received().get())){
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
          conn.setHost(HTTP_R.GetHeader("X-Origin"));
 | 
			
		||||
          streamname = HTTP_R.GetHeader("X-Stream");
 | 
			
		||||
          if ( !ss){
 | 
			
		||||
            ss = Util::Stream::getStream(streamname);
 | 
			
		||||
            if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
              fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
              ready4data = false;
 | 
			
		||||
              continue;
 | 
			
		||||
            }
 | 
			
		||||
            ss.setBlocking(false);
 | 
			
		||||
            inited = true;
 | 
			
		||||
            while ( !ss.spool()){
 | 
			
		||||
            }
 | 
			
		||||
            Strm.parsePacket(ss.Received());
 | 
			
		||||
          }
 | 
			
		||||
          if (HTTP_R.url.find(".m3u") == std::string::npos){
 | 
			
		||||
            temp = HTTP_R.url.find("/", 5) + 1;
 | 
			
		||||
            Segment = atoi(HTTP_R.url.substr(temp, HTTP_R.url.find("_", temp) - temp).c_str());
 | 
			
		||||
            temp = HTTP_R.url.find("_", temp) + 1;
 | 
			
		||||
            int frameCount = atoi(HTTP_R.url.substr(temp, HTTP_R.url.find(".ts", temp) - temp).c_str());
 | 
			
		||||
 | 
			
		||||
            std::stringstream sstream;
 | 
			
		||||
            sstream << "f " << Segment << "\n";
 | 
			
		||||
            for (int i = 0; i < frameCount; i++){
 | 
			
		||||
              sstream << "o \n";
 | 
			
		||||
            }
 | 
			
		||||
            ss.SendNow(sstream.str().c_str());
 | 
			
		||||
            Flash_RequestPending++;
 | 
			
		||||
          }else{
 | 
			
		||||
            if (ss.spool()){
 | 
			
		||||
              Strm.parsePacket(ss.Received());
 | 
			
		||||
            }
 | 
			
		||||
            if (HTTP_R.url.find(".m3u8") != std::string::npos){
 | 
			
		||||
              manifestType = "audio/x-mpegurl";
 | 
			
		||||
            }else{
 | 
			
		||||
              manifestType = "audio/mpegurl";
 | 
			
		||||
            }
 | 
			
		||||
            if ( !Strm.metadata.isNull()){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", manifestType);
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              if (Strm.metadata.isMember("length")){
 | 
			
		||||
                receive_marks = true;
 | 
			
		||||
              }
 | 
			
		||||
              std::string manifest = BuildIndex(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }else{
 | 
			
		||||
              pending_manifest = true;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          ready4data = true;
 | 
			
		||||
          HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        if (Flash_RequestPending){
 | 
			
		||||
          usleep(1000); //sleep 1ms
 | 
			
		||||
        }else{
 | 
			
		||||
          usleep(10000); //sleep 10ms
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (ready4data){
 | 
			
		||||
        if ( !inited){
 | 
			
		||||
          //we are ready, connect the socket!
 | 
			
		||||
          ss = Util::Stream::getStream(streamname);
 | 
			
		||||
          if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
            fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
            ss.close();
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
            ready4data = false;
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
          ss.setBlocking(false);
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          fprintf(stderr, "Everything connected, starting to send video data...\n");
 | 
			
		||||
#endif
 | 
			
		||||
          inited = true;
 | 
			
		||||
        }
 | 
			
		||||
        unsigned int now = Util::epoch();
 | 
			
		||||
        if (now != lastStats){
 | 
			
		||||
          lastStats = now;
 | 
			
		||||
          ss.SendNow(conn.getStats("HTTP_Live").c_str());
 | 
			
		||||
        }
 | 
			
		||||
        if (ss.spool()){
 | 
			
		||||
          while (Strm.parsePacket(ss.Received())){
 | 
			
		||||
            if (pending_manifest){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.protocol = "HTTP/1.1";
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              if (Strm.metadata.isMember("length")){
 | 
			
		||||
                receive_marks = true;
 | 
			
		||||
              }
 | 
			
		||||
              std::string manifest = BuildIndex(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", manifestType);
 | 
			
		||||
              HTTP_S.SetHeader("Connection", "keep-alive");
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }
 | 
			
		||||
            if ( !receive_marks && Strm.metadata.isMember("length")){
 | 
			
		||||
              receive_marks = true;
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.lastType() == DTSC::PAUSEMARK){
 | 
			
		||||
              TSBuf.flush();
 | 
			
		||||
              if (Flash_RequestPending > 0 && TSBuf.str().size()){
 | 
			
		||||
                HTTP_S.Clean();
 | 
			
		||||
                HTTP_S.protocol = "HTTP/1.1";
 | 
			
		||||
                HTTP_S.SetHeader("Content-Type", "video/mp2t");
 | 
			
		||||
                HTTP_S.SetHeader("Connection", "keep-alive");
 | 
			
		||||
                HTTP_S.SetBody("");
 | 
			
		||||
                HTTP_S.SetHeader("Content-Length", TSBuf.str().size());
 | 
			
		||||
                conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
                conn.SendNow(TSBuf.str().c_str(), TSBuf.str().size());
 | 
			
		||||
                TSBuf.str("");
 | 
			
		||||
                Flash_RequestPending--;
 | 
			
		||||
                PacketNumber = 0;
 | 
			
		||||
              }
 | 
			
		||||
              TSBuf.str("");
 | 
			
		||||
            }
 | 
			
		||||
            if ( !haveAvcc){
 | 
			
		||||
              avccbox.setPayload(Strm.metadata["video"]["init"].asString());
 | 
			
		||||
              haveAvcc = true;
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.lastType() == DTSC::VIDEO || Strm.lastType() == DTSC::AUDIO){
 | 
			
		||||
              Socket::Buffer ToPack;
 | 
			
		||||
              //write PAT and PMT TS packets
 | 
			
		||||
              if (PacketNumber == 0){
 | 
			
		||||
                PackData.DefaultPAT();
 | 
			
		||||
                TSBuf.write(PackData.ToString(), 188);
 | 
			
		||||
                PackData.DefaultPMT();
 | 
			
		||||
                TSBuf.write(PackData.ToString(), 188);
 | 
			
		||||
                PacketNumber += 2;
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              int PIDno = 0;
 | 
			
		||||
              char * ContCounter = 0;
 | 
			
		||||
              if (Strm.lastType() == DTSC::VIDEO){
 | 
			
		||||
                IsKeyFrame = Strm.getPacket(0).isMember("keyframe");
 | 
			
		||||
                if (IsKeyFrame){
 | 
			
		||||
                  TimeStamp = (Strm.getPacket(0)["time"].asInt() * 27000);
 | 
			
		||||
                }
 | 
			
		||||
                ToPack.append(avccbox.asAnnexB());
 | 
			
		||||
                while (Strm.lastData().size()){
 | 
			
		||||
                  ThisNaluSize = (Strm.lastData()[0] << 24) + (Strm.lastData()[1] << 16) + (Strm.lastData()[2] << 8) + Strm.lastData()[3];
 | 
			
		||||
                  Strm.lastData().replace(0, 4, TS::NalHeader, 4);
 | 
			
		||||
                  if (ThisNaluSize + 4 == Strm.lastData().size()){
 | 
			
		||||
                    ToPack.append(Strm.lastData());
 | 
			
		||||
                    break;
 | 
			
		||||
                  }else{
 | 
			
		||||
                    ToPack.append(Strm.lastData().c_str(), ThisNaluSize + 4);
 | 
			
		||||
                    Strm.lastData().erase(0, ThisNaluSize + 4);
 | 
			
		||||
                  }
 | 
			
		||||
                }
 | 
			
		||||
                ToPack.prepend(TS::Packet::getPESVideoLeadIn(0ul, Strm.getPacket(0)["time"].asInt() * 90));
 | 
			
		||||
                PIDno = 0x100;
 | 
			
		||||
                ContCounter = &VideoCounter;
 | 
			
		||||
              }else if (Strm.lastType() == DTSC::AUDIO){
 | 
			
		||||
                ToPack.append(TS::GetAudioHeader(Strm.lastData().size(), Strm.metadata["audio"]["init"].asString()));
 | 
			
		||||
                ToPack.append(Strm.lastData());
 | 
			
		||||
                ToPack.prepend(TS::Packet::getPESAudioLeadIn(ToPack.bytes(1073741824ul), Strm.getPacket(0)["time"].asInt() * 90));
 | 
			
		||||
                PIDno = 0x101;
 | 
			
		||||
                ContCounter = &AudioCounter;
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              //initial packet
 | 
			
		||||
              PackData.Clear();
 | 
			
		||||
              PackData.PID(PIDno);
 | 
			
		||||
              PackData.ContinuityCounter(( *ContCounter)++);
 | 
			
		||||
              PackData.UnitStart(1);
 | 
			
		||||
              if (IsKeyFrame){
 | 
			
		||||
                PackData.RandomAccess(1);
 | 
			
		||||
                PackData.PCR(TimeStamp);
 | 
			
		||||
              }
 | 
			
		||||
              unsigned int toSend = PackData.AddStuffing(ToPack.bytes(184));
 | 
			
		||||
              std::string gonnaSend = ToPack.remove(toSend);
 | 
			
		||||
              PackData.FillFree(gonnaSend);
 | 
			
		||||
              TSBuf.write(PackData.ToString(), 188);
 | 
			
		||||
              PacketNumber++;
 | 
			
		||||
 | 
			
		||||
              //rest of packets
 | 
			
		||||
              while (ToPack.size()){
 | 
			
		||||
                PackData.Clear();
 | 
			
		||||
                PackData.PID(PIDno);
 | 
			
		||||
                PackData.ContinuityCounter(( *ContCounter)++);
 | 
			
		||||
                toSend = PackData.AddStuffing(ToPack.bytes(184));
 | 
			
		||||
                gonnaSend = ToPack.remove(toSend);
 | 
			
		||||
                PackData.FillFree(gonnaSend);
 | 
			
		||||
                TSBuf.write(PackData.ToString(), 188);
 | 
			
		||||
                PacketNumber++;
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          if (pending_manifest && !Strm.metadata.isNull()){
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.protocol = "HTTP/1.1";
 | 
			
		||||
            HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
            if (Strm.metadata.isMember("length")){
 | 
			
		||||
              receive_marks = true;
 | 
			
		||||
            }
 | 
			
		||||
            HTTP_S.SetHeader("Content-Type", manifestType);
 | 
			
		||||
            HTTP_S.SetHeader("Connection", "keep-alive");
 | 
			
		||||
            std::string manifest = BuildIndex(streamname, Strm.metadata);
 | 
			
		||||
            HTTP_S.SetBody(manifest);
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
            pending_manifest = false;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if ( !ss.connected()){
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    conn.close();
 | 
			
		||||
    ss.SendNow(conn.getStats("HTTP_Live").c_str());
 | 
			
		||||
    ss.close();
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
    fprintf(stderr, "HLS: User %i disconnected.\n", conn.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
    return 0;
 | 
			
		||||
  } //Connector_HTTP_Dynamic main function
 | 
			
		||||
 | 
			
		||||
} //Connector_HTTP_Dynamic namespace
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(1935);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server("/tmp/mist/http_live");
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return Connector_HTTP::Connector_HTTP_Live(S);
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										224
									
								
								src/connectors/conn_http_progressive.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										224
									
								
								src/connectors/conn_http_progressive.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,224 @@
 | 
			
		|||
/// \file conn_http_progressive.cpp
 | 
			
		||||
/// Contains the main code for the HTTP Progressive Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/http_parser.h>
 | 
			
		||||
#include <mist/dtsc.h>
 | 
			
		||||
#include <mist/flv_tag.h>
 | 
			
		||||
#include <mist/amf.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
 | 
			
		||||
/// Holds everything unique to HTTP Progressive Connector.
 | 
			
		||||
namespace Connector_HTTP {
 | 
			
		||||
 | 
			
		||||
  /// Main function for Connector_HTTP_Progressive
 | 
			
		||||
  int Connector_HTTP_Progressive(Socket::Connection conn){
 | 
			
		||||
    bool progressive_has_sent_header = false;
 | 
			
		||||
    bool ready4data = false; ///< Set to true when streaming is to begin.
 | 
			
		||||
    DTSC::Stream Strm; ///< Incoming stream buffer.
 | 
			
		||||
    HTTP::Parser HTTP_R, HTTP_S; ///<HTTP Receiver en HTTP Sender.
 | 
			
		||||
    bool inited = false;
 | 
			
		||||
    Socket::Connection ss( -1);
 | 
			
		||||
    std::string streamname;
 | 
			
		||||
    FLV::Tag tag; ///< Temporary tag buffer.
 | 
			
		||||
 | 
			
		||||
    unsigned int lastStats = 0;
 | 
			
		||||
    unsigned int seek_sec = 0; //seek position in ms
 | 
			
		||||
    unsigned int seek_byte = 0; //seek position in bytes
 | 
			
		||||
    
 | 
			
		||||
    bool isMP3 = false;
 | 
			
		||||
 | 
			
		||||
    while (conn.connected()){
 | 
			
		||||
      //only parse input if available or not yet init'ed
 | 
			
		||||
      if ( !inited){
 | 
			
		||||
        if (conn.Received().size() || conn.spool()){
 | 
			
		||||
          //make sure it ends in a \n
 | 
			
		||||
          if ( *(conn.Received().get().rbegin()) != '\n'){
 | 
			
		||||
            std::string tmp = conn.Received().get();
 | 
			
		||||
            conn.Received().get().clear();
 | 
			
		||||
            if (conn.Received().size()){
 | 
			
		||||
              conn.Received().get().insert(0, tmp);
 | 
			
		||||
            }else{
 | 
			
		||||
              conn.Received().append(tmp);
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          if (HTTP_R.Read(conn.Received().get())){
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
            std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
            conn.setHost(HTTP_R.GetHeader("X-Origin"));
 | 
			
		||||
            //we assume the URL is the stream name with a 3 letter extension
 | 
			
		||||
            streamname = HTTP_R.getUrl().substr(1);
 | 
			
		||||
            size_t extDot = streamname.rfind('.');
 | 
			
		||||
            if (extDot != std::string::npos){
 | 
			
		||||
              if (streamname.substr(extDot + 1) == "mp3"){
 | 
			
		||||
                isMP3 = true;
 | 
			
		||||
              }
 | 
			
		||||
              streamname.resize(extDot);
 | 
			
		||||
            }; //strip the extension
 | 
			
		||||
            int start = 0;
 | 
			
		||||
            if ( !HTTP_R.GetVar("start").empty()){
 | 
			
		||||
              start = atoi(HTTP_R.GetVar("start").c_str());
 | 
			
		||||
            }
 | 
			
		||||
            if ( !HTTP_R.GetVar("starttime").empty()){
 | 
			
		||||
              start = atoi(HTTP_R.GetVar("starttime").c_str());
 | 
			
		||||
            }
 | 
			
		||||
            if ( !HTTP_R.GetVar("apstart").empty()){
 | 
			
		||||
              start = atoi(HTTP_R.GetVar("apstart").c_str());
 | 
			
		||||
            }
 | 
			
		||||
            if ( !HTTP_R.GetVar("ec_seek").empty()){
 | 
			
		||||
              start = atoi(HTTP_R.GetVar("ec_seek").c_str());
 | 
			
		||||
            }
 | 
			
		||||
            if ( !HTTP_R.GetVar("fs").empty()){
 | 
			
		||||
              start = atoi(HTTP_R.GetVar("fs").c_str());
 | 
			
		||||
            }
 | 
			
		||||
            //under 3 hours we assume seconds, otherwise byte position
 | 
			
		||||
            if (start < 10800){
 | 
			
		||||
              seek_sec = start * 1000; //ms, not s
 | 
			
		||||
            }else{
 | 
			
		||||
              seek_byte = start; //divide by 1mbit, then *1000 for ms.
 | 
			
		||||
            }
 | 
			
		||||
            ready4data = true;
 | 
			
		||||
            HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (ready4data){
 | 
			
		||||
        if ( !inited){
 | 
			
		||||
          //we are ready, connect the socket!
 | 
			
		||||
          ss = Util::Stream::getStream(streamname);
 | 
			
		||||
          if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
            fprintf(stderr, "Could not connect to server for %s!\n", streamname.c_str());
 | 
			
		||||
#endif
 | 
			
		||||
            ss.close();
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetBody("No such stream is available on the system. Please try again.\n");
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
            ready4data = false;
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
          if (seek_byte){
 | 
			
		||||
            //wait until we have a header
 | 
			
		||||
            while ( !Strm.metadata){
 | 
			
		||||
              if (ss.spool()){
 | 
			
		||||
                Strm.parsePacket(ss.Received()); //read the metadata
 | 
			
		||||
              }else{
 | 
			
		||||
                Util::sleep(5);
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
            int byterate = 0;
 | 
			
		||||
            if (Strm.metadata.isMember("video") && !isMP3){
 | 
			
		||||
              byterate += Strm.metadata["video"]["bps"].asInt();
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.metadata.isMember("audio")){
 | 
			
		||||
              byterate += Strm.metadata["audio"]["bps"].asInt();
 | 
			
		||||
            }
 | 
			
		||||
            seek_sec = (seek_byte / byterate) * 1000;
 | 
			
		||||
          }
 | 
			
		||||
          if (seek_sec){
 | 
			
		||||
            std::stringstream cmd;
 | 
			
		||||
            cmd << "s " << seek_sec << "\n";
 | 
			
		||||
            ss.SendNow(cmd.str().c_str());
 | 
			
		||||
          }
 | 
			
		||||
          ss.SendNow("p\n");
 | 
			
		||||
          inited = true;
 | 
			
		||||
        }
 | 
			
		||||
        unsigned int now = Util::epoch();
 | 
			
		||||
        if (now != lastStats){
 | 
			
		||||
          lastStats = now;
 | 
			
		||||
          ss.SendNow(conn.getStats("HTTP_Progressive").c_str());
 | 
			
		||||
        }
 | 
			
		||||
        if (ss.spool()){
 | 
			
		||||
          while (Strm.parsePacket(ss.Received())){
 | 
			
		||||
            if ( !progressive_has_sent_header){
 | 
			
		||||
              HTTP_S.Clean(); //make sure no parts of old requests are left in any buffers
 | 
			
		||||
              if (!isMP3){
 | 
			
		||||
                HTTP_S.SetHeader("Content-Type", "video/x-flv"); //Send the correct content-type for FLV files
 | 
			
		||||
              }else{
 | 
			
		||||
                HTTP_S.SetHeader("Content-Type", "audio/mpeg"); //Send the correct content-type for MP3 files
 | 
			
		||||
              }
 | 
			
		||||
              //HTTP_S.SetHeader("Transfer-Encoding", "chunked");
 | 
			
		||||
              HTTP_S.protocol = "HTTP/1.0";
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK")); //no SetBody = unknown length - this is intentional, we will stream the entire file
 | 
			
		||||
              if ( !isMP3){
 | 
			
		||||
                conn.SendNow(FLV::Header, 13); //write FLV header
 | 
			
		||||
                //write metadata
 | 
			
		||||
                tag.DTSCMetaInit(Strm);
 | 
			
		||||
                conn.SendNow(tag.data, tag.len);
 | 
			
		||||
                //write video init data, if needed
 | 
			
		||||
                if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
 | 
			
		||||
                  tag.DTSCVideoInit(Strm);
 | 
			
		||||
                  conn.SendNow(tag.data, tag.len);
 | 
			
		||||
                }
 | 
			
		||||
                //write audio init data, if needed
 | 
			
		||||
                if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
 | 
			
		||||
                  tag.DTSCAudioInit(Strm);
 | 
			
		||||
                  conn.SendNow(tag.data, tag.len);
 | 
			
		||||
                }
 | 
			
		||||
              }
 | 
			
		||||
              progressive_has_sent_header = true;
 | 
			
		||||
            }
 | 
			
		||||
            if ( !isMP3){
 | 
			
		||||
              tag.DTSCLoader(Strm);
 | 
			
		||||
              conn.SendNow(tag.data, tag.len); //write the tag contents
 | 
			
		||||
            }else{
 | 
			
		||||
              if(Strm.lastType() == DTSC::AUDIO){
 | 
			
		||||
                conn.SendNow(Strm.lastData()); //write the MP3 contents
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }else{
 | 
			
		||||
          Util::sleep(1);
 | 
			
		||||
        }
 | 
			
		||||
        if ( !ss.connected()){
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    conn.close();
 | 
			
		||||
    ss.SendNow(conn.getStats("HTTP_Dynamic").c_str());
 | 
			
		||||
    ss.close();
 | 
			
		||||
    return 0;
 | 
			
		||||
  } //Connector_HTTP main function
 | 
			
		||||
 | 
			
		||||
} //Connector_HTTP namespace
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(1935);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server("/tmp/mist/http_progressive");
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return Connector_HTTP::Connector_HTTP_Progressive(S);
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										395
									
								
								src/connectors/conn_http_smooth.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										395
									
								
								src/connectors/conn_http_smooth.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,395 @@
 | 
			
		|||
/// \file conn_http_dynamic.cpp
 | 
			
		||||
/// Contains the main code for the HTTP Dynamic Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/http_parser.h>
 | 
			
		||||
#include <mist/json.h>
 | 
			
		||||
#include <mist/dtsc.h>
 | 
			
		||||
#include <mist/base64.h>
 | 
			
		||||
#include <mist/amf.h>
 | 
			
		||||
#include <mist/mp4.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
 | 
			
		||||
/// Holds everything unique to HTTP Dynamic Connector.
 | 
			
		||||
namespace Connector_HTTP {
 | 
			
		||||
  /// Returns a Smooth-format manifest file
 | 
			
		||||
  std::string BuildManifest(std::string & MovieId, JSON::Value & metadata){
 | 
			
		||||
    std::stringstream Result;
 | 
			
		||||
    Result << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
 | 
			
		||||
    Result << "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" TimeScale=\"10000000\" ";
 | 
			
		||||
    if (metadata.isMember("vod")){
 | 
			
		||||
      Result << "Duration=\"" << metadata["lastms"].asInt() << "\"";
 | 
			
		||||
    }else{
 | 
			
		||||
      Result << "Duration=\"0\" IsLive=\"TRUE\" LookAheadFragmentCount=\"2\" DVRWindowLength=\"" + metadata["buffer_window"].asString() + "0000\" CanSeek=\"TRUE\" CanPause=\"TRUE\" ";
 | 
			
		||||
    }
 | 
			
		||||
    Result << ">\n";
 | 
			
		||||
    if (metadata.isMember("audio")){
 | 
			
		||||
      Result << "  <StreamIndex Type=\"audio\" QualityLevels=\"1\" Name=\"audio\" Chunks=\"" << metadata["keytime"].size()
 | 
			
		||||
          << "\" Url=\"Q({bitrate})/A({start time})\">\n";
 | 
			
		||||
      Result << "    <QualityLevel Index=\"0\" Bitrate=\"" << metadata["audio"]["bps"].asInt() * 8 << "\" CodecPrivateData=\"";
 | 
			
		||||
      Result << std::hex;
 | 
			
		||||
      for (int i = 0; i < metadata["audio"]["init"].asString().size(); i++){
 | 
			
		||||
        Result << std::setfill('0') << std::setw(2) << std::right << (int)metadata["audio"]["init"].asString()[i];
 | 
			
		||||
      }
 | 
			
		||||
      Result << std::dec;
 | 
			
		||||
      Result << "\" SamplingRate=\"" << metadata["audio"]["rate"].asInt()
 | 
			
		||||
          << "\" Channels=\"2\" BitsPerSample=\"16\" PacketSize=\"4\" AudioTag=\"255\" FourCC=\"AACL\"  />\n";
 | 
			
		||||
      for (int i = 0; i < metadata["keytime"].size() - 1; i++){
 | 
			
		||||
        Result << "    <c ";
 | 
			
		||||
        if (i == 0){
 | 
			
		||||
          Result << "t=\"" << metadata["keytime"][0u].asInt() * 10000 << "\" ";
 | 
			
		||||
        }
 | 
			
		||||
        Result << "d=\"" << 10000 * (metadata["keytime"][i + 1].asInt() - metadata["keytime"][i].asInt()) << "\" />\n";
 | 
			
		||||
      }
 | 
			
		||||
      Result << "    <c d=\"" << 10000 * (metadata["lastms"].asInt() - metadata["keytime"][metadata["keytime"].size() - 1].asInt()) << "\" />\n";
 | 
			
		||||
      Result << "   </StreamIndex>\n";
 | 
			
		||||
    }
 | 
			
		||||
    if (metadata.isMember("video")){
 | 
			
		||||
      Result << "  <StreamIndex Type=\"video\" QualityLevels=\"1\" Name=\"video\" Chunks=\"" << metadata["keytime"].size()
 | 
			
		||||
          << "\" Url=\"Q({bitrate})/V({start time})\" MaxWidth=\"" << metadata["video"]["width"].asInt() << "\" MaxHeight=\""
 | 
			
		||||
          << metadata["video"]["height"].asInt() << "\" DisplayWidth=\"" << metadata["video"]["width"].asInt() << "\" DisplayHeight=\""
 | 
			
		||||
          << metadata["video"]["height"].asInt() << "\">\n";
 | 
			
		||||
      Result << "    <QualityLevel Index=\"0\" Bitrate=\"" << metadata["video"]["bps"].asInt() * 8 << "\" CodecPrivateData=\"";
 | 
			
		||||
      MP4::AVCC avccbox;
 | 
			
		||||
      avccbox.setPayload(metadata["video"]["init"].asString());
 | 
			
		||||
      std::string tmpString = avccbox.asAnnexB();
 | 
			
		||||
      Result << std::hex;
 | 
			
		||||
      for (int i = 0; i < tmpString.size(); i++){
 | 
			
		||||
        Result << std::setfill('0') << std::setw(2) << std::right << (int)tmpString[i];
 | 
			
		||||
      }
 | 
			
		||||
      Result << std::dec;
 | 
			
		||||
      Result << "\" MaxWidth=\"" << metadata["video"]["width"].asInt() << "\" MaxHeight=\"" << metadata["video"]["height"].asInt()
 | 
			
		||||
          << "\" FourCC=\"AVC1\" />\n";
 | 
			
		||||
      for (int i = 0; i < metadata["keytime"].size() - 1; i++){
 | 
			
		||||
        Result << "    <c ";
 | 
			
		||||
        if (i == 0){
 | 
			
		||||
          Result << "t=\"" << metadata["keytime"][0u].asInt() * 10000 << "\" ";
 | 
			
		||||
        }
 | 
			
		||||
        Result << "d=\"" << 10000 * (metadata["keytime"][i + 1].asInt() - metadata["keytime"][i].asInt()) << "\" />\n";
 | 
			
		||||
      }
 | 
			
		||||
      Result << "    <c d=\"" << 10000 * (metadata["lastms"].asInt() - metadata["keytime"][metadata["keytime"].size() - 1].asInt()) << "\" />\n";
 | 
			
		||||
      Result << "   </StreamIndex>\n";
 | 
			
		||||
    }
 | 
			
		||||
    Result << "</SmoothStreamingMedia>\n";
 | 
			
		||||
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
    std::cerr << "Sending this manifest:" << std::endl << Result << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
    return Result.str();
 | 
			
		||||
  } //BuildManifest
 | 
			
		||||
 | 
			
		||||
  /// Main function for Connector_HTTP_Dynamic
 | 
			
		||||
  int Connector_HTTP_Dynamic(Socket::Connection conn){
 | 
			
		||||
    std::deque<std::string> FlashBuf;
 | 
			
		||||
    int FlashBufSize = 0;
 | 
			
		||||
    long long int FlashBufTime = 0;
 | 
			
		||||
 | 
			
		||||
    DTSC::Stream Strm; //Incoming stream buffer.
 | 
			
		||||
    HTTP::Parser HTTP_R, HTTP_S; //HTTP Receiver en HTTP Sender.
 | 
			
		||||
 | 
			
		||||
    bool ready4data = false; //Set to true when streaming is to begin.
 | 
			
		||||
    bool pending_manifest = false;
 | 
			
		||||
    bool inited = false;
 | 
			
		||||
    Socket::Connection ss( -1);
 | 
			
		||||
    std::string streamname;
 | 
			
		||||
    std::string recBuffer = "";
 | 
			
		||||
 | 
			
		||||
    bool wantsVideo = false;
 | 
			
		||||
    bool wantsAudio = false;
 | 
			
		||||
 | 
			
		||||
    std::string Quality;
 | 
			
		||||
    int Segment = -1;
 | 
			
		||||
    long long int ReqFragment = -1;
 | 
			
		||||
    int temp;
 | 
			
		||||
    std::string tempStr;
 | 
			
		||||
    int Flash_RequestPending = 0;
 | 
			
		||||
    unsigned int lastStats = 0;
 | 
			
		||||
    conn.setBlocking(false); //do not block on conn.spool() when no data is available
 | 
			
		||||
 | 
			
		||||
    while (conn.connected()){
 | 
			
		||||
      if (conn.spool() || conn.Received().size()){
 | 
			
		||||
        //make sure it ends in a \n
 | 
			
		||||
        if ( *(conn.Received().get().rbegin()) != '\n'){
 | 
			
		||||
          std::string tmp = conn.Received().get();
 | 
			
		||||
          conn.Received().get().clear();
 | 
			
		||||
          if (conn.Received().size()){
 | 
			
		||||
            conn.Received().get().insert(0, tmp);
 | 
			
		||||
          }else{
 | 
			
		||||
            conn.Received().append(tmp);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if (HTTP_R.Read(conn.Received().get())){
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          std::cout << "Received request: " << HTTP_R.getUrl() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
          conn.setHost(HTTP_R.GetHeader("X-Origin"));
 | 
			
		||||
          if (HTTP_R.url.find("Manifest") == std::string::npos){
 | 
			
		||||
            streamname = HTTP_R.url.substr(8, HTTP_R.url.find("/", 8) - 12);
 | 
			
		||||
            if ( !ss){
 | 
			
		||||
              ss = Util::Stream::getStream(streamname);
 | 
			
		||||
              if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
                fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
                ss.close();
 | 
			
		||||
                HTTP_S.Clean();
 | 
			
		||||
                HTTP_S.SetBody("No such stream " + streamname + " is available on the system. Please try again.\n");
 | 
			
		||||
                conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
                ready4data = false;
 | 
			
		||||
                continue;
 | 
			
		||||
              }
 | 
			
		||||
              ss.setBlocking(false);
 | 
			
		||||
              inited = true;
 | 
			
		||||
            }
 | 
			
		||||
            Quality = HTTP_R.url.substr(HTTP_R.url.find("/Q(", 8) + 3);
 | 
			
		||||
            Quality = Quality.substr(0, Quality.find(")"));
 | 
			
		||||
            tempStr = HTTP_R.url.substr(HTTP_R.url.find(")/") + 2);
 | 
			
		||||
            wantsAudio = false;
 | 
			
		||||
            wantsVideo = false;
 | 
			
		||||
            if (tempStr[0] == 'A'){
 | 
			
		||||
              wantsAudio = true;
 | 
			
		||||
            }
 | 
			
		||||
            if (tempStr[0] == 'V'){
 | 
			
		||||
              wantsVideo = true;
 | 
			
		||||
            }
 | 
			
		||||
            tempStr = tempStr.substr(tempStr.find("(") + 1);
 | 
			
		||||
            ReqFragment = atoll(tempStr.substr(0, tempStr.find(")")).c_str());
 | 
			
		||||
            std::stringstream sstream;
 | 
			
		||||
            sstream << "s " << (ReqFragment / 10000) << "\no \n";
 | 
			
		||||
            ss.SendNow(sstream.str().c_str());
 | 
			
		||||
            Flash_RequestPending++;
 | 
			
		||||
          }else{
 | 
			
		||||
            streamname = HTTP_R.url.substr(8, HTTP_R.url.find("/", 8) - 12);
 | 
			
		||||
            if ( !Strm.metadata.isNull()){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }else{
 | 
			
		||||
              pending_manifest = true;
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          ready4data = true;
 | 
			
		||||
          HTTP_R.Clean(); //clean for any possible next requests
 | 
			
		||||
        }
 | 
			
		||||
      }else{
 | 
			
		||||
        if (Flash_RequestPending){
 | 
			
		||||
          usleep(1000); //sleep 1ms
 | 
			
		||||
        }else{
 | 
			
		||||
          usleep(10000); //sleep 10ms
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (ready4data){
 | 
			
		||||
        if ( !inited){
 | 
			
		||||
          //we are ready, connect the socket!
 | 
			
		||||
          ss = Util::Stream::getStream(streamname);
 | 
			
		||||
          if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
            fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
            ss.close();
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetBody("No such stream " + streamname + " is available on the system. Please try again.\n");
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("404", "Not found"));
 | 
			
		||||
            ready4data = false;
 | 
			
		||||
            continue;
 | 
			
		||||
          }
 | 
			
		||||
          ss.setBlocking(false);
 | 
			
		||||
          inited = true;
 | 
			
		||||
        }
 | 
			
		||||
        unsigned int now = Util::epoch();
 | 
			
		||||
        if (now != lastStats){
 | 
			
		||||
          lastStats = now;
 | 
			
		||||
          ss.SendNow(conn.getStats("HTTP_Smooth").c_str());
 | 
			
		||||
        }
 | 
			
		||||
        if (ss.spool()){
 | 
			
		||||
          while (Strm.parsePacket(ss.Received())){
 | 
			
		||||
            if (pending_manifest){
 | 
			
		||||
              HTTP_S.Clean();
 | 
			
		||||
              HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
              HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
              std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
              HTTP_S.SetBody(manifest);
 | 
			
		||||
              conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
              pending_manifest = false;
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.lastType() == DTSC::PAUSEMARK){
 | 
			
		||||
#if DEBUG >= 4
 | 
			
		||||
              fprintf(stderr, "Received a %s fragment of %i bytes.\n", Strm.getPacket(0)["datatype"].asString().c_str(), FlashBufSize);
 | 
			
		||||
#endif
 | 
			
		||||
              if (Flash_RequestPending > 0 && FlashBufSize){
 | 
			
		||||
                //static std::string btstrp;
 | 
			
		||||
                //btstrp = GenerateBootstrap(streamname, Strm.metadata, ReqFragment, FlashBufTime, Strm.getPacket(0)["time"]);
 | 
			
		||||
                HTTP_S.Clean();
 | 
			
		||||
                HTTP_S.SetHeader("Content-Type", "video/mp4");
 | 
			
		||||
                HTTP_S.SetBody("");
 | 
			
		||||
 | 
			
		||||
                unsigned int myDuration;
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
                MP4::MFHD mfhd_box;
 | 
			
		||||
                for (int i = 0; i < Strm.metadata["keytime"].size(); i++){
 | 
			
		||||
                  if (Strm.metadata["keytime"][i].asInt() >= (ReqFragment / 10000)){
 | 
			
		||||
                    if (Strm.metadata.isMember("keynum")){
 | 
			
		||||
                      mfhd_box.setSequenceNumber(Strm.metadata["keynum"][i].asInt());
 | 
			
		||||
                    }else{
 | 
			
		||||
                      mfhd_box.setSequenceNumber(i + 1);
 | 
			
		||||
                    }
 | 
			
		||||
                    if (Strm.metadata.isMember("keylen")){
 | 
			
		||||
                      myDuration = Strm.metadata["keylen"][i].asInt() * 10000;
 | 
			
		||||
                    }else{
 | 
			
		||||
                      if (i != Strm.metadata["keytime"].size()){
 | 
			
		||||
                        myDuration = Strm.metadata["keytime"][i + 1].asInt() - Strm.metadata["keytime"][i].asInt();
 | 
			
		||||
                      }else{
 | 
			
		||||
                        myDuration = Strm.metadata["lastms"].asInt() - Strm.metadata["keytime"][i].asInt();
 | 
			
		||||
                      }
 | 
			
		||||
                      myDuration = myDuration * 10000;
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
                  }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                MP4::TFHD tfhd_box;
 | 
			
		||||
                tfhd_box.setFlags(MP4::tfhdSampleFlag);
 | 
			
		||||
                tfhd_box.setTrackID(1);
 | 
			
		||||
                tfhd_box.setDefaultSampleFlags(0x000000C0 | MP4::noIPicture | MP4::noDisposable | MP4::noKeySample);
 | 
			
		||||
 | 
			
		||||
                MP4::TRUN trun_box;
 | 
			
		||||
                //maybe reinsert dataOffset
 | 
			
		||||
                trun_box.setFlags(MP4::trundataOffset | MP4::trunfirstSampleFlags | MP4::trunsampleDuration | MP4::trunsampleSize);
 | 
			
		||||
                trun_box.setDataOffset(42);
 | 
			
		||||
                trun_box.setFirstSampleFlags(0x00000040 | MP4::isIPicture | MP4::noDisposable | MP4::isKeySample);
 | 
			
		||||
                for (int i = 0; i < FlashBuf.size(); i++){
 | 
			
		||||
                  MP4::trunSampleInformation trunSample;
 | 
			
		||||
                  trunSample.sampleSize = FlashBuf[i].size();
 | 
			
		||||
                  trunSample.sampleDuration = (((double)myDuration / FlashBuf.size()) * i) - (((double)myDuration / FlashBuf.size()) * (i - 1));
 | 
			
		||||
                  trun_box.setSampleInformation(trunSample, i);
 | 
			
		||||
                }
 | 
			
		||||
                MP4::SDTP sdtp_box;
 | 
			
		||||
                sdtp_box.setVersion(0);
 | 
			
		||||
                sdtp_box.setValue(0x24, 4);
 | 
			
		||||
                for (int i = 1; i < FlashBuf.size(); i++){
 | 
			
		||||
                  sdtp_box.setValue(0x14, 4 + i);
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                MP4::TRAF traf_box;
 | 
			
		||||
                traf_box.setContent(tfhd_box, 0);
 | 
			
		||||
                traf_box.setContent(trun_box, 1);
 | 
			
		||||
                traf_box.setContent(sdtp_box, 2);
 | 
			
		||||
                
 | 
			
		||||
                // if live, send fragref box if we can
 | 
			
		||||
                if (Strm.metadata.isMember("live")){
 | 
			
		||||
                  MP4::UUID_TrackFragmentReference fragref_box;
 | 
			
		||||
                  fragref_box.setVersion(1);
 | 
			
		||||
                  fragref_box.setFragmentCount(0);
 | 
			
		||||
                  int fragCount = 0;
 | 
			
		||||
                  for (int i = 0; i < Strm.metadata["keytime"].size(); i++){
 | 
			
		||||
                    if (Strm.metadata["keytime"][i].asInt() > (ReqFragment / 10000)){
 | 
			
		||||
                      fragref_box.setTime(fragCount, Strm.metadata["keytime"][i].asInt() * 10000);
 | 
			
		||||
                      fragref_box.setDuration(fragCount, Strm.metadata["keylen"][i].asInt() * 10000);
 | 
			
		||||
                      fragCount++;
 | 
			
		||||
                      fragref_box.setFragmentCount(fragCount);
 | 
			
		||||
                    }
 | 
			
		||||
                  }
 | 
			
		||||
                  traf_box.setContent(fragref_box, 3);
 | 
			
		||||
                  std::cout << fragref_box.toPrettyString(6) << std::endl;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                MP4::MOOF moof_box;
 | 
			
		||||
                moof_box.setContent(mfhd_box, 0);
 | 
			
		||||
                moof_box.setContent(traf_box, 1);
 | 
			
		||||
 | 
			
		||||
                //setting tha offsets!
 | 
			
		||||
                trun_box.setDataOffset(moof_box.boxedSize() + 8);
 | 
			
		||||
                traf_box.setContent(trun_box, 1);
 | 
			
		||||
                moof_box.setContent(traf_box, 1);
 | 
			
		||||
 | 
			
		||||
                //std::cerr << "\t[encoded] = " << ((MP4::TRUN&)(((MP4::TRAF&)(moof_box.getContent(1))).getContent(1))).getDataOffset() << std::endl;
 | 
			
		||||
 | 
			
		||||
                HTTP_S.SetHeader("Content-Length", FlashBufSize + 8 + moof_box.boxedSize()); //32+33+btstrp.size());
 | 
			
		||||
                conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
 | 
			
		||||
                conn.SendNow(moof_box.asBox(), moof_box.boxedSize());
 | 
			
		||||
 | 
			
		||||
                unsigned long size = htonl(FlashBufSize+8);
 | 
			
		||||
                conn.SendNow((char*) &size, 4);
 | 
			
		||||
                conn.SendNow("mdat", 4);
 | 
			
		||||
                while (FlashBuf.size() > 0){
 | 
			
		||||
                  conn.SendNow(FlashBuf.front());
 | 
			
		||||
                  FlashBuf.pop_front();
 | 
			
		||||
                }
 | 
			
		||||
                Flash_RequestPending--;
 | 
			
		||||
              }
 | 
			
		||||
              FlashBuf.clear();
 | 
			
		||||
              FlashBufSize = 0;
 | 
			
		||||
            }
 | 
			
		||||
            if ((wantsAudio && Strm.lastType() == DTSC::AUDIO) || (wantsVideo && Strm.lastType() == DTSC::VIDEO)){
 | 
			
		||||
              FlashBuf.push_back(Strm.lastData());
 | 
			
		||||
              FlashBufSize += Strm.lastData().size();
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          if (pending_manifest && !Strm.metadata.isNull()){
 | 
			
		||||
            HTTP_S.Clean();
 | 
			
		||||
            HTTP_S.SetHeader("Content-Type", "text/xml");
 | 
			
		||||
            HTTP_S.SetHeader("Cache-Control", "no-cache");
 | 
			
		||||
            std::string manifest = BuildManifest(streamname, Strm.metadata);
 | 
			
		||||
            HTTP_S.SetBody(manifest);
 | 
			
		||||
            conn.SendNow(HTTP_S.BuildResponse("200", "OK"));
 | 
			
		||||
            pending_manifest = false;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        if ( !ss.connected()){
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    conn.close();
 | 
			
		||||
    ss.SendNow(conn.getStats("HTTP_Smooth").c_str());
 | 
			
		||||
    ss.close();
 | 
			
		||||
    return 0;
 | 
			
		||||
  } //Connector_HTTP_Smooth main function
 | 
			
		||||
 | 
			
		||||
} //Connector_HTTP_Smooth namespace
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(1935);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server("/tmp/mist/http_smooth");
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return Connector_HTTP::Connector_HTTP_Dynamic(S);
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										50
									
								
								src/connectors/conn_raw.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/connectors/conn_raw.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,50 @@
 | 
			
		|||
/// \file conn_raw.cpp
 | 
			
		||||
/// Contains the main code for the RAW connector.
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
 | 
			
		||||
/// Contains the main code for the RAW connector.
 | 
			
		||||
/// Expects a single commandline argument telling it which stream to connect to,
 | 
			
		||||
/// then outputs the raw stream to stdout.
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addOption("stream_name", JSON::fromString("{\"arg_num\":1, \"help\":\"Name of the stream to write to stdout.\"}"));
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
 | 
			
		||||
  //connect to the proper stream
 | 
			
		||||
  Socket::Connection S = Util::Stream::getStream(conf.getString("stream_name"));
 | 
			
		||||
  S.setBlocking(false);
 | 
			
		||||
  if ( !S.connected()){
 | 
			
		||||
    std::cout << "Could not open stream " << conf.getString("stream_name") << std::endl;
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  long long int lastStats = 0;
 | 
			
		||||
  long long int started = Util::epoch();
 | 
			
		||||
  while (std::cout.good()){
 | 
			
		||||
    if (S.spool()){
 | 
			
		||||
      while (S.Received().size()){
 | 
			
		||||
        std::cout.write(S.Received().get().c_str(), S.Received().get().size());
 | 
			
		||||
        S.Received().get().clear();
 | 
			
		||||
      }
 | 
			
		||||
    }else{
 | 
			
		||||
      Util::sleep(10); //sleep 10ms if no data
 | 
			
		||||
    }
 | 
			
		||||
    unsigned int now = Util::epoch();
 | 
			
		||||
    if (now != lastStats){
 | 
			
		||||
      lastStats = now;
 | 
			
		||||
      std::stringstream st;
 | 
			
		||||
      st << "S localhost RAW " << (Util::epoch() - started) << " " << S.dataDown() << " " << S.dataUp() << "\n";
 | 
			
		||||
      S.SendNow(st.str().c_str());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  std::stringstream st;
 | 
			
		||||
  st << "S localhost RAW " << (Util::epoch() - started) << " " << S.dataDown() << " " << S.dataUp() << "\n";
 | 
			
		||||
  S.SendNow(st.str().c_str());
 | 
			
		||||
  S.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										616
									
								
								src/connectors/conn_rtmp.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										616
									
								
								src/connectors/conn_rtmp.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,616 @@
 | 
			
		|||
/// \file conn_rtmp.cpp
 | 
			
		||||
/// Contains the main code for the RTMP Connector
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <mist/flv_tag.h>
 | 
			
		||||
#include <mist/amf.h>
 | 
			
		||||
#include <mist/rtmpchunks.h>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/timing.h>
 | 
			
		||||
 | 
			
		||||
/// Holds all functions and data unique to the RTMP Connector
 | 
			
		||||
namespace Connector_RTMP {
 | 
			
		||||
 | 
			
		||||
  //for connection to server
 | 
			
		||||
  bool ready4data = false; ///< Set to true when streaming starts.
 | 
			
		||||
  bool inited = false; ///< Set to true when ready to connect to Buffer.
 | 
			
		||||
  bool nostats = false; ///< Set to true if no stats should be sent anymore (push mode).
 | 
			
		||||
  bool stopparsing = false; ///< Set to true when all parsing needs to be cancelled.
 | 
			
		||||
 | 
			
		||||
  //for reply to play command
 | 
			
		||||
  int play_trans = -1;
 | 
			
		||||
  int play_streamid = -1;
 | 
			
		||||
  int play_msgtype = -1;
 | 
			
		||||
 | 
			
		||||
  //generic state keeping
 | 
			
		||||
  bool stream_inited = false; ///true if init data for audio/video was sent
 | 
			
		||||
 | 
			
		||||
  Socket::Connection Socket; ///< Socket connected to user
 | 
			
		||||
  Socket::Connection SS; ///< Socket connected to server
 | 
			
		||||
  std::string streamname; ///< Stream that will be opened
 | 
			
		||||
  void parseChunk(Socket::Buffer & buffer); ///< Parses a single RTMP chunk.
 | 
			
		||||
  void sendCommand(AMF::Object & amfreply, int messagetype, int stream_id); ///< Sends a RTMP command either in AMF or AMF3 mode.
 | 
			
		||||
  void parseAMFCommand(AMF::Object & amfdata, int messagetype, int stream_id); ///< Parses a single AMF command message.
 | 
			
		||||
  int Connector_RTMP(Socket::Connection conn);
 | 
			
		||||
} //Connector_RTMP namespace;
 | 
			
		||||
 | 
			
		||||
/// Main Connector_RTMP function
 | 
			
		||||
int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
 | 
			
		||||
  Socket = conn;
 | 
			
		||||
  Socket.setBlocking(false);
 | 
			
		||||
  FLV::Tag tag, init_tag;
 | 
			
		||||
  DTSC::Stream Strm;
 | 
			
		||||
 | 
			
		||||
  while ( !Socket.Received().available(1537) && Socket.connected()){
 | 
			
		||||
    Socket.spool();
 | 
			
		||||
    Util::sleep(5);
 | 
			
		||||
  }
 | 
			
		||||
  RTMPStream::handshake_in = Socket.Received().remove(1537);
 | 
			
		||||
  RTMPStream::rec_cnt += 1537;
 | 
			
		||||
 | 
			
		||||
  if (RTMPStream::doHandshake()){
 | 
			
		||||
    Socket.SendNow(RTMPStream::handshake_out);
 | 
			
		||||
    while ( !Socket.Received().available(1536) && Socket.connected()){
 | 
			
		||||
      Socket.spool();
 | 
			
		||||
      Util::sleep(5);
 | 
			
		||||
    }
 | 
			
		||||
    Socket.Received().remove(1536);
 | 
			
		||||
    RTMPStream::rec_cnt += 1536;
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
    fprintf(stderr, "Handshake succcess!\n");
 | 
			
		||||
#endif
 | 
			
		||||
  }else{
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
    fprintf(stderr, "Handshake fail!\n");
 | 
			
		||||
#endif
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  unsigned int lastStats = 0;
 | 
			
		||||
  bool firsttime = true;
 | 
			
		||||
 | 
			
		||||
  while (Socket.connected()){
 | 
			
		||||
    if (Socket.spool() || firsttime){
 | 
			
		||||
      parseChunk(Socket.Received());
 | 
			
		||||
      firsttime = false;
 | 
			
		||||
    }else{
 | 
			
		||||
      Util::sleep(1); //sleep 1ms to prevent high CPU usage
 | 
			
		||||
    }
 | 
			
		||||
    if (ready4data){
 | 
			
		||||
      if ( !inited){
 | 
			
		||||
        //we are ready, connect the socket!
 | 
			
		||||
        SS = Util::Stream::getStream(streamname);
 | 
			
		||||
        if ( !SS.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
          fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
          Socket.close(); //disconnect user
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        SS.setBlocking(false);
 | 
			
		||||
        SS.SendNow("p\n");
 | 
			
		||||
        inited = true;
 | 
			
		||||
      }
 | 
			
		||||
      if (inited && !nostats){
 | 
			
		||||
        long long int now = Util::epoch();
 | 
			
		||||
        if (now != lastStats){
 | 
			
		||||
          lastStats = now;
 | 
			
		||||
          SS.SendNow(Socket.getStats("RTMP").c_str());
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (SS.spool()){
 | 
			
		||||
        while (Strm.parsePacket(SS.Received())){
 | 
			
		||||
          if (play_trans != -1){
 | 
			
		||||
            //send a status reply
 | 
			
		||||
            AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
            amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
            amfreply.addContent(AMF::Object("", (double)play_trans)); //same transaction ID
 | 
			
		||||
            amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
            amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Reset"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing and resetting..."));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("details", "DDV"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
            sendCommand(amfreply, play_msgtype, play_streamid);
 | 
			
		||||
            //send streamisrecorded if stream, well, is recorded.
 | 
			
		||||
            if (Strm.metadata.isMember("length") && Strm.metadata["length"].asInt() > 0){
 | 
			
		||||
              Socket.Send(RTMPStream::SendUSR(4, 1)); //send UCM StreamIsRecorded (4), stream 1
 | 
			
		||||
            }
 | 
			
		||||
            //send streambegin
 | 
			
		||||
            Socket.Send(RTMPStream::SendUSR(0, 1)); //send UCM StreamBegin (0), stream 1
 | 
			
		||||
            //and more reply
 | 
			
		||||
            amfreply = AMF::Object("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
            amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
            amfreply.addContent(AMF::Object("", (double)play_trans)); //same transaction ID
 | 
			
		||||
            amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
            amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Start"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing!"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("details", "DDV"));
 | 
			
		||||
            amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
            sendCommand(amfreply, play_msgtype, play_streamid);
 | 
			
		||||
            RTMPStream::chunk_snd_max = 102400; //100KiB
 | 
			
		||||
            Socket.Send(RTMPStream::SendCTL(1, RTMPStream::chunk_snd_max)); //send chunk size max (msg 1)
 | 
			
		||||
            //send dunno?
 | 
			
		||||
            Socket.Send(RTMPStream::SendUSR(32, 1)); //send UCM no clue?, stream 1
 | 
			
		||||
            play_trans = -1;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          //sent init data if needed
 | 
			
		||||
          if ( !stream_inited){
 | 
			
		||||
            init_tag.DTSCMetaInit(Strm);
 | 
			
		||||
            Socket.SendNow(RTMPStream::SendMedia(init_tag));
 | 
			
		||||
            if (Strm.metadata.isMember("audio") && Strm.metadata["audio"].isMember("init")){
 | 
			
		||||
              init_tag.DTSCAudioInit(Strm);
 | 
			
		||||
              Socket.SendNow(RTMPStream::SendMedia(init_tag));
 | 
			
		||||
            }
 | 
			
		||||
            if (Strm.metadata.isMember("video") && Strm.metadata["video"].isMember("init")){
 | 
			
		||||
              init_tag.DTSCVideoInit(Strm);
 | 
			
		||||
              Socket.SendNow(RTMPStream::SendMedia(init_tag));
 | 
			
		||||
            }
 | 
			
		||||
            stream_inited = true;
 | 
			
		||||
          }
 | 
			
		||||
          //sent a tag
 | 
			
		||||
          tag.DTSCLoader(Strm);
 | 
			
		||||
          Socket.SendNow(RTMPStream::SendMedia(tag));
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
          fprintf(stderr, "Sent tag to %i: [%u] %s\n", Socket.getSocket(), tag.tagTime(), tag.tagType().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  Socket.close();
 | 
			
		||||
  SS.SendNow(Socket.getStats("RTMP").c_str());
 | 
			
		||||
  SS.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //Connector_RTMP
 | 
			
		||||
 | 
			
		||||
/// Tries to get and parse one RTMP chunk at a time.
 | 
			
		||||
void Connector_RTMP::parseChunk(Socket::Buffer & inbuffer){
 | 
			
		||||
  //for DTSC conversion
 | 
			
		||||
  static JSON::Value meta_out;
 | 
			
		||||
  static std::stringstream prebuffer; // Temporary buffer before sending real data
 | 
			
		||||
  static bool sending = false;
 | 
			
		||||
  static unsigned int counter = 0;
 | 
			
		||||
  //for chunk parsing
 | 
			
		||||
  static RTMPStream::Chunk next;
 | 
			
		||||
  FLV::Tag F;
 | 
			
		||||
  static AMF::Object amfdata("empty", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
  static AMF::Object amfelem("empty", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
  static AMF::Object3 amf3data("empty", AMF::AMF3_DDV_CONTAINER);
 | 
			
		||||
  static AMF::Object3 amf3elem("empty", AMF::AMF3_DDV_CONTAINER);
 | 
			
		||||
 | 
			
		||||
  while (next.Parse(inbuffer)){
 | 
			
		||||
 | 
			
		||||
    //send ACK if we received a whole window
 | 
			
		||||
    if ((RTMPStream::rec_cnt - RTMPStream::rec_window_at > RTMPStream::rec_window_size)){
 | 
			
		||||
      RTMPStream::rec_window_at = RTMPStream::rec_cnt;
 | 
			
		||||
      Socket.Send(RTMPStream::SendCTL(3, RTMPStream::rec_cnt)); //send ack (msg 3)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch (next.msg_type_id){
 | 
			
		||||
      case 0: //does not exist
 | 
			
		||||
#if DEBUG >= 2
 | 
			
		||||
        fprintf(stderr, "UNKN: Received a zero-type message. This is an error.\n");
 | 
			
		||||
#endif
 | 
			
		||||
        break; //happens when connection breaks unexpectedly
 | 
			
		||||
      case 1: //set chunk size
 | 
			
		||||
        RTMPStream::chunk_rec_max = ntohl(*(int*)next.data.c_str());
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "CTRL: Set chunk size: %i\n", RTMPStream::chunk_rec_max);
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
      case 2: //abort message - we ignore this one
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "CTRL: Abort message\n");
 | 
			
		||||
#endif
 | 
			
		||||
        //4 bytes of stream id to drop
 | 
			
		||||
        break;
 | 
			
		||||
      case 3: //ack
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
        fprintf(stderr, "CTRL: Acknowledgement\n");
 | 
			
		||||
#endif
 | 
			
		||||
        RTMPStream::snd_window_at = ntohl(*(int*)next.data.c_str());
 | 
			
		||||
        RTMPStream::snd_window_at = RTMPStream::snd_cnt;
 | 
			
		||||
        break;
 | 
			
		||||
      case 4: {
 | 
			
		||||
        //2 bytes event type, rest = event data
 | 
			
		||||
        //types:
 | 
			
		||||
        //0 = stream begin, 4 bytes ID
 | 
			
		||||
        //1 = stream EOF, 4 bytes ID
 | 
			
		||||
        //2 = stream dry, 4 bytes ID
 | 
			
		||||
        //3 = setbufferlen, 4 bytes ID, 4 bytes length
 | 
			
		||||
        //4 = streamisrecorded, 4 bytes ID
 | 
			
		||||
        //6 = pingrequest, 4 bytes data
 | 
			
		||||
        //7 = pingresponse, 4 bytes data
 | 
			
		||||
        //we don't need to process this
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        short int ucmtype = ntohs(*(short int*)next.data.c_str());
 | 
			
		||||
        switch (ucmtype){
 | 
			
		||||
          case 0:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM StreamBegin %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 1:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM StreamEOF %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 2:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM StreamDry %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 3:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM SetBufferLength %i %i\n", ntohl(*((int*)(next.data.c_str()+2))), ntohl(*((int*)(next.data.c_str()+6))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 4:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM StreamIsRecorded %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 6:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM PingRequest %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          case 7:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM PingResponse %i\n", ntohl(*((int*)(next.data.c_str()+2))));
 | 
			
		||||
            break;
 | 
			
		||||
          default:
 | 
			
		||||
            fprintf(stderr, "CTRL: UCM Unknown (%hi)\n", ucmtype);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
        break;
 | 
			
		||||
      case 5: //window size of other end
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "CTRL: Window size\n");
 | 
			
		||||
#endif
 | 
			
		||||
        RTMPStream::rec_window_size = ntohl(*(int*)next.data.c_str());
 | 
			
		||||
        RTMPStream::rec_window_at = RTMPStream::rec_cnt;
 | 
			
		||||
        Socket.Send(RTMPStream::SendCTL(3, RTMPStream::rec_cnt)); //send ack (msg 3)
 | 
			
		||||
        break;
 | 
			
		||||
      case 6:
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "CTRL: Set peer bandwidth\n");
 | 
			
		||||
#endif
 | 
			
		||||
        //4 bytes window size, 1 byte limit type (ignored)
 | 
			
		||||
        RTMPStream::snd_window_size = ntohl(*(int*)next.data.c_str());
 | 
			
		||||
        Socket.Send(RTMPStream::SendCTL(5, RTMPStream::snd_window_size)); //send window acknowledgement size (msg 5)
 | 
			
		||||
        break;
 | 
			
		||||
      case 8: //audio data
 | 
			
		||||
      case 9: //video data
 | 
			
		||||
      case 18: //meta data
 | 
			
		||||
        if (SS.connected()){
 | 
			
		||||
          F.ChunkLoader(next);
 | 
			
		||||
          JSON::Value pack_out = F.toJSON(meta_out);
 | 
			
		||||
          if ( !pack_out.isNull()){
 | 
			
		||||
            if ( !sending){
 | 
			
		||||
              counter++;
 | 
			
		||||
              if (counter > 8){
 | 
			
		||||
                sending = true;
 | 
			
		||||
                SS.SendNow(meta_out.toNetPacked());
 | 
			
		||||
                SS.SendNow(prebuffer.str().c_str(), prebuffer.str().size()); //write buffer
 | 
			
		||||
                prebuffer.str(""); //clear buffer
 | 
			
		||||
                SS.SendNow(pack_out.toNetPacked());
 | 
			
		||||
              }else{
 | 
			
		||||
                prebuffer << pack_out.toNetPacked();
 | 
			
		||||
              }
 | 
			
		||||
            }else{
 | 
			
		||||
              SS.SendNow(pack_out.toNetPacked());
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }else{
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          fprintf(stderr, "Received useless media data\n");
 | 
			
		||||
#endif
 | 
			
		||||
          Socket.close();
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
      case 15:
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Received AFM3 data message\n");
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
      case 16:
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Received AFM3 shared object\n");
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
      case 17: {
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Received AFM3 command message\n");
 | 
			
		||||
#endif
 | 
			
		||||
        if (next.data[0] != 0){
 | 
			
		||||
          next.data = next.data.substr(1);
 | 
			
		||||
          amf3data = AMF::parse3(next.data);
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          amf3data.Print();
 | 
			
		||||
#endif
 | 
			
		||||
        }else{
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
          fprintf(stderr, "Received AFM3-0 command message\n");
 | 
			
		||||
#endif
 | 
			
		||||
          next.data = next.data.substr(1);
 | 
			
		||||
          amfdata = AMF::parse(next.data);
 | 
			
		||||
          parseAMFCommand(amfdata, 17, next.msg_stream_id);
 | 
			
		||||
        } //parsing AMF0-style
 | 
			
		||||
      }
 | 
			
		||||
        break;
 | 
			
		||||
      case 19:
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Received AFM0 shared object\n");
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
      case 20: { //AMF0 command message
 | 
			
		||||
        amfdata = AMF::parse(next.data);
 | 
			
		||||
        parseAMFCommand(amfdata, 20, next.msg_stream_id);
 | 
			
		||||
      }
 | 
			
		||||
        break;
 | 
			
		||||
      case 22:
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Received aggregate message\n");
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
        fprintf(stderr, "Unknown chunk received! Probably protocol corruption, stopping parsing of incoming data.\n");
 | 
			
		||||
#endif
 | 
			
		||||
        Connector_RTMP::stopparsing = true;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
} //parseChunk
 | 
			
		||||
 | 
			
		||||
void Connector_RTMP::sendCommand(AMF::Object & amfreply, int messagetype, int stream_id){
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
  std::cerr << amfreply.Print() << std::endl;
 | 
			
		||||
#endif
 | 
			
		||||
  if (messagetype == 17){
 | 
			
		||||
    Socket.SendNow(RTMPStream::SendChunk(3, messagetype, stream_id, (char)0 + amfreply.Pack()));
 | 
			
		||||
  }else{
 | 
			
		||||
    Socket.SendNow(RTMPStream::SendChunk(3, messagetype, stream_id, amfreply.Pack()));
 | 
			
		||||
  }
 | 
			
		||||
} //sendCommand
 | 
			
		||||
 | 
			
		||||
void Connector_RTMP::parseAMFCommand(AMF::Object & amfdata, int messagetype, int stream_id){
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
  fprintf(stderr, "Received command: %s\n", amfdata.Print().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
#if DEBUG >= 8
 | 
			
		||||
  fprintf(stderr, "AMF0 command: %s\n", amfdata.getContentP(0)->StrValue().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
  if (amfdata.getContentP(0)->StrValue() == "connect"){
 | 
			
		||||
    double objencoding = 0;
 | 
			
		||||
    if (amfdata.getContentP(2)->getContentP("objectEncoding")){
 | 
			
		||||
      objencoding = amfdata.getContentP(2)->getContentP("objectEncoding")->NumValue();
 | 
			
		||||
    }
 | 
			
		||||
#if DEBUG >= 6
 | 
			
		||||
    int tmpint;
 | 
			
		||||
    if (amfdata.getContentP(2)->getContentP("videoCodecs")){
 | 
			
		||||
      tmpint = (int)amfdata.getContentP(2)->getContentP("videoCodecs")->NumValue();
 | 
			
		||||
      if (tmpint & 0x04){
 | 
			
		||||
        fprintf(stderr, "Sorensen video support detected\n");
 | 
			
		||||
      }
 | 
			
		||||
      if (tmpint & 0x80){
 | 
			
		||||
        fprintf(stderr, "H264 video support detected\n");
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if (amfdata.getContentP(2)->getContentP("audioCodecs")){
 | 
			
		||||
      tmpint = (int)amfdata.getContentP(2)->getContentP("audioCodecs")->NumValue();
 | 
			
		||||
      if (tmpint & 0x04){
 | 
			
		||||
        fprintf(stderr, "MP3 audio support detected\n");
 | 
			
		||||
      }
 | 
			
		||||
      if (tmpint & 0x400){
 | 
			
		||||
        fprintf(stderr, "AAC audio support detected\n");
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
    RTMPStream::chunk_snd_max = 4096;
 | 
			
		||||
    Socket.Send(RTMPStream::SendCTL(1, RTMPStream::chunk_snd_max)); //send chunk size max (msg 1)
 | 
			
		||||
    Socket.Send(RTMPStream::SendCTL(5, RTMPStream::snd_window_size)); //send window acknowledgement size (msg 5)
 | 
			
		||||
    Socket.Send(RTMPStream::SendCTL(6, RTMPStream::rec_window_size)); //send rec window acknowledgement size (msg 6)
 | 
			
		||||
    Socket.Send(RTMPStream::SendUSR(0, 1)); //send UCM StreamBegin (0), stream 1
 | 
			
		||||
    //send a _result reply
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "_result")); //result success
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("")); //server properties
 | 
			
		||||
    amfreply.getContentP(2)->addContent(AMF::Object("fmsVer", "FMS/3,5,5,2004"));
 | 
			
		||||
    amfreply.getContentP(2)->addContent(AMF::Object("capabilities", (double)31));
 | 
			
		||||
    amfreply.getContentP(2)->addContent(AMF::Object("mode", (double)1));
 | 
			
		||||
    amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("code", "NetConnection.Connect.Success"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("description", "Connection succeeded."));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("clientid", 1337));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("objectEncoding", objencoding));
 | 
			
		||||
    //amfreply.getContentP(3)->addContent(AMF::Object("data", AMF::AMF0_ECMA_ARRAY));
 | 
			
		||||
    //amfreply.getContentP(3)->getContentP(4)->addContent(AMF::Object("version", "3,5,4,1004"));
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    //send onBWDone packet - no clue what it is, but real server sends it...
 | 
			
		||||
    //amfreply = AMF::Object("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    //amfreply.addContent(AMF::Object("", "onBWDone"));//result
 | 
			
		||||
    //amfreply.addContent(amfdata.getContent(1));//same transaction ID
 | 
			
		||||
    //amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL));//null
 | 
			
		||||
    //sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    return;
 | 
			
		||||
  } //connect
 | 
			
		||||
  if (amfdata.getContentP(0)->StrValue() == "createStream"){
 | 
			
		||||
    //send a _result reply
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "_result")); //result success
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)1)); //stream ID - we use 1
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    Socket.Send(RTMPStream::SendUSR(0, 1)); //send UCM StreamBegin (0), stream 1
 | 
			
		||||
    return;
 | 
			
		||||
  } //createStream
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "closeStream") || (amfdata.getContentP(0)->StrValue() == "deleteStream")){
 | 
			
		||||
    if (SS.connected()){
 | 
			
		||||
      SS.close();
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "getStreamLength") || (amfdata.getContentP(0)->StrValue() == "getMovLen")){
 | 
			
		||||
    //send a _result reply
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "_result")); //result success
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0)); //zero length
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    return;
 | 
			
		||||
  } //getStreamLength
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "publish")){
 | 
			
		||||
    if (amfdata.getContentP(3)){
 | 
			
		||||
      streamname = amfdata.getContentP(3)->StrValue();
 | 
			
		||||
      /// \todo implement push for MistPlayer or restrict and change to getLive
 | 
			
		||||
      SS = Util::Stream::getStream(streamname);
 | 
			
		||||
      if ( !SS.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
        fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
        Socket.close(); //disconnect user
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      SS.Send("P ");
 | 
			
		||||
      SS.Send(Socket.getHost().c_str());
 | 
			
		||||
      SS.Send("\n");
 | 
			
		||||
      nostats = true;
 | 
			
		||||
    }
 | 
			
		||||
    //send a _result reply
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "_result")); //result success
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("", 1, AMF::AMF0_BOOL)); //publish success?
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    Socket.Send(RTMPStream::SendUSR(0, 1)); //send UCM StreamBegin (0), stream 1
 | 
			
		||||
    //send a status reply
 | 
			
		||||
    amfreply = AMF::Object("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
    amfreply.addContent(AMF::Object("", 0, AMF::AMF0_NUMBER)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Publish.Start"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("description", "Stream is now published!"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    return;
 | 
			
		||||
  } //getStreamLength
 | 
			
		||||
  if (amfdata.getContentP(0)->StrValue() == "checkBandwidth"){
 | 
			
		||||
    //send a _result reply
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "_result")); //result success
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    sendCommand(amfreply, messagetype, stream_id);
 | 
			
		||||
    return;
 | 
			
		||||
  } //checkBandwidth
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "play") || (amfdata.getContentP(0)->StrValue() == "play2")){
 | 
			
		||||
    //set reply number and stream name, actual reply is sent up in the SS.spool() handler
 | 
			
		||||
    play_trans = amfdata.getContentP(1)->NumValue();
 | 
			
		||||
    play_msgtype = messagetype;
 | 
			
		||||
    play_streamid = stream_id;
 | 
			
		||||
    streamname = amfdata.getContentP(3)->StrValue();
 | 
			
		||||
    Connector_RTMP::ready4data = true; //start sending video data!
 | 
			
		||||
    return;
 | 
			
		||||
  } //play
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "seek")){
 | 
			
		||||
    //set reply number and stream name, actual reply is sent up in the SS.spool() handler
 | 
			
		||||
    play_trans = amfdata.getContentP(1)->NumValue();
 | 
			
		||||
    play_msgtype = messagetype;
 | 
			
		||||
    play_streamid = stream_id;
 | 
			
		||||
    stream_inited = false;
 | 
			
		||||
 | 
			
		||||
    AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
    amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
    amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
    amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
    amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Seek.Notify"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("description", "Seeking to the specified time"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("details", "DDV"));
 | 
			
		||||
    amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
    sendCommand(amfreply, play_msgtype, play_streamid);
 | 
			
		||||
    SS.Send("s ");
 | 
			
		||||
    SS.Send(JSON::Value((long long int)amfdata.getContentP(3)->NumValue()).asString().c_str());
 | 
			
		||||
    SS.Send("\n");
 | 
			
		||||
    return;
 | 
			
		||||
  } //seek
 | 
			
		||||
  if ((amfdata.getContentP(0)->StrValue() == "pauseRaw") || (amfdata.getContentP(0)->StrValue() == "pause")){
 | 
			
		||||
    if (amfdata.getContentP(3)->NumValue()){
 | 
			
		||||
      SS.Send("q\n"); //quit playing
 | 
			
		||||
      //send a status reply
 | 
			
		||||
      AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
      amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
      amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
      amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
      amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Pause.Notify"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("description", "Pausing playback"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("details", "DDV"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
      sendCommand(amfreply, play_msgtype, play_streamid);
 | 
			
		||||
    }else{
 | 
			
		||||
      SS.Send("p\n"); //start playing
 | 
			
		||||
      //send a status reply
 | 
			
		||||
      AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
 | 
			
		||||
      amfreply.addContent(AMF::Object("", "onStatus")); //status reply
 | 
			
		||||
      amfreply.addContent(amfdata.getContent(1)); //same transaction ID
 | 
			
		||||
      amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL)); //null - command info
 | 
			
		||||
      amfreply.addContent(AMF::Object("")); //info
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Unpause.Notify"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("description", "Resuming playback"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("details", "DDV"));
 | 
			
		||||
      amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
 | 
			
		||||
      sendCommand(amfreply, play_msgtype, play_streamid);
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  } //seek
 | 
			
		||||
 | 
			
		||||
#if DEBUG >= 2
 | 
			
		||||
  fprintf(stderr, "AMF0 command not processed! :(\n");
 | 
			
		||||
#endif
 | 
			
		||||
} //parseAMFCommand
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addConnectorOptions(1935);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server(conf.getInteger("listen_port"), conf.getString("listen_interface"));
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return Connector_RTMP::Connector_RTMP(S);
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										205
									
								
								src/connectors/conn_ts.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										205
									
								
								src/connectors/conn_ts.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,205 @@
 | 
			
		|||
/// \file conn_ts.cpp
 | 
			
		||||
/// Contains the main code for the TS Connector
 | 
			
		||||
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <sys/time.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <mist/socket.h>
 | 
			
		||||
#include <mist/config.h>
 | 
			
		||||
#include <mist/stream.h>
 | 
			
		||||
#include <mist/ts_packet.h> //TS support
 | 
			
		||||
#include <mist/dtsc.h> //DTSC support
 | 
			
		||||
#include <mist/mp4.h> //For initdata conversion
 | 
			
		||||
/// The main function of the connector
 | 
			
		||||
/// \param conn A connection with the client
 | 
			
		||||
/// \param streamname The name of the stream
 | 
			
		||||
int TS_Handler(Socket::Connection conn, std::string streamname){
 | 
			
		||||
  std::string ToPack;
 | 
			
		||||
  TS::Packet PackData;
 | 
			
		||||
  std::string DTMIData;
 | 
			
		||||
  int PacketNumber = 0;
 | 
			
		||||
  long long unsigned int TimeStamp = 0;
 | 
			
		||||
  int ThisNaluSize;
 | 
			
		||||
  char VideoCounter = 0;
 | 
			
		||||
  char AudioCounter = 0;
 | 
			
		||||
  bool WritePesHeader;
 | 
			
		||||
  bool IsKeyFrame;
 | 
			
		||||
  bool FirstKeyFrame = true;
 | 
			
		||||
  bool FirstIDRInKeyFrame;
 | 
			
		||||
  MP4::AVCC avccbox;
 | 
			
		||||
  bool haveAvcc = false;
 | 
			
		||||
 | 
			
		||||
  DTSC::Stream Strm;
 | 
			
		||||
  bool inited = false;
 | 
			
		||||
  Socket::Connection ss;
 | 
			
		||||
 | 
			
		||||
  while (conn.connected()){
 | 
			
		||||
    if ( !inited){
 | 
			
		||||
      ss = Util::Stream::getStream(streamname);
 | 
			
		||||
      if ( !ss.connected()){
 | 
			
		||||
#if DEBUG >= 1
 | 
			
		||||
        fprintf(stderr, "Could not connect to server!\n");
 | 
			
		||||
#endif
 | 
			
		||||
        conn.close();
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      ss.SendNow("p\n");
 | 
			
		||||
      inited = true;
 | 
			
		||||
    }
 | 
			
		||||
    if (ss.spool()){
 | 
			
		||||
      while (Strm.parsePacket(ss.Received())){
 | 
			
		||||
        if ( !haveAvcc){
 | 
			
		||||
          avccbox.setPayload(Strm.metadata["video"]["init"].asString());
 | 
			
		||||
          haveAvcc = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (Strm.lastType() == DTSC::VIDEO){
 | 
			
		||||
          DTMIData = Strm.lastData();
 | 
			
		||||
          if (Strm.getPacket(0).isMember("keyframe")){
 | 
			
		||||
            IsKeyFrame = true;
 | 
			
		||||
            FirstIDRInKeyFrame = true;
 | 
			
		||||
          }else{
 | 
			
		||||
            IsKeyFrame = false;
 | 
			
		||||
            FirstKeyFrame = false;
 | 
			
		||||
          }
 | 
			
		||||
          if (IsKeyFrame){
 | 
			
		||||
            TimeStamp = (Strm.getPacket(0)["time"].asInt() * 27000);
 | 
			
		||||
          }
 | 
			
		||||
          int TSType;
 | 
			
		||||
          bool FirstPic = true;
 | 
			
		||||
          while (DTMIData.size()){
 | 
			
		||||
            ThisNaluSize = (DTMIData[0] << 24) + (DTMIData[1] << 16) + (DTMIData[2] << 8) + DTMIData[3];
 | 
			
		||||
            DTMIData.erase(0, 4); //Erase the first four characters;
 | 
			
		||||
            TSType = (int)DTMIData[0] & 0x1F;
 | 
			
		||||
            if (TSType == 0x05){
 | 
			
		||||
              if (FirstPic){
 | 
			
		||||
                ToPack += avccbox.asAnnexB();
 | 
			
		||||
                FirstPic = false;
 | 
			
		||||
              }
 | 
			
		||||
              if (IsKeyFrame){
 | 
			
		||||
                if ( !FirstKeyFrame && FirstIDRInKeyFrame){
 | 
			
		||||
                  ToPack.append(TS::NalHeader, 4);
 | 
			
		||||
                  FirstIDRInKeyFrame = false;
 | 
			
		||||
                }else{
 | 
			
		||||
                  ToPack.append(TS::ShortNalHeader, 3);
 | 
			
		||||
                }
 | 
			
		||||
              }
 | 
			
		||||
            }else if (TSType == 0x01){
 | 
			
		||||
              if (FirstPic){
 | 
			
		||||
                ToPack.append(TS::NalHeader, 4);
 | 
			
		||||
                FirstPic = false;
 | 
			
		||||
              }else{
 | 
			
		||||
                ToPack.append(TS::ShortNalHeader, 3);
 | 
			
		||||
              }
 | 
			
		||||
            }else{
 | 
			
		||||
              ToPack.append(TS::NalHeader, 4);
 | 
			
		||||
            }
 | 
			
		||||
            ToPack.append(DTMIData, 0, ThisNaluSize);
 | 
			
		||||
            DTMIData.erase(0, ThisNaluSize);
 | 
			
		||||
          }
 | 
			
		||||
          WritePesHeader = true;
 | 
			
		||||
          while (ToPack.size()){
 | 
			
		||||
            if ((PacketNumber % 42) == 0){
 | 
			
		||||
              PackData.DefaultPAT();
 | 
			
		||||
              conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
              PackData.DefaultPMT();
 | 
			
		||||
              conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
              PacketNumber += 2;
 | 
			
		||||
            }
 | 
			
		||||
            PackData.Clear();
 | 
			
		||||
            PackData.PID(0x100);
 | 
			
		||||
            PackData.ContinuityCounter(VideoCounter);
 | 
			
		||||
            VideoCounter++;
 | 
			
		||||
            if (WritePesHeader){
 | 
			
		||||
              PackData.UnitStart(1);
 | 
			
		||||
              if (IsKeyFrame){
 | 
			
		||||
                PackData.RandomAccess(1);
 | 
			
		||||
                PackData.PCR(TimeStamp);
 | 
			
		||||
              }else{
 | 
			
		||||
                PackData.AdaptationField(1);
 | 
			
		||||
              }
 | 
			
		||||
              PackData.AddStuffing(184 - (20 + ToPack.size()));
 | 
			
		||||
              PackData.PESVideoLeadIn(ToPack.size(), Strm.getPacket(0)["time"].asInt() * 90);
 | 
			
		||||
              WritePesHeader = false;
 | 
			
		||||
            }else{
 | 
			
		||||
              PackData.AdaptationField(1);
 | 
			
		||||
              PackData.AddStuffing(184 - (ToPack.size()));
 | 
			
		||||
            }
 | 
			
		||||
            PackData.FillFree(ToPack);
 | 
			
		||||
            conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
            PacketNumber++;
 | 
			
		||||
          }
 | 
			
		||||
        }else if (Strm.lastType() == DTSC::AUDIO){
 | 
			
		||||
          WritePesHeader = true;
 | 
			
		||||
          DTMIData = Strm.lastData();
 | 
			
		||||
          ToPack = TS::GetAudioHeader(DTMIData.size(), Strm.metadata["audio"]["init"].asString());
 | 
			
		||||
          ToPack += DTMIData;
 | 
			
		||||
          while (ToPack.size()){
 | 
			
		||||
            if ((PacketNumber % 42) == 0){
 | 
			
		||||
              PackData.DefaultPAT();
 | 
			
		||||
              conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
              PackData.DefaultPMT();
 | 
			
		||||
              conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
              PacketNumber += 2;
 | 
			
		||||
            }
 | 
			
		||||
            PackData.Clear();
 | 
			
		||||
            PackData.PID(0x101);
 | 
			
		||||
            PackData.ContinuityCounter(AudioCounter);
 | 
			
		||||
            AudioCounter++;
 | 
			
		||||
            if (WritePesHeader){
 | 
			
		||||
              PackData.UnitStart(1);
 | 
			
		||||
              PackData.AddStuffing(184 - (14 + ToPack.size()));
 | 
			
		||||
              PackData.PESAudioLeadIn(ToPack.size(), Strm.getPacket(0)["time"].asInt() * 90);
 | 
			
		||||
              WritePesHeader = false;
 | 
			
		||||
            }else{
 | 
			
		||||
              PackData.AdaptationField(1);
 | 
			
		||||
              PackData.AddStuffing(184 - (ToPack.size()));
 | 
			
		||||
            }
 | 
			
		||||
            PackData.FillFree(ToPack);
 | 
			
		||||
            conn.SendNow(PackData.ToString(), 188);
 | 
			
		||||
            PacketNumber++;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** argv){
 | 
			
		||||
  Util::Config conf(argv[0], PACKAGE_VERSION);
 | 
			
		||||
  conf.addOption("streamname",
 | 
			
		||||
      JSON::fromString("{\"arg\":\"string\",\"arg_num\":1,\"help\":\"The name of the stream that this connector will transmit.\"}"));
 | 
			
		||||
  conf.addConnectorOptions(8888);
 | 
			
		||||
  conf.parseArgs(argc, argv);
 | 
			
		||||
  Socket::Server server_socket = Socket::Server(conf.getInteger("listen_port"), conf.getString("listen_interface"));
 | 
			
		||||
  if ( !server_socket.connected()){
 | 
			
		||||
    return 1;
 | 
			
		||||
  }
 | 
			
		||||
  conf.activate();
 | 
			
		||||
 | 
			
		||||
  while (server_socket.connected() && conf.is_active){
 | 
			
		||||
    Socket::Connection S = server_socket.accept();
 | 
			
		||||
    if (S.connected()){ //check if the new connection is valid
 | 
			
		||||
      pid_t myid = fork();
 | 
			
		||||
      if (myid == 0){ //if new child, start MAINHANDLER
 | 
			
		||||
        return TS_Handler(S, conf.getString("streamname"));
 | 
			
		||||
      }else{ //otherwise, do nothing or output debugging text
 | 
			
		||||
#if DEBUG >= 5
 | 
			
		||||
        fprintf(stderr, "Spawned new process %i for socket %i\n", (int)myid, S.getSocket());
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } //while connected
 | 
			
		||||
  server_socket.close();
 | 
			
		||||
  return 0;
 | 
			
		||||
} //main
 | 
			
		||||
							
								
								
									
										132
									
								
								src/connectors/embed.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								src/connectors/embed.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,132 @@
 | 
			
		|||
function mistembed(streamname)
 | 
			
		||||
{
 | 
			
		||||
	// return the current flash version
 | 
			
		||||
	function flashVersion()
 | 
			
		||||
	{
 | 
			
		||||
		var version = 0;
 | 
			
		||||
 | 
			
		||||
		try
 | 
			
		||||
		{
 | 
			
		||||
			// check in the mimeTypes
 | 
			
		||||
			version = navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin.description.replace(/([^0-9\.])/g, '').split('.')[0];
 | 
			
		||||
		}catch(e){}
 | 
			
		||||
 | 
			
		||||
		try
 | 
			
		||||
		{
 | 
			
		||||
			// for our special friend IE
 | 
			
		||||
			version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable("$version").replace(/([^0-9\,])/g, '').split(',')[0];
 | 
			
		||||
		}catch(e){}
 | 
			
		||||
 | 
			
		||||
		return parseInt(version, 10);
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	// what does the browser support - used in hasSupport()
 | 
			
		||||
	supports =
 | 
			
		||||
	{
 | 
			
		||||
		flashversion: flashVersion()
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	// return true if a type is supported
 | 
			
		||||
	function hasSupport(type)
 | 
			
		||||
	{
 | 
			
		||||
		switch(type)
 | 
			
		||||
		{
 | 
			
		||||
			case 'f4v':		return supports.flashversion >= 11;		break;
 | 
			
		||||
			case 'rtmp':	return supports.flashversion >= 10;		break;
 | 
			
		||||
			case 'flv':		return supports.flashversion >= 7;		break;
 | 
			
		||||
 | 
			
		||||
			default:			return false;
 | 
			
		||||
		}
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	// build HTML for certain kinds of types
 | 
			
		||||
	function buildPlayer(src, container, videowidth, videoheight)
 | 
			
		||||
	{
 | 
			
		||||
		// used to recalculate the width/height
 | 
			
		||||
      var ratio;
 | 
			
		||||
 | 
			
		||||
		// get the container's width/height
 | 
			
		||||
		var containerwidth = parseInt(container.scrollWidth, 10);
 | 
			
		||||
		var containerheight = parseInt(container.scrollHeight, 10);
 | 
			
		||||
 | 
			
		||||
		if(videowidth > containerwidth && containerwidth > 0)
 | 
			
		||||
		{
 | 
			
		||||
			ratio = videowidth / containerwidth;
 | 
			
		||||
 | 
			
		||||
			videowidth /= ratio;
 | 
			
		||||
			videoheight /= ratio;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if(videoheight > containerheight && containerheight > 0)
 | 
			
		||||
		{
 | 
			
		||||
			ratio = videoheight / containerheight;
 | 
			
		||||
 | 
			
		||||
			videowidth /= ratio;
 | 
			
		||||
			videoheight /= ratio;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		switch(src.type)
 | 
			
		||||
		{
 | 
			
		||||
			case 'f4v':
 | 
			
		||||
			case 'rtmp':
 | 
			
		||||
			case 'flv':
 | 
			
		||||
         	container.innerHTML = '<object width="' + videowidth + '" height="' + videoheight + '"><param name="movie" value="http://fpdownload.adobe.com/strobe/FlashMediaPlayback.swf"></param><param name="flashvars" value="src=' + encodeURI(src.url) + '&controlBarMode=floating&expandedBufferTime=4&minContinuousPlaybackTime=10"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://fpdownload.adobe.com/strobe/FlashMediaPlayback.swf" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + videowidth + '" height="' + videoheight + '" flashvars="src=' + encodeURI(src.url) + '&controlBarMode=floating&expandedBufferTime=4&minContinuousPlaybackTime=10"></embed></object>';
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
			case 'fallback':
 | 
			
		||||
				container.innerHTML = '<strong>No support for any player found</strong>';
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	var video = mistvideo[streamname],
 | 
			
		||||
	    container = document.createElement('div'),
 | 
			
		||||
		 scripts = document.getElementsByTagName('script'),
 | 
			
		||||
		 me = scripts[scripts.length - 1];
 | 
			
		||||
 | 
			
		||||
	// create the container
 | 
			
		||||
	me.parentNode.insertBefore(container, me);
 | 
			
		||||
	// set the class to 'mistvideo'
 | 
			
		||||
	container.setAttribute('class', 'mistvideo');
 | 
			
		||||
	// remove script tag
 | 
			
		||||
	me.parentNode.removeChild(me);
 | 
			
		||||
 | 
			
		||||
	if(video.error)
 | 
			
		||||
	{
 | 
			
		||||
		// there was an error; display it
 | 
			
		||||
		container.innerHTML = ['<strong>Error: ', video.error, '</strong>'].join('');
 | 
			
		||||
	}else if(video.source.length < 1)
 | 
			
		||||
	{
 | 
			
		||||
		// no stream sources
 | 
			
		||||
		container.innerHTML = '<strong>Error: no streams found</strong>';
 | 
			
		||||
	}else{
 | 
			
		||||
		// no error, and sources found. Check the video types and output the best
 | 
			
		||||
		// available video player.
 | 
			
		||||
		var i, video
 | 
			
		||||
			 foundPlayer = false,
 | 
			
		||||
			 len = video.source.length;
 | 
			
		||||
 | 
			
		||||
		for(i = 0; i < len; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if( hasSupport( video.source[i].type ) )
 | 
			
		||||
			{
 | 
			
		||||
				// we support this kind of video, so build it.
 | 
			
		||||
				buildPlayer(video.source[i], container.parentNode, video.width, video.height);
 | 
			
		||||
 | 
			
		||||
				// we've build a player, so we're done here
 | 
			
		||||
				foundPlayer = true;
 | 
			
		||||
				break;   // break for() loop
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if(!foundPlayer)
 | 
			
		||||
		{
 | 
			
		||||
			// of all the streams given, none was supported (eg. no flash and HTML5 video). Display error
 | 
			
		||||
			buildPlayer({type: 'fallback'}, container.parentNode, video.width, video.height);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								src/connectors/icon.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/connectors/icon.h
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue