+diff --git a/src/streamproxy.c b/src/streamproxy.c
+index 3f82412..321d9c9 100644
+--- a/src/streamproxy.c
++++ b/src/streamproxy.c
+@@ -42,6 +42,25 @@ char *reason = "";
+
+ int active_pids[MAX_PIDS];
+
++int g_video_pid = 0;
++int g_audio_pid = 0;
++int g_pcr_pid = 0;
++int g_pmt_pid = 0;
++
++struct proc_dmx_pid
++{
++ char *proc_path;
++ char *str_match;
++ int pid;
++};
++
++struct proc_dmx_pid proc_dmx_setup[] = {
++ {"/proc/stb/encoder/0/video_pid", "video", 0},
++ {"/proc/stb/encoder/0/audio_pid", "audio", 0},
++ {"/proc/stb/encoder/0/pcr_pid", "pcr", 0},
++ {"/proc/stb/encoder/0/pmt_pid", "pmt", 0},
++};
++
+ int handle_upstream(void);
+ int handle_upstream_line(void);
+
+@@ -81,6 +100,7 @@ int main(int argc, char **argv)
+ int i;
+ for (i=0; i<MAX_PIDS; ++i)
+ active_pids[i] = -1;
++
+
+ while (1)
+ {
+@@ -113,6 +133,8 @@ int main(int argc, char **argv)
+ goto bad_gateway;
+ }
+
++ memset(upstream_request, 0, 256);
++
+ snprintf(upstream_request, sizeof(upstream_request), "GET /web/stream?StreamService=%s HTTP/1.0\r\n%s%s\r\n", service_ref, xff_header, authorization);
+
+ if (write(upstream, upstream_request, strlen(upstream_request)) != strlen(upstream_request))
+@@ -168,6 +190,7 @@ bad_gateway:
+ int handle_upstream(void)
+ {
+ char buffer[MAX_LINE_LENGTH];
++ memset(buffer, 0, MAX_LINE_LENGTH);
+ int n = read(upstream, buffer, MAX_LINE_LENGTH);
+ if (n == 0)
+ return 1;
+@@ -180,6 +203,7 @@ int handle_upstream(void)
+
+ char *c = buffer;
+
++
+ while (n)
+ {
+ char *next_line;
+@@ -218,6 +242,9 @@ int handle_upstream(void)
+
+ int handle_upstream_line(void)
+ {
++ int i;
++ const char *str;
++
+ switch (upstream_state)
+ {
+ case 0:
+@@ -244,104 +271,137 @@ int handle_upstream_line(void)
+ if (response_line[0] == '+') {
+ /* parse (and possibly open) demux */
+ int demux = atoi(response_line + 1);
+-
++ {
++ FILE *f = fopen("/proc/stb/encoder/0/demux", "w");
++ if(f)
++ {
++ fprintf(f, "%d\n", demux);
++ fclose(f);
++ }
++ }
+
+- /* parse new pids */
+- const char *p = strchr(response_line, ':');
+- int old_active_pids[MAX_PIDS];
+-
+- memcpy(old_active_pids, active_pids, sizeof(active_pids));
+-
+- int nr_pids = 0, i, j;
+- while (p)
++ for(i = 0 ; i < sizeof(proc_dmx_setup)/sizeof(proc_dmx_setup[0]) ; i++)
+ {
+- ++p;
+- int pid = strtoul(p, 0, 0x10);
+- p = strchr(p, ',');
++ str = strstr(response_line, proc_dmx_setup[i].str_match);
++ if(str)
++ {
++ while(*str != ',') str--;
++ proc_dmx_setup[i].pid = strtoul(str+1, 0, 0x10);
++ FILE *f = fopen(proc_dmx_setup[i].proc_path, "w");
++ if(f)
++ {
++ fprintf(f, "%d\n", proc_dmx_setup[i].pid);
++ fclose(f);
++ }
++ }
+
+- /* do not add pids twice */
+- for (i = 0; i < nr_pids; ++i)
+- if (active_pids[i] == pid)
+- break;
++ }
+
+- if (i != nr_pids)
+- continue;
+
+- active_pids[nr_pids++] = pid;
++ if(proc_dmx_setup[3].pid && ( (g_video_pid != proc_dmx_setup[0].pid) || (g_audio_pid != proc_dmx_setup[1].pid) || (g_pcr_pid != proc_dmx_setup[2].pid)) )
++ {
++ g_video_pid = proc_dmx_setup[0].pid;
++ g_audio_pid = proc_dmx_setup[1].pid;
++ g_pcr_pid = proc_dmx_setup[2].pid;
++
++
++ /* parse new pids */
++ const char *p = strchr(response_line, ':');
++ int old_active_pids[MAX_PIDS];
+
+- if (nr_pids == MAX_PIDS)
+- break;
+- }
+-
+- for (i = nr_pids; i < MAX_PIDS; ++i)
+- active_pids[i] = -1;
++ memcpy(old_active_pids, active_pids, sizeof(active_pids));
+
+- /* check for added pids */
+- for (i = 0; i < nr_pids; ++i)
+- {
+- for (j = 0; j < MAX_PIDS; ++j)
+- if (active_pids[i] == old_active_pids[j])
++ int nr_pids = 0, i, j;
++ while (p)
++ {
++ ++p;
++ int pid = strtoul(p, 0, 0x10);
++ p = strchr(p, ',');
++
++ /* do not add pids twice */
++ for (i = 0; i < nr_pids; ++i)
++ if (active_pids[i] == pid)
++ break;
++
++ if (i != nr_pids)
++ continue;
++
++ active_pids[nr_pids++] = pid;
++
++ if (nr_pids == MAX_PIDS)
+ break;
+- if (j == MAX_PIDS) {
+- if (demux_fd < 0) {
+- struct dmx_pes_filter_params flt;
+- char demuxfn[32];
+- sprintf(demuxfn, "/dev/dvb/adapter0/demux%d", demux);
+- demux_fd = open(demuxfn, O_RDWR | O_NONBLOCK);
++ }
++
++ for (i = nr_pids; i < MAX_PIDS; ++i)
++ active_pids[i] = -1;
++
++ /* check for added pids */
++ for (i = 0; i < nr_pids; ++i)
++ {
++ for (j = 0; j < MAX_PIDS; ++j)
++ if (active_pids[i] == old_active_pids[j])
++ break;
++ if (j == MAX_PIDS) {
+ if (demux_fd < 0) {
+- reason = "DEMUX OPEN FAILED";
+- return 2;
+- }
++ struct dmx_pes_filter_params flt;
++ char demuxfn[32];
++ sprintf(demuxfn, "/dev/dvb/adapter0/demux%d", demux);
++ demux_fd = open(demuxfn, O_RDWR | O_NONBLOCK);
++ if (demux_fd < 0) {
++ reason = "DEMUX OPEN FAILED";
++ return 2;
++ }
+
+- ioctl(demux_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
++ ioctl(demux_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
+
+- flt.pid = active_pids[i];
+- flt.input = DMX_IN_FRONTEND;
++ flt.pid = active_pids[i];
++ flt.input = DMX_IN_FRONTEND;
+ #if DVB_API_VERSION > 3
+- flt.output = DMX_OUT_TSDEMUX_TAP;
+- flt.pes_type = DMX_PES_OTHER;
++ flt.output = DMX_OUT_TSDEMUX_TAP;
++ flt.pes_type = DMX_PES_OTHER;
+ #else
+- flt.output = DMX_OUT_TAP;
+- flt.pes_type = DMX_TAP_TS;
++ flt.output = DMX_OUT_TAP;
++ flt.pes_type = DMX_TAP_TS;
+ #endif
+- flt.flags = DMX_IMMEDIATE_START;
++ flt.flags = DMX_IMMEDIATE_START;
+
+- if (ioctl(demux_fd, DMX_SET_PES_FILTER, &flt) < 0) {
+- reason = "DEMUX PES FILTER SET FAILED";
+- return 2;
++ if (ioctl(demux_fd, DMX_SET_PES_FILTER, &flt) < 0) {
++ reason = "DEMUX PES FILTER SET FAILED";
++ return 2;
++ }
+ }
+- }
+- else {
+- uint16_t pid = active_pids[i];
+- int ret;
++ else {
++ uint16_t pid = active_pids[i];
++ int ret;
+ #if DVB_API_VERSION > 3
+- ret = ioctl(demux_fd, DMX_ADD_PID, &pid);
++ ret = ioctl(demux_fd, DMX_ADD_PID, &pid);
+ #else
+- ret = ioctl(demux_fd, DMX_ADD_PID, pid);
++ ret = ioctl(demux_fd, DMX_ADD_PID, pid);
+ #endif
+- if (ret < 0) {
+- reason = "DMX_ADD_PID FAILED";
+- return 2;
++ if (ret < 0) {
++ reason = "DMX_ADD_PID FAILED";
++ return 2;
++ }
+ }
+ }
+ }
+- }
+-
+- /* check for removed pids */
+- for (i = 0; i < MAX_PIDS; ++i)
+- {
+- if (old_active_pids[i] == -1)
+- continue;
+- for (j = 0; j < nr_pids; ++j)
+- if (old_active_pids[i] == active_pids[j])
+- break;
+- if (j == nr_pids) {
++
++ /* check for removed pids */
++ for (i = 0; i < MAX_PIDS; ++i)
++ {
++ if (old_active_pids[i] == -1)
++ continue;
++ for (j = 0; j < nr_pids; ++j)
++ if (old_active_pids[i] == active_pids[j])
++ break;
++ if (j == nr_pids) {
+ #if DVB_API_VERSION > 3
+- uint16_t pid = old_active_pids[i];
+- ioctl(demux_fd, DMX_REMOVE_PID, &pid);
++ uint16_t pid = old_active_pids[i];
++ ioctl(demux_fd, DMX_REMOVE_PID, &pid);
+ #else
+- ioctl(demux_fd, DMX_REMOVE_PID, old_active_pids[i]);
++ ioctl(demux_fd, DMX_REMOVE_PID, old_active_pids[i]);
+ #endif
++ }
+ }
+ }
+ if (upstream_state == 2) {