summaryrefslogtreecommitdiffstats
path: root/recipes-graphics/wayland/weston/0002-Add-gst-recorder-for-h264-output-streaming.patch
blob: 36c4d93e5c7787d077295f52dd5b9bd00db89ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
From 3ff09fecbe55d2f84f9a83f8965dffb913c00db7 Mon Sep 17 00:00:00 2001
From: Damian Hobson-Garcia <dhobsong@igel.co.jp>
Date: Thu, 27 Apr 2017 16:47:00 +0900
Subject: [PATCH 2/3] Add gst-recorder for h264 output streaming

Following patch ported to Weston 1.11 with minor updates
--------
To use gst-recorder run weston with arg --gst-record. Add
recorder=true property to output section of one of the outputs
(real or virtual). Use properties ip, port to set host of RTP
stream. Use property bitrate to set desirable maximum h264 bitrate.
--------
---
 Makefile.am                   |   17 +
 configure.ac                  |   12 +
 src/compositor-drm.c          |  199 ++++++-
 src/compositor-drm.h          |    3 +
 src/gst-recorder.c            | 1213 +++++++++++++++++++++++++++++++++++++++++
 src/gst-recorder.h            |   58 ++
 src/main.c                    |    4 +-
 src/media-ctl/libmediactl.c   |  955 ++++++++++++++++++++++++++++++++
 src/media-ctl/libv4l2subdev.c |  759 ++++++++++++++++++++++++++
 src/media-ctl/mediactl-priv.h |   64 +++
 src/media-ctl/mediactl.h      |  423 ++++++++++++++
 src/media-ctl/tools.h         |   32 ++
 src/media-ctl/v4l2subdev.h    |  258 +++++++++
 13 files changed, 3995 insertions(+), 2 deletions(-)
 create mode 100644 src/gst-recorder.c
 create mode 100644 src/gst-recorder.h
 create mode 100644 src/media-ctl/libmediactl.c
 create mode 100644 src/media-ctl/libv4l2subdev.c
 create mode 100644 src/media-ctl/mediactl-priv.h
 create mode 100644 src/media-ctl/mediactl.h
 create mode 100644 src/media-ctl/tools.h
 create mode 100644 src/media-ctl/v4l2subdev.h

diff --git a/Makefile.am b/Makefile.am
index 98cd683..301c444 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -328,6 +328,23 @@ drm_backend_la_SOURCES += src/vaapi-recorder.c src/vaapi-recorder.h
 drm_backend_la_LIBADD += $(LIBVA_LIBS)
 drm_backend_la_CFLAGS += $(LIBVA_CFLAGS)
 endif
+
+if ENABLE_GST_RECORDER
+drm_backend_la_SOURCES +=			\
+	src/gst-recorder.c 			\
+	src/gst-recorder.h			\
+	src/v4l2-device.h			\
+	src/media-ctl/libmediactl.c		\
+	src/media-ctl/libv4l2subdev.c		\
+	src/media-ctl/mediactl-priv.h		\
+	src/media-ctl/mediactl.h		\
+	src/media-ctl/tools.h			\
+	src/media-ctl/v4l2subdev.h
+drm_backend_la_LIBADD += $(LIBVA_LIBS) 		\
+	-lgstallocators-1.0			\
+	-lgstvideo-1.0
+drm_backend_la_CFLAGS += $(LIBVA_CFLAGS)
+endif
 endif
 
 if ENABLE_WAYLAND_COMPOSITOR
diff --git a/configure.ac b/configure.ac
index 1d11864..c0717b3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -249,6 +249,17 @@ if test "x$enable_rpi_compositor" = "xyes"; then
 fi
 AM_CONDITIONAL(INSTALL_RPI_COMPOSITOR, test "x$have_bcm_host" = "xyes")
 
+AC_ARG_ENABLE(gst-recorder, [  --enable-gst-recorder],,
+	      enable_gst_recorder=yes)
+if test x$enable_gst_recorder != xno; then
+  AC_DEFINE([BUILD_GST_RECORDER], [1], [Build the gst recorder])
+  PKG_CHECK_MODULES(GSTREAMER, [gstreamer-1.0 >= 1.0])
+
+  CPPFLAGS="$CPPFLAGS $GSTREAMER_CFLAGS"
+  LIBS="$LIBS $GSTREAMER_LIBS -lgstapp-1.0"
+fi
+AM_CONDITIONAL(ENABLE_GST_RECORDER, test "x$enable_gst_recorder" != xno)
+
 
 AC_ARG_ENABLE([fbdev-compositor], [  --enable-fbdev-compositor],,
               enable_fbdev_compositor=yes)
@@ -734,4 +745,5 @@ AC_MSG_RESULT([
 	libwebp Support			${have_webp}
 	libunwind Support		${have_libunwind}
 	VA H.264 encoding Support	${have_libva}
+	GStreamer H.264 enc. Support	${enable_gst_recorder}
 ])
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index fc5a2ff..8b65637 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -57,6 +57,7 @@
 #include "v4l2-renderer.h"
 #include "launcher-util.h"
 #include "vaapi-recorder.h"
+#include "gst-recorder.h"
 #include "presentation-time-server-protocol.h"
 #include "linux-dmabuf.h"
 
@@ -117,6 +118,8 @@ struct drm_backend {
 
 	int use_v4l2;
 
+	int enable_recorder;
+
 	uint32_t prev_state;
 
 	struct udev_input input;
@@ -197,7 +200,12 @@ struct drm_output {
 	int current_image;
 	pixman_region32_t previous_damage;
 
+#ifdef BUILD_VAAPI_RECORDER
 	struct vaapi_recorder *recorder;
+#endif
+#ifdef BUILD_GST_RECORDER
+	struct gst_recorder *recorder;
+#endif
 	struct wl_listener recorder_frame_listener;
 
 	/* not real output device */
@@ -2350,6 +2358,23 @@ parse_modeline(const char *s, drmModeModeInfo *mode)
 	return 0;
 }
 
+static int parse_crop_rect(const char *s, struct v4l2_rect* crop)
+{
+	crop->left = 0;
+	crop->top = 0;
+	crop->width = 0;
+	crop->height = 0;
+
+	if (sscanf(s, "%dx%d@%dx%d",
+		   &crop->width,
+		   &crop->height,
+		   &crop->top,
+		   &crop->left) != 4)
+		return -1;
+
+	return 0;
+}
+
 static void
 setup_output_seat_constraint(struct drm_backend *b,
 			     struct weston_output *output,
@@ -3478,7 +3503,171 @@ recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
 		recorder_destroy(output);
 	}
 }
-#else
+#endif
+
+#ifdef BUILD_GST_RECORDER
+static void
+recorder_destroy(struct drm_backend *c, struct drm_output *output)
+{
+	wl_list_remove(&output->recorder_frame_listener.link);
+
+	gst_recorder_destroy(output->recorder);
+	output->recorder = NULL;
+
+	output->base.disable_planes--;
+
+	weston_log("[gst recorder] done\n");
+}
+
+static void
+recorder_frame_notify(struct wl_listener *listener, void *data)
+{
+	int ret = 0;
+	struct drm_output *output;
+	struct drm_backend *c;
+	int fd;
+
+	output = container_of(listener, struct drm_output,
+			      recorder_frame_listener);
+	c = (struct drm_backend *) output->base.compositor->backend;
+
+	if (!output->recorder) {
+		weston_log("%s: output have no recorder enabled\n",
+			output->base.name);
+		return;
+	}
+
+	if (!output->current) {
+		weston_log("%s: frame notify while current frame == NULL\n",
+			   output->base.name);
+		return;
+	}
+
+	ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle, DRM_CLOEXEC, &fd);
+	if (!ret) {
+		ret = gst_recorder_frame_dmafd(output->recorder, fd,
+					       output->current->stride);
+	}
+
+	if (ret < 0) {
+		weston_log("[gst recorder] aborted: %m\n");
+		recorder_destroy(c, output);
+	}
+}
+
+static int
+recorder_enable(struct drm_backend *c, struct drm_output *output)
+{
+	int enable_recorder = 0;
+	struct gst_recorder_settings *settings;
+	struct weston_config_section *section;
+	char* s;
+	struct v4l2_rect crop = { .width = output->base.current_mode->width,
+				  .height = output->base.current_mode->height,
+				  .top = 0,
+				  .left = 0 };
+
+	if (output->recorder)
+		return -1;
+
+	section = weston_config_get_section(c->compositor->config, "output", "name",
+					    output->base.name);
+
+	weston_config_section_get_bool(section, "recorder", &enable_recorder, 0);
+
+	if (!enable_recorder)
+		return 0;
+
+	/* TODO: add support for NV16 or NV12 */
+	if (output->gbm_format != GBM_FORMAT_XRGB8888) {
+		weston_log("[gst recorder] %s: "
+			   "output format not supported\n", output->base.name);
+		return -1;
+	}
+
+	settings = malloc(sizeof(* settings));
+	weston_config_section_get_string(section, "ip", &settings->ip, NULL);
+	if (!settings->ip)
+		goto err;
+	weston_config_section_get_int(section, "port", &settings->port, -1);
+	/* default gives about 16 Mbit/s at 1280x720@60FPS */
+	weston_config_section_get_int(section, "bitrate", &settings->bitrate, 300000);
+
+	settings->width = output->base.current_mode->width;
+	settings->height = output->base.current_mode->height;
+
+	settings->crop = crop;
+
+	weston_config_section_get_string(section, "crop", &s, NULL);
+	if (s) {
+		if (parse_crop_rect(s, &settings->crop)) {
+			weston_log("[gst recorder] %s:"
+				   " failed to parse crop parameter\n",
+				   output->base.name);
+			goto err;
+		}
+	}
+
+
+	output->recorder =
+		gst_recorder_create(settings);
+	if (!output->recorder) {
+		weston_log("[gst recorder] %s:"
+			" failed to create gst recorder\n",
+			output->base.name);
+		goto err;
+	}
+
+	output->base.disable_planes++;
+
+	output->recorder_frame_listener.notify = recorder_frame_notify;
+	wl_signal_add(&output->base.frame_signal,
+		      &output->recorder_frame_listener);
+
+	weston_output_schedule_repaint(&output->base);
+
+	weston_log("[gst recorder] %s:"
+		" recorder initialized\n",
+		output->base.name);
+
+	return 0;
+err:
+	weston_log("[gst recorder] %s:"
+		" invalid settings\n",
+		output->base.name);
+	free(settings);
+	return -1;
+}
+
+static void
+recorders_enable(struct drm_backend *c)
+{
+	struct drm_output *output;
+
+	wl_list_for_each(output, &c->compositor->output_list, base.link) {
+		recorder_enable(c, output);
+	}
+}
+
+static void
+recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
+		 void *data)
+{
+	struct drm_backend *c = data;
+	struct drm_output *output;
+
+	/* fix this */
+	wl_list_for_each(output, &c->compositor->output_list, base.link) {
+		if (!output->recorder)
+			recorder_enable(c, output);
+		else
+			recorder_destroy(c, output);
+	}
+}
+#endif
+
+#if !defined(BUILD_VAAPI_RECORDER) && !defined(BUILD_GST_RECORDER)
+
 static void
 recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
 		 void *data)
@@ -3571,6 +3760,7 @@ drm_backend_create(struct weston_compositor *compositor,
 	b->compositor = compositor;
 	b->use_pixman = config->use_pixman;
 	b->use_v4l2 = config->use_v4l2;
+	b->enable_recorder = config->enable_recorder;
 	b->configure_output = config->configure_output;
 	b->use_current_mode = config->use_current_mode;
 
@@ -3627,6 +3817,10 @@ drm_backend_create(struct weston_compositor *compositor,
 		}
 	}
 
+#ifdef BUILD_GST_RECORDER
+	gst_recorder_init();
+#endif
+
 	b->base.destroy = drm_destroy;
 	b->base.restore = drm_restore;
 
@@ -3688,6 +3882,9 @@ drm_backend_create(struct weston_compositor *compositor,
 					    recorder_binding, b);
 	weston_compositor_add_debug_binding(compositor, KEY_W,
 					    renderer_switch_binding, b);
