1 #include <lib/base/filepush.h>
2 #include <lib/base/eerror.h>
9 eFilePushThread::eFilePushThread(): m_messagepump(eApp, 0)
15 CONNECT(m_messagepump.recv_msg, eFilePushThread::recvEvent);
18 static void signal_handler(int x)
22 void eFilePushThread::thread()
24 off_t dest_pos = 0, source_pos = 0;
25 size_t bytes_read = 0;
27 off_t current_span_offset = 0;
28 size_t current_span_remaining = 0;
30 size_t written_since_last_sync = 0;
32 int already_empty = 0;
33 eDebug("FILEPUSH THREAD START");
35 /* we set the signal to not restart syscalls, so we can detect our signal. */
37 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
39 sigaction(SIGUSR1, &act, 0);
43 dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
44 source_pos = m_raw_source.lseek(0, SEEK_CUR);
46 /* m_stop must be evaluated after each syscall. */
49 /* first try flushing the bufptr */
50 if (m_buf_start != m_buf_end)
52 // TODO: take care of boundaries.
53 filterRecordData(m_buffer + m_buf_start, m_buf_end - m_buf_start);
54 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
55 // eDebug("wrote %d bytes", w);
61 // ... we would stop the thread
64 // posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
67 written_since_last_sync += w;
69 if (written_since_last_sync >= 2048*1024)
72 written_since_last_sync = 0;
75 // printf("FILEPUSH: wrote %d bytes\n", w);
80 /* now fill our buffer. */
82 if (m_sg && !current_span_remaining)
84 m_sg->getNextSourceSpan(source_pos, bytes_read, current_span_offset, current_span_remaining);
86 if (source_pos != current_span_offset)
87 source_pos = m_raw_source.lseek(current_span_offset, SEEK_SET);
91 size_t maxread = sizeof(m_buffer);
93 /* if we have a source span, don't read past the end */
94 if (m_sg && maxread > current_span_remaining)
95 maxread = current_span_remaining;
101 m_buf_end = m_raw_source.read(m_buffer, maxread);
108 if (errno == EOVERFLOW)
110 eWarning("OVERFLOW while recording");
113 eDebug("eFilePushThread *read error* (%m) - not yet handled");
117 /* on EOF, try COMMITting once. */
118 if (m_send_pvr_commit && !already_empty)
120 eDebug("sending PVR commit");
122 if (::ioctl(m_fd_dest, PVR_COMMIT) < 0 && errno == EINTR)
124 eDebug("commit done");
125 /* well check again */
130 eDebug("FILEPUSH: end-of-file! (currently unhandled)");
131 if (!m_raw_source.lseek(0, SEEK_SET))
140 source_pos += m_buf_end;
141 bytes_read += m_buf_end;
143 current_span_remaining -= m_buf_end;
146 // printf("FILEPUSH: read %d bytes\n", m_buf_end);
149 eDebug("FILEPUSH THREAD STOP");
152 void eFilePushThread::start(int fd_source, int fd_dest)
154 m_raw_source.setfd(fd_source);
159 int eFilePushThread::start(const char *filename, int fd_dest)
161 if (m_raw_source.open(filename) < 0)
168 void eFilePushThread::stop()
170 /* if we aren't running, don't bother stopping. */
179 void eFilePushThread::pause()
184 void eFilePushThread::seek(int whence, off_t where)
186 m_raw_source.lseek(where, whence);
189 void eFilePushThread::resume()
195 void eFilePushThread::flush()
197 m_buf_start = m_buf_end = 0;
200 void eFilePushThread::enablePVRCommit(int s)
202 m_send_pvr_commit = s;
205 void eFilePushThread::setScatterGather(iFilePushScatterGather *sg)
210 void eFilePushThread::sendEvent(int evt)
212 m_messagepump.send(evt);
215 void eFilePushThread::recvEvent(const int &evt)
220 void eFilePushThread::filterRecordData(const unsigned char *data, int len)