6 #define PACKET_BUFFER_SIZE 100
10 Reorderer::Reorderer(Protocol* sender)
11 : sender(sender), last_seq(-1)
15 void Reorderer::send_packet(uint16_t proto, const string& data, int seq)
18 if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
19 printf("Gave up waiting for packets [%d,%d>\n",
20 last_seq + 1, packet_buffer.top().seq);
22 last_seq = packet_buffer.top().seq - 1;
29 packet_buffer.push(packet);
31 while (!packet_buffer.empty() &&
32 (last_seq == -1 || packet_buffer.top().seq <= last_seq + 1)) {
33 int front_seq = packet_buffer.top().seq;
34 if (front_seq < last_seq + 1) {
35 printf("Duplicate packet or way out-of-order: seq=%d front_seq=%d\n",
36 front_seq, last_seq + 1);
40 //if (packet_buffer.size() > 1) {
41 // printf("seq=%d (REORDER %d)\n", front_seq, int(packet_buffer.size()));
43 // printf("seq=%d\n", front_seq);
45 const string &data = packet_buffer.top().data;
46 check_ts_discontinuity(packet_buffer.top().proto, data, silence);
47 sender->send_packet(proto, data, seq);
50 if (!silence && !packet_buffer.empty()) {
51 printf("Reordering with packet buffer size %d: seq=%d new_front_seq=%d\n", int(packet_buffer.size()), front_seq, packet_buffer.top().seq);
57 void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence)
59 if (data.size() == 1344) {
60 for (int i = 0; i < 7; ++i) {
61 const char *pkt = &data[i * 188 + 28];
62 int pid = (ntohl(*(uint32_t *)(pkt)) & 0x1fff00) >> 8;
67 int has_payload = pkt[3] & 0x10;
68 int cc = pkt[3] & 0xf;
70 int last_cc = ccs[pid];
71 if (!silence && cc != ((last_cc + 1) & 0xf)) {
72 printf("Pid %d discontinuity (expected %d, got %d)\n", pid, (last_cc + 1) & 0xf, cc);