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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* main.cpp
*
* Created on: 2013. 9. 12.
* Author: kos
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
#include "ePreDefine.h"
#include "eParser.h"
#include "eUpstreamSocket.h"
#include "eTransCodingDevice.h"
#include "uStringTool.h"
#include "eFilePumpThread.h"
#include "eDemuxPumpThread.h"
#include "eNetworkPumpThread.h"
#ifdef DEBUG_LOG
FILE* fpLog = fopen("/tmp/filestreamproxy.log", "w");
//#undef LOG
//#define LOG(X,...) { do{}while(0); }
#endif
using namespace std;
//-------------------------------------------------------------------------------
eFilePumpThread* hFilePumpThread = 0;
eDemuxPumpThread* hDemuxPumpThread = 0;
eNetworkPumpThread* hNetworkPumpThread = 0;
eTransCodingDevice* hTranscodingDevice = 0;
void SignalHandler(int aSigNo);
//-------------------------------------------------------------------------------
/*
GET /1:0:19:2B66:3F3:1:C00000:0:0:0: HTTP/1.1
Host: 192.168.102.177:8002
User-Agent: VLC/2.0.8 LibVLC/2.0.8
Range: bytes=0-
Connection: close
Icy-MetaData: 1
GET /file?file=/hdd/movie/20131023%201005%20-%20DW%20-%20Germany%20Today.ts HTTP/1.1
*/
int main(int argc, char** argv)
{
char request[MAX_LINE_LENGTH] = {0};
int videopid = 0, audiopid = 0;
signal(SIGINT, SignalHandler);
if (!ReadRequest(request)) {
RETURN_ERR_400();
}
#ifdef DEBUG_LOG
LOG("%s", request);
#endif
if (strncmp(request, "GET /", 5)) {
RETURN_ERR_400();
}
char* http = strchr(request + 5, ' ');
if (!http || strncmp(http, " HTTP/1.", 7)) {
RETURN_ERR_400("Not support request (%s).", http);
}
#ifdef DEBUG_LOG
LOG("%s", request + 5);
#endif
eTransCodingDevice transcoding;
if(transcoding.Open() == false) {
RETURN_ERR_502("Opne device failed.");
}
hTranscodingDevice = &transcoding;
bool ispidseted = false;
eNetworkPumpThread networkpump(transcoding.GetDeviceFd());
hNetworkPumpThread = &networkpump;
if(strncmp(request + 4, "/file?", 6) != 0) {
char authorization[MAX_LINE_LENGTH] = {0};
if(eParser::Authorization(authorization)) {
RETURN_ERR_401();
}
eUpstreamSocket upstreamsocket;
if(!upstreamsocket.Connect()) {
RETURN_ERR_502("Upstream connect failed.");
}
std::string responsedata = "";
if(upstreamsocket.Request(eParser::ServiceRef(request + 5, authorization), responsedata) < 0) {
RETURN_ERR_502("Upstream request failed.");
}
int demuxno = 0;
std::string wwwauthenticate = "";
std::vector<unsigned long> pidlist;
ispidseted = eParser::LiveStreamPid(responsedata, pidlist, demuxno, videopid, audiopid, wwwauthenticate);
if(ispidseted) {
if(transcoding.SetStreamPid(videopid, audiopid) == false) {
RETURN_ERR_502("Pid setting failed.");
}
} else {
#ifdef DEBUG_LOG
LOG("Invalid upstream response.");
#endif
RETURN_ERR_502("Invalid upstream response.");
}
#ifdef DEBUG_LOG
LOG("stream pids parsing result : %d, video : %d, audio : %d, pids size : [%d]", ispidseted, videopid, audiopid, pidlist.size());
for(int j = 0; j < pidlist.size(); ++j) {
LOG("saved pid : [%x]", pidlist[j]);
}
#endif
eDemuxPumpThread demuxpump;
if(!demuxpump.Open(demuxno)) {
RETURN_ERR_502("%s", demuxpump.GetMessage().c_str());
}
demuxpump.SetDeviceFd(transcoding.GetDeviceFd());
demuxpump.Start();
hDemuxPumpThread = &demuxpump;
if(pidlist.size() > 0) {
if(demuxpump.GetState() < eDemuxState::stSetedFilter) {
if(!demuxpump.SetFilter(pidlist)) {
#ifdef DEBUG_LOG
LOG("Demux setting filter failed.");
#endif
RETURN_ERR_502("Demux setting filter failed.");
}
}
if(!demuxpump.SetPidList(pidlist)) {
#ifdef DEBUG_LOG
LOG("PID setting failed.");
#endif
RETURN_ERR_502("PID setting failed.");
}
} else {
#ifdef DEBUG_LOG
LOG("No found PID for selected stream.");
#endif
RETURN_ERR_502("No found PID for selected stream.");
}
#ifdef NORMAL_STREAMPROXY
demuxpump.Join();
#else
if(transcoding.StartTranscoding() == false) {
RETURN_ERR_502("Transcoding start failed.");
}
networkpump.Start();
networkpump.Join();
demuxpump.Stop();
demuxpump.Join();
#endif
} else {
std::string srcfilename = "";
eParser::FileName(request, http, srcfilename);
ispidseted = eParser::MetaData(srcfilename, videopid, audiopid);
if(ispidseted) {
if(transcoding.SetStreamPid(videopid, audiopid) == false) {
#ifdef DEBUG_LOG
LOG("No found PID for selected stream.");
#endif
RETURN_ERR_502("Pid setting failed.");
}
}
#ifdef DEBUG_LOG
LOG("meta parsing result : %d, video : %d, audio : %d", ispidseted, videopid, audiopid);
#endif
eFilePumpThread filepump(transcoding.GetDeviceFd());
if(filepump.Open(srcfilename) == false) {
#ifdef DEBUG_LOG
LOG("TS file open failed.");
#endif
RETURN_ERR_502("TS file open failed.");
}
filepump.Start();
hFilePumpThread = &filepump;
if(transcoding.StartTranscoding() == false) {
#ifdef DEBUG_LOG
LOG("Transcoding start failed.");
#endif
RETURN_ERR_502("Transcoding start failed.");
}
filepump.SeekOffset(0);
networkpump.Start();
networkpump.Join();
filepump.Stop();
filepump.Join();
}
#ifdef DEBUG_LOG
fclose(fpLog);
#endif
return 0;
}
//-------------------------------------------------------------------------------
char* ReadRequest(char* aRequest)
{
return fgets(aRequest, MAX_LINE_LENGTH-1, stdin);
}
//-------------------------------------------------------------------------------
void SignalHandler(int aSigNo)
{
switch(aSigNo) {
case (SIGINT):
#ifdef DEBUG_LOG
LOG("Detected SIGINT.");
#endif
if(hFilePumpThread) {
hFilePumpThread->Stop();
}
if(hDemuxPumpThread) {
hDemuxPumpThread->Stop();
}
if(hNetworkPumpThread) {
hNetworkPumpThread->Stop();
}
if(hTranscodingDevice) {
hTranscodingDevice->Close();
}
sleep(2);
exit(1);
}
}
//-------------------------------------------------------------------------------
|