rogue
Loading...
Searching...
No Matches
StreamWriterChannel.cpp
Go to the documentation of this file.
1
19#include "rogue/Directives.h"
20
22
23#include <stdint.h>
24#include <sys/time.h>
25
26#include <memory>
27#include <thread>
28
29#include "rogue/GilRelease.h"
33
36
37#ifndef NO_PYTHON
38 #include <boost/python.hpp>
39namespace bp = boost::python;
40#endif
41
43ruf::StreamWriterChannelPtr ruf::StreamWriterChannel::create(ruf::StreamWriterPtr writer, uint8_t channel) {
44 ruf::StreamWriterChannelPtr s = std::make_shared<ruf::StreamWriterChannel>(writer, channel);
45 return (s);
46}
47
49void ruf::StreamWriterChannel::setup_python() {
50#ifndef NO_PYTHON
51 bp::class_<ruf::StreamWriterChannel, ruf::StreamWriterChannelPtr, bp::bases<ris::Slave>, boost::noncopyable>(
52 "StreamWriterChannel",
53 bp::no_init)
54 .def("getFrameCount", &ruf::StreamWriterChannel::getFrameCount)
55 .def("waitFrameCount", &ruf::StreamWriterChannel::waitFrameCount)
56 .def("setFrameCount", &ruf::StreamWriterChannel::setFrameCount);
57 bp::implicitly_convertible<ruf::StreamWriterChannelPtr, ris::SlavePtr>();
58#endif
59}
60
62ruf::StreamWriterChannel::StreamWriterChannel(ruf::StreamWriterPtr writer, uint8_t channel) {
63 writer_ = writer;
64 channel_ = channel;
65 frameCount_ = 0;
66}
67
69ruf::StreamWriterChannel::~StreamWriterChannel() {}
70
72void ruf::StreamWriterChannel::acceptFrame(ris::FramePtr frame) {
73 uint8_t ichan;
74
76 ris::FrameLockPtr fLock = frame->lock();
77
78 // Support for channelized traffic
79 if (channel_ == 0)
80 ichan = frame->getChannel();
81 else
82 ichan = channel_;
83
84 writer_->writeFile(ichan, frame);
85
86 std::unique_lock<std::mutex> lock(mtx_);
87 frameCount_++;
88 cond_.notify_all();
89}
90
91uint32_t ruf::StreamWriterChannel::getFrameCount() {
92 return frameCount_;
93}
94
95void ruf::StreamWriterChannel::setFrameCount(uint32_t count) {
97 std::unique_lock<std::mutex> lock(mtx_);
98 frameCount_ = count;
99}
100
101bool ruf::StreamWriterChannel::waitFrameCount(uint32_t count, uint64_t timeout) {
102 struct timeval endTime;
103 struct timeval sumTime;
104 struct timeval curTime;
105
106 rogue::GilRelease noGil;
107 std::unique_lock<std::mutex> lock(mtx_);
108
109 if (timeout != 0) {
110 gettimeofday(&curTime, NULL);
111
112 div_t divResult = div(timeout, 1000000);
113 sumTime.tv_sec = divResult.quot;
114 sumTime.tv_usec = divResult.rem;
115
116 timeradd(&curTime, &sumTime, &endTime);
117 }
118
119 while (frameCount_ < count) {
120 cond_.wait_for(lock, std::chrono::microseconds(1000));
121
122 if (timeout != 0) {
123 gettimeofday(&curTime, NULL);
124 if (timercmp(&curTime, &endTime, >)) break;
125 }
126 }
127
128 return (frameCount_ >= count);
129}
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110
std::shared_ptr< rogue::utilities::fileio::StreamWriter > StreamWriterPtr
std::shared_ptr< rogue::utilities::fileio::StreamWriterChannel > StreamWriterChannelPtr