+	/* enable GST recording-streaming */
+	if (b->enable_recorder)
+		recorders_enable(b);
 
 	if (compositor->renderer->import_dmabuf) {
 		if (linux_dmabuf_setup(compositor) < 0)
diff --git a/src/compositor-drm.h b/src/compositor-drm.h
index d3d15af..65305d9 100644
--- a/src/compositor-drm.h
+++ b/src/compositor-drm.h
@@ -90,6 +90,9 @@ struct weston_drm_backend_config {
 	/** Whether to use the v4l2 renderer insted of the OpenGL ES renderer. */
 	bool use_v4l2;
 
+	/** Whether to use the v4l2 renderer insted of the OpenGL ES renderer. */
+	bool enable_recorder;
+
 	/** The seat to be used for input and output.
 	 *
 	 * If NULL the default "seat0" will be used.  The backend will
diff --git a/src/gst-recorder.c b/src/gst-recorder.c
new file mode 100644
index 0000000..2e3b359
--- /dev/null
+++ b/src/gst-recorder.c
@@ -0,0 +1,1213 @@
+/*
+ * Copyright © 2016 Cogent Embedded Inc
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * TODO:
+ * 1) Add format parameter to virtual display to render in another format
+ * with v4l2-renderer
+ * 2) Add capability to use already NV12 rendered frame
+ */
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+#include <pthread.h>
+
+#include "compositor.h"
+#include "gst-recorder.h"
+
+/* VSP includes */
+#include <media-ctl/mediactl.h>
+#include <media-ctl/v4l2subdev.h>
+
+/* Gstreamer includes */
+#include <gst/gst.h>
+#include <gst/app/gstappsrc.h>
+#include <gst/rtp/gstrtpbuffer.h>
+
+#include <gst/video/video.h>
+#include <gst/video/gstvideometa.h>
+#include <gst/video/gstvideopool.h>
+#include <gst/allocators/gstdmabuf.h>
+
+struct vsp_data;
+
+#define DEFAULT_FPS		60
+
+typedef enum _vsp_port_n {
+	VSP_PORT_INPUT	= 0,
+	VSP_PORT_INPUT0 = VSP_PORT_INPUT,
+	VSP_PORT_INPUT1,
+	VSP_PORT_INPUT2,
+	VSP_PORT_INPUT3,
+	VSP_PORT_OUTPUT
+} vsp_port_n;
+
+struct gst_recorder {
+	struct gst_recorder_settings *set;
+	int frame_count;
+	int input_count;
+
+	int error;
+	int destroying;
+	pthread_t worker_thread;
+	pthread_mutex_t mutex;
+	pthread_cond_t input_cond;
+
+	struct {
+		int valid;
+		int prime_fd, stride;
+	} input;
+
+	/* GLib */
+	GMainContext *gcontext;
+	/* Gstreamer stuff */
+	GstElement *pipeline;
+	/* AppSrc */
+	GstAppSrc *appsrc;
+	/* ...and source pad */
+	GstPad *appsrc_pad;
+	/* OMX encoder buffer pool */
+	GstBufferPool *omx_pool;
+	/* bus */
+	GstBus *bus;
+	/* timestamp */
+	GstClockTime timestamp;
+	uint32_t ts_last_frame;
+	/* to be removed */
+	guint callback_tag;
+
+	struct vsp_data *vsp;
+};
+
+/*******************************************************************************
+ * VSP related code
+ ******************************************************************************/
+
+#define VSP_OUTPUT_BUFFERS_PLANE	2
+
+/* #define VSP_OUTPUT_NV16			1 */
+
+/* ...number of input/output pads (WPF1-3 are not implemented) */
+#define VSP_PADS_NUM                    1
+
+/*******************************************************************************
+ * Local types definition
+ ******************************************************************************/
+
+struct vsp_media_pad
+{
+	struct media_pad	   *infmt_pad;
+	struct media_pad	   *outfmt_pad;
+	struct media_entity	   *entity;
+	int                     fd;
+};
+
+typedef struct vsp_media_pad vsp_media_pad_t;
+
+struct vsp_data
+{
+	/* ...media device */
+	struct media_device *media;
+
+	/* ...VSP input/output pads */
+	vsp_media_pad_t input, output;
+
+	/* mutex */
+	pthread_mutex_t mutex;
+
+	/* user count */
+	int users;
+};
+
+struct vsp_data *vsp_g = NULL;
+
+/* ...type declarations */
+typedef struct vsp_data vsp_data_t;
+
+static int
+gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride);
+
+/* ...module initialization (a bit of salami) */
+static vsp_data_t *
+vsp_init(const char *devname)
+{
+	vsp_data_t                     *vsp;
+	struct media_device            *media;
+	const struct media_device_info *info;
+	const char                     *dev;
+	char                            buf[256];
+	char                           *endp, *p;
+	struct media_entity            *entity;
+
+	/* ...create data structure */
+	if ((vsp = malloc(sizeof(*vsp))) == NULL)
+	{
+		weston_log("failed to allocate memory\n");
+		errno = ENOMEM;
+		return NULL;
+	}
+	memset(vsp, 0, sizeof(*vsp));
+
+	pthread_mutex_init(&vsp->mutex, NULL);
+
+	/* ...create media device */
+	if ((vsp->media = media = media_device_new(devname)) == NULL)
+	{
+		weston_log("failed to open device '%s'\n", devname);
+		goto error;
+	}
+	else if ((errno = -media_device_enumerate(media)) != 0)
+	{
+		weston_log("failed to enumerate device '%s'\n", devname);
+		goto error_media;
+	}
+	else if ((info = media_get_info(media)) == NULL)
+	{
+		weston_log("failed to get media info data\n");
+		goto error_media;
+	}
+	else
+	{
+		dev = ((p = strchr(info->bus_info, ':')) ? p + 1 : info->bus_info);
+		weston_log("open media device: %s (%s)\n", info->bus_info, dev);
+	}
+
+	/* ...reset links */
+	if (media_reset_links(media) != 0)
+	{
+		weston_log("failed to reset media device\n");
+		goto error_media;
+	}
+
+	/* ...setup RPF.0:1 -> WPF.0:0 link */
+	snprintf(buf, sizeof(buf), "'%s rpf.0':1 -> '%s wpf.0':0 [1]", dev, dev);
+	if (media_parse_setup_link(media, buf, &endp) != 0)
+	{
+		weston_log("failed to setup link '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	/* ...setup WPF.0:1 -> WPF.0 output link */
+	snprintf(buf, sizeof(buf), "'%s wpf.0':1 -> '%s wpf.0 output':0 [1]", dev, dev);
+	if (media_parse_setup_link(media, buf, &endp) != 0)
+	{
+		weston_log("failed to setup link '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	/* ...specify input/output-format of RPF pad */
+	snprintf(buf, sizeof(buf), "'%s rpf.0':0", dev);
+	if ((vsp->input.infmt_pad = media_parse_pad(media, buf, NULL)) == NULL)
+	{
+		weston_log("failed to parse pad '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	snprintf(buf, sizeof(buf), "'%s rpf.0':1", dev);
+	if ((vsp->input.outfmt_pad = media_parse_pad(media, buf, NULL)) == NULL)
+	{
+		weston_log("failed to parse pad '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	snprintf(buf, sizeof(buf), "%s rpf.0", dev);
+	if ((vsp->input.entity = media_get_entity_by_name(media, buf, strlen(buf))) == NULL)
+	{
+		weston_log("failed to parse entity '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	/* ...get input file-descriptor */
+	snprintf(buf, sizeof(buf), "%s rpf.0 input", dev);
+	if ((entity = media_get_entity_by_name(media, buf, strlen(buf))) == NULL)
+	{
+		weston_log("entity '%s' not found\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+	else if (v4l2_subdev_open(entity) != 0)
+	{
+		weston_log("failed to open subdev '%s'\n", buf);
+		goto error_media;
+	}
+	else if ((vsp->input.fd = open(media_entity_get_devname(entity), O_RDWR/* | O_NONBLOCK*/)) < 0)
+	{
+		weston_log("failed to open device '%s'\n", media_entity_get_devname(entity));
+		goto error_media;
+	}
+	else
+	{
+		weston_log("input pad setup ('%s':'%s')\n", buf, media_entity_get_devname(entity));
+	}
+
+	/* ...specify input/output formats of WPF pad */
+	snprintf(buf, sizeof(buf), "'%s wpf.0':0", dev);
+	if ((vsp->output.infmt_pad = media_parse_pad(media, buf, NULL)) == NULL)
+	{
+		weston_log("failed to parse pad '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	snprintf(buf, sizeof(buf), "'%s wpf.0':1", dev);
+	if ((vsp->output.outfmt_pad = media_parse_pad(media, buf, NULL)) == NULL)
+	{
+		weston_log("failed to parse pad '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	snprintf(buf, sizeof(buf), "%s wpf.0", dev);
+	if ((vsp->output.entity = media_get_entity_by_name(media, buf, strlen(buf))) == NULL)
+	{
+		weston_log("failed to parse entity '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+
+	/* ...get a file descriptor for the output */
+	snprintf(buf, sizeof(buf), "%s wpf.0 output", dev);
+	if ((entity = media_get_entity_by_name(media, buf, strlen(buf))) == NULL)
+	{
+		weston_log("failed to get entity '%s'\n", buf);
+		errno = EINVAL;
+		goto error_media;
+	}
+	else if (v4l2_subdev_open(entity) != 0)
+	{
+		weston_log("failed to open subdev '%s'\n", buf);
+		goto error_media;
+	}
+	else if ((vsp->output.fd = open(media_entity_get_devname(entity), O_RDWR | O_NONBLOCK)) < 0)
+	{
+		weston_log("failed to open device '%s'\n", media_entity_get_devname(entity));
+		goto error_media;
+	}
+	else
+	{
+		weston_log("output pad setup (%s:%s)\n", buf, media_entity_get_devname(entity));
+	}
+
+	weston_log("vsp-device '%s' created\n", devname);
+
+	return vsp;
+
+error_media:
+	/* ...destroy media device and all associated structures */
+	media_device_unref(vsp->media);
+
+error:
+	/* ...destroy data structure */
+	free(vsp);
+	return NULL;
+}
+
+static void
+vsp_deinit(vsp_data_t *vsp)
+{
+	/* ...destroy media device and all associated structures */
+	media_device_unref(vsp->media);
+
+	/* ...destroy data structure */
+	free(vsp);
+}
+
+/* ...set V4L2 device format */
+static int
+vsp_set_format(int fd, struct v4l2_format *fmt)
+{
+	/* ...set format */
+	if (ioctl(fd, VIDIOC_S_FMT, fmt) < 0) {
+		weston_log("format set (fd=%d) failed: %d\n",
+			fd, errno);
+	}
+
+	return 0;
+}
+
+/* ...start streaming on specific V4L2 device */
+static int
+vsp_streaming_enable(vsp_data_t *vsp, vsp_port_n port, int enable)
+{
+	vsp_media_pad_t *pad = (port == VSP_PORT_INPUT) ? &vsp->input : &vsp->output;
+	int fd = pad->fd;
+	int type = (port == VSP_PORT_INPUT) ?
+		V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+
+	return ioctl(fd, (enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF), &type);
+}
+
+/* ...prepare VSP filter for operation */
+static int
+vsp_set_formats(vsp_data_t *vsp, int width, int height, struct v4l2_rect* crop)
+{
+	vsp_media_pad_t *input = &vsp->input, *output = &vsp->output;
+	struct v4l2_mbus_framefmt mfmt = { .width = width, .height = height };
+	struct v4l2_format format;
+
+	/* ...configure RPF input pads; specify pixel format and size (one of YUV variants) */
+	mfmt.width = width;
+	mfmt.height = height;
+	mfmt.code = V4L2_MBUS_FMT_ARGB8888_1X32;
+	if (v4l2_subdev_set_format(input->infmt_pad->entity,
+		&mfmt, input->infmt_pad->index, V4L2_SUBDEV_FORMAT_ACTIVE) < 0) {
+		weston_log("VSP: input pad in format set failed: %d\n", errno);
+		return -1;
+	}
+
+	 /* set a crop paramters */
+	if (v4l2_subdev_set_selection(input->infmt_pad->entity, crop, input->infmt_pad->index,
+				      V4L2_SEL_TGT_CROP, V4L2_SUBDEV_FORMAT_ACTIVE)) {
+		weston_log("set crop parameter failed: %dx%d@(%d,%d).\n",
+			   crop->width, crop->height, crop->left, crop->top);
+		return -1;
+	}
+
+	/* ...output is NV12 or NV16 or I420*/
+	mfmt.width = crop->width;
+	mfmt.height = crop->height;
+	mfmt.code = V4L2_MBUS_FMT_AYUV8_1X32;
+	if (v4l2_subdev_set_format(input->outfmt_pad->entity,
+		&mfmt, input->outfmt_pad->index, V4L2_SUBDEV_FORMAT_ACTIVE) < 0) {
+		weston_log("VSP: input pad out format set failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...specify input format */
+	memset(&format, 0, sizeof(format));
+	format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	format.fmt.pix_mp.width = /* crop-> */width;
+	format.fmt.pix_mp.height = /* crop-> */height;
+	format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_ABGR32;
+	format.fmt.pix_mp.num_planes = 1;
+	/* ...set input port format */
+	if (vsp_set_format(input->fd, &format) < 0) {
+		weston_log("VSP: input set format failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...both input and output are ARGB8888 always (now effective) */
+	if (v4l2_subdev_set_format(output->infmt_pad->entity,
+		&mfmt, output->infmt_pad->index, V4L2_SUBDEV_FORMAT_ACTIVE) < 0) {
+		weston_log("VSP: output pad in format set failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...specify cropping area, probably? - tbd */
+	if (v4l2_subdev_set_format(output->outfmt_pad->entity,
+		&mfmt, output->outfmt_pad->index, V4L2_SUBDEV_FORMAT_ACTIVE) < 0) {
+		weston_log("VSP: output pad in format set failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...setup output pads */
+	memset(&format, 0, sizeof(format));
+	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	format.fmt.pix_mp.width = crop->width;
+	format.fmt.pix_mp.height = crop->height;
+#ifdef VSP_OUTPUT_NV16
+	format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV16M;
+#else
+	format.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
+#endif
+	format.fmt.pix_mp.num_planes = VSP_OUTPUT_BUFFERS_PLANE;
+	/* ...set output buffer format */
+	if (vsp_set_format(output->fd, &format) < 0) {
+		weston_log("VSP: output set format failed: %d\n", errno);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int
+vsp_request_buffers(vsp_data_t *vsp, vsp_port_n port, unsigned int num)
+{
+	vsp_media_pad_t *pad = (port == VSP_PORT_INPUT) ? &vsp->input : &vsp->output;
+	struct v4l2_requestbuffers reqbuf;
+	int fd = pad->fd;
+
+	/* ...input buffers are DMA-fd, output buffers allocated by kernel */
+	memset(&reqbuf, 0, sizeof(reqbuf));
+	reqbuf.type = (port == VSP_PORT_INPUT) ?
+		V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	reqbuf.memory = V4L2_MEMORY_DMABUF;
+	reqbuf.count = num;
+	if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
+		weston_log("VSP: %s REQBUFS failed: %d\n",
+			(port == VSP_PORT_INPUT) ? "input" : "output", errno);
+		return -1;
+	}
+
+	if (reqbuf.count != num) {
+		weston_log("VSP: %s failed to request %d (!= %d) bufs\n",
+			(port == VSP_PORT_INPUT) ? "input" : "output", num, reqbuf.count);
+		return -1;
+	}
+	return 0;
+}
+
+
+/* ...enqueue dmafd buffer */
+static int
+vsp_input_buffer_queue_dmafd(vsp_data_t *vsp, int i, int dmafd)
+{
+	vsp_media_pad_t    *pad = &vsp->input;
+	struct v4l2_buffer  buf;
+	struct v4l2_plane   planes[1];
+
+	/* ...set buffer parameters */
+	memset(&buf, 0, sizeof(buf));
+	memset(planes, 0, sizeof(planes));
+	buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.index = i;
+	buf.m.planes = planes;
+	buf.length = 1;
+	buf.m.planes[0].m.fd = dmafd;
+
+	/* ...submit buffer */
+	if (ioctl(pad->fd, VIDIOC_QBUF, &buf) < 0) {
+		weston_log("VSP: input dmafd (%d) buffer (%i) queue failed: %d\n",
+			dmafd, i, errno);
+		return -1;
+	}
+
+	return 0;
+}
+
+/* ...dequeue dmafd buffer */
+static int
+vsp_input_buffer_dequeue_dmafd(vsp_data_t *vsp)
+{
+	vsp_media_pad_t    *pad = &vsp->input;
+	struct v4l2_buffer  buf;
+	struct v4l2_plane   planes[1];
+
+	/* ...set buffer parameters */
+	memset(&buf, 0, sizeof(buf));
+	memset(planes, 0, sizeof(planes));
+	buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.m.planes = planes;
+	buf.length = 1;
+
+	if (ioctl(pad->fd, VIDIOC_DQBUF, &buf) < 0) {
+		weston_log("VSP: input dmafd buffer de-queue failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...return buffer index */
+	return buf.index;
+}
+
+/* ...enqueue output buffer */
+static int
+vsp_output_buffer_queue_dmafd(vsp_data_t *vsp, int i, int dmafd[])
+{
+	vsp_media_pad_t    *pad = &vsp->output;
+	struct v4l2_plane   planes[2];
+	struct v4l2_buffer  buf;
+
+	/* ...set buffer parameters (single-plane ARGB always) */
+	memset(&buf, 0, sizeof(buf));
+	memset(planes, 0, sizeof(planes));
+	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.index = i;
+	buf.m.planes = planes;
+	buf.length = VSP_OUTPUT_BUFFERS_PLANE;
+	buf.m.planes[0].m.fd = dmafd[0];
+	buf.m.planes[1].m.fd = dmafd[1];
+
+	/* ...submit buffer */
+	if (ioctl(pad->fd, VIDIOC_QBUF, &buf) < 0) {
+		weston_log("VSP: output dmafd queue failed: %d\n", errno);
+		return -1;
+	}
+
+	return 0;
+}
+
+/* ...dequeue output buffer */
+static int
+vsp_output_buffer_dequeue_dmafd(vsp_data_t *vsp)
+{
+	vsp_media_pad_t    *pad = &vsp->output;
+	struct v4l2_buffer  buf;
+	struct v4l2_plane   planes[2];
+
+	/* ...set buffer parameters */
+	memset(&buf, 0, sizeof(buf));
+	memset(planes, 0, sizeof(planes));
+	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	buf.memory = V4L2_MEMORY_DMABUF;
+	buf.m.planes = planes;
+	buf.length = VSP_OUTPUT_BUFFERS_PLANE;
+
+	if (ioctl(pad->fd, VIDIOC_DQBUF, &buf) < 0) {
+		weston_log("VSP: output dmafd de-queue failed: %d\n", errno);
+		return -1;
+	}
+
+	/* ...return dequeue buffer index */
+	return buf.index;
+}
+
+/* ...get capturing interface file descriptor */
+static int
+vsp_capture_fd(vsp_data_t *vsp)
+{
+	return vsp->output.fd;
+}
+
+/*******************************************************************************
+ * Gstreamer stuff
+ ******************************************************************************/
+
+static void
+print_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
+{
+	int i, num;
+
+	num = gst_tag_list_get_tag_size (list, tag);
+	for (i = 0; i < num; ++i) {
+		const GValue *val;
+
+		/* Note: when looking for specific tags, use the gst_tag_list_get_xyz() API,
+		 * we only use the GValue approach here because it is more generic */
+		val = gst_tag_list_get_value_index (list, tag, i);
+		if (G_VALUE_HOLDS_STRING (val)) {
+			weston_log("\t%20s : %s\n", tag, g_value_get_string (val));
+		} else if (G_VALUE_HOLDS_UINT (val)) {
+			weston_log("\t%20s : %u\n", tag, g_value_get_uint (val));
+		} else if (G_VALUE_HOLDS_DOUBLE (val)) {
+			weston_log("\t%20s : %g\n", tag, g_value_get_double (val));
+		} else if (G_VALUE_HOLDS_BOOLEAN (val)) {
+			weston_log("\t%20s : %s\n", tag,
+					(g_value_get_boolean (val)) ? "true" : "false");
+		} else if (GST_VALUE_HOLDS_BUFFER (val)) {
+			GstBuffer *buf = gst_value_get_buffer (val);
+			guint buffer_size = gst_buffer_get_size (buf);
+
+			weston_log("\t%20s : buffer of size %u\n", tag, buffer_size);
+		} else if (GST_VALUE_HOLDS_DATE_TIME (val)) {
+			GstDateTime *dt = g_value_get_boxed (val);
+			gchar *dt_str = gst_date_time_to_iso8601_string (dt);
+
+			weston_log("\t%20s : %s\n", tag, dt_str);
+			g_free (dt_str);
+		} else {
+			weston_log("\t%20s : tag of type '%s'\n", tag, G_VALUE_TYPE_NAME (val));
+		}
+	}
+}
+
+static gboolean
+gst_bus_callback(GstBus *bus, GstMessage *message, gpointer user_data)
+{
+	GTimeVal time;
+	struct gst_recorder *r = user_data;
+
+	if (!r->pipeline) {
+		weston_log("gst_pipeline: unexpected gst bus callback event, while pipeline==null\n");
+		return TRUE;
+	}
+
+	g_get_current_time(&time);
+
+	switch (GST_MESSAGE_TYPE(message))
+	{
+		case GST_MESSAGE_QOS:
+			{
+				GstFormat format;
+				guint64 processed;
+				guint64 dropped;
+
+				gst_message_parse_qos_stats (message, &format, &processed, &dropped);
+				weston_log("gst_pipeline: qos from: %s processed %lld, dropped %lld\n",
+					GST_OBJECT_NAME (message->src),
+					processed, dropped);
+			}
+		break;
+		case GST_MESSAGE_STREAM_STATUS:
+			{
+				const GValue *val;
+
+				val = gst_message_get_stream_status_object (message);
+				weston_log("gst_pipeline: stream status type %s, value %p\n",
+					G_VALUE_TYPE_NAME(val),
+					g_value_get_object(val));
+			}
+		break;
+		case GST_MESSAGE_TAG:
+			{
+				GstTagList *tags = NULL;
+
+				gst_message_parse_tag (message, &tags);
+				weston_log("gst_pipeline: tag from element %s:\n",
+					GST_OBJECT_NAME (message->src));
+				gst_tag_list_foreach (tags, print_one_tag, NULL);
+				weston_log("\n");
+			}
+		break;
+		case GST_MESSAGE_STATE_CHANGED:
+			{
+				GstState oldstate, newstate;
+
+				gst_message_parse_state_changed(message, &oldstate, &newstate, NULL);
+				weston_log("gst_pipeline: element %s changed state from %s to %s.\n",
+					GST_OBJECT_NAME (message->src),
+					gst_element_state_get_name (oldstate),
+					gst_element_state_get_name (newstate));
+
+				/* if gstreamer become ready */
+				if ((GST_MESSAGE_SRC(message) == GST_OBJECT(r->appsrc)) &&
+					(newstate == GST_STATE_PAUSED)) {
+					weston_log("gst_pipeline: pipeline ready\n");
+				}
+			}
+		break;
+		case GST_MESSAGE_ERROR:
+			{
+				GError *err;
+				gchar *debug_info;
+				gst_message_parse_error(message, &err, &debug_info);
+				weston_log("gst_pipeline: error received from element %s: %s\n",
+					GST_OBJECT_NAME(message->src), err->message);
+				weston_log("gst_pipeline: debugging information: %s\n",
+					debug_info ? debug_info : "none");
+				g_clear_error (&err);
+				g_free (debug_info);
+			}
+		break;
+		case GST_MESSAGE_WARNING:
+			{
+				GError *err;
+				gchar *debug_info;
+				gst_message_parse_warning(message, &err, &debug_info);
+				weston_log("gst_pipeline: warning received from element %s: %s\n",
+					GST_OBJECT_NAME(message->src), err->message);
+				weston_log("gst_pipeline: debugging information: %s\n",
+					debug_info ? debug_info : "none");
+				g_clear_error (&err);
+				g_free (debug_info);
+			}
+		break;
+		default:
+			weston_log("gst_pipeline: %s from %s\n",
+				GST_MESSAGE_TYPE_NAME(message), GST_OBJECT_NAME (message->src));
+		break;
+	}
+	return TRUE;
+}
+
+static void *
+worker_thread_function(void *data)
+{
+	GstMessage *msg;
+	struct gst_recorder *r = data;
+
+	pthread_mutex_lock(&r->mutex);
+
+	while (!r->destroying) {
+		if (!r->input.valid)
+			pthread_cond_wait(&r->input_cond, &r->mutex);
+
+		/* If the thread is awaken by destroy_worker_thread(),
+		 * there might not be valid input */
+		if (!r->input.valid)
+			continue;
+
+		/* TODO: move it to separate thread? */
+		g_main_context_iteration(r->gcontext, FALSE);
+
+		do {
+			msg = gst_bus_pop_filtered(r->bus,
+						   GST_MESSAGE_ANY);
+			if (msg) {
+				gst_bus_callback(r->bus, msg, r);
+			}
+		} while (msg);
+
+		/* check input */
+		gst_recorder_process_dmafd(r, r->input.prime_fd, r->input.stride);
+
+		r->input.valid = 0;
+	}
+
+	pthread_mutex_unlock(&r->mutex);
+
+	return NULL;
+}
+
+static int
+setup_worker_thread(struct gst_recorder *r)
+{
+	r->gcontext = g_main_context_new();
+	pthread_mutex_init(&r->mutex, NULL);
+	pthread_cond_init(&r->input_cond, NULL);
+	pthread_create(&r->worker_thread, NULL, worker_thread_function, r);
+
+	return 1;
+}
+
+static void
+destroy_worker_thread(struct gst_recorder *r)
+{
+	/* Make sure the worker thread finishes */
+	r->destroying = 1;
+
+	pthread_cond_signal(&r->input_cond);
+
+	pthread_join(r->worker_thread, NULL);
+
+	pthread_mutex_destroy(&r->mutex);
+	pthread_cond_destroy(&r->input_cond);
+}
+
+void weston_debug_function(GstDebugCategory* category, GstDebugLevel level,
+                          const gchar* file, const char* function,
+                          gint line, GObject* object, GstDebugMessage* message,
+                          gpointer data)
+{
+	weston_log("[GST]:%s %s:%d %s\n", file, function, line, gst_debug_message_get(message));
+}
+
+void
+gst_recorder_init(void)
+{
+	gst_init(NULL, 0);
+
+	/* VSP init */
+	vsp_g = vsp_init("/dev/media0");
+	if (!vsp_g)
+		weston_log("[gst recorder] VSP init failed\n");
+}
+
+static int
+gst_recorder_find_omx_pool(struct gst_recorder *r)
+{
+	int ret = 0;
+	GstCaps *caps;
+	GstQuery *query;
+	GstBufferPool *pool;
+	GstStructure *config;
+	guint size, min, max;
+
+	caps = gst_caps_new_simple (	"video/x-raw",
+#ifdef VSP_OUTPUT_NV16
+					"format", G_TYPE_STRING, "NV16",
+#else
+					"format", G_TYPE_STRING, "NV12",
+#endif
+					"width", G_TYPE_INT, r->set->crop.width,
+					"height", G_TYPE_INT, r->set->crop.height,
+					"framerate", GST_TYPE_FRACTION, 0, DEFAULT_FPS,
+					NULL);
+
+	/* find a pool for the negotiated caps now */
+	query = gst_query_new_allocation (caps, TRUE);
+
+	if (!gst_pad_peer_query (r->appsrc_pad, query)) {
+		/* query failed, not a problem, we use the query defaults */
+		weston_log("allocation query failed\n");
+		ret = -1;
+		goto err;
+	}
+
+	weston_log("goot %d pools\n", gst_query_get_n_allocation_pools (query));
+	if (gst_query_get_n_allocation_pools (query) > 0) {
+		/* we got configuration from our peer, parse them */
+		gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
+		weston_log(" pool settings size %d, min %d, max %d\n", size, min, max);
+	} else {
+		weston_log("no pool queried\n");
+		ret = -1;
+		goto err;
+	}
+
+	config = gst_buffer_pool_get_config (pool);
+	gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
+	gst_buffer_pool_config_set_params (config, caps, size, min, max);
+	gst_buffer_pool_set_config (pool, config);
+
+	/* and activate */
+	gst_buffer_pool_set_active (pool, TRUE);
+
+	r->omx_pool = pool;
+
+err:
+	gst_query_unref (query);
+	return ret;
+}
+
+static int
+gst_recorder_omx_buffer_acquire(struct gst_recorder *r, GstBuffer **ret_buf, int fd[])
+{
+	unsigned int i;
+	GstFlowReturn ret;
+	GstBuffer *buf;
+	guint n_mem;
+	GstMemory *mem;
+
+	ret = gst_buffer_pool_acquire_buffer(r->omx_pool, &buf, NULL);
+	if (ret != GST_FLOW_OK) {
+		weston_log("OMX buffer acquire failed\n");
+		return -1;
+	}
+
+	n_mem = gst_buffer_n_memory(buf);
+	if (n_mem < 1) {
+		weston_log("Buffer with no mem!\n");
+		goto err_release;
+	}
+
+	for (i = 0; i < n_mem; i++) {
+		mem = gst_buffer_peek_memory (buf, i);
+		if (!gst_is_dmabuf_memory (mem)) {
+			weston_log("Mem not dmabuf\n");
+			goto err_release;
+		}
+		fd[i] = gst_dmabuf_memory_get_fd (mem);
+	}
+
+	*ret_buf = buf;
+
+	return 0;
+err_release:
+	gst_buffer_pool_release_buffer(r->omx_pool, buf);
+	return -1;
+}
+
+static int
+gst_recorder_omx_buffer_release(struct gst_recorder *r, GstBuffer *buf)
+{
+	gst_buffer_pool_release_buffer(r->omx_pool, buf);
+
+	return 0;
+}
+
+struct gst_recorder *
+gst_recorder_create(struct gst_recorder_settings *settings)
+{
+	struct gst_recorder *r;
+	char gst_pipe[1024];
+	char *ptr = gst_pipe;
+	GError *perror = NULL;
+
+	weston_log("gst_recorder_create (%dx%d) crop %dx%d at %d,%d\n",
+		   settings->width, settings->height, settings->crop.width,
+		   settings->crop.height, settings->crop.top, settings->crop.left);
+
+	if (!vsp_g) {
+		weston_log("gst_recorder_create: no VSP\n");
+		return NULL;
+	}
+
+	r = calloc(1, sizeof *r);
+	if (!r)
+		return NULL;
+	memset(r, 0, sizeof *r);
+
+	r->set = settings;
+	r->timestamp = 0;
+
+	r->vsp = vsp_g;
+	vsp_g->users++;
+
+	/* GST init */
+	/* source (GST_FORMAT_BYTES) */
+	ptr += sprintf(ptr,
+		"appsrc name=src ! ");
+
+	/* omx */
+	ptr += sprintf(ptr,
+		"omxh264enc target-bitrate=%d control-rate=2 name=my_encoder ! "
+		"video/x-h264,width=%d,height=%d ! ",
+		r->set->bitrate, r->set->crop.width, r->set->crop.height);
+
+	/* rtp payloader */
+	ptr += sprintf(ptr,
+		"rtph264pay config-interval=1 name=my_h264pay ! queue  ! ");
+
+	/* usp sink */
+	ptr += sprintf(ptr,
+		"udpsink host=%s ", r->set->ip);
+	if (r->set->port > 0)
+		ptr += sprintf(ptr,
+			" port=%d name=my_udpsink", r->set->port);
+
+	weston_log("gst_pipeline: starting: %s\n", gst_pipe);
+
+	/* launch */
+	r->pipeline = gst_parse_launch (gst_pipe, &perror);
+	if (!r->pipeline) {
+		weston_log("gst_pipeline: can not start pipeline: %s\n", perror->message);
+		goto err_gst;
+	}
+
+	/* get appsrc */
+	r->appsrc = gst_bin_get_by_name(GST_BIN (r->pipeline), "src");
+	if (!r->appsrc) {
+		weston_log("gst_pipeline: can not get appsrc\n");
+		goto err_gst;
+	}
+
+	/* get bus */
+	r->bus = gst_pipeline_get_bus (GST_PIPELINE(r->pipeline));
+	if (!r->bus) {
+		weston_log("gst_pipeline: can not get bus\n");
+		goto err_gst;
+	}
+
+	setup_worker_thread(r);
+
+	/* setup caps */
+	g_object_set(G_OBJECT(r->appsrc), "caps",
+		gst_caps_new_simple (	"video/x-raw",
+#ifdef VSP_OUTPUT_NV16
+					"format", G_TYPE_STRING, "NV16",
+#else
+					"format", G_TYPE_STRING, "NV12",
+#endif
+					"width", G_TYPE_INT, r->set->crop.width,
+					"height", G_TYPE_INT, r->set->crop.height,
+					"framerate", GST_TYPE_FRACTION, 0, DEFAULT_FPS,
+					NULL), NULL);
+
+	r->appsrc_pad = gst_element_get_static_pad(GST_ELEMENT_CAST(r->appsrc), "src");
+	if (!r->appsrc_pad)
+		weston_log("Failed to get src0 pad of appsrc\n");
+
+	/* set playing */
+	if (gst_element_set_state (r->pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
+		weston_log("gst_pipeline: can not change state to PLAYING\n");
+		goto err_gst;
+	}
+
+	if (gst_recorder_find_omx_pool(r) != 0) {
+		weston_log("failed to find OMX buffer pool\n");
+		goto err_gst_stop;
+	}
+
+	weston_log("gst_recorder_create done\n");
+
+	return r;
+
+err_gst_stop:
+	gst_element_set_state (r->pipeline, GST_STATE_NULL);
+	destroy_worker_thread(r);
+err_gst:
+	free(r->pipeline);
+	free(r);
+
+	return NULL;
+}
+
+void
+gst_recorder_destroy(struct gst_recorder *r)
+{
+	r->vsp->users--;
+
+	if (r->pipeline) {
+		gst_element_set_state (r->pipeline, GST_STATE_NULL);
+		gst_object_unref(r->omx_pool);
+
+		destroy_worker_thread(r);
+
+		gst_object_unref(GST_OBJECT(r->bus));
+		gst_object_unref (r->pipeline);
+		r->pipeline = NULL;
+	}
+	free(r);
+}
+
+static int
+gst_recorder_set_timestamp(struct gst_recorder *r, GstBuffer *buffer)
+{
+	uint32_t cur_time = weston_compositor_get_time();
+
+	if (r->timestamp == 0) {
+		/* first frame assume around DEFAULT_FPS FPS */
+		GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(1, GST_SECOND, DEFAULT_FPS);
+	} else {
+		uint32_t delta = cur_time - r->ts_last_frame;
+		/* delta in mS */
+		GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(delta, GST_SECOND, 1000);
+	}
+
+	r->timestamp += GST_BUFFER_DURATION(buffer);
+	GST_BUFFER_PTS(buffer) = r->timestamp;
+	GST_BUFFER_DTS(buffer) = r->timestamp;
+
+	r->ts_last_frame = cur_time;
+
+	return 0;
+}
+
+
+static int
+gst_recorder_process_dmafd(struct gst_recorder *r, int fd, int stride)
+{
+	int ret;
+	GstBuffer *buf;
+	int omx_fd[2];
+
+        /* get GST buffer */
+	if (gst_recorder_omx_buffer_acquire(r, &buf, omx_fd) < 0) {
+		weston_log("VSP: can not acquire GST buffer, dropping frame\n");
+		return 0;
+	}
+
+	pthread_mutex_lock(&r->vsp->mutex);
+	/* setup vsp */
+	if (vsp_set_formats(r->vsp, r->set->width, r->set->height, &r->set->crop) < 0) {
+		weston_log("VSP: format set failed\n");
+		goto err;
+	}
+
+	/* input */
+	if (vsp_request_buffers(r->vsp, VSP_PORT_INPUT, 1) < 0) {
+		weston_log("VSP: input buffer allocation failed\n");
+		goto err_vsp;
+	}
+
+	/* output */
+	if (vsp_request_buffers(r->vsp, VSP_PORT_OUTPUT, 1) < 0) {
+		weston_log("VSP: output buffer allocation failed\n");
+		goto err_vsp;
+	}
+
+	/* queue output biffer */
+	if (vsp_output_buffer_queue_dmafd(r->vsp, 0, omx_fd) < 0) {
+		weston_log("can not queue OMX buffer %d to VSP\n", 0);
+		gst_recorder_omx_buffer_release(r, buf);
+		goto err_vsp;
+	}
+
+	/* queue input vsp buffer */
+	if (vsp_input_buffer_queue_dmafd(r->vsp, 0, fd) < 0) {
+		weston_log("VSP: failed to queue input buffer\n");
+		goto err_vsp;
+	}
+
+	/* start input */
+	if (vsp_streaming_enable(r->vsp, VSP_PORT_INPUT, 1) < 0) {
+		weston_log("VSP: failed to start input\n");
+		goto err_vsp;
+	}
+
+	/* start output */
+	if (vsp_streaming_enable(r->vsp, VSP_PORT_OUTPUT, 1) < 0) {
+		weston_log("VSP: failed to start output\n");
+		goto err_vsp;
+	}
+
+	/* dequeue input (do we need this?) */
+	if (vsp_input_buffer_dequeue_dmafd(r->vsp) < 0) {
+		weston_log("VSP: failed to dequeue input buffer\n");
+		/* don't care */
+	}
+
+	/* dequeue output */
+	if (vsp_output_buffer_dequeue_dmafd(r->vsp) < 0) {
+		weston_log("VSP: failed to dequeu output buffer\n");
+		gst_recorder_omx_buffer_release(r, buf);
+		/* fall through */
+	} else {
+		/* set timestamp */
+		gst_recorder_set_timestamp(r, buf);
+
+		ret = gst_app_src_push_buffer(r->appsrc, buf);
+		r->frame_count++;
+
+		if (ret != GST_FLOW_OK) {
+			/* some error, stop sending data */
+			weston_log("gst_pipeline: some error %d\n", ret);
+		}
+
+	}
+	/* stop input */
+	vsp_streaming_enable(r->vsp, VSP_PORT_INPUT, 0);
+	/* stop output */
+	vsp_streaming_enable(r->vsp, VSP_PORT_OUTPUT, 0);
+
+	/* deinit */
+	vsp_request_buffers(r->vsp, VSP_PORT_INPUT, 0);
+	vsp_request_buffers(r->vsp, VSP_PORT_OUTPUT, 0);
+
+	pthread_mutex_unlock(&r->vsp->mutex);
+	return 0;
+
+err_vsp:
+	/* drop gst buffer */
+	/* finish vsp here */
+err:
+	pthread_mutex_unlock(&r->vsp->mutex);
+	return -1;
+}
+
+int
+gst_recorder_frame_dmafd(struct gst_recorder *r, int fd, int stride)
+{
+	int ret = 0;
+	
+	pthread_mutex_lock(&r->mutex);
+
+	if (r->error) {
+		errno = r->error;
+		ret = -1;
+		goto unlock;
+	}
+		
+	/* The mutex is never released while encoding, so this point should
+	 * never be reached if input.valid is true. */
+	assert(!r->input.valid);
+
+	r->input.prime_fd = fd;
+	r->input.stride = stride;
+	r->input.valid = 1;
+	pthread_cond_signal(&r->input_cond);
+
+unlock:
+	pthread_mutex_unlock(&r->mutex);
+
+	return 0;
+}
diff --git a/src/gst-recorder.h b/src/gst-recorder.h
new file mode 100644
index 0000000..e1c53ff
--- /dev/null
+++ b/src/gst-recorder.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2016 Cogent Embedded Inc
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _GST_RECORDER_H_
+#define _GST_RECORDER_H_
+
+/* VSP includes */
+#include <media-ctl/mediactl.h>
+#include <media-ctl/v4l2subdev.h>
+
+struct gst_recorder;
+
+struct gst_recorder_settings {
+	int width;
+	int height;
+	int bitrate;
+	char *ip;
+	int port;
+	int latency_test;
+	int refresh_ratio;
+
+	/* Cropping */
+	struct v4l2_rect crop;
+};
+
+void
+gst_recorder_init(void);
+struct gst_recorder *
+gst_recorder_create(struct gst_recorder_settings *settings);
+void
+gst_recorder_destroy(struct gst_recorder *r);
+int
+gst_recorder_frame(struct gst_recorder *r, int fd, int stride);
+int
+gst_recorder_frame_mmap(struct gst_recorder *r, void *data, int stride);
+int
+gst_recorder_frame_dmafd(struct gst_recorder *r, int fd, int stride);
+
+#endif /* _GST_RECORDER_H_ */
diff --git a/src/main.c b/src/main.c
index c2168eb..e287a88 100644
--- a/src/main.c
+++ b/src/main.c
@@ -278,7 +278,8 @@ usage(int error_code)
 		"  --tty=TTY\t\tThe tty to use\n"
 		"  --use-pixman\t\tUse the pixman (CPU) renderer\n"
 		"  --use-v4l2\t\tUse the v4l2 renderer\n"
-		"  --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
+		"  --current-mode\tPrefer current KMS mode over EDID preferred mode\n"
+		"  --gst-record\t\tEnable GStreamer recording\n\n");
 #endif
 
 #if defined(BUILD_FBDEV_COMPOSITOR)
@@ -752,6 +753,7 @@ load_drm_backend(struct weston_compositor *c, const char *backend,
 		{ WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
 		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
 		{ WESTON_OPTION_BOOLEAN, "use-v4l2", 0, &config.use_v4l2 },
+		{ WESTON_OPTION_BOOLEAN, "gst-record", 0, &config.enable_recorder },
 	};
 
 	parse_options(options, ARRAY_LENGTH(options), argc, argv);
diff --git a/src/media-ctl/libmediactl.c b/src/media-ctl/libmediactl.c
new file mode 100644
index 0000000..f15b1a3
--- /dev/null
+++ b/src/media-ctl/libmediactl.c
@@ -0,0 +1,955 @@
+/*
+ * Media controller interface library
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/media.h>
+#include <linux/videodev2.h>
+
+#include "mediactl.h"
+#include "mediactl-priv.h"
+#include "tools.h"
+
+/* -----------------------------------------------------------------------------
+ * Graph access
+ */
+
+struct media_pad *media_entity_remote_source(struct media_pad *pad)
+{
+	unsigned int i;
+
+	if (!(pad->flags & MEDIA_PAD_FL_SINK))
+		return NULL;
+
+	for (i = 0; i < pad->entity->num_links; ++i) {
+		struct media_link *link = &pad->entity->links[i];
+
+		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
+			continue;
+
+		if (link->sink == pad)
+			return link->source;
+	}
+
+	return NULL;
+}
+
+struct media_entity *media_get_entity_by_name(struct media_device *media,
+					      const char *name, size_t length)
+{
+	unsigned int i;
+
+	/* A match is impossible if the entity name is longer than the maximum
+	 * size we can get from the kernel.
+	 */
+	if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
+		return NULL;
+
+	for (i = 0; i < media->entities_count; ++i) {
+		struct media_entity *entity = &media->entities[i];
+
+		if (strncmp(entity->info.name, name, length) == 0 &&
+		    entity->info.name[length] == '\0')
+			return entity;
+	}
+
+	return NULL;
+}
+
+struct media_entity *media_get_entity_by_id(struct media_device *media,
+					    __u32 id)
+{
+	bool next = id & MEDIA_ENT_ID_FLAG_NEXT;
+	unsigned int i;
+
+	id &= ~MEDIA_ENT_ID_FLAG_NEXT;
+
+	for (i = 0; i < media->entities_count; ++i) {
+		struct media_entity *entity = &media->entities[i];
+
+		if ((entity->info.id == id && !next) ||
+		    (entity->info.id > id && next))
+			return entity;
+	}
+
+	return NULL;
+}
+
+unsigned int media_get_entities_count(struct media_device *media)
+{
+	return media->entities_count;
+}
+
+struct media_entity *media_get_entity(struct media_device *media, unsigned int index)
+{
+	if (index >= media->entities_count)
+		return NULL;
+
+	return &media->entities[index];
+}
+
+const struct media_pad *media_entity_get_pad(struct media_entity *entity, unsigned int index)
+{
+	if (index >= entity->info.pads)
+		return NULL;
+
+	return &entity->pads[index];
+}
+
+unsigned int media_entity_get_links_count(struct media_entity *entity)
+{
+	return entity->num_links;
+}
+
+const struct media_link *media_entity_get_link(struct media_entity *entity, unsigned int index)
+{
+	if (index >= entity->num_links)
+		return NULL;
+
+	return &entity->links[index];
+}
+
+const char *media_entity_get_devname(struct media_entity *entity)
+{
+	return entity->devname[0] ? entity->devname : NULL;
+}
+
+struct media_entity *media_get_default_entity(struct media_device *media,
+					      unsigned int type)
+{
+	switch (type) {
+	case MEDIA_ENT_T_DEVNODE_V4L:
+		return media->def.v4l;
+	case MEDIA_ENT_T_DEVNODE_FB:
+		return media->def.fb;
+	case MEDIA_ENT_T_DEVNODE_ALSA:
+		return media->def.alsa;
+	case MEDIA_ENT_T_DEVNODE_DVB:
+		return media->def.dvb;
+	}
+
+	return NULL;
+}
+
+const struct media_device_info *media_get_info(struct media_device *media)
+{
+	return &media->info;
+}
+
+const char *media_get_devnode(struct media_device *media)
+{
+	return media->devnode;
+}
+
+const struct media_entity_desc *media_entity_get_info(struct media_entity *entity)
+{
+	return &entity->info;
+}
+
+/* -----------------------------------------------------------------------------
+ * Open/close
+ */
+
+static int media_device_open(struct media_device *media)
+{
+	int ret;
+
+	if (media->fd != -1)
+		return 0;
+
+	media_dbg(media, "Opening media device %s\n", media->devnode);
+
+	media->fd = open(media->devnode, O_RDWR);
+	if (media->fd < 0) {
+		ret = -errno;
+		media_dbg(media, "%s: Can't open media device %s\n",
+			  __func__, media->devnode);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void media_device_close(struct media_device *media)
+{
+	if (media->fd != -1) {
+		close(media->fd);
+		media->fd = -1;
+	}
+}
+
+/* -----------------------------------------------------------------------------
+ * Link setup
+ */
+
+int media_setup_link(struct media_device *media,
+		     struct media_pad *source,
+		     struct media_pad *sink,
+		     __u32 flags)
+{
+	struct media_link *link;
+	struct media_link_desc ulink;
+	unsigned int i;
+	int ret;
+
+	ret = media_device_open(media);
+	if (ret < 0)
+		goto done;
+
+	for (i = 0; i < source->entity->num_links; i++) {
+		link = &source->entity->links[i];
+
+		if (link->source->entity == source->entity &&
+		    link->source->index == source->index &&
+		    link->sink->entity == sink->entity &&
+		    link->sink->index == sink->index)
+			break;
+	}
+
+	if (i == source->entity->num_links) {
+		media_dbg(media, "%s: Link not found\n", __func__);
+		ret = -ENOENT;
+		goto done;
+	}
+
+	/* source pad */
+	ulink.source.entity = source->entity->info.id;
+	ulink.source.index = source->index;
+	ulink.source.flags = MEDIA_PAD_FL_SOURCE;
+
+	/* sink pad */
+	ulink.sink.entity = sink->entity->info.id;
+	ulink.sink.index = sink->index;
+	ulink.sink.flags = MEDIA_PAD_FL_SINK;
+
+	ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
+
+	ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
+	if (ret == -1) {
+		ret = -errno;
+		media_dbg(media, "%s: Unable to setup link (%s)\n",
+			  __func__, strerror(errno));
+		goto done;
+	}
+
+	link->flags = ulink.flags;
+	link->twin->flags = ulink.flags;
+
+	ret = 0;
+
+done:
+	return ret;
+}
+
+int media_reset_links(struct media_device *media)
+{
+	unsigned int i, j;
+	int ret;
+
+	for (i = 0; i < media->entities_count; ++i) {
+		struct media_entity *entity = &media->entities[i];
+
+		for (j = 0; j < entity->num_links; j++) {
+			struct media_link *link = &entity->links[j];
+
+			if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
+			    link->source->entity != entity)
+				continue;
+
+			ret = media_setup_link(media, link->source, link->sink,
+					       link->flags & ~MEDIA_LNK_FL_ENABLED);
+			if (ret < 0)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
+/* -----------------------------------------------------------------------------
+ * Entities, pads and links enumeration
+ */
+
+static struct media_link *media_entity_add_link(struct media_entity *entity)
+{
+	if (entity->num_links >= entity->max_links) {
+		struct media_link *links = entity->links;
+		unsigned int max_links = entity->max_links * 2;
+		unsigned int i;
+
+		links = realloc(links, max_links * sizeof *links);
+		if (links == NULL)
+			return NULL;
+
+		for (i = 0; i < entity->num_links; ++i)
+			links[i].twin->twin = &links[i];
+
+		entity->max_links = max_links;
+		entity->links = links;
+	}
+
+	return &entity->links[entity->num_links++];
+}
+
+static int media_enum_links(struct media_device *media)
+{
+	__u32 id;
+	int ret = 0;
+
+	for (id = 1; id <= media->entities_count; id++) {
+		struct media_entity *entity = &media->entities[id - 1];
+		struct media_links_enum links;
+		unsigned int i;
+
+		links.entity = entity->info.id;
+		links.pads = calloc(entity->info.pads, sizeof(struct media_pad_desc));
+		links.links = calloc(entity->info.links, sizeof(struct media_link_desc));
+
+		if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
+			ret = -errno;
+			media_dbg(media,
+				  "%s: Unable to enumerate pads and links (%s).\n",
+				  __func__, strerror(errno));
+			free(links.pads);
+			free(links.links);
+			return ret;
+		}
+
+		for (i = 0; i < entity->info.pads; ++i) {
+			entity->pads[i].entity = entity;
+			entity->pads[i].index = links.pads[i].index;
+			entity->pads[i].flags = links.pads[i].flags;
+		}
+
+		for (i = 0; i < entity->info.links; ++i) {
+			struct media_link_desc *link = &links.links[i];
+			struct media_link *fwdlink;
+			struct media_link *backlink;
+			struct media_entity *source;
+			struct media_entity *sink;
+
+			source = media_get_entity_by_id(media, link->source.entity);
+			sink = media_get_entity_by_id(media, link->sink.entity);
+
+			if (source == NULL || sink == NULL) {
+				media_dbg(media,
+					  "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
+					  id, i, link->source.entity,
+					  link->source.index,
+					  link->sink.entity,
+					  link->sink.index);
+				ret = -EINVAL;
+			} else {
+				fwdlink = media_entity_add_link(source);
+				fwdlink->source = &source->pads[link->source.index];
+				fwdlink->sink = &sink->pads[link->sink.index];
+				fwdlink->flags = link->flags;
+
+				backlink = media_entity_add_link(sink);
+				backlink->source = &source->pads[link->source.index];
+				backlink->sink = &sink->pads[link->sink.index];
+				backlink->flags = link->flags;
+
+				fwdlink->twin = backlink;
+				backlink->twin = fwdlink;
+			}
+		}
+
+		free(links.pads);
+		free(links.links);
+	}
+
+	return ret;
+}
+
+#ifdef HAVE_LIBUDEV
+
+#include <libudev.h>
+
+static inline int media_udev_open(struct udev **udev)
+{
+	*udev = udev_new();
+	if (*udev == NULL)
+		return -ENOMEM;
+	return 0;
+}
+
+static inline void media_udev_close(struct udev *udev)
+{
+	if (udev != NULL)
+		udev_unref(udev);
+}
+
+static int media_get_devname_udev(struct udev *udev,
+		struct media_entity *entity)
+{
+	struct udev_device *device;
+	dev_t devnum;
+	const char *p;
+	int ret = -ENODEV;
+
+	if (udev == NULL)
+		return -EINVAL;
+
+	devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
+	media_dbg(entity->media, "looking up device: %u:%u\n",
+		  major(devnum), minor(devnum));
+	device = udev_device_new_from_devnum(udev, 'c', devnum);
+	if (device) {
+		p = udev_device_get_devnode(device);
+		if (p) {
+			strncpy(entity->devname, p, sizeof(entity->devname));
+			entity->devname[sizeof(entity->devname) - 1] = '\0';
+		}
+		ret = 0;
+	}
+
+	udev_device_unref(device);
+
+	return ret;
+}
+
+#else	/* HAVE_LIBUDEV */
+
+struct udev;
+
+static inline int media_udev_open(struct udev **udev) { return 0; }
+
+static inline void media_udev_close(struct udev *udev) { }
+
+static inline int media_get_devname_udev(struct udev *udev,
+		struct media_entity *entity)
+{
+	return -ENOTSUP;
+}
+
+#endif	/* HAVE_LIBUDEV */
+
+static int media_get_devname_sysfs(struct media_entity *entity)
+{
+	struct stat devstat;
+	char devname[32];
+	char sysname[32];
+	char target[1024];
+	char *p;
+	int ret;
+
+	sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
+		entity->info.v4l.minor);
+	ret = readlink(sysname, target, sizeof(target) - 1);
+	if (ret < 0)
+		return -errno;
+
+	target[ret] = '\0';
+	p = strrchr(target, '/');
+	if (p == NULL)
+		return -EINVAL;
+
+	sprintf(devname, "/dev/%s", p + 1);
+	ret = stat(devname, &devstat);
+	if (ret < 0)
+		return -errno;
+
+	/* Sanity check: udev might have reordered the device nodes.
+	 * Make sure the major/minor match. We should really use
+	 * libudev.
+	 */
+	if (major(devstat.st_rdev) == entity->info.v4l.major &&
+	    minor(devstat.st_rdev) == entity->info.v4l.minor)
+		strcpy(entity->devname, devname);
+
+	return 0;
+}
+
+static int media_enum_entities(struct media_device *media)
+{
+	struct media_entity *entity;
+	struct udev *udev;
+	unsigned int size;
+	__u32 id;
+	int ret;
+
+	ret = media_udev_open(&udev);
+	if (ret < 0)
+		media_dbg(media, "Can't get udev context\n");
+
+	for (id = 0, ret = 0; ; id = entity->info.id) {
+		size = (media->entities_count + 1) * sizeof(*media->entities);
+		media->entities = realloc(media->entities, size);
+
+		entity = &media->entities[media->entities_count];
+		memset(entity, 0, sizeof(*entity));
+		entity->fd = -1;
+		entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
+		entity->media = media;
+
+		ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
+		if (ret < 0) {
+			ret = errno != EINVAL ? -errno : 0;
+			break;
+		}
+
+		/* Number of links (for outbound links) plus number of pads (for
+		 * inbound links) is a good safe initial estimate of the total
+		 * number of links.
+		 */
+		entity->max_links = entity->info.pads + entity->info.links;
+
+		entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
+		entity->links = malloc(entity->max_links * sizeof(*entity->links));
+		if (entity->pads == NULL || entity->links == NULL) {
+			ret = -ENOMEM;
+			break;
+		}
+
+		media->entities_count++;
+
+		if (entity->info.flags & MEDIA_ENT_FL_DEFAULT) {
+			switch (entity->info.type) {
+			case MEDIA_ENT_T_DEVNODE_V4L:
+				media->def.v4l = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_FB:
+				media->def.fb = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_ALSA:
+				media->def.alsa = entity;
+				break;
+			case MEDIA_ENT_T_DEVNODE_DVB:
+				media->def.dvb = entity;
+				break;
+			}
+		}
+
+		/* Find the corresponding device name. */
+		if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
+		    media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
+			continue;
+
+		/* Try to get the device name via udev */
+		if (!media_get_devname_udev(udev, entity))
+			continue;
+
+		/* Fall back to get the device name via sysfs */
+		media_get_devname_sysfs(entity);
+	}
+
+	media_udev_close(udev);
+	return ret;
+}
+
+int media_device_enumerate(struct media_device *media)
+{
+	int ret;
+
+	if (media->entities)
+		return 0;
+
+	ret = media_device_open(media);
+	if (ret < 0)
+		return ret;
+
+	ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
+	if (ret < 0) {
+		ret = -errno;
+		media_dbg(media, "%s: Unable to retrieve media device "
+			  "information for device %s (%s)\n", __func__,
+			  media->devnode, strerror(errno));
+		goto done;
+	}
+
+	media_dbg(media, "Enumerating entities\n");
+
+	ret = media_enum_entities(media);
+	if (ret < 0) {
+		media_dbg(media,
+			  "%s: Unable to enumerate entities for device %s (%s)\n",
+			  __func__, media->devnode, strerror(-ret));
+		goto done;
+	}
+
+	media_dbg(media, "Found %u entities\n", media->entities_count);
+	media_dbg(media, "Enumerating pads and links\n");
+
+	ret = media_enum_links(media);
+	if (ret < 0) {
+		media_dbg(media,
+			  "%s: Unable to enumerate pads and linksfor device %s\n",
+			  __func__, media->devnode);
+		goto done;
+	}
+
+	ret = 0;
+
+done:
+	return ret;
+}
+
+/* -----------------------------------------------------------------------------
+ * Create/destroy
+ */
+
+static void media_debug_default(void *ptr, ...)
+{
+}
+
+void media_debug_set_handler(struct media_device *media,
+			     void (*debug_handler)(void *, ...),
+			     void *debug_priv)
+{
+	if (debug_handler) {
+		media->debug_handler = debug_handler;
+		media->debug_priv = debug_priv;
+	} else {
+		media->debug_handler = media_debug_default;
+		media->debug_priv = NULL;
+	}
+}
+
+static struct media_device *__media_device_new(void)
+{
+	struct media_device *media;
+
+	media = calloc(1, sizeof(*media));
+	if (media == NULL)
+		return NULL;
+
+	media->fd = -1;
+	media->refcount = 1;
+
+	media_debug_set_handler(media, NULL, NULL);
+
+	return media;
+}
+
+struct media_device *media_device_new(const char *devnode)
+{
+	struct media_device *media;
+
+	media = __media_device_new();
+	if (media == NULL)
+		return NULL;
+
+	media->devnode = strdup(devnode);
+	if (media->devnode == NULL) {
+		media_device_unref(media);
+		return NULL;
+	}
+
+	return media;
+}
+
+struct media_device *media_device_new_emulated(struct media_device_info *info)
+{
+	struct media_device *media;
+
+	media = __media_device_new();
+	if (media == NULL)
+		return NULL;
+
+	media->info = *info;
+
+	return media;
+}
+
+struct media_device *media_device_ref(struct media_device *media)
+{
+	media->refcount++;
+	return media;
+}
+
+void media_device_unref(struct media_device *media)
+{
+	unsigned int i;
+
+	media->refcount--;
+	if (media->refcount > 0)
+		return;
+
+	for (i = 0; i < media->entities_count; ++i) {
+		struct media_entity *entity = &media->entities[i];
+
+		free(entity->pads);
+		free(entity->links);
+		if (entity->fd != -1)
+			close(entity->fd);
+	}
+
+	free(media->entities);
+	free(media->devnode);
+	free(media);
+}
+
+int media_device_add_entity(struct media_device *media,
+			    const struct media_entity_desc *desc,
+			    const char *devnode)
+{
+	struct media_entity **defent = NULL;
+	struct media_entity *entity;
+	unsigned int size;
+
+	size = (media->entities_count + 1) * sizeof(*media->entities);
+	entity = realloc(media->entities, size);
+	if (entity == NULL)
+		return -ENOMEM;
+
+	media->entities = entity;
+	media->entities_count++;
+
+	entity = &media->entities[media->entities_count - 1];
+	memset(entity, 0, sizeof *entity);
+
+	entity->fd = -1;
+	entity->media = media;
+	strncpy(entity->devname, devnode, sizeof entity->devname);
+	entity->devname[sizeof entity->devname - 1] = '\0';
+
+	entity->info.id = 0;
+	entity->info.type = desc->type;
+	entity->info.flags = 0;
+	memcpy(entity->info.name, desc->name, sizeof entity->info.name);
+
+	switch (entity->info.type) {
+	case MEDIA_ENT_T_DEVNODE_V4L:
+		defent = &media->def.v4l;
+		entity->info.v4l = desc->v4l;
+		break;
+	case MEDIA_ENT_T_DEVNODE_FB:
+		defent = &media->def.fb;
+		entity->info.fb = desc->fb;
+		break;
+	case MEDIA_ENT_T_DEVNODE_ALSA:
+		defent = &media->def.alsa;
+		entity->info.alsa = desc->alsa;
+		break;
+	case MEDIA_ENT_T_DEVNODE_DVB:
+		defent = &media->def.dvb;
+		entity->info.dvb = desc->dvb;
+		break;
+	}
+
+	if (desc->flags & MEDIA_ENT_FL_DEFAULT) {
+		entity->info.flags |= MEDIA_ENT_FL_DEFAULT;
+		if (defent)
+			*defent = entity;
+	}
+
+	return 0;
+}
+
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int entity_id, pad;
+	struct media_entity *entity;
+	char *end;
+
+	/* endp can be NULL. To avoid spreading NULL checks across the function,
+	 * set endp to &end in that case.
+	 */
+	if (endp == NULL)
+		endp = &end;
+
+	for (; isspace(*p); ++p);
+
+	if (*p == '"' || *p == '\'') {
+		for (end = (char *)p + 1; *end && *end != '"' && *end != '\''; ++end);
+		if (*end != '"' && *end != '\'') {
+			media_dbg(media, "missing matching '\"'\n");
+			*endp = end;
+			return NULL;
+		}
+
+		entity = media_get_entity_by_name(media, p + 1, end - p - 1);
+		if (entity == NULL) {
+			media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
+			*endp = (char *)p + 1;
+			return NULL;
+		}
+
+		++end;
+	} else {
+		entity_id = strtoul(p, &end, 10);
+		entity = media_get_entity_by_id(media, entity_id);
+		if (entity == NULL) {
+			media_dbg(media, "no such entity %d\n", entity_id);
+			*endp = (char *)p;
+			return NULL;
+		}
+	}
+	for (; isspace(*end); ++end);
+
+	if (*end != ':') {
+		media_dbg(media, "Expected ':'\n", *end);
+		*endp = end;
+		return NULL;
+	}
+
+	for (p = end + 1; isspace(*p); ++p);
+
+	pad = strtoul(p, &end, 10);
+
+	if (pad >= entity->info.pads) {
+		media_dbg(media, "No pad '%d' on entity \"%s\". Maximum pad number is %d\n",
+				pad, entity->info.name, entity->info.pads - 1);
+		*endp = (char *)p;
+		return NULL;
+	}
+
+	for (p = end; isspace(*p); ++p);
+	*endp = (char *)p;
+
+	return &entity->pads[pad];
+}
+
+struct media_link *media_parse_link(struct media_device *media,
+				    const char *p, char **endp)
+{
+	struct media_link *link;
+	struct media_pad *source;
+	struct media_pad *sink;
+	unsigned int i;
+	char *end;
+
+	source = media_parse_pad(media, p, &end);
+	if (source == NULL) {
+		*endp = end;
+		return NULL;
+	}
+
+	if (end[0] != '-' || end[1] != '>') {
+		*endp = end;
+		media_dbg(media, "Expected '->'\n");
+		return NULL;
+	}
+
+	p = end + 2;
+
+	sink = media_parse_pad(media, p, &end);
+	if (sink == NULL) {
+		*endp = end;
+		return NULL;
+	}
+
+	*endp = end;
+
+	for (i = 0; i < source->entity->num_links; i++) {
+		link = &source->entity->links[i];
+
+		if (link->source == source && link->sink == sink)
+			return link;
+	}
+
+	media_dbg(media, "No link between \"%s\":%d and \"%s\":%d\n",
+			source->entity->info.name, source->index,
+			sink->entity->info.name, sink->index);
+	return NULL;
+}
+
+int media_parse_setup_link(struct media_device *media,
+			   const char *p, char **endp)
+{
+	struct media_link *link;
+	__u32 flags;
+	char *end;
+
+	link = media_parse_link(media, p, &end);
+	if (link == NULL) {
+		media_dbg(media,
+			  "%s: Unable to parse link\n", __func__);
+		*endp = end;
+		return -EINVAL;
+	}
+
+	p = end;
+	if (*p++ != '[') {
+		media_dbg(media, "Unable to parse link flags: expected '['.\n");
+		*endp = (char *)p - 1;
+		return -EINVAL;
+	}
+
+	flags = strtoul(p, &end, 10);
+	for (p = end; isspace(*p); p++);
+	if (*p++ != ']') {
+		media_dbg(media, "Unable to parse link flags: expected ']'.\n");
+		*endp = (char *)p - 1;
+		return -EINVAL;
+	}
+
+	for (; isspace(*p); p++);
+	*endp = (char *)p;
+
+	media_dbg(media,
+		  "Setting up link %u:%u -> %u:%u [%u]\n",
+		  link->source->entity->info.id, link->source->index,
+		  link->sink->entity->info.id, link->sink->index,
+		  flags);
+
+	return media_setup_link(media, link->source, link->sink, flags);
+}
+
+void media_print_streampos(struct media_device *media, const char *p,
+			   const char *end)
+{
+	int pos;
+
+	pos = end - p + 1;
+
+	if (pos < 0)
+		pos = 0;
+	if (pos > strlen(p))
+		pos = strlen(p);
+
+	media_dbg(media, "\n");
+	media_dbg(media, " %s\n", p);
+	media_dbg(media, " %*s\n", pos, "^");
+}
+
+int media_parse_setup_links(struct media_device *media, const char *p)
+{
+	char *end;
+	int ret;
+
+	do {
+		ret = media_parse_setup_link(media, p, &end);
+		if (ret < 0) {
+			media_print_streampos(media, p, end);
+			return ret;
+		}
+
+		p = end + 1;
+	} while (*end == ',');
+
+	return *end ? -EINVAL : 0;
+}
diff --git a/src/media-ctl/libv4l2subdev.c b/src/media-ctl/libv4l2subdev.c
new file mode 100644
index 0000000..4ede4fa
--- /dev/null
+++ b/src/media-ctl/libv4l2subdev.c
@@ -0,0 +1,759 @@
+/*
+ * V4L2 subdev interface library
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/v4l2-subdev.h>
+
+#include "mediactl.h"
+#include "mediactl-priv.h"
+#include "tools.h"
+#include "v4l2subdev.h"
+
+int v4l2_subdev_open(struct media_entity *entity)
+{
+	if (entity->fd != -1)
+		return 0;
+
+	entity->fd = open(entity->devname, O_RDWR);
+	if (entity->fd == -1) {
+		int ret = -errno;
+		media_dbg(entity->media,
+			  "%s: Failed to open subdev device node %s\n", __func__,
+			  entity->devname);
+		return ret;
+	}
+
+	return 0;
+}
+
+void v4l2_subdev_close(struct media_entity *entity)
+{
+	close(entity->fd);
+	entity->fd = -1;
+}
+
+int v4l2_subdev_get_format(struct media_entity *entity,
+	struct v4l2_mbus_framefmt *format, unsigned int pad,
+	enum v4l2_subdev_format_whence which)
+{
+	struct v4l2_subdev_format fmt;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.pad = pad;
+	fmt.which = which;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
+	if (ret < 0)
+		return -errno;
+
+	*format = fmt.format;
+	return 0;
+}
+
+int v4l2_subdev_set_format(struct media_entity *entity,
+	struct v4l2_mbus_framefmt *format, unsigned int pad,
+	enum v4l2_subdev_format_whence which)
+{
+	struct v4l2_subdev_format fmt;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.pad = pad;
+	fmt.which = which;
+	fmt.format = *format;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
+	if (ret < 0)
+		return -errno;
+
+	*format = fmt.format;
+	return 0;
+}
+
+int v4l2_subdev_get_selection(struct media_entity *entity,
+	struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+	enum v4l2_subdev_format_whence which)
+{
+	union {
+		struct v4l2_subdev_selection sel;
+		struct v4l2_subdev_crop crop;
+	} u;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&u.sel, 0, sizeof(u.sel));
+	u.sel.pad = pad;
+	u.sel.target = target;
+	u.sel.which = which;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
+	if (ret >= 0) {
+		*rect = u.sel.r;
+		return 0;
+	}
+	if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
+		return -errno;
+
+	memset(&u.crop, 0, sizeof(u.crop));
+	u.crop.pad = pad;
+	u.crop.which = which;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
+	if (ret < 0)
+		return -errno;
+
+	*rect = u.crop.rect;
+	return 0;
+}
+
+int v4l2_subdev_set_selection(struct media_entity *entity,
+	struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+	enum v4l2_subdev_format_whence which)
+{
+	union {
+		struct v4l2_subdev_selection sel;
+		struct v4l2_subdev_crop crop;
+	} u;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&u.sel, 0, sizeof(u.sel));
+	u.sel.pad = pad;
+	u.sel.target = target;
+	u.sel.which = which;
+	u.sel.r = *rect;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
+	if (ret >= 0) {
+		*rect = u.sel.r;
+		return 0;
+	}
+	if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
+		return -errno;
+
+	memset(&u.crop, 0, sizeof(u.crop));
+	u.crop.pad = pad;
+	u.crop.which = which;
+	u.crop.rect = *rect;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
+	if (ret < 0)
+		return -errno;
+
+	*rect = u.crop.rect;
+	return 0;
+}
+
+#if 0
+int v4l2_subdev_get_dv_timings_caps(struct media_entity *entity,
+	struct v4l2_dv_timings_cap *caps)
+{
+	unsigned int pad = caps->pad;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(caps, 0, sizeof(*caps));
+	caps->pad = pad;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_DV_TIMINGS_CAP, caps);
+	if (ret < 0)
+		return -errno;
+
+	return 0;
+}
+
+int v4l2_subdev_query_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings)
+{
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(timings, 0, sizeof(*timings));
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_QUERY_DV_TIMINGS, timings);
+	if (ret < 0)
+		return -errno;
+
+	return 0;
+}
+
+int v4l2_subdev_get_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings)
+{
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(timings, 0, sizeof(*timings));
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_DV_TIMINGS, timings);
+	if (ret < 0)
+		return -errno;
+
+	return 0;
+}
+
+int v4l2_subdev_set_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings)
+{
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_DV_TIMINGS, timings);
+	if (ret < 0)
+		return -errno;
+
+	return 0;
+}
+#endif
+
+int v4l2_subdev_get_frame_interval(struct media_entity *entity,
+				   struct v4l2_fract *interval)
+{
+	struct v4l2_subdev_frame_interval ival;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&ival, 0, sizeof(ival));
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
+	if (ret < 0)
+		return -errno;
+
+	*interval = ival.interval;
+	return 0;
+}
+
+int v4l2_subdev_set_frame_interval(struct media_entity *entity,
+				   struct v4l2_fract *interval)
+{
+	struct v4l2_subdev_frame_interval ival;
+	int ret;
+
+	ret = v4l2_subdev_open(entity);
+	if (ret < 0)
+		return ret;
+
+	memset(&ival, 0, sizeof(ival));
+	ival.interval = *interval;
+
+	ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
+	if (ret < 0)
+		return -errno;
+
+	*interval = ival.interval;
+	return 0;
+}
+
+static int v4l2_subdev_parse_format(struct media_device *media,
+				    struct v4l2_mbus_framefmt *format,
+				    const char *p, char **endp)
+{
+	enum v4l2_mbus_pixelcode code;
+	unsigned int width, height;
+	char *end;
+
+	/*
+	 * Compatibility with the old syntax: consider space as valid
+	 * separator between the media bus pixel code and the size.
+	 */
+	for (; isspace(*p); ++p);
+	for (end = (char *)p;
+	     *end != '/' && *end != ' ' && *end != '\0'; ++end);
+
+	code = v4l2_subdev_string_to_pixelcode(p, end - p);
+	if (code == (enum v4l2_mbus_pixelcode)-1) {
+		media_dbg(media, "Invalid pixel code '%.*s'\n", end - p, p);
+		return -EINVAL;
+	}
+
+	p = end + 1;
+	width = strtoul(p, &end, 10);
+	if (*end != 'x') {
+		media_dbg(media, "Expected 'x'\n");
+		return -EINVAL;
+	}
+
+	p = end + 1;
+	height = strtoul(p, &end, 10);
+	*endp = end;
+
+	memset(format, 0, sizeof(*format));
+	format->width = width;
+	format->height = height;
+	format->code = code;
+
+	return 0;
+}
+
+static int v4l2_subdev_parse_rectangle(struct media_device *media,
+				       struct v4l2_rect *r, const char *p,
+				       char **endp)
+{
+	char *end;
+
+	if (*p++ != '(') {
+		media_dbg(media, "Expected '('\n");
+		*endp = (char *)p - 1;
+		return -EINVAL;
+	}
+
+	r->left = strtoul(p, &end, 10);
+	if (*end != ',') {
+		media_dbg(media, "Expected ','\n");
+		*endp = end;
+		return -EINVAL;
+	}
+
+	p = end + 1;
+	r->top = strtoul(p, &end, 10);
+	if (*end++ != ')') {
+		media_dbg(media, "Expected ')'\n");
+		*endp = end - 1;
+		return -EINVAL;
+	}
+	if (*end != '/') {
+		media_dbg(media, "Expected '/'\n");
+		*endp = end;
+		return -EINVAL;
+	}
+
+	p = end + 1;
+	r->width = strtoul(p, &end, 10);
+	if (*end != 'x') {
+		media_dbg(media, "Expected 'x'\n");
+		*endp = end;
+		return -EINVAL;
+	}
+
+	p = end + 1;
+	r->height = strtoul(p, &end, 10);
+	*endp = end;
+
+	return 0;
+}
+
+static int v4l2_subdev_parse_frame_interval(struct media_device *media,
+					    struct v4l2_fract *interval,
+					    const char *p, char **endp)
+{
+	char *end;
+
+	for (; isspace(*p); ++p);
+
+	interval->numerator = strtoul(p, &end, 10);
+
+	for (p = end; isspace(*p); ++p);
+	if (*p++ != '/') {
+		media_dbg(media, "Expected '/'\n");
+		*endp = (char *)p - 1;
+		return -EINVAL;
+	}
+
+	for (; isspace(*p); ++p);
+	interval->denominator = strtoul(p, &end, 10);
+
+	*endp = end;
+	return 0;
+}
+
+/*
+ * The debate over whether this function should be named icanhasstr() instead
+ * has been strong and heated. If you feel like this would be an important
+ * change, patches are welcome (or not).
+ */
+static bool strhazit(const char *str, const char **p)
+{
+	int len = strlen(str);
+
+	if (strncmp(str, *p, len))
+		return false;
+
+	for (*p += len; isspace(**p); ++*p);
+	return true;
+}
+
+static struct media_pad *v4l2_subdev_parse_pad_format(
+	struct media_device *media, struct v4l2_mbus_framefmt *format,
+	struct v4l2_rect *crop, struct v4l2_rect *compose,
+	struct v4l2_fract *interval, const char *p, char **endp)
+{
+	struct media_pad *pad;
+	bool first;
+	char *end;
+	int ret;
+
+	for (; isspace(*p); ++p);
+
+	pad = media_parse_pad(media, p, &end);
+	if (pad == NULL) {
+		*endp = end;
+		return NULL;
+	}
+
+	for (p = end; isspace(*p); ++p);
+	if (*p++ != '[') {
+		media_dbg(media, "Expected '['\n");
+		*endp = (char *)p - 1;
+		return NULL;
+	}
+
+	for (first = true; ; first = false) {
+		for (; isspace(*p); p++);
+
+		/*
+		 * Backward compatibility: if the first property starts with an
+		 * uppercase later, process it as a format description.
+		 */
+		if (strhazit("fmt:", &p) || (first && isupper(*p))) {
+			ret = v4l2_subdev_parse_format(media, format, p, &end);
+			if (ret < 0) {
+				*endp = end;
+				return NULL;
+			}
+
+			p = end;
+			continue;
+		}
+
+		/*
+		 * Backward compatibility: crop rectangles can be specified
+		 * implicitly without the 'crop:' property name.
+		 */
+		if (strhazit("crop:", &p) || *p == '(') {
+			ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
+			if (ret < 0) {
+				*endp = end;
+				return NULL;
+			}
+
+			p = end;
+			continue;
+		}
+
+		if (strhazit("compose:", &p)) {
+			ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
+			if (ret < 0) {
+				*endp = end;
+				return NULL;
+			}
+
+			for (p = end; isspace(*p); p++);
+			continue;
+		}
+
+		if (*p == '@') {
+			ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
+			if (ret < 0) {
+				*endp = end;
+				return NULL;
+			}
+
+			p = end;
+			continue;
+		}
+
+		break;
+	}
+
+	if (*p != ']') {
+		media_dbg(media, "Expected ']'\n");
+		*endp = (char *)p;
+		return NULL;
+	}
+
+	*endp = (char *)p + 1;
+	return pad;
+}
+
+static int set_format(struct media_pad *pad,
+		      struct v4l2_mbus_framefmt *format)
+{
+	int ret;
+
+	if (format->width == 0 || format->height == 0)
+		return 0;
+
+	media_dbg(pad->entity->media,
+		  "Setting up format %s %ux%u on pad %s/%u\n",
+		  v4l2_subdev_pixelcode_to_string(format->code),
+		  format->width, format->height,
+		  pad->entity->info.name, pad->index);
+
+	ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
+				     V4L2_SUBDEV_FORMAT_ACTIVE);
+	if (ret < 0) {
+		media_dbg(pad->entity->media,
+			  "Unable to set format: %s (%d)\n",
+			  strerror(-ret), ret);
+		return ret;
+	}
+
+	media_dbg(pad->entity->media,
+		  "Format set: %s %ux%u\n",
+		  v4l2_subdev_pixelcode_to_string(format->code),
+		  format->width, format->height);
+
+	return 0;
+}
+
+static int set_selection(struct media_pad *pad, unsigned int target,
+			 struct v4l2_rect *rect)
+{
+	int ret;
+
+	if (rect->left == -1 || rect->top == -1)
+		return 0;
+
+	media_dbg(pad->entity->media,
+		  "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
+		  target, rect->left, rect->top, rect->width, rect->height,
+		  pad->entity->info.name, pad->index);
+
+	ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
+					target, V4L2_SUBDEV_FORMAT_ACTIVE);
+	if (ret < 0) {
+		media_dbg(pad->entity->media,
+			  "Unable to set selection rectangle: %s (%d)\n",
+			  strerror(-ret), ret);
+		return ret;
+	}
+
+	media_dbg(pad->entity->media,
+		  "Selection rectangle set: (%u,%u)/%ux%u\n",
+		  rect->left, rect->top, rect->width, rect->height);
+
+	return 0;
+}
+
+static int set_frame_interval(struct media_entity *entity,
+			      struct v4l2_fract *interval)
+{
+	int ret;
+
+	if (interval->numerator == 0)
+		return 0;
+
+	media_dbg(entity->media,
+		  "Setting up frame interval %u/%u on entity %s\n",
+		  interval->numerator, interval->denominator,
+		  entity->info.name);
+
+	ret = v4l2_subdev_set_frame_interval(entity, interval);
+	if (ret < 0) {
+		media_dbg(entity->media,
+			  "Unable to set frame interval: %s (%d)",
+			  strerror(-ret), ret);
+		return ret;
+	}
+
+	media_dbg(entity->media, "Frame interval set: %u/%u\n",
+		  interval->numerator, interval->denominator);
+
+	return 0;
+}
+
+
+static int v4l2_subdev_parse_setup_format(struct media_device *media,
+					  const char *p, char **endp)
+{
+	struct v4l2_mbus_framefmt format = { 0, 0, 0 };
+	struct media_pad *pad;
+	struct v4l2_rect crop = { -1, -1, -1, -1 };
+	struct v4l2_rect compose = crop;
+	struct v4l2_fract interval = { 0, 0 };
+	unsigned int i;
+	char *end;
+	int ret;
+
+	pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
+					   &interval, p, &end);
+	if (pad == NULL) {
+		media_print_streampos(media, p, end);
+		media_dbg(media, "Unable to parse format\n");
+		return -EINVAL;
+	}
+
+	if (pad->flags & MEDIA_PAD_FL_SINK) {
+		ret = set_format(pad, &format);
+		if (ret < 0)
+			return ret;
+	}
+
+	ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
+	if (ret < 0)
+		return ret;
+
+	ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
+	if (ret < 0)
+		return ret;
+
+	if (pad->flags & MEDIA_PAD_FL_SOURCE) {
+		ret = set_format(pad, &format);
+		if (ret < 0)
+			return ret;
+	}
+
+	ret = set_frame_interval(pad->entity, &interval);
+	if (ret < 0)
+		return ret;
+
+
+	/* If the pad is an output pad, automatically set the same format on
+	 * the remote subdev input pads, if any.
+	 */
+	if (pad->flags & MEDIA_PAD_FL_SOURCE) {
+		for (i = 0; i < pad->entity->num_links; ++i) {
+			struct media_link *link = &pad->entity->links[i];
+			struct v4l2_mbus_framefmt remote_format;
+
+			if (!(link->flags & MEDIA_LNK_FL_ENABLED))
+				continue;
+
+			if (link->source == pad &&
+			    link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
+				remote_format = format;
+				set_format(link->sink, &remote_format);
+			}
+		}
+	}
+
+	*endp = end;
+	return 0;
+}
+
+int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
+{
+	char *end;
+	int ret;
+
+	do {
+		ret = v4l2_subdev_parse_setup_format(media, p, &end);
+		if (ret < 0)
+			return ret;
+
+		p = end + 1;
+	} while (*end == ',');
+
+	return *end ? -EINVAL : 0;
+}
+
+static struct {
+	const char *name;
+	enum v4l2_mbus_pixelcode code;
+} mbus_formats[] = {
+	{ "Y8", V4L2_MBUS_FMT_Y8_1X8},
+	{ "Y10", V4L2_MBUS_FMT_Y10_1X10 },
+	{ "Y12", V4L2_MBUS_FMT_Y12_1X12 },
+	{ "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
+	{ "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
+	{ "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
+	{ "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
+	{ "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
+	{ "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
+	{ "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
+	{ "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
+	{ "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
+	{ "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
+	{ "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
+	{ "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
+	{ "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
+	{ "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
+	{ "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
+	{ "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
+	{ "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
+	{ "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
+	{ "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
+	{ "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
+	{ "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
+	{ "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
+	{ "AYUV32", V4L2_MBUS_FMT_AYUV8_1X32 },
+	{ "ARGB32", V4L2_MBUS_FMT_ARGB8888_1X32 },
+};
+
+const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
+		if (mbus_formats[i].code == code)
+			return mbus_formats[i].name;
+	}
+
+	return "unknown";
+}
+
+enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
+							 unsigned int length)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
+		if (strncmp(mbus_formats[i].name, string, length) == 0)
+			break;
+	}
+
+	if (i == ARRAY_SIZE(mbus_formats))
+		return (enum v4l2_mbus_pixelcode)-1;
+
+	return mbus_formats[i].code;
+}
diff --git a/src/media-ctl/mediactl-priv.h b/src/media-ctl/mediactl-priv.h
new file mode 100644
index 0000000..a0d3a55
--- /dev/null
+++ b/src/media-ctl/mediactl-priv.h
@@ -0,0 +1,64 @@
+/*
+ * Media controller interface library
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __MEDIA_PRIV_H__
+#define __MEDIA_PRIV_H__
+
+#include <linux/media.h>
+
+#include "mediactl.h"
+
+struct media_entity {
+	struct media_device *media;
+	struct media_entity_desc info;
+	struct media_pad *pads;
+	struct media_link *links;
+	unsigned int max_links;
+	unsigned int num_links;
+
+	char devname[32];
+	int fd;
+};
+
+struct media_device {
+	int fd;
+	int refcount;
+	char *devnode;
+
+	struct media_device_info info;
+	struct media_entity *entities;
+	unsigned int entities_count;
+
+	void (*debug_handler)(void *, ...);
+	void *debug_priv;
+
+	struct {
+		struct media_entity *v4l;
+		struct media_entity *fb;
+		struct media_entity *alsa;
+		struct media_entity *dvb;
+	} def;
+};
+
+#define media_dbg(media, ...) \
+	(media)->debug_handler((media)->debug_priv, __VA_ARGS__)
+
+#endif /* __MEDIA_PRIV_H__ */
diff --git a/src/media-ctl/mediactl.h b/src/media-ctl/mediactl.h
new file mode 100644
index 0000000..77ac182
--- /dev/null
+++ b/src/media-ctl/mediactl.h
@@ -0,0 +1,423 @@
+/*
+ * Media controller interface library
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __MEDIA_H__
+#define __MEDIA_H__
+
+#include <linux/media.h>
+
+struct media_link {
+	struct media_pad *source;
+	struct media_pad *sink;
+	struct media_link *twin;
+	__u32 flags;
+	__u32 padding[3];
+};
+
+struct media_pad {
+	struct media_entity *entity;
+	__u32 index;
+	__u32 flags;
+	__u32 padding[3];
+};
+
+struct media_device;
+struct media_entity;
+
+/**
+ * @brief Create a new media device.
+ * @param devnode - device node path.
+ *
+ * Create a media device instance for the given device node and return it. The
+ * device node is not accessed by this function, device node access errors will
+ * not be caught and reported here. The media device needs to be enumerated
+ * before it can be accessed, see media_device_enumerate().
+ *
+ * Media devices are reference-counted, see media_device_ref() and
+ * media_device_unref() for more information.
+ *
+ * @return A pointer to the new media device or NULL if memory cannot be
+ * allocated.
+ */
+struct media_device *media_device_new(const char *devnode);
+
+/**
+ * @brief Create a new emulated media device.
+ * @param info - device information.
+ *
+ * Emulated media devices are userspace-only objects not backed by a kernel
+ * media device. They are created for ALSA and V4L2 devices that are not
+ * associated with a media controller device.
+ *
+ * Only device query functions are available for media devices. Enumerating or
+ * setting up links is invalid.
+ *
+ * @return A pointer to the new media device or NULL if memory cannot be
+ * allocated.
+ */
+struct media_device *media_device_new_emulated(struct media_device_info *info);
+
+/**
+ * @brief Take a reference to the device.
+ * @param media - device instance.
+ *
+ * Media devices are reference-counted. Taking a reference to a device prevents
+ * it from being freed until all references are released. The reference count is
+ * initialized to 1 when the device is created.
+ *
+ * @return A pointer to @a media.
+ */
+struct media_device *media_device_ref(struct media_device *media);
+
+/**
+ * @brief Release a reference to the device.
+ * @param media - device instance.
+ *
+ * Release a reference to the media device. When the reference count reaches 0
+ * this function frees the device.
+ */
+void media_device_unref(struct media_device *media);
+
+/**
+ * @brief Add an entity to an existing media device
+ * @param media - device instance.
+ * @param desc - description of the entity to be added
+ * @param devnode - device node corresponding to the entity
+ *
+ * Entities are usually created and added to media devices automatically when
+ * the media device is enumerated through the media controller API. However,
+ * when an emulated media device (thus not backed with a kernel-side media
+ * controller device) is created, entities need to be manually added.
+ *
+ * Entities can also be manually added to a successfully enumerated media device
+ * to group several functions provided by separate kernel devices. The most
+ * common use case is to group the audio and video functions of a USB webcam in
+ * a single media device. Those functions are exposed through separate USB
+ * interfaces and handled through unrelated kernel drivers, they must thus be
+ * manually added to the same media device.
+ *
+ * This function adds a new entity to the given media device and initializes it
+ * from the given entity description and device node name. Only the following
+ * fields of the description are copied over to the new entity:
+ *
+ * - type
+ * - flags (MEDIA_ENT_FL_DEFAULT only)
+ * - name
+ * - v4l, fb, alsa or dvb (depending on the device type)
+ *
+ * All other fields of the newly created entity id are initialized to 0,
+ * including the entity ID.
+ *
+ * @return Zero on success or -ENOMEM if memory cannot be allocated.
+ */
+int media_device_add_entity(struct media_device *media,
+			    const struct media_entity_desc *desc,
+			    const char *devnode);
+
+/**
+ * @brief Set a handler for debug messages.
+ * @param media - device instance.
+ * @param debug_handler - debug message handler
+ * @param debug_priv - first argument to debug message handler
+ *
+ * Set a handler for debug messages that will be called whenever
+ * debugging information is to be printed. The handler expects an
+ * fprintf-like function.
+ */
+void media_debug_set_handler(
+	struct media_device *media, void (*debug_handler)(void *, ...),
+	void *debug_priv);
+
+/**
+ * @brief Enumerate the device topology
+ * @param media - device instance.
+ *
+ * Enumerate the media device entities, pads and links. Calling this function is
+ * mandatory before accessing the media device contents.
+ *
+ * @return Zero on success or a negative error code on failure.
+ */
+int media_device_enumerate(struct media_device *media);
+
+/**
+ * @brief Locate the pad at the other end of a link.
+ * @param pad - sink pad at one end of the link.
+ *
+ * Locate the source pad connected to @a pad through an enabled link. As only one
+ * link connected to a sink pad can be enabled at a time, the connected source
+ * pad is guaranteed to be unique.
+ *
+ * @return A pointer to the connected source pad, or NULL if all links connected
+ * to @a pad are disabled. Return NULL also if @a pad is not a sink pad.
+ */
+struct media_pad *media_entity_remote_source(struct media_pad *pad);
+
+/**
+ * @brief Get information about a media entity
+ * @param entity - media entity.
+ *
+ * The information structure is owned by the media entity object and will be
+ * freed when the object is destroyed.
+ *
+ * @return A pointer to the media entity information
+ */
+const struct media_entity_desc *media_entity_get_info(struct media_entity *entity);
+
+/**
+ * @brief Get an entity pad
+ * @param entity - media entity.
+ * @param index - pad index.
+ *
+ * This function returns a pointer to the pad object identified by its index
+ * for the given entity. If the pad index is out of bounds it will return NULL.
+ *
+ * @return A pointer to the pad
+ */
+const struct media_pad *media_entity_get_pad(struct media_entity *entity,
+					     unsigned int index);
+
+/**
+ * @brief Get the number of links
+ * @param entity - media entity.
+ *
+ * This function returns the total number of links that originate from or arrive
+ * at the the media entity.
+ *
+ * @return The number of links for the entity
+ */
+unsigned int media_entity_get_links_count(struct media_entity *entity);
+
+/**
+ * @brief Get an entity link
+ * @param entity - media entity.
+ * @param index - link index.
+ *
+ * This function returns a pointer to the link object identified by its index
+ * for the given entity. If the link index is out of bounds it will return NULL.
+ *
+ * @return A pointer to the link
+ */
+const struct media_link *media_entity_get_link(struct media_entity *entity,
+					       unsigned int index);
+
+/**
+ * @brief Get the device node name for an entity
+ * @param entity - media entity.
+ *
+ * This function returns the full path and name to the device node corresponding
+ * to the given entity.
+ *
+ * @return A pointer to the device node name or NULL if the entity has no
+ * associated device node
+ */
+const char *media_entity_get_devname(struct media_entity *entity);
+
+/**
+ * @brief Get the type of an entity.
+ * @param entity - the entity.
+ *
+ * @return The type of @a entity.
+ */
+static inline unsigned int media_entity_type(struct media_entity *entity)
+{
+	return media_entity_get_info(entity)->type & MEDIA_ENT_TYPE_MASK;
+}
+
+/**
+ * @brief Find an entity by its name.
+ * @param media - media device.
+ * @param name - entity name.
+ * @param length - size of @a name.
+ *
+ * Search for an entity with a name equal to @a name.
+ *
+ * @return A pointer to the entity if found, or NULL otherwise.
+ */
+struct media_entity *media_get_entity_by_name(struct media_device *media,
+	const char *name, size_t length);
+
+/**
+ * @brief Find an entity by its ID.
+ * @param media - media device.
+ * @param id - entity ID.
+ *
+ * This function searches for an entity based on its ID using an exact match or
+ * next ID method based on the given @a id. If @a id is ORed with
+ * MEDIA_ENT_ID_FLAG_NEXT, the function will return the entity with the smallest
+ * ID larger than @a id. Otherwise it will return the entity with an ID equal to
+ * @a id.
+ *
+ * @return A pointer to the entity if found, or NULL otherwise.
+ */
+struct media_entity *media_get_entity_by_id(struct media_device *media,
+	__u32 id);
+
+/**
+ * @brief Get the number of entities
+ * @param media - media device.
+ *
+ * This function returns the total number of entities in the media device. If
+ * entities haven't been enumerated yet it will return 0.
+ *
+ * @return The number of entities in the media device
+ */
+unsigned int media_get_entities_count(struct media_device *media);
+
+/**
+ * @brief Get the entities
+ * @param media - media device.
+ *
+ * This function returns a pointer to the array of entities for the media
+ * device. If entities haven't been enumerated yet it will return NULL.
+ *
+ * The array of entities is owned by the media device object and will be freed
+ * when the media object is destroyed.
+ *
+ * @return A pointer to an array of entities
+ */
+struct media_entity *media_get_entity(struct media_device *media, unsigned int index);
+
+/**
+ * @brief Get the default entity for a given type
+ * @param media - media device.
+ * @param type - entity type.
+ *
+ * This function returns the default entity of the requested type. @a type must
+ * be one of
+ *
+ *	MEDIA_ENT_T_DEVNODE_V4L
+ *	MEDIA_ENT_T_DEVNODE_FB
+ *	MEDIA_ENT_T_DEVNODE_ALSA
+ *	MEDIA_ENT_T_DEVNODE_DVB
+ *
+ * @return A pointer to the default entity for the type if it exists, or NULL
+ * otherwise.
+ */
+struct media_entity *media_get_default_entity(struct media_device *media,
+					      unsigned int type);
+
+/**
+ * @brief Get the media device information
+ * @param media - media device.
+ *
+ * The information structure is owned by the media device object and will be freed
+ * when the media object is destroyed.
+ *
+ * @return A pointer to the media device information
+ */
+const struct media_device_info *media_get_info(struct media_device *media);
+
+/**
+ * @brief Get the media device node name
+ * @param media - media device.
+ *
+ * The device node name string is owned by the media device object and will be
+ * freed when the media object is destroyed.
+ *
+ * @return A pointer to the media device node name
+ */
+const char *media_get_devnode(struct media_device *media);
+
+/**
+ * @brief Configure a link.
+ * @param media - media device.
+ * @param source - source pad at the link origin.
+ * @param sink - sink pad at the link target.
+ * @param flags - configuration flags.
+ *
+ * Locate the link between @a source and @a sink, and configure it by applying
+ * the new @a flags.
+ *
+ * Only the MEDIA_LINK_FLAG_ENABLED flag is writable.
+ *
+ * @return 0 on success, -1 on failure:
+ *	   -ENOENT: link not found
+ *	   - other error codes returned by MEDIA_IOC_SETUP_LINK
+ */
+int media_setup_link(struct media_device *media,
+	struct media_pad *source, struct media_pad *sink,
+	__u32 flags);
+
+/**
+ * @brief Reset all links to the disabled state.
+ * @param media - media device.
+ *
+ * Disable all links in the media device. This function is usually used after
+ * opening a media device to reset all links to a known state.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int media_reset_links(struct media_device *media);
+
+/**
+ * @brief Parse string to a pad on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string where parsing ended
+ *
+ * Parse NULL terminated string describing a pad and return its struct
+ * media_pad instance.
+ *
+ * @return Pointer to struct media_pad on success, NULL on failure.
+ */
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp);
+
+/**
+ * @brief Parse string to a link on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to p where parsing ended
+ *
+ * Parse NULL terminated string p describing a link and return its struct
+ * media_link instance.
+ *
+ * @return Pointer to struct media_link on success, NULL on failure.
+ */
+struct media_link *media_parse_link(struct media_device *media,
+				    const char *p, char **endp);
+
+/**
+ * @brief Parse string to a link on the media device and set it up.
+ * @param media - media device.
+ * @param p - input string
+ *
+ * Parse NULL terminated string p describing a link and its configuration
+ * and configure the link.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int media_parse_setup_link(struct media_device *media,
+			   const char *p, char **endp);
+
+/**
+ * @brief Parse string to link(s) on the media device and set it up.
+ * @param media - media device.
+ * @param p - input string
+ *
+ * Parse NULL terminated string p describing link(s) separated by
+ * commas (,) and configure the link(s).
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int media_parse_setup_links(struct media_device *media, const char *p);
+
+#endif
diff --git a/src/media-ctl/tools.h b/src/media-ctl/tools.h
new file mode 100644
index 0000000..815534c
--- /dev/null
+++ b/src/media-ctl/tools.h
@@ -0,0 +1,32 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __TOOLS_H__
+#define __TOOLS_H__
+
+#define ARRAY_SIZE(array)	(sizeof(array) / sizeof((array)[0]))
+#define FIELD_SIZEOF(t, f)	(sizeof(((t*)0)->f))
+
+void media_print_streampos(struct media_device *media, const char *p,
+			   const char *end);
+
+#endif /* __TOOLS_H__ */
+
diff --git a/src/media-ctl/v4l2subdev.h b/src/media-ctl/v4l2subdev.h
new file mode 100644
index 0000000..1cb53ff
--- /dev/null
+++ b/src/media-ctl/v4l2subdev.h
@@ -0,0 +1,258 @@
+/*
+ * V4L2 subdev interface library
+ *
+ * Copyright (C) 2010-2014 Ideas on board SPRL
+ *
+ * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __SUBDEV_H__
+#define __SUBDEV_H__
+
+#include <linux/v4l2-subdev.h>
+
+struct media_entity;
+
+/**
+ * @brief Open a sub-device.
+ * @param entity - sub-device media entity.
+ *
+ * Open the V4L2 subdev device node associated with @a entity. The file
+ * descriptor is stored in the media_entity structure.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_open(struct media_entity *entity);
+
+/**
+ * @brief Close a sub-device.
+ * @param entity - sub-device media entity.
+ *
+ * Close the V4L2 subdev device node associated with the @a entity and opened by
+ * a previous call to v4l2_subdev_open() (either explicit or implicit).
+ */
+void v4l2_subdev_close(struct media_entity *entity);
+
+/**
+ * @brief Retrieve the format on a pad.
+ * @param entity - subdev-device media entity.
+ * @param format - format to be filled.
+ * @param pad - pad number.
+ * @param which - identifier of the format to get.
+ *
+ * Retrieve the current format on the @a entity @a pad and store it in the
+ * @a format structure.
+ *
+ * @a which is set to V4L2_SUBDEV_FORMAT_TRY to retrieve the try format stored
+ * in the file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to retrieve the current
+ * active format.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_get_format(struct media_entity *entity,
+	struct v4l2_mbus_framefmt *format, unsigned int pad,
+	enum v4l2_subdev_format_whence which);
+
+/**
+ * @brief Set the format on a pad.
+ * @param entity - subdev-device media entity.
+ * @param format - format.
+ * @param pad - pad number.
+ * @param which - identifier of the format to set.
+ *
+ * Set the format on the @a entity @a pad to @a format. The driver is allowed to
+ * modify the requested format, in which case @a format is updated with the
+ * modifications.
+ *
+ * @a which is set to V4L2_SUBDEV_FORMAT_TRY to set the try format stored in the
+ * file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to configure the device with an
+ * active format.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_set_format(struct media_entity *entity,
+	struct v4l2_mbus_framefmt *format, unsigned int pad,
+	enum v4l2_subdev_format_whence which);
+
+/**
+ * @brief Retrieve a selection rectangle on a pad.
+ * @param entity - subdev-device media entity.
+ * @param r - rectangle to be filled.
+ * @param pad - pad number.
+ * @param target - selection target
+ * @param which - identifier of the format to get.
+ *
+ * Retrieve the @a target selection rectangle on the @a entity @a pad
+ * and store it in the @a rect structure.
+ *
+ * @a which is set to V4L2_SUBDEV_FORMAT_TRY to retrieve the try
+ * selection rectangle stored in the file handle, or
+ * V4L2_SUBDEV_FORMAT_ACTIVE to retrieve the current active selection
+ * rectangle.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_get_selection(struct media_entity *entity,
+	struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+	enum v4l2_subdev_format_whence which);
+
+/**
+ * @brief Set a selection rectangle on a pad.
+ * @param entity - subdev-device media entity.
+ * @param rect - crop rectangle.
+ * @param pad - pad number.
+ * @param target - selection target
+ * @param which - identifier of the format to set.
+ *
+ * Set the @a target selection rectangle on the @a entity @a pad to @a
+ * rect. The driver is allowed to modify the requested rectangle, in
+ * which case @a rect is updated with the modifications.
+ *
+ * @a which is set to V4L2_SUBDEV_FORMAT_TRY to set the try crop rectangle
+ * stored in the file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to configure the
+ * device with an active crop rectangle.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_set_selection(struct media_entity *entity,
+	struct v4l2_rect *rect, unsigned int pad, unsigned int target,
+	enum v4l2_subdev_format_whence which);
+
+/**
+ * @brief Query the digital video capabilities of a pad.
+ * @param entity - subdev-device media entity.
+ * @param cap - capabilities to be filled.
+ *
+ * Retrieve the digital video capabilities of the @a entity pad specified by
+ * @a cap.pad and store it in the @a cap structure.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_get_dv_timings_caps(struct media_entity *entity,
+	struct v4l2_dv_timings_cap *caps);
+
+/**
+ * @brief Query the digital video timings of a sub-device
+ * @param entity - subdev-device media entity.
+ * @param timings timings to be filled.
+ *
+ * Retrieve the detected digital video timings for the currently selected input
+ * of @a entity and store them in the @a timings structure.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_query_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings);
+
+/**
+ * @brief Get the current digital video timings of a sub-device
+ * @param entity - subdev-device media entity.
+ * @param timings timings to be filled.
+ *
+ * Retrieve the current digital video timings for the currently selected input
+ * of @a entity and store them in the @a timings structure.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_get_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings);
+
+/**
+ * @brief Set the digital video timings of a sub-device
+ * @param entity - subdev-device media entity.
+ * @param timings timings to be set.
+ *
+ * Set the digital video timings of @a entity to @a timings. The driver is
+ * allowed to modify the requested format, in which case @a timings is updated
+ * with the modifications.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_set_dv_timings(struct media_entity *entity,
+	struct v4l2_dv_timings *timings);
+
+/**
+ * @brief Retrieve the frame interval on a sub-device.
+ * @param entity - subdev-device media entity.
+ * @param interval - frame interval to be filled.
+ *
+ * Retrieve the current frame interval on subdev @a entity and store it in the
+ * @a interval structure.
+ *
+ * Frame interval retrieving is usually supported only on devices at the
+ * beginning of video pipelines, such as sensors.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+
+int v4l2_subdev_get_frame_interval(struct media_entity *entity,
+	struct v4l2_fract *interval);
+
+/**
+ * @brief Set the frame interval on a sub-device.
+ * @param entity - subdev-device media entity.
+ * @param interval - frame interval.
+ *
+ * Set the frame interval on subdev @a entity to @a interval. The driver is
+ * allowed to modify the requested frame interval, in which case @a interval is
+ * updated with the modifications.
+ *
+ * Frame interval setting is usually supported only on devices at the beginning
+ * of video pipelines, such as sensors.
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_set_frame_interval(struct media_entity *entity,
+	struct v4l2_fract *interval);
+
+/**
+ * @brief Parse a string and apply format, crop and frame interval settings.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string p where parsing ended (return)
+ *
+ * Parse string @a p and apply format, crop and frame interval settings to a
+ * subdev pad specified in @a p. @a endp will be written a pointer where
+ * parsing of @a p ended.
+ *
+ * Format strings are separeted by commas (,).
+ *
+ * @return 0 on success, or a negative error code on failure.
+ */
+int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p);
+
+/**
+ * @brief Convert media bus pixel code to string.
+ * @param code - input string
+ *
+ * Convert media bus pixel code @a code to a human-readable string.
+ *
+ * @return A pointer to a string on success, NULL on failure.
+ */
+const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code);
+
+/**
+ * @brief Parse string to media bus pixel code.
+ * @param string - input string
+ * @param lenght - length of the string
+ *
+ * Parse human readable string @a string to an media bus pixel code.
+ *
+ * @return media bus pixelcode on success, -1 on failure.
+ */
+enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
+							 unsigned int length);
+#endif
-- 
1.9.1