From 385cfcb761c5906bba2f97295b9dbfd067964b30 Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Tue, 25 Aug 2020 14:12:36 +0200
Subject: [PATCH] Added raw PES dump mode to TS analyser

---
 src/analysers/analyser_ts.cpp | 15 +++++++++++++++
 src/analysers/analyser_ts.h   |  2 ++
 2 files changed, 17 insertions(+)

diff --git a/src/analysers/analyser_ts.cpp b/src/analysers/analyser_ts.cpp
index 7c405e9c..2906bdfd 100644
--- a/src/analysers/analyser_ts.cpp
+++ b/src/analysers/analyser_ts.cpp
@@ -28,6 +28,7 @@ void AnalyserTS::init(Util::Config &conf){
                 "= TS stream pkts, 32 = raw PES packet bytes, 64 = raw TS packet bytes";
   conf.addOption("detail", opt);
   opt.null();
+
   opt["long"] = "pid";
   opt["short"] = "P";
   opt["arg"] = "num";
@@ -35,10 +36,21 @@ void AnalyserTS::init(Util::Config &conf){
   opt["help"] = "Only use the given PID, ignore others";
   conf.addOption("pid", opt);
   opt.null();
+
+  opt["long"] = "raw";
+  opt["short"] = "R";
+  opt["arg"] = "str";
+  opt["default"] = "";
+  opt["help"] = "Write raw PES payloads to given file";
+  conf.addOption("raw", opt);
+  opt.null();
 }
 
 AnalyserTS::AnalyserTS(Util::Config &conf) : Analyser(conf){
   pidOnly = conf.getInteger("pid");
+  if (conf.getString("raw").size()){
+    outFile.open(conf.getString("raw").c_str());
+  }
   bytes = 0;
 }
 
@@ -172,6 +184,9 @@ std::string AnalyserTS::printPES(const std::string &d, size_t PID){
   }
   res << std::endl;
 
+  if (outFile){
+    outFile.write(d.data() + 9 + headSize + padding, d.size()-(9 + headSize + padding));
+  }
   if (detail & 32){
     size_t counter = 0;
     for (size_t i = 9 + headSize + padding; i < d.size(); ++i){
diff --git a/src/analysers/analyser_ts.h b/src/analysers/analyser_ts.h
index 4257e99d..24880883 100644
--- a/src/analysers/analyser_ts.h
+++ b/src/analysers/analyser_ts.h
@@ -1,6 +1,7 @@
 #include "analyser.h"
 #include <mist/config.h>
 #include <mist/ts_packet.h>
+#include <fstream>
 
 class AnalyserTS : public Analyser{
 public:
@@ -11,6 +12,7 @@ public:
   std::string printPES(const std::string &d, size_t PID);
 
 private:
+  std::ofstream outFile;
   std::map<size_t, std::string> payloads;
   size_t pidOnly;
   TS::Packet packet;