609a5c9114b0c1b34658f014a355a3041bef6388
[senf.git] / Examples / DVBAdapter / ULEdec.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 // Definition of non-inline non-template functions
24
25 #include <string>
26 #include <iostream>
27 #include <iomanip>
28 #include <sys/ioctl.h>
29 #include <linux/sockios.h>
30 #include <linux/dvb/dmx.h> 
31
32 #include "Scheduler/Scheduler.hh"
33 #include "Packets/DefaultBundle/EthernetPacket.hh"
34 #include "Packets/MPEGDVBBundle/TransportPacket.hh"
35 #include "Packets/MPEGDVBBundle/SNDUPacket.hh"
36 #include "Utils/membind.hh"
37 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
38 #include "Packets/ParseInt.hh"
39 #include "Packets/Packet.hh"
40 #include "Packets/PacketData.hh"
41 #include "Packets/ParseInt.hh"
42
43 #define PID 271
44 #define TS_SYNC 0x47
45
46 #define prefix_
47 ///////////////////////////////cc.p////////////////////////////////////////
48
49 namespace {
50
51     static const unsigned BLOCK_SIZE = 16;
52
53     template <class Iterator>
54     void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
55     {
56         unsigned offset (0);
57         std::string ascii;
58         for (; i != i_end; ++i, ++offset) {
59             switch (offset % BLOCK_SIZE) {
60             case 0:
61                 if (!ascii.empty()) {
62                     stream << "  " << ascii << "\n";
63                     ascii = "";
64                 }
65                 stream << "  "
66                           << std::hex << std::setw(4) << std::setfill('0')
67                           << offset << ' ';
68                 break;
69             case BLOCK_SIZE/2:
70                 stream << " ";
71                 ascii += ' ';
72                 break;
73             }
74             stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
75                       << unsigned(*i);
76             ascii += (*i >= ' ' && *i < 126) ? *i : '.';
77         }
78         if (!ascii.empty()) {
79             for (; (offset % BLOCK_SIZE) != 0; ++offset) {
80                 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
81                     stream << " ";
82                 stream << "   ";
83             }
84             stream << "  " << ascii << "\n";
85         }
86         stream << std::dec;
87     }
88 }
89
90
91 class ULEdec
92 {
93     senf::DVBDemuxPESHandle demuxHandle;
94     senf::DVBDvrHandle dvrHandle;
95     
96     unsigned char receiver_state;
97     unsigned char priv_tscc;
98 public:
99     ULEdec()
100     {
101         struct dmx_pes_filter_params pes_filter;
102         memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
103         pes_filter.pid = PID;
104         pes_filter.input  = DMX_IN_FRONTEND;
105         pes_filter.output = DMX_OUT_TS_TAP;
106         pes_filter.pes_type = DMX_PES_OTHER;
107         pes_filter.flags = DMX_IMMEDIATE_START;
108         demuxHandle.protocol().setPESFilter( &pes_filter );
109         
110         senf::Scheduler::instance().add(
111             dvrHandle, senf::membind(&ULEdec::handlePacket, this));
112         
113         receiver_state = 1; // Idle
114     }
115
116 private:
117     void handlePacket(senf::FileHandle, senf::Scheduler::EventId event)
118     {
119         char ts_data[188];
120         dvrHandle.read(&ts_data[0], &ts_data[188]);
121         senf::TransportPacket tsPacket (
122                 senf::TransportPacket::create( boost::begin(ts_data) ));
123 //        packet.dump(std::cout);
124 //        senf::PacketData & packetData (packet.last().data());
125 //        hexdump(packetData.begin(), packetData.end(), std::cout);
126         
127         // Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control.
128         if ( (tsPacket->sync_byte() != TS_SYNC) || 
129              (tsPacket->transport_error_indicator() == true) || 
130              (tsPacket->transport_scrmbl_ctrl() != 0)) 
131         {
132             std::cerr << "invalid ts packet\n";
133             // drop partly decoded SNDU, reset state, resync on PUSI.
134             return;
135         }
136
137         unsigned char payload_pointer;
138         switch (receiver_state) {
139         case 1:  // Idle State
140             // resync on PUSI
141             if (tsPacket->pusi() == 0) 
142                 return; // skip packet
143             // Synchronize continuity counter
144             priv_tscc = tsPacket->continuity_counter();
145             // a PUSI value of 1 indicates the presence of a Payload Pointer.
146             payload_pointer = ts_data[4];
147             if (payload_pointer>181) {
148                 std::cerr << "invalid payload_pointer\n";
149                 return;
150             }
151             
152             // 
153             break;
154         case 2:  // Reassembly State
155             break;
156         }
157     }
158 };
159
160 int main(int argc, char const * argv[])
161 {
162     try {
163         ULEdec decoder;
164         senf::Scheduler::instance().process();
165     }
166     catch (std::exception const & ex) {
167         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
168     }
169     return 0;
170 }
171
172
173 ///////////////////////////////cc.e////////////////////////////////////////
174 #undef prefix_
175
176 \f
177 // Local Variables:
178 // mode: c++
179 // fill-column: 100
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"
184 // comment-column: 40
185 // End: