1 #include <lib/service/servicedvbrecord.h>
2 #include <lib/base/eerror.h>
6 DEFINE_REF(eDVBServiceRecord);
8 eDVBServiceRecord::eDVBServiceRecord(const eServiceReferenceDVB &ref): m_ref(ref), m_service_handler(1)
10 CONNECT(m_service_handler.serviceEvent, eDVBServiceRecord::serviceEvent);
15 void eDVBServiceRecord::serviceEvent(int event)
17 eDebug("RECORD service event %d", event);
20 case eDVBServicePMTHandler::eventTuned:
25 eDebug("Recording to %s...", m_filename.c_str());
26 ::remove(m_filename.c_str());
27 int fd = ::open(m_filename.c_str(), O_WRONLY|O_CREAT, 0644);
30 eDebug("eDVBServiceRecord - can't open hardcoded recording file!");
34 ePtr<iDVBDemux> demux;
35 if (m_service_handler.getDemux(demux))
37 eDebug("eDVBServiceRecord - NO DEMUX available!");
41 demux->createTSRecorder(m_record);
44 eDebug("eDVBServiceRecord - no ts recorder available.");
48 m_record->setTargetFD(fd);
52 case eDVBServicePMTHandler::eventNewProgramInfo:
54 if (m_state == stateIdle)
56 else if (m_want_record) /* doRecord can be called from Prepared and Recording state */
64 RESULT eDVBServiceRecord::prepare(const char *filename)
66 m_filename = filename;
67 if (m_state == stateIdle)
73 RESULT eDVBServiceRecord::start()
76 /* when tune wasn't yet successfully, doRecord stays in "prepared"-state which is fine. */
81 RESULT eDVBServiceRecord::stop()
83 eDebug("stop recording!!");
84 if (m_state == stateRecording)
87 m_state = statePrepared;
90 if (m_state == statePrepared)
99 int eDVBServiceRecord::doPrepare()
101 /* allocate a ts recorder if we don't already have one. */
102 if (m_state == stateIdle)
104 m_pids_active.clear();
105 m_state = statePrepared;
106 return m_service_handler.tune(m_ref);
111 int eDVBServiceRecord::doRecord()
113 int err = doPrepare();
119 eDebug("demux not available (tune failed?). cannot record.");
122 eDebug("starting recording..");
124 eDVBServicePMTHandler::program program;
125 if (m_service_handler.getProgramInfo(program))
126 eDebug("getting program info failed.");
129 std::set<int> pids_to_record;
131 pids_to_record.insert(0); // PAT
133 if (program.pmtPid != -1)
134 pids_to_record.insert(program.pmtPid); // PMT
136 eDebugNoNewLine("RECORD: have %d video stream(s)", program.videoStreams.size());
137 if (!program.videoStreams.empty())
139 eDebugNoNewLine(" (");
140 for (std::vector<eDVBServicePMTHandler::videoStream>::const_iterator
141 i(program.videoStreams.begin());
142 i != program.videoStreams.end(); ++i)
144 pids_to_record.insert(i->pid);
145 if (i != program.videoStreams.begin())
146 eDebugNoNewLine(", ");
147 eDebugNoNewLine("%04x", i->pid);
149 eDebugNoNewLine(")");
151 eDebugNoNewLine(", and %d audio stream(s)", program.audioStreams.size());
152 if (!program.audioStreams.empty())
154 eDebugNoNewLine(" (");
155 for (std::vector<eDVBServicePMTHandler::audioStream>::const_iterator
156 i(program.audioStreams.begin());
157 i != program.audioStreams.end(); ++i)
159 pids_to_record.insert(i->pid);
160 if (i != program.audioStreams.begin())
161 eDebugNoNewLine(", ");
162 eDebugNoNewLine("%04x", i->pid);
164 eDebugNoNewLine(")");
166 eDebug(", and the pcr pid is %04x", program.pcrPid);
167 if (program.pcrPid != 0x1fff)
168 pids_to_record.insert(program.pcrPid);
170 /* find out which pids are NEW and which pids are obsolete.. */
171 std::set<int> new_pids, obsolete_pids;
173 std::set_difference(pids_to_record.begin(), pids_to_record.end(),
174 m_pids_active.begin(), m_pids_active.end(),
175 std::inserter(new_pids, new_pids.begin()));
178 m_pids_active.begin(), m_pids_active.end(),
179 pids_to_record.begin(), pids_to_record.end(),
180 std::inserter(new_pids, new_pids.begin())
183 for (std::set<int>::iterator i(new_pids.begin()); i != new_pids.end(); ++i)
185 eDebug("ADD PID: %04x", *i);
186 m_record->addPID(*i);
188 for (std::set<int>::iterator i(obsolete_pids.begin()); i != obsolete_pids.end(); ++i)
190 eDebug("REMOVED PID: %04x", *i);
191 m_record->removePID(*i);
194 if (m_state != stateRecording)
197 m_state = stateRecording;