Allow size-specific resizers
[jpcrr.git] / streamtools / muxdump.cpp
blob88d6ff4cad0f577315ff3bc297d04e89ada4244a
1 #include "newpacket.hpp"
2 #include <iostream>
3 #include <sstream>
4 #include <list>
5 #include <map>
6 #include <vector>
7 #include <cstdio>
8 #include <cstring>
9 #include <stdexcept>
10 #include <algorithm>
12 struct input
14 input(const std::string& name)
15 : rchan(name)
17 curpacket = NULL;
18 eof = 0;
20 packet* peek_packet()
22 if(!curpacket && !eof) {
23 curpacket = rchan.read();
24 if(curpacket == NULL)
25 eof = 1;
27 return curpacket;
30 void discard_packet()
32 curpacket = NULL;
34 private:
35 read_channel rchan;
36 packet* curpacket;
37 int eof;
40 struct packet* first_of_inputs(std::list<input*>& rchans)
42 struct packet* f = NULL;
43 std::list<input*>::iterator j = rchans.begin();
44 for(std::list<input*>::iterator i = rchans.begin(); i != rchans.end(); ++i) {
45 struct packet* g = (*i)->peek_packet();
46 if(g && (!f || g->rp_timestamp < f->rp_timestamp)) {
47 f = g;
48 j = i;
51 (*j)->discard_packet();
52 return f;
55 int main(int argc, char** argv)
57 std::list<input*> rchans;
58 std::map<std::string, uint16_t> channel_assignments;
59 std::vector<struct channel> chans;
60 if(argc < 3) {
61 std::cerr << "syntax: muxdump.exe <input>... <output>" << std::endl;
62 exit(1);
65 write_channel wchan(argv[argc - 1]);
66 for(int i = 1; i < argc - 1; i++)
67 rchans.push_back(new input(argv[i]));
69 packet* p;
70 while((p = first_of_inputs(rchans))) {
71 if(!channel_assignments.count(p->rp_channel_name)) {
72 //No channel yet, create.
73 channel_assignments[p->rp_channel_name] = (uint16_t)chans.size();
74 struct channel c;
75 c.c_channel = (uint16_t)chans.size();
76 c.c_type = p->rp_major;
77 c.c_channel_name = p->rp_channel_name;
78 chans.push_back(c);
79 wchan.start_segment(chans);
81 uint16_t chan = channel_assignments[p->rp_channel_name];
82 if(chans[chan].c_type != p->rp_major) {
83 //Change type.
84 chans[chan].c_type = p->rp_major;
85 wchan.start_segment(chans);
87 p->rp_channel = chan;
88 wchan.write(*p);
89 delete p;
92 for(std::list<input*>::iterator i = rchans.begin(); i != rchans.end(); ++i)
93 delete(*i);
95 return 0;