very fist alpha version of PSI-Section-to-TransportPacket encoder
[senf.git] / Examples / psi2tsModule / psi2ts.cc
1 // $Id: Example.cc 661 2008-02-05 09:53:54Z tho $
2 //
3 // Copyright (C) 2008
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 /** \file
24     \brief psi2ts.cc non-inline non-template implementation */
25
26 #include "psi2ts.hh"
27 //#include "psi2ts.ih"
28
29 // Custom includes
30
31 //#include "psi2ts.cc.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 namespace {
36     template <class InputIterator, class Distance>
37     void advance_max(InputIterator& i, Distance n, InputIterator& i_end)
38     {
39         Distance d = std::min( std::distance(i, i_end), n);
40         std::advance( i, d);
41     }
42 }
43
44
45 prefix_ Psi2TsModule::Psi2TsModule()
46 {
47     continuity_counter_ = 0;
48     state_ = IDLE;
49     route( input, output );
50     input.onRequest( &Psi2TsModule::onRequest );
51 }
52
53 prefix_ void Psi2TsModule::onRequest()
54 {
55     senf::PacketData & section = input.read().data();
56     iterator sec_end = section.end();
57     iterator begin = section.begin();
58     iterator end = section.begin();
59     advance_max( end, 184, sec_end);
60     
61     do {
62         senf::TransportPacket tsPacket (senf::TransportPacket::create());
63         tsPacket->continuity_counter() = next_continuity_counter();
64         if (state_ == IDLE) {
65             state_ = PROC;
66             tsPacket->pusi() = true;
67         }
68         senf::DataPacket::createAfter( tsPacket, boost::make_iterator_range(begin, end));
69         tsPacket.finalize();
70         advance_max( begin, 184, sec_end);
71         advance_max( end,   184, sec_end);
72     } while (begin != end);
73     state_ = IDLE;
74 }
75
76 prefix_ unsigned Psi2TsModule::next_continuity_counter()
77 {
78     continuity_counter_ = (continuity_counter_ + 1) % 16;
79     return continuity_counter_;
80 }
81
82
83 ///////////////////////////////cc.e////////////////////////////////////////
84 #undef prefix_
85 //#include "psi2ts.cc.mpp"
86
87 \f
88 // Local Variables:
89 // mode: c++
90 // fill-column: 100
91 // comment-column: 40
92 // c-file-style: "senf"
93 // indent-tabs-mode: nil
94 // ispell-local-dictionary: "american"
95 // compile-command: "scons -u test"
96 // End: