? .dependencies
Index: server/livestreamer.c
===================================================================
RCS file: /var/cvsroot/streamdev/server/livestreamer.c,v
retrieving revision 1.11
diff -u -r1.11 livestreamer.c
--- server/livestreamer.c	15 Jan 2007 12:00:19 -0000	1.11
+++ server/livestreamer.c	2 Apr 2007 18:45:13 -0000
@@ -1,6 +1,10 @@
+#include <assert.h>
 #include <vdr/ringbuffer.h>
+#include <libsi/section.h>
+#include <libsi/descriptor.h>
 
 #include "server/livestreamer.h"
+#include "server/livefilter.h"
 #include "remux/ts2ps.h"
 #include "remux/ts2es.h"
 #include "remux/extern.h"
@@ -8,12 +12,31 @@
 
 // --- cStreamdevLiveReceiver -------------------------------------------------
 
+class cStreamdevLiveReceiver: public cReceiver {
+        friend class cStreamdevStreamer;
+
+private:
+	cStreamdevStreamer *m_Streamer;
+
+protected:
+	virtual void Activate(bool On);
+	virtual void Receive(uchar *Data, int Length);
+
+public:
+#if VDRVERSNUM < 10500
+	cStreamdevLiveReceiver(cStreamdevStreamer *Streamer, int Ca, int Priority, const int *Pids);
+#else
+	cStreamdevLiveReceiver(cStreamdevStreamer *Streamer, tChannelID ChannelID, int Priority, const int *Pids);
+#endif
+	virtual ~cStreamdevLiveReceiver();
+};
+
 #if VDRVERSNUM < 10500
-cStreamdevLiveReceiver::cStreamdevLiveReceiver(cStreamdevLiveStreamer *Streamer, int Ca, 
+cStreamdevLiveReceiver::cStreamdevLiveReceiver(cStreamdevStreamer *Streamer, int Ca, 
                                                int Priority, const int *Pids):
 		cReceiver(Ca, Priority, 0, Pids),
 #else
-cStreamdevLiveReceiver::cStreamdevLiveReceiver(cStreamdevLiveStreamer *Streamer, tChannelID ChannelID, 
+cStreamdevLiveReceiver::cStreamdevLiveReceiver(cStreamdevStreamer *Streamer, tChannelID ChannelID, 
                                                int Priority, const int *Pids):
 		cReceiver(ChannelID, Priority, 0, Pids),
 #endif
@@ -33,6 +56,179 @@
 		m_Streamer->ReportOverflow(Length - p);
 }
 
+inline void cStreamdevLiveReceiver::Activate(bool On) 
+{ 
+	Dprintf("LiveReceiver->Activate(%d)\n", On);
+	m_Streamer->Activate(On); 
+}
+
+// --- cStreamdevPatFilter ----------------------------------------------------
+
+class cStreamdevPatFilter : public cFilter {
+private:
+	int    pmtPid;
+	int    pmtSid;
+	int    pmtVersion;
+
+	const cChannel *m_Channel;
+	cStreamdevLiveStreamer *m_Streamer;
+
+	virtual void Process(u_short Pid, u_char Tid, const u_char *Data, int Length);
+public:
+	cStreamdevPatFilter(cStreamdevLiveStreamer *Streamer, const cChannel *Channel);
+};
+
+cStreamdevPatFilter::cStreamdevPatFilter(cStreamdevLiveStreamer *Streamer, const cChannel *Channel)
+{
+	Dprintf("cStreamdevPatFilter(\"%s\")", Channel->Name());
+	assert(Streamer);
+	m_Channel = Channel;
+	m_Streamer = Streamer;
+	pmtPid = 0;
+	pmtSid = 0;
+	pmtVersion = -1;
+	Set(0x00, 0x00);  // PAT
+}
+
+static const char * const psStreamTypes[] = {
+	"0", 
+	"ISO/IEC 11172 Video", 
+	"ISO/IEC 13818-2 Video", 
+	"ISO/IEC 11172 Audio", 
+	"ISO/IEC 13818-3 Audio",
+	"ISO/IEC 13818-1 Privete sections", 
+	"ISO/IEC 13818-1 Private PES data",
+	"ISO/IEC 13512 MHEG", 
+	"ISO/IEC 13818-1 Annex A DSM CC",
+	"0x09",
+	"ISO/IEC 13818-6 Multiprotocol encapsulation",
+	"ISO/IEC 13818-6 DSM-CC U-N Messages",
+	"ISO/IEC 13818-6 Stream Descriptors",
+	"ISO/IEC 13818-6 Sections (any type, including private data)",
+	"ISO/IEC 13818-1 auxiliary",
+	"ISO/IEC 13818-7 Audio with ADTS transport sytax",
+	"ISO/IEC 14496-2 Visual (MPEG-4)",
+	"ISO/IEC 14496-3 Audio with LATM transport syntax",
+	"0x12", "0x13", "0x14", "0x15", "0x16", "0x17", "0x18", "0x19", "0x1a",
+	"ISO/IEC 14496-10 Video (MPEG-4 part 10/AVC, aka H.264)",
+	"",
+};
+
+void cStreamdevPatFilter::Process(u_short Pid, u_char Tid, const u_char *Data, int Length)
+{
+	if (Pid == 0x00) {
+		if (Tid == 0x00 && !pmtPid) {
+			SI::PAT pat(Data, false);
+			if (!pat.CheckCRCAndParse())
+				return;
+			SI::PAT::Association assoc;
+			for (SI::Loop::Iterator it; pat.associationLoop.getNext(assoc, it); ) {
+				if (!assoc.isNITPid()) {
+					const cChannel *Channel =  Channels.GetByServiceID(Source(), Transponder(), assoc.getServiceId());
+					if (Channel && (Channel == m_Channel)) {
+						if (0 != (pmtPid = assoc.getPid())) {
+							Dprintf("cStreamdevPatFilter: PMT pid for channel %s: %d", Channel->Name(), pmtPid);
+							pmtSid = assoc.getServiceId();
+							if (Length < TS_SIZE - 5) {
+								// repack PAT to TS frame and send to client
+								uint8_t pat_ts[TS_SIZE] = {TS_SYNC_BYTE, 0x40 /* pusi=1 */, 0 /* pid=0 */, 0x10 /* adaption=1 */, 0 /* pointer */};
+								memcpy(pat_ts + 5, Data, Length);
+								m_Streamer->Put(pat_ts, TS_SIZE);
+							} else 
+								isyslog("cStreamdevPatFilter: PAT size %d too large to fit in one TS", Length);
+							m_Streamer->SetPid(pmtPid, true);
+							Add(pmtPid, 0x02);
+							pmtVersion = -1;
+							return;
+						}
+					}
+				}
+			}
+		}
+	} else if (Pid == pmtPid && Tid == SI::TableIdPMT && Source() && Transponder()) {
+		SI::PMT pmt(Data, false);
+		if (!pmt.CheckCRCAndParse())
+			return;
+		if (pmt.getServiceId() != pmtSid)
+			return; // skip broken PMT records
+		if (pmtVersion != -1) {
+			if (pmtVersion != pmt.getVersionNumber()) {
+				Dprintf("cStreamdevPatFilter: PMT version changed, detaching all pids");
+				m_Streamer->ClearPids();
+				Del(pmtPid, 0x02);
+				pmtPid = 0; // this triggers PAT scan
+			}
+			return;
+		}
+		pmtVersion = pmt.getVersionNumber();
+		SI::PMT::Stream stream;
+		int Ppid = pmt.getPCRPid();
+		if (Ppid && !m_Streamer->HasPid(Ppid)) {
+			Dprintf("cStreamdevPatFilter PMT scanner: adding PCR PID %d", Ppid);
+			m_Streamer->SetPid(Ppid, true);
+		}
+		for (SI::Loop::Iterator it; pmt.streamLoop.getNext(stream, it); ) {
+			if (stream.getPid() && !m_Streamer->HasPid(stream.getPid())) {
+				switch (stream.getStreamType()) {
+				case 0x01: // ISO/IEC 11172 Video
+				case 0x02: // ISO/IEC 13818-2 Video
+				case 0x03: // ISO/IEC 11172 Audio
+				case 0x04: // ISO/IEC 13818-3 Audio
+#if 0
+				case 0x07: // ISO/IEC 13512 MHEG
+				case 0x08: // ISO/IEC 13818-1 Annex A  DSM CC
+				case 0x0a: // ISO/IEC 13818-6 Multiprotocol encapsulation
+				case 0x0b: // ISO/IEC 13818-6 DSM-CC U-N Messages
+				case 0x0c: // ISO/IEC 13818-6 Stream Descriptors
+				case 0x0d: // ISO/IEC 13818-6 Sections (any type, including private data)
+				case 0x0e: // ISO/IEC 13818-1 auxiliary
+#endif
+				case 0x0f: // ISO/IEC 13818-7 Audio with ADTS transport syntax
+				case 0x10: // ISO/IEC 14496-2 Visual (MPEG-4)
+				case 0x11: // ISO/IEC 14496-3 Audio with LATM transport syntax
+				case 0x1b: // ISO/IEC 14496-10 Video (MPEG-4 part 10/AVC, aka H.264)
+					Dprintf("cStreamdevPatFilter PMT scanner adding PID %d (%s)",
+						stream.getPid(), psStreamTypes[stream.getStreamType()]);
+					m_Streamer->SetPid(stream.getPid(), true);
+					break;
+				case 0x05: // ISO/IEC 13818-1 private sections
+				case 0x06: // ISO/IEC 13818-1 PES packets containing private data
+					SI::Descriptor *d;
+					for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
+						switch (d->getDescriptorTag()) {
+						case SI::AC3DescriptorTag:
+							Dprintf("cStreamdevPatFilter PMT scanner: adding PID %d (%s) %s", 
+								stream.getPid(), psStreamTypes[stream.getStreamType()], "AC3");
+							m_Streamer->SetPid(stream.getPid(), true);
+							break;
+						case SI::TeletextDescriptorTag:
+							Dprintf("cStreamdevPatFilter PMT scanner: adding PID %d (%s) %s", 
+								stream.getPid(), psStreamTypes[stream.getStreamType()], "Teletext");
+							m_Streamer->SetPid(stream.getPid(), true);
+							break;
+						case SI::SubtitlingDescriptorTag:
+							Dprintf("cStreamdevPatFilter PMT scanner: adding PID %d (%s) %s", 
+								stream.getPid(), psStreamTypes[stream.getStreamType()], "DVBSUB");
+							m_Streamer->SetPid(stream.getPid(), true);
+							break;
+						default:
+							Dprintf("cStreamdevPatFilter PMT scanner: NOT adding PID %d (%s) %s", 
+								stream.getPid(), psStreamTypes[stream.getStreamType()], "UNKNOWN");
+							break;
+						}
+						delete d;
+					}
+					break;
+				default:
+					Dprintf("cStreamdevPatFilter PMT scanner: NOT adding PID %d (%s) %s",
+						stream.getPid(), psStreamTypes[stream.getStreamType()], "UNKNOWN");
+					break;
+				}
+			}
+		}
+	}
+}
+
 // --- cStreamdevLiveStreamer -------------------------------------------------
 
 cStreamdevLiveStreamer::cStreamdevLiveStreamer(int Priority):
@@ -43,6 +239,7 @@
 		m_Channel(NULL),
 		m_Device(NULL),
 		m_Receiver(NULL),
+		m_PatFilter(NULL),
 		m_PESRemux(NULL),
 		m_ESRemux(NULL),
 		m_PSRemux(NULL),
@@ -54,14 +251,30 @@
 {
 	Dprintf("Desctructing Live streamer\n");
 	Stop();
-	delete m_Receiver;
+	if (m_PatFilter) {
+		Detach();
+		DELETENULL(m_PatFilter);
+	}
+	DELETENULL(m_Receiver);
 	delete m_PESRemux;
 	delete m_ESRemux;
 	delete m_PSRemux;
 	delete m_ExtRemux;
-#if VDRVERSNUM >= 10300
-	//delete m_Filter; TODO
-#endif
+}
+
+bool cStreamdevLiveStreamer::HasPid(int Pid) 
+{
+	int idx;
+	for (idx = 0; idx < m_NumPids; ++idx)
+		if (m_Pids[idx] == Pid)
+			return true;
+	return false;
+}
+
+void cStreamdevLiveStreamer::ClearPids(void)
+{
+	m_NumPids = 0;
+	// do not detach yet, instead wait for next PMT update
 }
 
 bool cStreamdevLiveStreamer::SetPid(int Pid, bool On) 
@@ -126,7 +339,6 @@
 		}
 
 	case stPES: 
-		Dprintf("PES\n");
 		m_PESRemux = new cRemux(m_Channel->Vpid(), m_Channel->Apids(), m_Channel->Dpids(), 
 								m_Channel->Spids(), false);
 		if (Apid != 0)
@@ -149,13 +361,15 @@
 			    && SetPid(m_Channel->Dpid(0), true);
 
 	case stTS:
