switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / Examples / psi2tsModule / psi2ts.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief psi2ts.cc non-inline non-template implementation */
30
31 #include "psi2ts.hh"
32 //#include "psi2ts.ih"
33
34 // Custom includes
35
36 //#include "psi2ts.cc.mpp"
37
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace {
42     template <class InputIterator, class Distance>
43     void advance_max(InputIterator& i, Distance n, InputIterator& i_end)
44     {
45         Distance d = std::min( std::distance(i, i_end), n);
46         std::advance( i, d);
47     }
48 }
49
50
51 prefix_ Psi2TsModule::Psi2TsModule(unsigned pid, senf::ClockService::clock_type timeout)
52 {
53     pid_ = pid;
54     continuity_counter_ = 0;
55     state_ = IDLE;
56     route( input, output );
57     input.onRequest( &Psi2TsModule::onRequest );
58     timeout_ = timeout;
59 }
60 #include <senf/Utils/hexdump.hh>
61 prefix_ void Psi2TsModule::onRequest()
62 {
63     senf::PacketData & section = input.read().data();
64     iterator sec_end = section.end();
65     iterator begin = section.begin();
66     iterator end = section.begin();
67     advance_max( end, 183, sec_end);
68
69     do {
70         senf::TransportPacket tsPacket (senf::TransportPacket::create(188));
71         tsPacket->continuity_counter() = next_continuity_counter();
72         tsPacket->pid() = pid_;
73         if (state_ == IDLE) {
74             state_ = PROC;
75             tsPacket->setPUSI(true);
76             tsPacket->pointer_field() = 0;
77         }
78         senf::PacketData & payloadData (tsPacket.next().data());
79         std::fill(
80                 std::copy( begin, end, payloadData.begin() ),
81                 payloadData.end(),
82                 0xff
83         );
84         tsPacket.finalize();
85
86         output.write( tsPacket);
87
88         advance_max( begin, 184, sec_end);
89         advance_max( end,   184, sec_end);
90     } while (begin != end);
91     state_ = IDLE;
92 }
93
94 prefix_ unsigned Psi2TsModule::next_continuity_counter()
95 {
96     continuity_counter_ = (continuity_counter_ + 1) % 16;
97     return continuity_counter_;
98 }
99
100
101 //-/////////////////////////////////////////////////////////////////////////////////////////////////
102 #undef prefix_
103 //#include "psi2ts.cc.mpp"
104
105 \f
106 // Local Variables:
107 // mode: c++
108 // fill-column: 100
109 // comment-column: 40
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // End: