From dbe2b1f6612d8681b5be549ff2706586ce5dbf32 Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Fri, 22 Apr 2016 14:06:03 +0200
Subject: [PATCH] HLS now sends unique session IDs for splitting sessions from
 each other.

---
 src/output/output_hls.cpp  | 13 +++++++++----
 src/output/output_hls.h    |  2 +-
 src/output/output_http.cpp |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/output/output_hls.cpp b/src/output/output_hls.cpp
index 0c9dd826..0ed8f70c 100644
--- a/src/output/output_hls.cpp
+++ b/src/output/output_hls.cpp
@@ -31,7 +31,7 @@ namespace Mist {
         if (audioId != -1) {
           result << "_" << audioId;
         }
-        result << "/index.m3u8\r\n";
+        result << "/index.m3u8?sessId=" << getpid() << "\r\n";
       }
     }
     if (!vidTracks && audioId) {
@@ -134,7 +134,7 @@ namespace Mist {
   }
 
 
-  std::string OutHLS::liveIndex(int tid) {
+  std::string OutHLS::liveIndex(int tid, std::string & sessId) {
     updateMeta();
     std::stringstream result;
     //parse single track
@@ -161,7 +161,11 @@ namespace Mist {
         duration = myMeta.tracks[tid].lastms - starttime;
       }
       char lineBuf[400];
-      snprintf(lineBuf, 400, "#EXTINF:%lld, no desc\r\n%lld_%lld,ts\r\n", ((duration + 500) / 1000), starttime, starttime + duration);
+      if (sessId.size()){
+        snprintf(lineBuf, 400, "#EXTINF:%lld, no desc\r\n%lld_%lld.ts?sessId=%s\r\n", ((duration + 500) / 1000), starttime, starttime + duration, sessId.c_str());
+      }else{
+        snprintf(lineBuf, 400, "#EXTINF:%lld, no desc\r\n%lld_%lld.ts\r\n", ((duration + 500) / 1000), starttime, starttime + duration);
+      }
       lines.push_back(lineBuf);
     }
     unsigned int skippedLines = 0;
@@ -311,6 +315,7 @@ namespace Mist {
 
   void OutHLS::onHTTP() {
     std::string method = H.method;
+    std::string sessId = H.GetVar("sessId");
 
     if (H.url == "/crossdomain.xml") {
       H.Clean();
@@ -500,7 +505,7 @@ namespace Mist {
         manifest = liveIndex();
       } else {
         int selectId = atoi(request.substr(0, request.find("/")).c_str());
-        manifest = liveIndex(selectId);
+        manifest = liveIndex(selectId, sessId);
       }
       H.SetBody(manifest);
       H.SendResponse("200", "OK", myConn);
diff --git a/src/output/output_hls.h b/src/output/output_hls.h
index d90bcd1e..3a4d93c9 100644
--- a/src/output/output_hls.h
+++ b/src/output/output_hls.h
@@ -11,7 +11,7 @@ namespace Mist {
       void onHTTP();      
     protected:      
       std::string liveIndex();
-      std::string liveIndex(int tid);
+      std::string liveIndex(int tid, std::string & sessId);
 
       
       std::string pushLiveIndex();
diff --git a/src/output/output_http.cpp b/src/output/output_http.cpp
index 9a1dca33..54249727 100644
--- a/src/output/output_http.cpp
+++ b/src/output/output_http.cpp
@@ -211,7 +211,7 @@ namespace Mist {
   
   void HTTPOutput::onRequest(){
     while (H.Read(myConn)){
-      std::string ua = H.GetHeader("User-Agent") + H.GetHeader("X-Playback-Session-Id");
+      std::string ua = H.GetHeader("User-Agent") + H.GetHeader("X-Playback-Session-Id") + H.GetVar("sessId");
       crc = checksum::crc32(0, ua.data(), ua.size());
       INFO_MSG("Received request %s", H.getUrl().c_str());
       selectedTracks.clear();