<commit_before>/// \file conn_http_dynamic.cpp
/// Contains the main code for the HTTP Dynamic Connector

#include <iostream>
#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/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 Connectors.
namespace Connector_HTTP {
  
  std::set<std::string> videoTracks;///<< Holds valid video tracks for playback
  long long int audioTrack = 0;///<< Holds audio track ID for playback
  void getTracks(JSON::Value & metadata){
    videoTracks.clear();
    for (JSON::ObjIter it = metadata["tracks"].ObjBegin(); it != metadata["tracks"].ObjEnd(); it++){
      if (it->second["type"] == "video"){
        videoTracks.insert(it->first);
      }
      if (it->second["type"] == "audio"){
        audioTrack = it->second["trackid"].asInt();
      }
    }
  }
  
  
  ///\brief Builds a bootstrap for use in HTTP Dynamic streaming.
  ///\param streamName The name of the stream.
  ///\param metadata The current metadata, used to generate the index.
  ///\param fragnum The index of the current fragment
  ///\return The generated bootstrap.
  std::string dynamicBootstrap(std::string & streamName, JSON::Value & metadata, bool isLive = false, int fragnum = 0){
    std::string empty;
    
    MP4::ASRT asrt;
    asrt.setUpdate(false);
    asrt.setVersion(1);
    //asrt.setQualityEntry(empty, 0);
    if (isLive){
      asrt.setSegmentRun(1, 4294967295ul, 0);
    }else{
      asrt.setSegmentRun(1, metadata["keys"].size(), 0);
    }
    
    MP4::AFRT afrt;
    afrt.setUpdate(false);
    afrt.setVersion(1);
    afrt.setTimeScale(1000);
    //afrt.setQualityEntry(empty, 0);
    MP4::afrt_runtable afrtrun;
    long long int currTime = 0;
    for (int i = 0; i < metadata["keys"].size(); i++){
      if (metadata["keys"][i]["len"].asInt() > 0){
        afrtrun.firstFragment = metadata["keys"][i]["num"].asInt();
        afrtrun.firstTimestamp = metadata["keys"][i]["time"].asInt();
        afrtrun.duration = metadata["keys"][i]["len"].asInt();
        currTime = afrtrun.firstTimestamp + afrtrun.duration;
        afrt.setFragmentRun(afrtrun, i);
      }
    }
    
    MP4::ABST abst;
    abst.setVersion(1);
    abst.setBootstrapinfoVersion(1);
    abst.setProfile(0);
    abst.setUpdate(false);
    abst.setTimeScale(1000);
    abst.setLive(isLive);
    abst.setCurrentMediaTime(currTime);
    abst.setSmpteTimeCodeOffset(0);
    abst.setMovieIdentifier(streamName);
    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());<commit_msg>Fixed dynamic connector timing issue<commit_after>/// \file conn_http_dynamic.cpp
/// Contains the main code for the HTTP Dynamic Connector

#include <iostream>
#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/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 Connectors.
namespace Connector_HTTP {
  
  std::set<std::string> videoTracks;///<< Holds valid video tracks for playback
  long long int audioTrack = 0;///<< Holds audio track ID for playback
  void getTracks(JSON::Value & metadata){
    videoTracks.clear();
    for (JSON::ObjIter it = metadata["tracks"].ObjBegin(); it != metadata["tracks"].ObjEnd(); it++){
      if (it->second["type"] == "video"){
        videoTracks.insert(it->first);
      }
      if (it->second["type"] == "audio"){
        audioTrack = it->second["trackid"].asInt();
      }
    }
  }
  
  
  ///\brief Builds a bootstrap for use in HTTP Dynamic streaming.
  ///\param streamName The name of the stream.
  ///\param metadata The current metadata, used to generate the index.
  ///\param fragnum The index of the current fragment
  ///\return The generated bootstrap.
  std::string dynamicBootstrap(std::string & streamName, JSON::Value & metadata, bool isLive = false, int fragnum = 0){
    std::string empty;
    
    MP4::ASRT asrt;
    asrt.setUpdate(false);
    asrt.setVersion(1);
    //asrt.setQualityEntry(empty, 0);
    if (isLive){
      asrt.setSegmentRun(1, 4294967295ul, 0);
    }else{
      asrt.setSegmentRun(1, metadata["keys"].size(), 0);
    }
    
    MP4::AFRT afrt;
    afrt.setUpdate(false);
    afrt.setVersion(1);
    afrt.setTimeScale(1000);
    //afrt.setQualityEntry(empty, 0);
    MP4::afrt_runtable afrtrun;
    long long int currTime = 0;
    for (int i = 0; i < metadata["keys"].size(); i++){
      if (metadata["keys"][i]["len"].asInt() > 0){
        afrtrun.firstFragment = metadata["keys"][i]["num"].asInt();
        afrtrun.firstTimestamp = metadata["keys"][i]["time"].asInt();
        afrtrun.duration = metadata["keys"][i]["len"].asInt();
        currTime = afrtrun.firstTimestamp + afrtrun.duration;
        afrt.setFragmentRun(afrtrun, i);
      }
    }
    
    MP4::ABST abst;
    abst.setVersion(1);
    abst.setBootstrapinfoVersion(1);
    abst.setProfile(0);
    abst.setUpdate(false);
    abst.setTimeScale(1000);
    abst.setLive(isLive);
    abst.setCurrentMediaTime(metadata["lastms"].asInt());
    abst.setSmpteTimeCodeOffset(0);
    abst.setMovieIdentifier(streamName);
    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());
{'fraction_non_alphanumeric': 0.16519221489464372, 'fraction_numerical': 0.01479813414830304, 'mean_word_length': 3.5254730713246, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5883452', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2179, 'n_words': 469}