4a33ffde307eca9591497df0b7b9d6d41f472fd3
[senf.git] / Examples / DVBAdapter / MPEdec.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.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 <senf/Scheduler/Scheduler.hh>
33 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
34 #include <senf/Packets/MPEGDVBBundle/MPESection.hh>
35 #include <senf/Utils/membind.hh>
36 #include <senf/Utils/hexdump.hh>
37 #include <senf/Socket/Protocols/DVB.hh>
38 #include <senf/Packets.hh>
39
40 #define PID 500
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 class MySniffer
46 {
47     senf::DVBDemuxSectionHandle handle;
48
49 public:
50     MySniffer(unsigned short adapter=0, unsigned short device=0)
51         : handle( adapter, device )
52     {
53         struct dmx_sct_filter_params sec_filter;
54         memset(&sec_filter, 0, sizeof (struct dmx_sct_filter_params));
55         sec_filter.pid = PID;
56         sec_filter.filter.filter[0] = 62;
57         sec_filter.filter.mask[0] = 0xff;
58         sec_filter.flags = DMX_IMMEDIATE_START;
59         sec_filter.flags |= DMX_CHECK_CRC;
60
61         handle.protocol().setSectionFilter( &sec_filter );
62
63         senf::Scheduler::instance().add(
64             handle, senf::membind(&MySniffer::dumpSection, this));
65     }
66
67 private:
68     void dumpSection(senf::Scheduler::EventId event)
69     {
70         std::string data (handle.read());
71         senf::MPESection section (senf::MPESection::create(data));
72         section.dump(std::cout);
73         senf::PacketData & datagramData (section.next().data());
74         senf::hexdump(datagramData.begin(), datagramData.end(), std::cout);
75     }
76 };
77
78 int main(int argc, char const * argv[])
79 {
80     try {
81         MySniffer sniffer;
82         senf::Scheduler::instance().process();
83     }
84     catch (std::exception const & ex) {
85         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
86     }
87     return 0;
88 }
89
90
91 //-/////////////////////////////////////////////////////////////////////////////////////////////////
92 #undef prefix_
93
94 \f
95 // Local Variables:
96 // mode: c++
97 // fill-column: 100
98 // c-file-style: "senf"
99 // indent-tabs-mode: nil
100 // ispell-local-dictionary: "american"
101 // compile-command: "scons -u"
102 // comment-column: 40
103 // End: