70void ruf::StreamWriter::setup_python() {
72 bp::class_<ruf::StreamWriter, ruf::StreamWriterPtr, boost::noncopyable>(
"StreamWriter", bp::init<>())
73 .def(
"open", &ruf::StreamWriter::open)
74 .def(
"close", &ruf::StreamWriter::close)
75 .def(
"isOpen", &ruf::StreamWriter::isOpen)
76 .def(
"setRaw", &ruf::StreamWriter::setRaw)
77 .def(
"getRaw", &ruf::StreamWriter::getRaw)
78 .def(
"setBufferSize", &ruf::StreamWriter::setBufferSize)
79 .def(
"setMaxSize", &ruf::StreamWriter::setMaxSize)
80 .def(
"setDropErrors", &ruf::StreamWriter::setDropErrors)
81 .def(
"getChannel", &ruf::StreamWriter::getChannel)
82 .def(
"getTotalSize", &ruf::StreamWriter::getTotalSize)
83 .def(
"getCurrentSize", &ruf::StreamWriter::getCurrentSize)
84 .def(
"getBandwidth", &ruf::StreamWriter::getBandwidth)
85 .def(
"getFrameCount", &ruf::StreamWriter::getFrameCount)
86 .def(
"waitFrameCount", &ruf::StreamWriter::waitFrameCount);
117void ruf::StreamWriter::open(std::string file) {
121 std::lock_guard<std::mutex> lock(mtx_);
126 if (fd_ >= 0) ::close(fd_);
133 if (sizeLimit_ > 0) name.append(
".1");
137 if ((fd_ = ::open(name.c_str(),
138 O_WRONLY | O_CREAT | O_EXCL,
139 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
149 bandwidthHistory_.clear();
153 for (std::map<uint32_t, ruf::StreamWriterChannelPtr>::iterator it = channelMap_.begin(); it != channelMap_.end();
155 it->second->setFrameCount(0);
251double ruf::StreamWriter::getBandwidth() {
253 std::lock_guard<std::mutex> lock(mtx_);
255 auto now = std::chrono::steady_clock::now();
258 if (bandwidthHistory_.empty())
return (0.0);
260 auto windowStart = bandwidthHistory_.front().first;
261 double seconds = std::chrono::duration<double>(now - windowStart).count();
262 if (seconds <= 0.0)
return (
static_cast<double>(bandwidthBytes_));
263 return (
static_cast<double>(bandwidthBytes_) / seconds);
271bool ruf::StreamWriter::waitFrameCount(uint32_t count, uint64_t timeout) {
272 struct timeval endTime;
273 struct timeval sumTime;
274 struct timeval curTime;
277 std::unique_lock<std::mutex> lock(mtx_);
280 gettimeofday(&curTime, NULL);
282 div_t divResult = div(timeout, 1000000);
283 sumTime.tv_sec = divResult.quot;
284 sumTime.tv_usec = divResult.rem;
286 timeradd(&curTime, &sumTime, &endTime);
289 while (frameCount_ < count) {
290 cond_.wait_for(lock, std::chrono::microseconds(1000));
293 gettimeofday(&curTime, NULL);
294 if (timercmp(&curTime, &endTime, >))
break;
298 return (frameCount_ >= count);
301void ruf::StreamWriter::pruneBandwidth(std::chrono::steady_clock::time_point now) {
302 auto cutoff = now - std::chrono::seconds(1);
303 while (!bandwidthHistory_.empty() && bandwidthHistory_.front().first < cutoff) {
304 bandwidthBytes_ -= bandwidthHistory_.front().second;
305 bandwidthHistory_.pop_front();
318void ruf::StreamWriter::writeFile(uint8_t channel, std::shared_ptr<rogue::interfaces::stream::Frame> frame) {
319 ris::Frame::BufferIterator it;
323 if ((frame->getPayload() == 0) || (dropErrors_ && (frame->getError() != 0)))
return;
326 std::unique_lock<std::mutex> lock(mtx_);
331 size = frame->getPayload();
337 size = frame->getPayload() + 4;
346 value = frame->getFlags();
347 value |= (frame->getError() << 16);
348 value |= (channel << 24);
353 for (it = frame->beginBuffer(); it != frame->endBuffer(); ++it) intWrite((*it)->begin(), (*it)->getPayload());
362void ruf::StreamWriter::intWrite(
void* data, uint32_t size) {
366 if ((size + currBuffer_) > buffSize_) flush();
370 if (size > buffSize_) {
371 if (write(fd_, data, size) !=
static_cast<int32_t
>(size)) {
374 log_->error(
"Write failed, closing file!");
380 }
else if (buffSize_ > 0 && size > 0) {
381 std::memcpy(buffer_ + currBuffer_, data, size);
385 recordBandwidth(size);
389void ruf::StreamWriter::checkSize(uint32_t size) {
393 if (sizeLimit_ == 0)
return;
396 if (size > sizeLimit_)
397 throw(
rogue::GeneralError(
"StreamWriter::checkSize",
"Frame size is larger than file size limit"));
400 if ((size + currBuffer_ + currSize_) > sizeLimit_) {
407 name = baseName_ +
"." + std::to_string(fdIdx_);
411 if ((fd_ = ::open(name.c_str(),
412 O_WRONLY | O_CREAT | O_EXCL,
413 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) {