-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathoutput_subrip.cpp
More file actions
110 lines (100 loc) · 3.79 KB
/
Copy pathoutput_subrip.cpp
File metadata and controls
110 lines (100 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "output_subrip.h"
#include <sstream>
#include <mist/checksum.h>
#include <mist/defines.h>
#include <mist/http_parser.h>
namespace Mist{
OutSubRip::OutSubRip(Socket::Connection &conn) : HTTPOutput(conn){realTime = 0;}
OutSubRip::~OutSubRip(){}
void OutSubRip::init(Util::Config *cfg){
HTTPOutput::init(cfg);
capa["name"] = "SubRip";
capa["friendly"] = "SubRip (SRT/WebVTT) over HTTP";
capa["desc"] = "Pseudostreaming in SubRip Text (SRT) and WebVTT formats over HTTP";
capa["url_match"].append("/$.srt");
capa["url_match"].append("/$.vtt");
capa["url_match"].append("/$.webvtt");
capa["codecs"][0u][0u].append("subtitle");
capa["methods"][0u]["handler"] = "http";
capa["methods"][0u]["type"] = "html5/text/plain";
capa["methods"][0u]["hrn"] = "SRT subtitle progressive";
capa["methods"][0u]["priority"] = 8;
capa["methods"][0u]["url_rel"] = "/$.srt";
capa["methods"][1u]["handler"] = "http";
capa["methods"][1u]["type"] = "html5/text/vtt";
capa["methods"][1u]["hrn"] = "WebVTT subtitle progressive";
capa["methods"][1u]["priority"] = 9;
capa["methods"][1u]["url_rel"] = "/$.webvtt";
}
void OutSubRip::sendNext(){
// Reached the end we wanted? Stop here.
if (filter_to > 0 && thisTime > filter_to && filter_to > filter_from){
config->is_active = false;
return;
}
char *dataPointer = 0;
size_t len = 0;
thisPacket.getString("data", dataPointer, len);
// ignore empty subs
if (len == 0 || (len == 1 && dataPointer[0] == ' ')){return;}
std::stringstream tmp;
if (!webVTT){tmp << lastNum++ << std::endl;}
char tmpBuf[50];
size_t tmpLen =
sprintf(tmpBuf, "%.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64 ".%.3" PRIu64, (thisTime / 3600000),
((thisTime % 3600000) / 60000), (((thisTime % 3600000) % 60000) / 1000), thisTime % 1000);
tmp.write(tmpBuf, tmpLen);
tmp << " --> ";
uint64_t time = thisTime + thisPacket.getInt("duration");
if (time == thisTime){time += len * 75 + 800;}
tmpLen = sprintf(tmpBuf, "%.2" PRIu64 ":%.2" PRIu64 ":%.2" PRIu64 ".%.3" PRIu64, (time / 3600000),
((time % 3600000) / 60000), (((time % 3600000) % 60000) / 1000), time % 1000);
tmp.write(tmpBuf, tmpLen);
tmp << std::endl;
myConn.SendNow(tmp.str());
// prevent extra newlines
while (len && dataPointer[len - 1] == '\n'){--len;}
myConn.SendNow(dataPointer, len);
myConn.SendNow("\n\n");
}
void OutSubRip::sendHeader(){
H.setCORSHeaders();
H.SetHeader("Content-Type", (webVTT ? "text/vtt; charset=utf-8" : "text/plain; charset=utf-8"));
H.protocol = "HTTP/1.0";
H.SendResponse("200", "OK", myConn);
if (webVTT){myConn.SendNow("WEBVTT\n\n");}
sentHeader = true;
}
void OutSubRip::onHTTP(){
std::string method = H.method;
webVTT = (H.url.find(".vtt") != std::string::npos) || (H.url.find(".webvtt") != std::string::npos);
if (H.GetVar("track").size()){
size_t tid = atoll(H.GetVar("track").c_str());
if (M.getValidTracks().count(tid)){
userSelect.clear();
userSelect[tid].reload(streamName, tid);
}
}
filter_from = 0;
filter_to = 0;
index = 0;
if (H.GetVar("from") != ""){
filter_from = JSON::Value(H.GetVar("from")).asInt();
seek(filter_from);
}
if (H.GetVar("to") != ""){filter_to = JSON::Value(H.GetVar("to")).asInt();}
if (filter_to){realTime = 0;}
H.Clean();
H.setCORSHeaders();
if (method == "OPTIONS" || method == "HEAD"){
H.SetHeader("Content-Type", (webVTT ? "text/vtt; charset=utf-8" : "text/plain; charset=utf-8"));
H.protocol = "HTTP/1.0";
H.SendResponse("200", "OK", myConn);
H.Clean();
return;
}
lastNum = 0;
parseData = true;
wantRequest = false;
}
}// namespace Mist