void work(void) { auto inputPort = this->input(0); auto outputPort = this->output(0); while (inputPort->hasMessage()) { auto m = inputPort->popMessage(); outputPort->postMessage(m); } const auto &buffer = inputPort->buffer(); if (buffer.length != 0) { outputPort->postBuffer(buffer); inputPort->consume(inputPort->elements()); for (size_t i = 0; i < inputPort->elements(); i++) { if (std::generate_canonical<double, 10>(_gen) <= _probability) { Pothos::Label label; label.index = i; label.width = buffer.dtype.size(); if (not _ids.empty()) label.id = _ids.at(_randomId(_gen)); outputPort->postLabel(label); } } } }
status_t BnSurface::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case REGISTER_BUFFERS: { CHECK_INTERFACE(ISurface, data, reply); BufferHeap buffer; buffer.w = data.readInt32(); buffer.h = data.readInt32(); buffer.hor_stride = data.readInt32(); buffer.ver_stride= data.readInt32(); buffer.format = data.readInt32(); buffer.transform = data.readInt32(); buffer.flags = data.readInt32(); buffer.heap = interface_cast<IMemoryHeap>(data.readStrongBinder()); status_t err = registerBuffers(buffer); reply->writeInt32(err); return NO_ERROR; } break; case UNREGISTER_BUFFERS: { CHECK_INTERFACE(ISurface, data, reply); unregisterBuffers(); return NO_ERROR; } break; case POST_BUFFER: { CHECK_INTERFACE(ISurface, data, reply); ssize_t offset = data.readInt32(); postBuffer(offset); return NO_ERROR; } break; case CREATE_OVERLAY: { CHECK_INTERFACE(ISurface, data, reply); int w = data.readInt32(); int h = data.readInt32(); int f = data.readInt32(); sp<OverlayRef> o = createOverlay(w, h, f); return OverlayRef::writeToParcel(reply, o); } break; default: return BBinder::onTransact(code, data, reply, flags); } }
void work(void) { auto inputPort = this->input(0); auto outputPort = this->output(0); //extract message if (not inputPort->hasMessage()) return; auto msg = inputPort->popMessage(); //forward non-packet messages if (msg.type() != typeid(Pothos::Packet)) { outputPort->postMessage(msg); return; } const auto &packet = msg.extract<Pothos::Packet>(); //post output labels for (auto label : packet.labels) { outputPort->postLabel(label.toAdjusted( packet.payload.dtype.size(), 1)); //elements to bytes } //post start of frame label if (not _frameStartId.empty()) { outputPort->postLabel(Pothos::Label(_frameStartId, Pothos::Object(), 0, packet.payload.dtype.size())); } //post end of frame label if (not _frameEndId.empty()) { outputPort->postLabel(Pothos::Label(_frameEndId, Pothos::Object(), packet.payload.length-1, packet.payload.dtype.size())); } //post the payload outputPort->postBuffer(packet.payload); }
void work(void) { auto inputPort = this->input(0); auto outputPort = this->output(0); while (inputPort->hasMessage() and _elementsLeft != 0) { auto m = inputPort->popMessage(); outputPort->postMessage(m); _elementsLeft -= 1; } auto buffer = inputPort->buffer(); //input port type unspecified, inspect buffer for actual element count const size_t elems = std::min(_elementsLeft, buffer.elements()); if (elems != 0) { buffer.length = elems*buffer.dtype.size(); outputPort->postBuffer(buffer); inputPort->consume(buffer.length); _elementsLeft -= elems; } }
void NetworkSource::work(void) { const auto timeout = Poco::Timespan(this->workInfo().maxTimeoutNs/1000); auto outputPort = this->output(0); //recv the header, use output buffer when possible for zero-copy Poco::UInt16 type; Poco::UInt64 index; auto buffer = outputPort->buffer(); _ep.recv(type, index, buffer, timeout); //handle the output if (type == PothosPacketTypeBuffer) { _nextExpectedIndex = index + buffer.length; outputPort->popBuffer(buffer.length); outputPort->postBuffer(buffer); } else if (type == PothosPacketTypeMessage) { std::istringstream iss(std::string(buffer.as<char *>(), buffer.length)); Pothos::Object msg; msg.deserialize(iss); outputPort->postMessage(msg); } else if (type == PothosPacketTypeLabel) { std::istringstream iss(std::string(buffer.as<char *>(), buffer.length)); Pothos::Label label; label.index = index + outputPort->totalElements() - _nextExpectedIndex; label.data.deserialize(iss); outputPort->postLabel(label); } else this->yield(); }
void work(void) { auto inputPort = this->input(0); auto outputPort = this->output(0); size_t consumed = 0; //get input buffer auto inBuff = inputPort->buffer(); if (inBuff.length == 0) return; //label propagation offset incremented as preambles are posted size_t labelIndexOffset = 0; //track the index of the last found frame start label int lastFoundIndex = -1; for (auto &label : inputPort->labels()) { // Skip any label that doesn't yet appear in the data buffer if (label.index >= inputPort->elements()) continue; Pothos::Label outLabel(label); //modified and posted below //increment the offset as soon as we are past the last found index if (lastFoundIndex != -1 and size_t(lastFoundIndex) != label.index) { lastFoundIndex = -1; labelIndexOffset += _preambleBuff.elements(); } if (label.id == _frameStartId) { //post everything before this label Pothos::BufferChunk headBuff = inBuff; size_t headElems = label.index - consumed; headBuff.length = headElems*sizeof(Type); if (headBuff.length != 0) outputPort->postBuffer(headBuff); //fill the preamble buffer Pothos::BufferChunk newPreambleBuff(typeid(Type), _preambleBuff.elements()); std::memcpy(newPreambleBuff.as<void *>(), _preambleBuff.as<const void *>(), _preambleBuff.length); auto p = newPreambleBuff.as<Type *>() + _syncWordWidth; //encode the header field into bits char headerBits[NUM_HEADER_BITS]; FrameHeaderFields headerFields; headerFields.id = _headerId; headerFields.length = 0; if (label.data.canConvert(typeid(size_t))) { headerFields.length = label.data.template convert<size_t>()*label.width; } headerFields.chksum = headerFields.doChecksum(); encodeHeaderWord(headerBits, headerFields); //encode header fields as BPSK into the preamble buffer const auto sym = _preamble.back(); for (size_t i = 0; i < NUM_HEADER_BITS; i++) { *p++ = (headerBits[i] != 0)?+sym:-sym; } //post the preamble buffer outputPort->postBuffer(newPreambleBuff); //remove header from the remaining buffer inBuff.length -= headBuff.length; inBuff.address += headBuff.length; consumed += headBuff.elements(); //mark for increment on next label index lastFoundIndex = label.index; } else if (label.id == _frameEndId) { //post everything before this label Pothos::BufferChunk headBuff = inBuff; size_t headElems = label.index + label.width - consumed; //place at end of width headBuff.length = headElems*sizeof(Type); headBuff.length = std::min(headBuff.length, inBuff.length); //bounds check if (headBuff.length != 0) outputPort->postBuffer(headBuff); //post the buffer outputPort->postBuffer(_paddingBuff); //remove header from the remaining buffer inBuff.length -= headBuff.length; inBuff.address += headBuff.length; consumed += headBuff.elements(); //increment index for insertion of padding labelIndexOffset += _paddingBuff.elements(); } //propagate labels here with the offset outLabel.index += labelIndexOffset; outputPort->postLabel(outLabel); } //post the remaining bytes if (inBuff.length != 0) outputPort->postBuffer(inBuff); //consume the entire buffer inputPort->consume(inputPort->elements()); }
status_t BnSurface::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case REQUEST_BUFFER: { CHECK_INTERFACE(ISurface, data, reply); int bufferIdx = data.readInt32(); uint32_t w = data.readInt32(); uint32_t h = data.readInt32(); uint32_t format = data.readInt32(); uint32_t usage = data.readInt32(); sp<GraphicBuffer> buffer(requestBuffer(bufferIdx, w, h, format, usage)); if (buffer == NULL) return BAD_VALUE; return reply->write(*buffer); } case SET_BUFFER_COUNT: { CHECK_INTERFACE(ISurface, data, reply); int bufferCount = data.readInt32(); status_t err = setBufferCount(bufferCount); reply->writeInt32(err); return NO_ERROR; } case REGISTER_BUFFERS: { CHECK_INTERFACE(ISurface, data, reply); BufferHeap buffer; buffer.w = data.readInt32(); buffer.h = data.readInt32(); buffer.hor_stride = data.readInt32(); buffer.ver_stride= data.readInt32(); buffer.format = data.readInt32(); buffer.transform = data.readInt32(); buffer.flags = data.readInt32(); buffer.heap = interface_cast<IMemoryHeap>(data.readStrongBinder()); status_t err = registerBuffers(buffer); reply->writeInt32(err); return NO_ERROR; } break; case UNREGISTER_BUFFERS: { CHECK_INTERFACE(ISurface, data, reply); unregisterBuffers(); return NO_ERROR; } break; case POST_BUFFER: { CHECK_INTERFACE(ISurface, data, reply); ssize_t offset = data.readInt32(); postBuffer(offset); return NO_ERROR; } break; case CREATE_OVERLAY: { CHECK_INTERFACE(ISurface, data, reply); int w = data.readInt32(); int h = data.readInt32(); int f = data.readInt32(); int orientation = data.readInt32(); sp<OverlayRef> o = createOverlay(w, h, f, orientation); return OverlayRef::writeToParcel(reply, o); } break; default: return BBinder::onTransact(code, data, reply, flags); } }