-		if (Apid != 0)
-			return SetPid(m_Channel->Vpid(),  true)
-			    && SetPid(Apid, true);
-		else
-			return SetPid(m_Channel->Vpid(),  true)
-			    && SetPid(m_Channel->Apid(0), true)
-			    && SetPid(m_Channel->Dpid(0), true);
+		// This should never happen, but ...
+		if (m_PatFilter) {
+			Detach();
+			DELETENULL(m_PatFilter);
+			ClearPids();
+		}
+		// Set pids from PMT
+		m_PatFilter = new cStreamdevPatFilter(this, m_Channel);
+		return (m_PatFilter ? true : false);
 
 	case stExtern:
 		m_ExtRemux = new cExternRemux(m_Channel->Vpid(), m_Channel->Apids(), m_Channel->Dpids(),
@@ -270,14 +484,28 @@
 	
 void cStreamdevLiveStreamer::Attach(void) 
 { 
-	printf("RIGHT ATTACH\n");
-	m_Device->AttachReceiver(m_Receiver); 
+	Dprintf("RIGHT ATTACH\n");
+	if (m_Device) {
+		if (m_Receiver) {
+			m_Device->Detach(m_Receiver); 
+	        	m_Device->AttachReceiver(m_Receiver); 
+		}
+		if (m_PatFilter) {
+			m_Device->Detach(m_PatFilter); 
+			m_Device->AttachFilter(m_PatFilter); 
+		}
+	}
 }
 
 void cStreamdevLiveStreamer::Detach(void) 
 { 
-	printf("RIGHT DETACH\n");
-	m_Device->Detach(m_Receiver); 
+	Dprintf("RIGHT DETACH\n");
+	if (m_Device) {
+		if (m_Receiver)
+			m_Device->Detach(m_Receiver); 
+		if (m_PatFilter)
+			m_Device->Detach(m_PatFilter); 
+	}
 }
 
 std::string cStreamdevLiveStreamer::Report(void) 
Index: server/livestreamer.h
===================================================================
RCS file: /var/cvsroot/streamdev/server/livestreamer.h,v
retrieving revision 1.10
diff -u -r1.10 livestreamer.h
--- server/livestreamer.h	15 Jan 2007 12:00:19 -0000	1.10
+++ server/livestreamer.h	2 Apr 2007 18:45:13 -0000
@@ -5,34 +5,14 @@
 #include <vdr/receiver.h>
 
 #include "server/streamer.h"
-#include "server/livefilter.h"
 #include "common.h"
 
 class cTS2PSRemux;
 class cTS2ESRemux;
 class cExternRemux;
 class cRemux;
-
-// --- cStreamdevLiveReceiver -------------------------------------------------
-
-class cStreamdevLiveReceiver: public cReceiver {
-	friend class cStreamdevLiveStreamer;
-
-private:
-	cStreamdevLiveStreamer *m_Streamer;
-
-protected:
-	virtual void Activate(bool On);
-	virtual void Receive(uchar *Data, int Length);
-
-public:
-#if VDRVERSNUM < 10500
-	cStreamdevLiveReceiver(cStreamdevLiveStreamer *Streamer, int Ca, int Priority, const int *Pids);
-#else
-	cStreamdevLiveReceiver(cStreamdevLiveStreamer *Streamer, tChannelID ChannelID, int Priority, const int *Pids);
-#endif
-	virtual ~cStreamdevLiveReceiver();
-};
+class cStreamdevPatFilter;
+class cStreamdevLiveReceiver;
 
 // --- cStreamdevLiveStreamer -------------------------------------------------
 
@@ -45,6 +25,7 @@
 	const cChannel         *m_Channel;
 	cDevice                *m_Device;
 	cStreamdevLiveReceiver *m_Receiver;
+	cStreamdevPatFilter    *m_PatFilter;
 	cRemux                 *m_PESRemux;
 	cTS2ESRemux            *m_ESRemux;
 	cTS2PSRemux            *m_PSRemux;
@@ -66,16 +47,11 @@
 	virtual void Attach(void);
 	virtual void Detach(void);
 
+	bool HasPid(int Pid);
+	void ClearPids(void); 
+
 	// Statistical purposes:
 	virtual std::string Report(void);
 };
 
-// --- cStreamdevLiveReceiver reverse inlines ---------------------------------
-
-inline void cStreamdevLiveReceiver::Activate(bool On) 
-{ 
-	Dprintf("LiveReceiver->Activate(%d)\n", On);
-	m_Streamer->Activate(On); 
-}
-
 #endif // VDR_STREAMDEV_LIVESTREAMER_H
