1 #include <lib/base/filepush.h>
2 #include <lib/base/eerror.h>
9 eFilePushThread::eFilePushThread(int io_prio_class, int io_prio_level)
10 :prio_class(io_prio_class), prio(io_prio_level), m_messagepump(eApp, 0)
16 CONNECT(m_messagepump.recv_msg, eFilePushThread::recvEvent);
19 static void signal_handler(int x)
23 void eFilePushThread::thread()
25 setIoPrio(prio_class, prio);
27 off_t dest_pos = 0, source_pos = 0;
28 size_t bytes_read = 0;
30 off_t current_span_offset = 0;
31 size_t current_span_remaining = 0;
33 size_t written_since_last_sync = 0;
35 int already_empty = 0;
36 eDebug("FILEPUSH THREAD START");
38 /* we set the signal to not restart syscalls, so we can detect our signal. */
40 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
42 sigaction(SIGUSR1, &act, 0);
46 source_pos = m_raw_source.lseek(0, SEEK_CUR);
48 /* m_stop must be evaluated after each syscall. */
51 /* first try flushing the bufptr */
52 if (m_buf_start != m_buf_end)
54 // TODO: take care of boundaries.
55 filterRecordData(m_buffer + m_buf_start, m_buf_end - m_buf_start);
56 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
57 // eDebug("wrote %d bytes", w);
63 // ... we would stop the thread
66 written_since_last_sync += w;
68 if (written_since_last_sync >= 512*1024)
70 int toflush = written_since_last_sync > 2*1024*1024 ?
71 2*1024*1024 : written_since_last_sync &~ 4095; // write max 2MB at once
72 dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
74 posix_fadvise(m_fd_dest, dest_pos, toflush, POSIX_FADV_DONTNEED);
75 written_since_last_sync -= toflush;
78 // printf("FILEPUSH: wrote %d bytes\n", w);
83 /* now fill our buffer. */
85 if (m_sg && !current_span_remaining)
87 m_sg->getNextSourceSpan(source_pos, bytes_read, current_span_offset, current_span_remaining);
89 if (source_pos != current_span_offset)
90 source_pos = m_raw_source.lseek(current_span_offset, SEEK_SET);
94 size_t maxread = sizeof(m_buffer);
96 /* if we have a source span, don't read past the end */
97 if (m_sg && maxread > current_span_remaining)
98 maxread = current_span_remaining;
104 m_buf_end = m_raw_source.read(m_buffer, maxread);
111 if (errno == EOVERFLOW)
113 eWarning("OVERFLOW while recording");
116 eDebug("eFilePushThread *read error* (%m) - not yet handled");
120 /* on EOF, try COMMITting once. */
121 if (m_send_pvr_commit && !already_empty)
123 eDebug("sending PVR commit");
125 if (::ioctl(m_fd_dest, PVR_COMMIT) < 0 && errno == EINTR)
127 eDebug("commit done");
128 /* well check again */
133 eDebug("FILEPUSH: end-of-file! (currently unhandled)");
134 if (!m_raw_source.lseek(0, SEEK_SET))
143 source_pos += m_buf_end;
144 bytes_read += m_buf_end;
146 current_span_remaining -= m_buf_end;
149 // printf("FILEPUSH: read %d bytes\n", m_buf_end);
151 fdatasync(m_fd_dest);
153 eDebug("FILEPUSH THREAD STOP");
156 void eFilePushThread::start(int fd_source, int fd_dest)
158 m_raw_source.setfd(fd_source);
163 int eFilePushThread::start(const char *filename, int fd_dest)
165 if (m_raw_source.open(filename) < 0)
172 void eFilePushThread::stop()
174 /* if we aren't running, don't bother stopping. */
183 void eFilePushThread::pause()
188 void eFilePushThread::seek(int whence, off_t where)
190 m_raw_source.lseek(where, whence);
193 void eFilePushThread::resume()
199 void eFilePushThread::flush()
201 m_buf_start = m_buf_end = 0;
204 void eFilePushThread::enablePVRCommit(int s)
206 m_send_pvr_commit = s;
209 void eFilePushThread::setScatterGather(iFilePushScatterGather *sg)
214 void eFilePushThread::sendEvent(int evt)
216 m_messagepump.send(evt);
219 void eFilePushThread::recvEvent(const int &evt)
224 void eFilePushThread::filterRecordData(const unsigned char *data, int len)