3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gr_stream_mux.h>
28 #include <gr_io_signature.h>
33 gr_make_stream_mux (size_t itemsize, const std::vector<int> &lengths)
35 return gr_stream_mux_sptr (new gr_stream_mux (itemsize, lengths));
38 gr_stream_mux::gr_stream_mux (size_t itemsize, const std::vector<int> &lengths)
39 : gr_block ("stream_mux",
40 gr_make_io_signature (1, -1, itemsize),
41 gr_make_io_signature (1, 1, itemsize)),
49 gr_stream_mux::~gr_stream_mux(void)
54 gr_stream_mux::forecast (int noutput_items, gr_vector_int &ninput_items_required)
56 unsigned ninputs = ninput_items_required.size ();
57 for (unsigned i = 0; i < ninputs; i++)
58 ninput_items_required[i] = 0;
63 gr_stream_mux::general_work(int noutput_items,
64 gr_vector_int &ninput_items,
65 gr_vector_const_void_star &input_items,
66 gr_vector_void_star &output_items)
69 char *out = (char *) output_items[0];
75 std::vector<int> consume_vector(d_lengths.size(), 0);
78 printf("mux: nouput_items: %d d_stream: %d\n", noutput_items, d_stream);
79 for(int i = 0; i < d_lengths.size(); i++)
80 printf("\tninput_items[%d]: %d\n", i, ninput_items[i]);
83 in = (const char *) input_items[d_stream];
87 printf("Cleaning up residual bytes (%d) from stream %d\n", d_residual, d_stream);
90 // get the number of items available in input stream up to the
92 N=std::min(d_residual, ninput_items[d_stream]);
94 // get the number of items we can put into the output buffer
95 M=std::min(N, noutput_items);
97 // copy the items to the output buff
98 memcpy(out, in, M*d_itemsize);
100 // increment the output accumulator
103 // keep track of items consumed
104 consume_vector[d_stream]=M;
106 // keep track if there are residual items left from the input stream
110 printf("Stream: %d (%x) Wrote: %d bytes Output has: %d bytes residual: %d bytes\n",
111 d_stream, in, M, acc, d_residual);
114 // if no residual items, we're done with this input stream for
117 if(d_stream == d_lengths.size() - 1) {
118 d_stream=0; // wrap stream pointer
121 d_stream++; // or increment the stream pointer
124 printf("Going to next stream: %d\n", d_stream);
126 in = ((const char *) (input_items[d_stream])) + d_itemsize*consume_vector[d_stream];
131 while (acc<noutput_items){
132 // get the number of items available in input stream up to the
133 // num items required
134 N=std::min(d_lengths[d_stream], ninput_items[d_stream]);
136 // get the number of items we can put into the output buffer
137 M=std::min(N, noutput_items-acc);
139 // copy the items to the output buff
140 memcpy(out+acc*d_itemsize,in,M*d_itemsize);
142 // increment the output accumulator
145 // keep track of items consumed
146 consume_vector[d_stream]+=M;
148 // keep track if there are residual items left from the input stream
149 d_residual=d_lengths[d_stream] - M;
152 printf("Stream: %d (%x) Wrote: %d bytes Output has: %d bytes residual: %d bytes\n",
153 d_stream, in, M, acc, d_residual);
156 // if no residual items, we're done with this input stream for
159 if(d_stream == d_lengths.size() - 1) {
160 d_stream=0; // wrap stream pointer
163 d_stream++; // or increment the stream pointer
166 printf("Going to next stream: %d\n", d_stream);
169 // get next stream pointer
170 in = ((const char *) (input_items[d_stream])) + d_itemsize*consume_vector[d_stream];
178 for (unsigned int j=0;j<d_lengths.size();j++){
179 consume(j,consume_vector[j]);
182 printf("consuming: %d on stream: %d\n", consume_vector[j], j);
187 printf("mux: returning: %d\n\n", acc);