Repository navigation
Expand file tree
/
Copy pathHLSStream.cpp
More file actions
440 lines (372 loc) · 14.2 KB
/
Copy pathHLSStream.cpp
File metadata and controls
440 lines (372 loc) · 14.2 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "HLSStream.hpp"
#include <string.h>
#include <algorithm>
#include <chrono>
#include <cstring>
#include <sstream>
#include "EventType.hpp"
#include "Helpers.hpp"
#include "Logger.hpp"
#include "Settings.hpp"
#include "Utils.hpp"
#define CHANNEL_LIST_TAG 0x01
#define CHANNEL_TAG 0x02
#define SEGMENT_PLAYLIST_TAG 0x03
#define SEGMENT_TAG 0x04
#define CHANNEL_INNER_TAG 0x05
#define SEGMENT_INNER_PLAYLIST_TAG 0x06
#define REMAINING_FETCHES 10
#define MAX_TRACKS_PER_PLAYLIST 4
#define TIMEOUT_TRACK 15000
#define TRACK_DELTA 150
HLSStream::HLSStream() {}
bool HLSStream::HandleExistsResource(struct Request *entry) {
bool exists = Stream::HandleExistsResource(entry);
auto mux = GetResource(entry->resource_id);
if (!exists) {
char *path = (char *)entry->iov[1].iov_base;
if (Utils::EndsWith(path, "/playlist.m3u8")) {
mux->tag = CHANNEL_LIST_TAG;
} else if (Utils::EndsWith(path, "/index.m3u8")) {
mux->tag = CHANNEL_TAG;
mux->transfer_mode = TRANSFER_MODE_WAIT_UNTIL_COMPLETE;
mux->in_memory = true;
} else if (Utils::EndsWith(path, ".m3u8")) {
mux->tag = SEGMENT_PLAYLIST_TAG;
mux->in_memory = true;
} else {
mux->tag = SEGMENT_TAG;
}
} else {
if (mux->tag == CHANNEL_TAG) {
} else if (mux->tag == SEGMENT_PLAYLIST_TAG) {
struct SegmentPlaylist *playlist = playlists_.at(entry->resource_id);
playlist->remaining_fetches = REMAINING_FETCHES;
if (mux->is_completed) {
GenerateSegmentClientList(entry->resource_id, mux);
}
}
}
return exists;
}
int HLSStream::NotifyCacheCompleted(uint64_t resource_id, struct iovec *buffer,
int size) {
auto mux = GetResource(resource_id);
if (mux->tag == SEGMENT_TAG) {
AddResourceTTL(resource_id, TIMEOUT_TRACK);
return Stream::NotifyCacheCompleted(resource_id, buffer, size);
}
if (mux->tag == SEGMENT_INNER_PLAYLIST_TAG) {
uint64_t parent_id = segments_.at(resource_id);
bool proceed = ProcessSegmentList(parent_id, buffer, size);
RemoveSegment(resource_id);
if (proceed) {
// Notify parent segment is completed
resource_id = parent_id;
size = 0;
auto mux = GetResource(resource_id);
GenerateSegmentClientList(resource_id, mux);
} else {
return 1;
}
}
struct iovec *new_buffer = new struct iovec[size];
if (mux->tag == CHANNEL_TAG) {
ProcessChannel(mux, buffer, size, 0, new_buffer);
} else if (mux->tag == CHANNEL_INNER_TAG) {
uint64_t parent_id = segments_.at(resource_id);
ProcessChannel(mux, buffer, size, parent_id, new_buffer);
RemoveSegment(resource_id);
}
int response = Stream::NotifyCacheCompleted(resource_id, new_buffer, size);
for (int i = 0; i < size; i++) {
if (new_buffer[i].iov_len > 0) {
free(new_buffer[i].iov_base);
}
}
delete[] new_buffer;
return response;
}
void HLSStream::ProcessChannel(struct Mux *mux, struct iovec *buffer, int size,
uint64_t parent_id, struct iovec *new_buffer) {
struct Request *channel = mux->requests[0];
for (int i = 0; i < size; i++) {
if (buffer[i].iov_len == 0) continue;
std::string content((char *)buffer[i].iov_base);
std::stringstream stream(content);
std::string line;
while (std::getline(stream, line)) {
if (line.rfind("#EXT-X-STREAM-INF:", 0) == 0) {
std::string url;
std::getline(stream, url);
if (url.rfind("http", 0) == 0) {
std::string path =
url.replace(url.begin(), url.begin() + 7, "/");
path = path.substr(path.find("/", 1));
Log(__FILE__, __LINE__, Log::kDebug)
<< "Found playlist: " << path;
uint64_t resource_id = parent_id;
// Create playlist is the request is triggered by user
if (parent_id == 0) {
resource_id = Helpers::GetResourceId(path.c_str());
if (playlists_.find(resource_id) == playlists_.end()) {
CreatePlaylist(resource_id, mux->resource_id, mux->url);
}
}
RequestFile(resource_id, url, SEGMENT_INNER_PLAYLIST_TAG, 100);
break;
}
}
}
int pos = content.find(Settings::Proxy);
if (pos != std::string::npos) {
while (pos != std::string::npos) {
content.replace(pos, Settings::Proxy.length(), Settings::BaseUrl);
pos = content.find(Settings::Proxy);
}
new_buffer[i].iov_len = content.length();
new_buffer[i].iov_base = malloc(new_buffer[i].iov_len);
memset(new_buffer[i].iov_base, 0, new_buffer[i].iov_len);
memcpy(new_buffer[i].iov_base, (void *)content.c_str(),
content.length());
UpdateHeader(mux, content.length());
}
}
}
bool HLSStream::ProcessSegmentList(uint64_t parent_id, struct iovec *buffer,
int size) {
struct SegmentPlaylist *playlist = playlists_.at(parent_id);
if (playlist->remaining_fetches <= 0) {
RemovePlaylist(parent_id);
return false;
}
playlist->remaining_fetches--;
long last_stamp = playlist->last_stamp;
if (last_stamp == 0) {
last_stamp = Helpers::GetTicks() + TRACK_DELTA;
}
int sequence = 0;
bool found = false;
for (int i = 0; i < size; i++) {
if (buffer[i].iov_len == 0) continue;
std::string content((char *)buffer[i].iov_base);
std::stringstream stream(content);
std::string line;
while (std::getline(stream, line)) {
if (line.rfind("#EXTINF:", 0) == 0) {
std::string url;
std::getline(stream, url);
if (url.rfind("http", 0) == 0) {
found = true;
std::string seconds = line.substr(8, line.find(',') - 8);
seconds.erase(std::remove(seconds.begin(), seconds.end(), '.'),
seconds.end());
auto stamp = std::stoi(seconds);
uint64_t uid = Helpers::GetResourceId(url.c_str());
sequence++;
if (std::find(playlist->track_uids.begin(),
playlist->track_uids.end(),
uid) != playlist->track_uids.end()) {
continue;
}
std::string tmp = url;
tmp = tmp.replace(tmp.begin(), tmp.begin() + 7, "/");
//Log(__FILE__, __LINE__) << "Found TS: " << tmp;
Log(__FILE__, __LINE__) << "Found TS: " << last_stamp;
int pos = url.find(Settings::Proxy);
if (pos != std::string::npos) {
url.replace(pos, Settings::Proxy.length(), Settings::BaseUrl);
}
std::string block = line + "\n" + url + "\n";
playlist->track_stamps.push_back(last_stamp);
playlist->track_uids.push_back(uid);
playlist->track_urls.push_back(block);
playlist->track_sequence.push_back(sequence);
last_stamp += stamp;
}
} else if (line.rfind("#EXT-X-MEDIA-SEQUENCE:", 0) == 0) {
std::string tmp = line.substr(22);
sequence = std::stoi(tmp);
sequence--;
continue;
}
}
}
if (playlist->last_stamp != last_stamp) {
playlist->last_stamp = last_stamp;
CleanTracks(playlist);
}
if (!found) {
RequestFile(playlist->resource_id, playlist->url, CHANNEL_INNER_TAG, 500);
} else {
RequestFile(playlist->resource_id, playlist->url, CHANNEL_INNER_TAG,
2500);
}
return true;
}
void HLSStream::CreatePlaylist(uint64_t resource_id, uint64_t channel_id,
std::string channel_url) {
auto playlist = new SegmentPlaylist();
playlist->resource_id = resource_id;
playlist->channel_id = channel_id;
playlist->url = channel_url;
playlist->remaining_fetches = REMAINING_FETCHES;
playlist->last_stamp = 0;
playlists_.insert(
std::pair<uint64_t, struct SegmentPlaylist *>(resource_id, playlist));
struct Mux *mux = CreateMux(resource_id, std::string());
mux->tag = SEGMENT_PLAYLIST_TAG;
mux->type = RESOURCE_TYPE_CACHE;
mux->transfer_mode = TRANSFER_MODE_WAIT_UNTIL_COMPLETE;
mux->in_memory = true;
mux->is_completed = false;
std::stringstream ss;
ss << "HTTP/1.1 200 OK\r\n"
<< "Server: Astra\r\n"
<< "Cache-Control: no-cache\r\n"
<< "Content-Length: 0\r\n"
<< "Access-Control-Allow-Origin: *\r\n"
<< "Access-Control-Allow-Methods: GET\r\n"
<< "Access-Control-Allow-Credentials: true\r\n"
<< "Content-Type: application/vnd.apple.mpegURL\r\n"
<< "Connection: close\r\n"
<< "\r\n";
std::string header = ss.str();
int size = header.size() + 1;
mux->header.iov_len = size;
mux->header.iov_base = malloc(size);
memset(mux->header.iov_base, 0, size);
memcpy(mux->header.iov_base, header.c_str(), size);
std::pair<uint64_t, struct Mux *> item(resource_id, mux);
resources_.insert(item);
}
void HLSStream::RequestFile(uint64_t parent_id, std::string url, int tag,
int msecs) {
uint64_t resource_id = Helpers::GetResourceId(url.c_str());
struct Mux *mux = CreateMux(resource_id, std::string());
mux->tag = tag;
mux->in_memory = true;
std::pair<uint64_t, struct Mux *> item(resource_id, mux);
resources_.insert(item);
CreateSegment(resource_id, parent_id);
auto inner = Utils::CreateRequest(5);
inner->resource_id = resource_id;
inner->event_type = EVENT_TYPE_DNS_FETCHAAAA;
auto stream = Utils::StreamRequest(inner);
// Prevent print the request on the console :P
stream->is_processing = true;
mux->requests.push_back(stream);
int size = url.size() + 1;
inner->iov[2].iov_len = size;
inner->iov[2].iov_base = malloc(size);
memset(inner->iov[2].iov_base, 0, size);
memcpy(inner->iov[2].iov_base, url.c_str(), size);
std::stringstream ss;
ss << "User-Agent: Lavf/60.3.100\r\n"
<< "Connection: keep-alive\r\n"
<< "Ranges: bytes=0-\r\n"
<< "Icy-MetaData: 1\r\n"
<< "Accept: */*\r\n";
std::string header = ss.str();
size = header.size() + 1;
inner->iov[3].iov_len = size;
inner->iov[3].iov_base = malloc(size);
memset(inner->iov[3].iov_base, 0, size);
memcpy(inner->iov[3].iov_base, header.c_str(), size);
Helpers::Nop(ring_, inner, msecs);
}
void HLSStream::GenerateSegmentClientList(uint64_t resource_id,
struct Mux *mux) {
// Only generate playlist if there are pending requests
if (mux->requests.empty()) {
return;
}
struct SegmentPlaylist *playlist = playlists_.at(resource_id);
long ticks = Helpers::GetTicks();
std::stringstream ss;
ss << "#EXTM3U\r\n";
ss << "#EXT-X-VERSION:3\r\n";
ss << "#EXT-X-TARGETDURATION:3\r\n";
// Only copy the tracks that are after the current ticks
int pivot = 0;
const std::vector<long> &stamps = playlist->track_stamps;
for (int i = 0; i < stamps.size(); i++) {
if (stamps[i] > ticks) {
pivot = i;
// if (pivot > 0) {
// pivot--;
//}
break;
}
}
ss << "#EXT-X-MEDIA-SEQUENCE:" << playlist->track_sequence.at(pivot)
<< "\r\n";
const std::vector<std::string> &urls = playlist->track_urls;
for (int i = 0; i < MAX_TRACKS_PER_PLAYLIST; i++) {
if ((pivot + i) >= urls.size()) {
break;
}
ss << urls.at(pivot + i);
}
std::string content = ss.str();
int size = content.size();
mux->buffer[0].iov_len = size;
mux->buffer[0].iov_base = malloc(size);
memset(mux->buffer[0].iov_base, 0, size);
memcpy(mux->buffer[0].iov_base, content.c_str(), size);
UpdateHeader(mux, size);
}
void HLSStream::CreateSegment(uint64_t resource_id, uint64_t parent) {
segments_.insert(std::pair<uint64_t, uint64_t>(resource_id, parent));
}
void HLSStream::RemovePlaylist(uint64_t resource_id) {
Log(__FILE__, __LINE__, Log::kWarning)
<< "Removing playlist " << resource_id;
auto playlist = playlists_.at(resource_id);
if (ExistsResource(playlist->channel_id)) {
auto channel_mux = GetResource(playlist->channel_id);
const std::vector<struct Request *> &requests = channel_mux->requests;
for (struct Request *const c : requests) {
Utils::ReleaseRequest(c);
}
ReleaseResource(channel_mux->resource_id);
}
delete playlist;
playlists_.erase(resource_id);
}
void HLSStream::RemoveSegment(uint64_t resource_id) {
auto mux = GetResource(resource_id);
const std::vector<struct Request *> &requests = mux->requests;
for (struct Request *const c : requests) {
Utils::ReleaseRequest(c);
}
ReleaseResource(resource_id);
segments_.erase(resource_id);
}
void HLSStream::UpdateHeader(struct Mux *mux, int content_length) {
std::string header((char *)mux->header.iov_base);
header = Utils::ReplaceHeaderTag(header, "Content-Length",
std::to_string(content_length));
if (mux->header.iov_len > 0) {
free(mux->header.iov_base);
}
mux->header.iov_len = header.size();
mux->header.iov_base = malloc(mux->header.iov_len);
memset(mux->header.iov_base, 0, mux->header.iov_len);
memcpy(mux->header.iov_base, (void *)header.c_str(), header.size());
}
void HLSStream::CleanTracks(struct SegmentPlaylist *playlist) {
if (playlist->track_urls.size() < 10) {
return;
}
int count = playlist->track_urls.size() - 10;
std::cout<< "LAN_[" << __FILE__ << ":" << __LINE__ << "] "<< count << std::endl;
playlist->track_urls.erase(playlist->track_urls.begin(),
playlist->track_urls.begin() + count);
playlist->track_uids.erase(playlist->track_uids.begin(),
playlist->track_uids.begin() + count);
playlist->track_stamps.erase(playlist->track_stamps.begin(),
playlist->track_stamps.begin() + count);
playlist->track_sequence.erase(playlist->track_sequence.begin(),
playlist->track_sequence.begin() + count);
}