Support transcoding solo2/duo2.
[vuplus_openvuplus_3.0] / meta-openvuplus / recipes-vuplus / enigma2 / enigma2-transtreamproxy / transcoding.patch
1 diff --git a/src/streamproxy.c b/src/streamproxy.c
2 index 3f82412..321d9c9 100644
3 --- a/src/streamproxy.c
4 +++ b/src/streamproxy.c
5 @@ -42,6 +42,25 @@ char *reason = "";
6  
7  int active_pids[MAX_PIDS];
8  
9 +int g_video_pid = 0;
10 +int g_audio_pid = 0;
11 +int g_pcr_pid = 0;
12 +int g_pmt_pid = 0;
13 +
14 +struct proc_dmx_pid
15 +{
16 +       char *proc_path;
17 +       char *str_match;
18 +       int pid;
19 +};
20 +
21 +struct proc_dmx_pid proc_dmx_setup[] = {
22 +       {"/proc/stb/encoder/0/video_pid", "video", 0},
23 +       {"/proc/stb/encoder/0/audio_pid", "audio", 0},
24 +       {"/proc/stb/encoder/0/pcr_pid", "pcr", 0},
25 +       {"/proc/stb/encoder/0/pmt_pid", "pmt", 0},
26 +};
27 +
28  int handle_upstream(void);
29  int handle_upstream_line(void);
30  
31 @@ -81,6 +100,7 @@ int main(int argc, char **argv)
32         int i;
33         for (i=0; i<MAX_PIDS; ++i)
34                 active_pids[i] = -1;
35 +
36         
37         while (1)
38         {
39 @@ -113,6 +133,8 @@ int main(int argc, char **argv)
40                 goto bad_gateway;
41         }
42  
43 +       memset(upstream_request, 0, 256);
44 +
45         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);
46  
47         if (write(upstream, upstream_request, strlen(upstream_request)) != strlen(upstream_request))
48 @@ -168,6 +190,7 @@ bad_gateway:
49  int handle_upstream(void)
50  {
51         char buffer[MAX_LINE_LENGTH];
52 +       memset(buffer, 0, MAX_LINE_LENGTH);
53         int n = read(upstream, buffer, MAX_LINE_LENGTH);
54         if (n == 0)
55                 return 1;
56 @@ -180,6 +203,7 @@ int handle_upstream(void)
57         
58         char *c = buffer;
59         
60 +       
61         while (n)
62         {
63                 char *next_line;
64 @@ -218,6 +242,9 @@ int handle_upstream(void)
65  
66  int handle_upstream_line(void)
67  {
68 +       int i;
69 +       const char *str;
70 +
71         switch (upstream_state)
72         {
73         case 0:
74 @@ -244,104 +271,137 @@ int handle_upstream_line(void)
75                 if (response_line[0] == '+') {
76                                         /* parse (and possibly open) demux */
77                         int demux = atoi(response_line + 1);
78 -                       
79 +                       {
80 +                               FILE *f = fopen("/proc/stb/encoder/0/demux", "w");
81 +                               if(f)
82 +                               {
83 +                                       fprintf(f, "%d\n", demux);
84 +                                       fclose(f);
85 +                               }
86 +                       }
87  
88 -                                       /* parse new pids */
89 -                       const char *p = strchr(response_line, ':');
90 -                       int old_active_pids[MAX_PIDS];
91 -                       
92 -                       memcpy(old_active_pids, active_pids, sizeof(active_pids));
93 -                       
94 -                       int nr_pids = 0, i, j;
95 -                       while (p)
96 +                       for(i = 0 ; i < sizeof(proc_dmx_setup)/sizeof(proc_dmx_setup[0]) ; i++)
97                         {
98 -                               ++p;
99 -                               int pid = strtoul(p, 0, 0x10);
100 -                               p = strchr(p, ',');
101 +                               str = strstr(response_line, proc_dmx_setup[i].str_match);
102 +                               if(str)
103 +                               {
104 +                                       while(*str != ',') str--;
105 +                                       proc_dmx_setup[i].pid = strtoul(str+1, 0, 0x10);                        
106 +                                       FILE *f = fopen(proc_dmx_setup[i].proc_path, "w");
107 +                                       if(f)
108 +                                       {
109 +                                               fprintf(f, "%d\n", proc_dmx_setup[i].pid);
110 +                                               fclose(f);
111 +                                       }
112 +                               }
113                                 
114 -                                       /* do not add pids twice */
115 -                               for (i = 0; i < nr_pids; ++i)
116 -                                       if (active_pids[i] == pid)
117 -                                               break;
118 +                       }
119  
120 -                               if (i != nr_pids)
121 -                                       continue;
122  
123 -                               active_pids[nr_pids++] = pid;
124 +                       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)) )
125 +                       {
126 +                               g_video_pid = proc_dmx_setup[0].pid;
127 +                               g_audio_pid = proc_dmx_setup[1].pid;
128 +                               g_pcr_pid = proc_dmx_setup[2].pid;
129 +
130 +
131 +                               /* parse new pids */
132 +                               const char *p = strchr(response_line, ':');
133 +                               int old_active_pids[MAX_PIDS];
134                                 
135 -                               if (nr_pids == MAX_PIDS)
136 -                                       break;
137 -                       }
138 -                       
139 -                       for (i = nr_pids; i < MAX_PIDS; ++i)
140 -                               active_pids[i] = -1;
141 +                               memcpy(old_active_pids, active_pids, sizeof(active_pids));
142                                 
143 -                                       /* check for added pids */
144 -                       for (i = 0; i < nr_pids; ++i)
145 -                       {
146 -                               for (j = 0; j < MAX_PIDS; ++j)
147 -                                       if (active_pids[i] == old_active_pids[j])
148 +                               int nr_pids = 0, i, j;
149 +                               while (p)
150 +                               {
151 +                                       ++p;
152 +                                       int pid = strtoul(p, 0, 0x10);
153 +                                       p = strchr(p, ',');
154 +                                       
155 +                                               /* do not add pids twice */
156 +                                       for (i = 0; i < nr_pids; ++i)
157 +                                               if (active_pids[i] == pid)
158 +                                                       break;
159 +
160 +                                       if (i != nr_pids)
161 +                                               continue;
162 +
163 +                                       active_pids[nr_pids++] = pid;
164 +                                       
165 +                                       if (nr_pids == MAX_PIDS)
166                                                 break;
167 -                               if (j == MAX_PIDS) {
168 -                                       if (demux_fd < 0) {
169 -                                               struct dmx_pes_filter_params flt; 
170 -                                               char demuxfn[32];
171 -                                               sprintf(demuxfn, "/dev/dvb/adapter0/demux%d", demux);
172 -                                               demux_fd = open(demuxfn, O_RDWR | O_NONBLOCK);
173 +                               }
174 +                               
175 +                               for (i = nr_pids; i < MAX_PIDS; ++i)
176 +                                       active_pids[i] = -1;
177 +                                       
178 +                                               /* check for added pids */
179 +                               for (i = 0; i < nr_pids; ++i)
180 +                               {
181 +                                       for (j = 0; j < MAX_PIDS; ++j)
182 +                                               if (active_pids[i] == old_active_pids[j])
183 +                                                       break;
184 +                                       if (j == MAX_PIDS) {
185                                                 if (demux_fd < 0) {
186 -                                                       reason = "DEMUX OPEN FAILED";
187 -                                                       return 2;
188 -                                               }
189 +                                                       struct dmx_pes_filter_params flt; 
190 +                                                       char demuxfn[32];
191 +                                                       sprintf(demuxfn, "/dev/dvb/adapter0/demux%d", demux);
192 +                                                       demux_fd = open(demuxfn, O_RDWR | O_NONBLOCK);
193 +                                                       if (demux_fd < 0) {
194 +                                                               reason = "DEMUX OPEN FAILED";
195 +                                                               return 2;
196 +                                                       }
197  
198 -                                               ioctl(demux_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
199 +                                                       ioctl(demux_fd, DMX_SET_BUFFER_SIZE, 1024*1024);
200  
201 -                                               flt.pid = active_pids[i];
202 -                                               flt.input = DMX_IN_FRONTEND;
203 +                                                       flt.pid = active_pids[i];
204 +                                                       flt.input = DMX_IN_FRONTEND;
205  #if DVB_API_VERSION > 3
206 -                                               flt.output = DMX_OUT_TSDEMUX_TAP;
207 -                                               flt.pes_type = DMX_PES_OTHER;
208 +                                                       flt.output = DMX_OUT_TSDEMUX_TAP;
209 +                                                       flt.pes_type = DMX_PES_OTHER;
210  #else
211 -                                               flt.output = DMX_OUT_TAP;
212 -                                               flt.pes_type = DMX_TAP_TS;
213 +                                                       flt.output = DMX_OUT_TAP;
214 +                                                       flt.pes_type = DMX_TAP_TS;
215  #endif
216 -                                               flt.flags = DMX_IMMEDIATE_START;
217 +                                                       flt.flags = DMX_IMMEDIATE_START;
218  
219 -                                               if (ioctl(demux_fd, DMX_SET_PES_FILTER, &flt) < 0) {
220 -                                                       reason = "DEMUX PES FILTER SET FAILED";
221 -                                                       return 2;
222 +                                                       if (ioctl(demux_fd, DMX_SET_PES_FILTER, &flt) < 0) {
223 +                                                               reason = "DEMUX PES FILTER SET FAILED";
224 +                                                               return 2;
225 +                                                       }
226                                                 }
227 -                                       }
228 -                                       else {
229 -                                               uint16_t pid = active_pids[i];
230 -                                               int ret;
231 +                                               else {
232 +                                                       uint16_t pid = active_pids[i];
233 +                                                       int ret;
234  #if DVB_API_VERSION > 3
235 -                                               ret = ioctl(demux_fd, DMX_ADD_PID, &pid);
236 +                                                       ret = ioctl(demux_fd, DMX_ADD_PID, &pid);
237  #else
238 -                                               ret = ioctl(demux_fd, DMX_ADD_PID, pid);
239 +                                                       ret = ioctl(demux_fd, DMX_ADD_PID, pid);
240  #endif
241 -                                               if (ret < 0) {
242 -                                                       reason = "DMX_ADD_PID FAILED";
243 -                                                       return 2;
244 +                                                       if (ret < 0) {
245 +                                                               reason = "DMX_ADD_PID FAILED";
246 +                                                               return 2;
247 +                                                       }
248                                                 }
249                                         }
250                                 }
251 -                       }
252 -                       
253 -                                       /* check for removed pids */
254 -                       for (i = 0; i < MAX_PIDS; ++i)
255 -                       {
256 -                               if (old_active_pids[i] == -1)
257 -                                       continue;
258 -                               for (j = 0; j < nr_pids; ++j)
259 -                                       if (old_active_pids[i] == active_pids[j])
260 -                                               break;
261 -                               if (j == nr_pids) {
262 +                               
263 +                                               /* check for removed pids */
264 +                               for (i = 0; i < MAX_PIDS; ++i)
265 +                               {
266 +                                       if (old_active_pids[i] == -1)
267 +                                               continue;
268 +                                       for (j = 0; j < nr_pids; ++j)
269 +                                               if (old_active_pids[i] == active_pids[j])
270 +                                                       break;
271 +                                       if (j == nr_pids) {
272  #if DVB_API_VERSION > 3
273 -                                       uint16_t pid = old_active_pids[i];
274 -                                       ioctl(demux_fd, DMX_REMOVE_PID, &pid);
275 +                                               uint16_t pid = old_active_pids[i];
276 +                                               ioctl(demux_fd, DMX_REMOVE_PID, &pid);
277  #else
278 -                                       ioctl(demux_fd, DMX_REMOVE_PID, old_active_pids[i]);
279 +                                               ioctl(demux_fd, DMX_REMOVE_PID, old_active_pids[i]);
280  #endif
281 +                                       }
282                                 }
283                         }
284                         if (upstream_state == 2) {