ce0023ccdb5dffd0560bdb2f0147b2e76dd158b6
[senf.git] / Examples / psi2tsModule / psi2ts.cc
1 // $Id$
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
33 #define prefix_
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 namespace {
37     template <class InputIterator, class Distance>
38     void advance_max(InputIterator& i, Distance n, InputIterator& i_end)
39     {
40         Distance d = std::min( std::distance(i, i_end), n);
41         std::advance( i, d);
42     }
43 }
44
45
46 prefix_ Psi2TsModule::Psi2TsModule(unsigned pid, senf::ClockService::clock_type timeout)
47 {
48     pid_ = pid;
49     continuity_counter_ = 0;
50     state_ = IDLE;
51     route( input, output );
52     input.onRequest( &Psi2TsModule::onRequest );
53     timeout_ = timeout;
54 }
55 #include <senf/Utils/hexdump.hh>
56 prefix_ void Psi2TsModule::onRequest()
57 {
58     senf::PacketData & section = input.read().data();
59     iterator sec_end = section.end();
60     iterator begin = section.begin();
61     iterator end = section.begin();
62     advance_max( end, 183, sec_end);
63
64     do {
65         senf::TransportPacket tsPacket (senf::TransportPacket::create(188));
66         tsPacket->continuity_counter() = next_continuity_counter();
67         tsPacket->pid() = pid_;
68         if (state_ == IDLE) {
69             state_ = PROC;
70             tsPacket->setPUSI(true);
71             tsPacket->pointer_field() = 0;
72         }
73         senf::PacketData & payloadData (tsPacket.next().data());
74         std::fill(
75                 std::copy( begin, end, payloadData.begin() ),
76                 payloadData.end(),
77                 0xff
78         );
79         tsPacket.finalize();
80
81         output.write( tsPacket);
82
83         advance_max( begin, 184, sec_end);
84         advance_max( end,   184, sec_end);
85     } while (begin != end);
86     state_ = IDLE;
87 }
88
89 prefix_ unsigned Psi2TsModule::next_continuity_counter()
90 {
91     continuity_counter_ = (continuity_counter_ + 1) % 16;
92     return continuity_counter_;
93 }
94
95
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
97 #undef prefix_
98 //#include "psi2ts.cc.mpp"
99
100 \f
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // comment-column: 40
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // End: