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");
34 // this is a race. FIXME.
36 /* we set the signal to not restart syscalls, so we can detect our signal. */
38 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
40 sigaction(SIGUSR1, &act, 0);
42 dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
43 source_pos = m_raw_source.lseek(0, SEEK_CUR);
45 /* m_stop must be evaluated after each syscall. */
48 /* first try flushing the bufptr */
49 if (m_buf_start != m_buf_end)
51 // TODO: take care of boundaries.
52 filterRecordData(m_buffer + m_buf_start, m_buf_end - m_buf_start);
53 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
54 // eDebug("wrote %d bytes", w);
60 // ... we would stop the thread
63 // posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
66 written_since_last_sync += w;
68 if (written_since_last_sync >= 2048*1024)
71 written_since_last_sync = 0;
74 // printf("FILEPUSH: wrote %d bytes\n", w);
79 /* now fill our buffer. */
81 if (m_sg && !current_span_remaining)
83 m_sg->getNextSourceSpan(source_pos, bytes_read, current_span_offset, current_span_remaining);
85 if (source_pos != current_span_offset)
86 source_pos = m_raw_source.lseek(current_span_offset, SEEK_SET);
90 size_t maxread = sizeof(m_buffer);
92 /* if we have a source span, don't read past the end */
93 if (m_sg && maxread > current_span_remaining)
94 maxread = current_span_remaining;
100 m_buf_end = m_raw_source.read(m_buffer, maxread);
107 if (errno == EOVERFLOW)
109 eWarning("OVERFLOW while recording");
112 eDebug("eFilePushThread *read error* (%m) - not yet handled");
116 /* on EOF, try COMMITting once. */
117 if (m_send_pvr_commit && !already_empty)
119 eDebug("sending PVR commit");
121 if (::ioctl(m_fd_dest, PVR_COMMIT) < 0 && errno == EINTR)
123 eDebug("commit done");
124 /* well check again */
129 eDebug("FILEPUSH: end-of-file! (currently unhandled)");
130 if (!m_raw_source.lseek(0, SEEK_SET))
139 source_pos += m_buf_end;
140 bytes_read += m_buf_end;
142 current_span_remaining -= m_buf_end;
145 // printf("FILEPUSH: read %d bytes\n", m_buf_end);
148 eDebug("FILEPUSH THREAD STOP");
151 void eFilePushThread::start(int fd_source, int fd_dest)
153 m_raw_source.setfd(fd_source);
158 int eFilePushThread::start(const char *filename, int fd_dest)
160 if (m_raw_source.open(filename) < 0)
167 void eFilePushThread::stop()
169 if (!thread_running()) /* FIXME: races */
176 void eFilePushThread::pause()
181 void eFilePushThread::seek(int whence, off_t where)
183 m_raw_source.lseek(where, whence);
186 void eFilePushThread::resume()
192 void eFilePushThread::flush()
194 m_buf_start = m_buf_end = 0;
197 void eFilePushThread::enablePVRCommit(int s)
199 m_send_pvr_commit = s;
202 void eFilePushThread::setScatterGather(iFilePushScatterGather *sg)
207 void eFilePushThread::sendEvent(int evt)
209 m_messagepump.send(evt);
212 void eFilePushThread::recvEvent(const int &evt)
217 void eFilePushThread::filterRecordData(const unsigned char *data, int len)