summaryrefslogtreecommitdiff
path: root/src/sip-media-stream.c
blob: 37e14df1abfdf1e923b3fcc056babd2debe6e974 (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
/*
 * sip-media-stream.c - Source for TpsipMediaStream
 * Copyright (C) 2006 Collabora Ltd.
 * Copyright (C) 2006-2010 Nokia Corporation
 *   @author Kai Vehmanen <first.surname@nokia.com>
 *   @author Mikhail Zabaluev <mikhail.zabaluev@nokia.com>
 *
 * Based on telepathy-gabble implementation (gabble-media-stream).
 *   @author Ole Andre Vadla Ravnaas <ole.andre.ravnaas@collabora.co.uk>
 *
 * This work 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 work 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 work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <dbus/dbus-glib.h>
#include <stdlib.h>
#include <string.h>

#include <telepathy-glib/dbus.h>
#include <telepathy-glib/enums.h>
#include <telepathy-glib/errors.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/svc-media-interfaces.h>
#include <telepathy-glib/util.h>

#include "config.h"

#include <tpsip/codec-param-formats.h>

#include "sip-media-stream.h"
#include "sip-media-session.h"

#include <sofia-sip/msg_parser.h>

#include "signals-marshal.h"

#define DEBUG_FLAG TPSIP_DEBUG_MEDIA
#include "debug.h"


#define same_boolean(old, new) ((!(old)) == (!(new)))


static void stream_handler_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE(TpsipMediaStream,
    tpsip_media_stream,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_MEDIA_STREAM_HANDLER,
      stream_handler_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
      tp_dbus_properties_mixin_iface_init);
  )

/* signal enum */
enum
{
    SIG_READY,
    SIG_SUPPORTED_CODECS,
    SIG_STATE_CHANGED,
    SIG_DIRECTION_CHANGED,
    SIG_LOCAL_MEDIA_UPDATED,
    SIG_UNHOLD_FAILURE,

    SIG_LAST_SIGNAL
};

static guint signals[SIG_LAST_SIGNAL] = {0};

/* properties */
enum
{
  PROP_MEDIA_SESSION = 1,
  PROP_OBJECT_PATH,
  PROP_ID,
  PROP_MEDIA_TYPE,
  PROP_STATE,
  PROP_DIRECTION,
  PROP_PENDING_SEND_FLAGS,
  PROP_HOLD_STATE,
  PROP_CREATED_LOCALLY,
  PROP_NAT_TRAVERSAL,
  PROP_STUN_SERVERS,
  PROP_RELAY_INFO,
  LAST_PROPERTY
};

static GPtrArray *tpsip_media_stream_relay_info_empty = NULL;

/* private structure */
typedef struct _TpsipMediaStreamPrivate TpsipMediaStreamPrivate;

struct _TpsipMediaStreamPrivate
{
  TpsipMediaSession *session;     /* see gobj. prop. 'media-session' */
  gchar *object_path;             /* see gobj. prop. 'object-path' */
  guint id;                       /* see gobj. prop. 'id' */
  guint media_type;               /* see gobj. prop. 'media-type' */
  guint state;                    /* see gobj. prop. 'state' */
  guint direction;                /* see gobj. prop. 'direction' */
  guint pending_send_flags;       /* see gobj. prop. 'pending-send-flags' */
  gboolean hold_state;            /* see gobj. prop. 'hold-state' */
  gboolean created_locally;       /* see gobj. prop. 'created-locally' */

  gchar *stream_sdp;              /* SDP description of the stream */

  GValue native_codecs;           /* intersected codec list */
  GValue native_candidates;

  const sdp_media_t *remote_media; /* pointer to the SDP media structure
                                    *  owned by the session object */

  guint remote_candidate_counter;
  gchar *remote_candidate_id;

  gchar *native_candidate_id;

  gboolean ready_received;              /* our ready method has been called */
  gboolean playing;                     /* stream set to playing */
  gboolean sending;                     /* stream set to sending */
  gboolean pending_remote_receive;      /* TRUE if remote is to agree to receive media */
  gboolean native_cands_prepared;       /* all candidates discovered */
  gboolean native_codecs_prepared;      /* all codecs discovered */
  gboolean push_remote_cands_pending;   /* SetRemoteCandidates emission is pending */
  gboolean push_remote_codecs_pending;  /* SetRemoteCodecs emission is pending */
  gboolean codec_intersect_pending;     /* codec intersection is pending */
  gboolean requested_hold_state;        /* hold state last requested from the stream handler */
  gboolean dispose_has_run;
};

#define TPSIP_MEDIA_STREAM_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), TPSIP_TYPE_MEDIA_STREAM, TpsipMediaStreamPrivate))

static void push_remote_codecs (TpsipMediaStream *stream);
static void push_remote_candidates (TpsipMediaStream *stream);
static void push_active_candidate_pair (TpsipMediaStream *stream);
static void priv_update_sending (TpsipMediaStream *stream,
                                 TpMediaStreamDirection direction);
static void priv_update_local_sdp(TpsipMediaStream *stream);
static void priv_generate_sdp (TpsipMediaStream *stream);

#if 0
#ifdef ENABLE_DEBUG
static const char *debug_tp_protocols[] = {
  "TP_MEDIA_STREAM_PROTO_UDP (0)",
  "TP_MEDIA_STREAM_PROTO_TCP (1)"
};

static const char *debug_tp_transports[] = {
  "TP_MEDIA_STREAM_TRANSPORT_TYPE_LOCAL (0)",
  "TP_MEDIA_STREAM_TRANSPORT_TYPE_DERIVED (1)",
  "TP_MEDIA_STREAM_TRANSPORT_TYPE_RELAY (2)"
};
#endif /* ENABLE_DEBUG */
#endif /* 0 */

/***********************************************************************
 * Set: Gobject interface
 ***********************************************************************/

static void
tpsip_media_stream_init (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  priv->playing = FALSE;
  priv->sending = FALSE;

  g_value_init (&priv->native_codecs, TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CODEC_LIST);
  g_value_take_boxed (&priv->native_codecs,
      dbus_g_type_specialized_construct (TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CODEC_LIST));

  g_value_init (&priv->native_candidates, TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE_LIST);
  g_value_take_boxed (&priv->native_candidates,
      dbus_g_type_specialized_construct (TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE_LIST));

  priv->native_cands_prepared = FALSE;
  priv->native_codecs_prepared = FALSE;

  priv->push_remote_cands_pending = FALSE;
  priv->push_remote_codecs_pending = FALSE;
}

static void
tpsip_media_stream_constructed (GObject *obj)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (
      TPSIP_MEDIA_STREAM (obj));
  GObjectClass *parent_object_class =
      G_OBJECT_CLASS (tpsip_media_stream_parent_class);
  DBusGConnection *bus;

  /* call base class method */
  if (parent_object_class->constructed != NULL)
    parent_object_class->constructed (obj);

  /* XXX: overloading the remote pending send flag to check
   * if this is a locally offered stream. The code creating such streams
   * always sets the flag, because the remote end is supposed to decide
   * whether it wants to send.
   * This may look weird during a local hold. However, the pending flag
   * will be harmlessly cleared once the offer-answer is complete. */
  if ((priv->direction & TP_MEDIA_STREAM_DIRECTION_SEND) != 0
      && (priv->pending_send_flags & TP_MEDIA_STREAM_PENDING_REMOTE_SEND) != 0)
    {
      /* Block sending until the stream is remotely accepted */
      priv->pending_remote_receive = TRUE;
    }

  /* go for the bus */
  bus = tp_get_bus ();
  dbus_g_connection_register_g_object (bus, priv->object_path, obj);
}

static void
tpsip_media_stream_get_property (GObject    *object,
			         guint       property_id,
			         GValue     *value,
			         GParamSpec *pspec)
{
  TpsipMediaStream *stream = TPSIP_MEDIA_STREAM (object);
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  switch (property_id)
    {
    case PROP_MEDIA_SESSION:
      g_value_set_object (value, priv->session);
      break;
    case PROP_OBJECT_PATH:
      g_value_set_string (value, priv->object_path);
      break;
    case PROP_ID:
      g_value_set_uint (value, priv->id);
      break;
    case PROP_MEDIA_TYPE:
      g_value_set_uint (value, priv->media_type);
      break;
    case PROP_STATE:
      g_value_set_uint (value, priv->state);
      break;
    case PROP_DIRECTION:
      g_value_set_uint (value, priv->direction);
      break;
    case PROP_PENDING_SEND_FLAGS:
      g_value_set_uint (value, priv->pending_send_flags);
      break;
    case PROP_HOLD_STATE:
      g_value_set_boolean (value, priv->hold_state);
      break;
    case PROP_CREATED_LOCALLY:
      g_value_set_boolean (value, priv->created_locally);
      break;
    case PROP_NAT_TRAVERSAL:
      g_value_set_static_string (value, "none");
      break;
    case PROP_STUN_SERVERS:
      g_return_if_fail (priv->session != NULL);
      g_object_get_property (G_OBJECT (priv->session), "stun-servers", value);
      break;
    case PROP_RELAY_INFO:
      g_value_set_static_boxed (value, tpsip_media_stream_relay_info_empty);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
tpsip_media_stream_set_property (GObject      *object,
			         guint         property_id,
			         const GValue *value,
			         GParamSpec   *pspec)
{
  TpsipMediaStream *stream = TPSIP_MEDIA_STREAM (object);
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  switch (property_id)
    {
    case PROP_MEDIA_SESSION:
      priv->session = g_value_get_object (value);
      break;
    case PROP_OBJECT_PATH:
      g_free (priv->object_path);
      priv->object_path = g_value_dup_string (value);
      break;
    case PROP_ID:
      priv->id = g_value_get_uint (value);
      break;
    case PROP_MEDIA_TYPE:
      priv->media_type = g_value_get_uint (value);
      break;
    case PROP_STATE:
      priv->state = g_value_get_uint (value);
      break;
    case PROP_DIRECTION:
      priv->direction = g_value_get_uint (value);
      break;
    case PROP_PENDING_SEND_FLAGS:
      priv->pending_send_flags = g_value_get_uint (value);
      break;
    case PROP_HOLD_STATE:
      priv->hold_state = g_value_get_boolean (value);
      break;
    case PROP_CREATED_LOCALLY:
      priv->created_locally = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void tpsip_media_stream_dispose (GObject *object);
static void tpsip_media_stream_finalize (GObject *object);

static void
tpsip_media_stream_class_init (TpsipMediaStreamClass *klass)
{
  static TpDBusPropertiesMixinPropImpl stream_handler_props[] = {
      { "CreatedLocally", "created-locally", NULL },
      { "NATTraversal", "nat-traversal", NULL },
      { "STUNServers", "stun-servers", NULL },
      { "RelayInfo", "relay-info", NULL },
      { NULL }
  };

  static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
      { TP_IFACE_MEDIA_STREAM_HANDLER,
        tp_dbus_properties_mixin_getter_gobject_properties,
        NULL,
        stream_handler_props,
      },
      { NULL }
  };

  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GType stream_type = G_OBJECT_CLASS_TYPE (klass);
  GParamSpec *param_spec;

  g_type_class_add_private (klass, sizeof (TpsipMediaStreamPrivate));

  object_class->constructed = tpsip_media_stream_constructed;

  object_class->get_property = tpsip_media_stream_get_property;
  object_class->set_property = tpsip_media_stream_set_property;

  object_class->dispose = tpsip_media_stream_dispose;
  object_class->finalize = tpsip_media_stream_finalize;

  param_spec = g_param_spec_object ("media-session", "TpsipMediaSession object",
      "SIP media session object that owns this media stream object.",
      TPSIP_TYPE_MEDIA_SESSION,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MEDIA_SESSION, param_spec);

  param_spec = g_param_spec_string ("object-path", "D-Bus object path",
      "The D-Bus object path used for this object on the bus.",
      NULL,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_OBJECT_PATH, param_spec);

  param_spec = g_param_spec_uint ("id", "Stream ID",
      "A stream number for the stream used in the D-Bus API.",
      0, G_MAXUINT,
      0,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ID, param_spec);

  param_spec = g_param_spec_uint ("media-type", "Stream media type",
      "A constant indicating which media type the stream carries.",
      TP_MEDIA_STREAM_TYPE_AUDIO, TP_MEDIA_STREAM_TYPE_VIDEO,
      TP_MEDIA_STREAM_TYPE_AUDIO,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MEDIA_TYPE, param_spec);

  param_spec = g_param_spec_uint ("state", "Connection state",
      "Connection state of the media stream",
      TP_MEDIA_STREAM_STATE_DISCONNECTED, TP_MEDIA_STREAM_STATE_CONNECTED,
      TP_MEDIA_STREAM_STATE_DISCONNECTED,
      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_STATE, param_spec);

  /* We don't change the following two as individual properties
   * after construction, use tpsip_media_stream_set_direction() */

  param_spec = g_param_spec_uint ("direction", "Stream direction",
      "A value indicating the current direction of the stream",
      TP_MEDIA_STREAM_DIRECTION_NONE, TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL,
      TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_DIRECTION, param_spec);

  param_spec = g_param_spec_uint ("pending-send-flags", "Pending send flags",
      "Flags indicating the current pending send state of the stream",
      0,
      TP_MEDIA_STREAM_PENDING_LOCAL_SEND | TP_MEDIA_STREAM_PENDING_REMOTE_SEND,
      0,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class,
                                   PROP_PENDING_SEND_FLAGS,
                                   param_spec);

  param_spec = g_param_spec_boolean ("hold-state", "Hold state",
      "Hold state of the media stream as reported by the stream engine",
      FALSE,
      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class,
                                   PROP_HOLD_STATE,
                                   param_spec);

  param_spec = g_param_spec_boolean ("created-locally", "Created locally?",
      "True if this stream was created by the local user", FALSE,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CREATED_LOCALLY,
      param_spec);

  param_spec = g_param_spec_string ("nat-traversal", "NAT traversal",
      "NAT traversal mechanism for this stream", NULL,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_NAT_TRAVERSAL,
      param_spec);

  param_spec = g_param_spec_boxed ("stun-servers", "STUN servers",
      "Array of IP address-port pairs for available STUN servers",
      TP_ARRAY_TYPE_SOCKET_ADDRESS_IP_LIST,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_STUN_SERVERS, param_spec);

  param_spec = g_param_spec_boxed ("relay-info", "Relay info",
      "Array of mappings containing relay server information",
      TP_ARRAY_TYPE_STRING_VARIANT_MAP_LIST,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_RELAY_INFO, param_spec);

  tpsip_media_stream_relay_info_empty = g_ptr_array_new ();

  /* signals not exported by DBus interface */
  signals[SIG_READY] =
    g_signal_new ("ready",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  signals[SIG_SUPPORTED_CODECS] =
    g_signal_new ("supported-codecs",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__UINT,
                  G_TYPE_NONE, 1, G_TYPE_UINT);

  signals[SIG_STATE_CHANGED] =
    g_signal_new ("state-changed",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__UINT,
                  G_TYPE_NONE, 1, G_TYPE_UINT);

  signals[SIG_DIRECTION_CHANGED] =
    g_signal_new ("direction-changed",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  _tpsip_marshal_VOID__UINT_UINT,
                  G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);

  signals[SIG_LOCAL_MEDIA_UPDATED] =
    g_signal_new ("local-media-updated",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  signals[SIG_UNHOLD_FAILURE] =
    g_signal_new ("unhold-failure",
                  stream_type,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  klass->dbus_props_class.interfaces = prop_interfaces;
  tp_dbus_properties_mixin_class_init (object_class,
      G_STRUCT_OFFSET (TpsipMediaStreamClass, dbus_props_class));
}

void
tpsip_media_stream_dispose (GObject *object)
{
  TpsipMediaStream *self = TPSIP_MEDIA_STREAM (object);
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  /* release any references held by the object here */

  if (G_OBJECT_CLASS (tpsip_media_stream_parent_class)->dispose)
    G_OBJECT_CLASS (tpsip_media_stream_parent_class)->dispose (object);

  DEBUG ("exit");
}

void
tpsip_media_stream_finalize (GObject *object)
{
  TpsipMediaStream *self = TPSIP_MEDIA_STREAM (object);
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  /* free any data held directly by the object here */
  g_free (priv->object_path);
  g_free (priv->stream_sdp);

  g_value_unset (&priv->native_codecs);
  g_value_unset (&priv->native_candidates);

  g_free (priv->native_candidate_id);
  g_free (priv->remote_candidate_id);

  G_OBJECT_CLASS (tpsip_media_stream_parent_class)->finalize (object);

  DEBUG ("exit");
}

/***********************************************************************
 * Set: Media.StreamHandler interface implementation (same for 0.12/0.13???)
 ***********************************************************************/

/**
 * tpsip_media_stream_codec_choice
 *
 * Implements DBus method CodecChoice
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_codec_choice (TpSvcMediaStreamHandler *iface,
                               guint codec_id,
                               DBusGMethodInvocation *context)
{
  /* Inform the connection manager of the current codec choice. 
   * -> note: not implemented by tp-gabble either (2006/May) */

  DEBUG ("not implemented (ignoring)");

  tp_svc_media_stream_handler_return_from_codec_choice (context);
}

/**
 * tpsip_media_stream_error
 *
 * Implements DBus method Error
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_error (TpSvcMediaStreamHandler *iface,
                          guint errno,
                          const gchar *message,
                          DBusGMethodInvocation *context)
{
  DEBUG("StreamHandler.Error called: %u %s", errno, message);

  tpsip_media_stream_close (TPSIP_MEDIA_STREAM (iface));

  tp_svc_media_stream_handler_return_from_error (context);
}

/**
 * tpsip_media_stream_native_candidates_prepared
 *
 * Implements DBus method NativeCandidatesPrepared
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_native_candidates_prepared (TpSvcMediaStreamHandler *iface,
                                             DBusGMethodInvocation *context)
{
  /* purpose: "Informs the connection manager that all possible native candisates
   *          have been discovered for the moment." 
   */

  TpsipMediaStream *obj = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);

  DEBUG("enter");

  priv->native_cands_prepared = TRUE;

  if (priv->native_codecs_prepared)
    priv_generate_sdp (obj);

  push_active_candidate_pair (obj);

  tp_svc_media_stream_handler_return_from_native_candidates_prepared (context);
}


/**
 * tpsip_media_stream_new_active_candidate_pair
 *
 * Implements DBus method NewActiveCandidatePair
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_new_active_candidate_pair (TpSvcMediaStreamHandler *iface,
                                            const gchar *native_candidate_id,
                                            const gchar *remote_candidate_id,
                                            DBusGMethodInvocation *context)
{
  TpsipMediaStream *obj = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);

  DEBUG("stream engine reported new active candidate pair %s-%s",
        native_candidate_id, remote_candidate_id);

  if (priv->remote_candidate_id == NULL
      || strcmp (priv->remote_candidate_id, remote_candidate_id))
    {
      GError *err;
      err = g_error_new (TP_ERRORS,
                         TP_ERROR_INVALID_ARGUMENT,
                         "Remote candidate ID does not match the locally "
                                "stored data");
      dbus_g_method_return_error (context, err);
      g_error_free (err);
      return;
    }

  tp_svc_media_stream_handler_return_from_new_active_candidate_pair (context);
}


/**
 * tpsip_media_stream_new_native_candidate
 *
 * Implements DBus method NewNativeCandidate
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_new_native_candidate (TpSvcMediaStreamHandler *iface,
                                       const gchar *candidate_id,
                                       const GPtrArray *transports,
                                       DBusGMethodInvocation *context)
{
  TpsipMediaStream *obj = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;
  GPtrArray *candidates;
  GValue candidate = { 0, };
  GValue transport = { 0, };
  gint tr_goodness;

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);

  if (priv->stream_sdp != NULL)
    {
      MESSAGE ("Stream %u: SDP already generated, ignoring native candidate '%s'", priv->id, candidate_id);
      tp_svc_media_stream_handler_return_from_new_native_candidate (context);
      return;
    }

  g_return_if_fail (transports->len >= 1);

  /* Rate the preferability of the address */
  g_value_init (&transport, TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_TRANSPORT);
  g_value_set_static_boxed (&transport, g_ptr_array_index (transports, 0));
  tr_goodness = tpsip_media_session_rate_native_transport (priv->session,
                                                           &transport);

  candidates = g_value_get_boxed (&priv->native_candidates);

  if (tr_goodness > 0)
    {
      DEBUG("native candidate '%s' is rated as preferable", candidate_id);
      g_free (priv->native_candidate_id);
      priv->native_candidate_id = g_strdup (candidate_id);

      /* Drop the candidates received previously */
      g_value_reset (&priv->native_candidates);
      candidates = dbus_g_type_specialized_construct (
                                TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE_LIST);
      g_value_take_boxed (&priv->native_candidates, candidates);
    }

  g_value_init (&candidate, TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE);
  g_value_take_boxed (&candidate,
      dbus_g_type_specialized_construct (TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE));

  dbus_g_type_struct_set (&candidate,
      0, candidate_id,
      1, transports,
      G_MAXUINT);

  g_ptr_array_add (candidates, g_value_get_boxed (&candidate));

  SESSION_DEBUG(priv->session, "put native candidate '%s' from stream-engine into cache", candidate_id);

  tp_svc_media_stream_handler_return_from_new_native_candidate (context);
}

static void
priv_set_local_codecs (TpsipMediaStream *self,
                       const GPtrArray *codecs)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);
  GValue val = { 0, };

  SESSION_DEBUG(priv->session, "putting list of %d locally supported "
                "codecs from stream-engine into cache", codecs->len);
  g_value_init (&val, TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CODEC_LIST);
  g_value_set_static_boxed (&val, codecs);
  g_value_copy (&val, &priv->native_codecs);

  priv->native_codecs_prepared = TRUE;
  if (priv->native_cands_prepared)
    priv_generate_sdp (self);
}

static void
tpsip_media_stream_codecs_updated (TpSvcMediaStreamHandler *iface,
                                   const GPtrArray *codecs,
                                   DBusGMethodInvocation *context)
{
  TpsipMediaStream *self = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  if (!priv->native_codecs_prepared)
    {
      GError e = { TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
          "CodecsUpdated may not be called before codecs have been provided "
          "with SetLocalCodecs or Ready" };

      SESSION_DEBUG(priv->session,
          "CodecsUpdated called before SetLocalCodecs or Ready");

      dbus_g_method_return_error (context, &e);
    }
  else
    {
      GValue val = { 0, };

      SESSION_DEBUG(priv->session, "putting list of %d locally supported "
          "codecs from CodecsUpdated into cache", codecs->len);
      g_value_init (&val, TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CODEC_LIST);
      g_value_set_static_boxed (&val, codecs);
      g_value_copy (&val, &priv->native_codecs);

      /* This doesn't use priv_generate_sdp because it short-circuits if
       * priv->stream_sdp is already set. We want to update it.
       */
      if (priv->native_cands_prepared)
        priv_update_local_sdp (self);

      tp_svc_media_stream_handler_return_from_codecs_updated (context);
    }
}

/**
 * tpsip_media_stream_ready
 *
 * Implements DBus method Ready
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_ready (TpSvcMediaStreamHandler *iface,
                          const GPtrArray *codecs,
                          DBusGMethodInvocation *context)
{
  /* purpose: "Inform the connection manager that a client is ready to handle
   *          this StreamHandler. Also provide it with info about all supported
   *          codecs."
   *
   * - note, with SIP we don't send the invite just yet (we need
   *   candidates first
   */

  TpsipMediaStream *obj = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;

  DEBUG ("enter");

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);

  if (priv->ready_received)
    {
      MESSAGE ("Ready called more than once");
      tp_svc_media_stream_handler_return_from_ready (context);
      return;
    }

  priv->ready_received = TRUE;

  if (codecs->len != 0)
    priv_set_local_codecs (obj, codecs);

  /* Push the initial sending/playing state */
  tp_svc_media_stream_handler_emit_set_stream_playing (
        iface, priv->playing);
  tp_svc_media_stream_handler_emit_set_stream_sending (
        iface, priv->sending);

  priv->native_codecs_prepared = TRUE;
  if (priv->native_cands_prepared)
    priv_generate_sdp (obj);

  if (priv->push_remote_cands_pending)
    {
      priv->push_remote_cands_pending = FALSE;
      push_remote_candidates (obj);
    }
  if (priv->push_remote_codecs_pending)
    {
      priv->push_remote_codecs_pending = FALSE;
      push_remote_codecs (obj);
    }

  /* note: for inbound sessions, emit active candidate pair once 
           remote info is set */
  push_active_candidate_pair (obj);

  tp_svc_media_stream_handler_return_from_ready (context);
}

static void
tpsip_media_stream_set_local_codecs (TpSvcMediaStreamHandler *iface,
                                     const GPtrArray *codecs,
                                     DBusGMethodInvocation *context)
{
  priv_set_local_codecs (TPSIP_MEDIA_STREAM (iface), codecs);
  tp_svc_media_stream_handler_return_from_set_local_codecs (context);
}

/**
 * tpsip_media_stream_stream_state
 *
 * Implements DBus method StreamState
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_stream_state (TpSvcMediaStreamHandler *iface,
                               guint state,
                               DBusGMethodInvocation *context)
{
  /* purpose: "Informs the connection manager of the stream's current state
   *           State is as specified in *ChannelTypeStreamedMedia::GetStreams."
   *
   * - set the stream state for session
   */

  TpsipMediaStream *obj = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);

  if (priv->state != state)
    {
      DEBUG("changing stream state from %u to %u", priv->state, state);
      priv->state = state;
      g_signal_emit (obj, signals[SIG_STATE_CHANGED], 0, state);
    }

  tp_svc_media_stream_handler_return_from_stream_state (context);
}

/**
 * tpsip_media_stream_supported_codecs
 *
 * Implements DBus method SupportedCodecs
 * on interface org.freedesktop.Telepathy.Media.StreamHandler
 */
static void
tpsip_media_stream_supported_codecs (TpSvcMediaStreamHandler *iface,
                                   const GPtrArray *codecs,
                                   DBusGMethodInvocation *context)
{
  /* purpose: "Inform the connection manager of the supported codecs for this session.
   *          This is called after the connection manager has emitted SetRemoteCodecs
   *          to notify what codecs are supported by the peer, and will thus be an
   *          intersection of all locally supported codecs (passed to Ready)
   *          and those supported by the peer."
   *
   * - emit SupportedCodecs
   */ 

  TpsipMediaStream *self = TPSIP_MEDIA_STREAM (iface);
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  DEBUG("got codec intersection containing %u codecs from stream-engine",
        codecs->len);

  /* Uncomment the line below if there's need to limit the local codec list
   * with the intersection for later SDP negotiations.
   * TODO: Make sure to update the SDP for the stream as well. */
  /* g_value_set_boxed (&priv->native_codecs, codecs); */

  if (priv->codec_intersect_pending)
    {
      if (priv->push_remote_codecs_pending)
        {
          /* The remote codec list has been updated since the intersection
           * has started, plunge into a new intersection immediately */
          priv->push_remote_codecs_pending = FALSE;
          push_remote_codecs (self);
        }
      else
        {
          priv->codec_intersect_pending = FALSE;
          g_signal_emit (self, signals[SIG_SUPPORTED_CODECS], 0, codecs->len);
        }
    }
  else
    WARNING("SupportedCodecs called when no intersection is ongoing");

  tp_svc_media_stream_handler_return_from_supported_codecs (context);
}

static void
tpsip_media_stream_hold_state (TpSvcMediaStreamHandler *self,
                               gboolean held,
                               DBusGMethodInvocation *context)
{
  g_object_set (self, "hold-state", held, NULL);
  tp_svc_media_stream_handler_return_from_hold_state (context);
}

static void
tpsip_media_stream_unhold_failure (TpSvcMediaStreamHandler *self,
                                   DBusGMethodInvocation *context)
{
  /* Not doing anything to hold_state or requested_hold_state,
   * because the session is going to put all streams on hold after getting
   * the signal below */

  g_signal_emit (self, signals[SIG_UNHOLD_FAILURE], 0);
  tp_svc_media_stream_handler_return_from_unhold_failure (context);
}

/***********************************************************************
 * Helper functions follow (not based on generated templates)
 ***********************************************************************/

guint
tpsip_media_stream_get_id (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);
  return priv->id;
}

guint
tpsip_media_stream_get_media_type (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);
  return priv->media_type;
}

void
tpsip_media_stream_close (TpsipMediaStream *self)
{
  tp_svc_media_stream_handler_emit_close (self);
}

/**
 * Described the local stream configuration in SDP (RFC2327),
 * or NULL if stream not configured yet.
 */
const char *tpsip_media_stream_local_sdp (TpsipMediaStream *obj)
{
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (obj);
  return priv->stream_sdp;
}

TpMediaStreamDirection
tpsip_media_stream_direction_from_remote_media (const sdp_media_t *media)
{
  sdp_mode_t mode = media->m_mode;
  return ((mode & sdp_recvonly)? TP_MEDIA_STREAM_DIRECTION_SEND : 0)
       | ((mode & sdp_sendonly)? TP_MEDIA_STREAM_DIRECTION_RECEIVE : 0);
}

static gboolean
tpsip_sdp_codecs_differ (const sdp_rtpmap_t *m1, const sdp_rtpmap_t *m2)
{
  while (m1 != NULL && m2 != NULL)
    {
      if (sdp_rtpmap_cmp (m1, m2) != 0)
        return TRUE;
      m1 = m1->rm_next;
      m2 = m2->rm_next;
    }
  return m1 != NULL || m2 != NULL;
}

/*
 * Returns stream direction as requested by the latest local or remote
 * direction change.
 */
static TpMediaStreamDirection
priv_get_requested_direction (TpsipMediaStreamPrivate *priv)
{
  TpMediaStreamDirection direction;

  direction = priv->direction;
  if ((priv->pending_send_flags & TP_MEDIA_STREAM_PENDING_LOCAL_SEND) != 0)
    direction |= TP_MEDIA_STREAM_DIRECTION_SEND;
  return direction;
}

/** 
 * Sets the remote candidates and codecs for this stream, as 
 * received via signaling.
 * 
 * Parses the SDP information, updates TP remote candidates and
 * codecs if the client is ready.
 * 
 * Note that the pointer to the media description structure is saved,
 * implying that the structure shall not go away for the lifetime of
 * the stream, preferably kept in the memory home attached to
 * the session object.
 *
 * @return TRUE if the remote information has been accepted,
 *         FALSE if the update is not acceptable.
 */
gboolean
tpsip_media_stream_set_remote_media (TpsipMediaStream *stream,
                                     const sdp_media_t *new_media,
                                     guint direction_up_mask,
                                     guint pending_send_mask)
{
  TpsipMediaStreamPrivate *priv;
  sdp_connection_t *sdp_conn;
  const sdp_media_t *old_media;
  gboolean transport_changed = TRUE;
  gboolean codecs_changed = TRUE;
  guint old_direction;
  guint new_direction;

  DEBUG ("enter");

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  /* Do sanity checks */

  g_return_val_if_fail (new_media != NULL, FALSE);

  if (new_media->m_rejected || new_media->m_port == 0)
    {
      DEBUG("the stream is rejected remotely");
      return FALSE;
    }

  if (new_media->m_proto != sdp_proto_rtp)
    {
      WARNING ("Stream %u: the remote protocol is not RTP/AVP", priv->id);
      return FALSE;
    }

  sdp_conn = sdp_media_connections (new_media);
  if (sdp_conn == NULL)
    {
      WARNING ("Stream %u: no valid remote connections", priv->id);
      return FALSE;
    }

  if (new_media->m_rtpmaps == NULL)
    {
      WARNING ("Stream %u: no remote codecs", priv->id);
      return FALSE;
    }

  /* Note: always update the pointer to the current media structure
   * because of memory management done in the session object */
  old_media = priv->remote_media;
  priv->remote_media = new_media;

  /* Check if there was any media update at all */

  if (sdp_media_cmp (old_media, new_media) == 0)
    {
      DEBUG("no media changes detected for the stream");
      return TRUE;
    }

  old_direction = priv_get_requested_direction (priv);
  new_direction = tpsip_media_stream_direction_from_remote_media (new_media);

  /* Make sure the peer can only enable sending or receiving direction
   * if it's allowed to */
  new_direction &= old_direction | direction_up_mask;

  if (old_media != NULL)
    {
      /* Check if the transport candidate needs to be changed */
      if (!sdp_connection_cmp (sdp_media_connections (old_media), sdp_conn))
        transport_changed = FALSE;

      /* Check if the codec list needs to be updated */
      codecs_changed = tpsip_sdp_codecs_differ (old_media->m_rtpmaps,
                                                new_media->m_rtpmaps);

      /* Disable sending at this point if it will be disabled
       * accordingly to the new direction */
      priv_update_sending (stream,
                           priv->direction & new_direction);
    }

  /* First add the new candidate, then update the codec set.
   * The offerer isn't supposed to send us anything from the new transport
   * until we accept; if it's the answer, both orderings have problems. */

  if (transport_changed)
    {
      /* Make sure we stop sending before we use the new set of codecs
       * intended for the new connection */
      if (codecs_changed) 
        tpsip_media_stream_set_sending (stream, FALSE);

      push_remote_candidates (stream);
    }

  if (codecs_changed)
    {
      if (!priv->codec_intersect_pending)
        {
          priv->codec_intersect_pending = TRUE;
          push_remote_codecs (stream);
        }
      else
        {
          priv->push_remote_codecs_pending = TRUE;
        }
    }

  /* TODO: this will go to session change commit code */

  /* note: for outbound sessions (for which remote cands become
   *       available at a later stage), emit active candidate pair 
   *       (and playing status?) once remote info set */
  push_active_candidate_pair (stream);

  /* Set the final direction and update pending send flags */
  tpsip_media_stream_set_direction (stream,
                                    new_direction,
                                    pending_send_mask);

  return TRUE;
}

/**
 * Converts a sofia-sip media type enum to Telepathy media type.
 * See <sofia-sip/sdp.h> and <telepathy-constants.h>.
 *
 * @return G_MAXUINT if the media type cannot be mapped
 */
guint
tpsip_tp_media_type (sdp_media_e sip_mtype)
{
  switch (sip_mtype)
    {
      case sdp_media_audio: return TP_MEDIA_STREAM_TYPE_AUDIO;
      case sdp_media_video: return TP_MEDIA_STREAM_TYPE_VIDEO; 
      default: return G_MAXUINT;
    }
}

/**
 * Sets the media state to playing or non-playing. When not playing,
 * received RTP packets may not be played locally.
 */
void tpsip_media_stream_set_playing (TpsipMediaStream *stream, gboolean playing)
{
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  if (same_boolean (priv->playing, playing))
    return;

  DEBUG("set playing to %s", playing? "TRUE" : "FALSE");

  priv->playing = playing;

  if (priv->ready_received)
    tp_svc_media_stream_handler_emit_set_stream_playing (
        (TpSvcMediaStreamHandler *)stream, playing);
}

/**
 * Sets the media state to sending or non-sending. When not sending,
 * captured media are not sent over the network.
 */
void
tpsip_media_stream_set_sending (TpsipMediaStream *stream, gboolean sending)
{
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  if (same_boolean(priv->sending, sending))
    return;

  DEBUG("set sending to %s", sending? "TRUE" : "FALSE");

  priv->sending = sending;

  if (priv->ready_received)
    tp_svc_media_stream_handler_emit_set_stream_sending (
        (TpSvcMediaStreamHandler *)stream, sending);
}

static void
priv_update_sending (TpsipMediaStream *stream,
                     TpMediaStreamDirection direction)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);
  gboolean sending = TRUE;

  /* XXX: the pending send flag check is probably an overkill
   * considering that effective sending direction and pending send should be
   * mutually exclusive */
  if ((direction & TP_MEDIA_STREAM_DIRECTION_SEND) == 0
      || priv->pending_remote_receive
      || (priv->pending_send_flags & TP_MEDIA_STREAM_PENDING_LOCAL_SEND) != 0
      || !tpsip_media_session_is_accepted (priv->session))
    {
      sending = FALSE;
    }

  tpsip_media_stream_set_sending (stream, sending);
}

void
tpsip_media_stream_set_direction (TpsipMediaStream *stream,
                                  TpMediaStreamDirection direction,
                                  guint pending_send_mask)
{
  TpsipMediaStreamPrivate *priv;
  guint pending_send_flags;
  TpMediaStreamDirection old_sdp_direction;

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);
  pending_send_flags = priv->pending_send_flags & pending_send_mask;

  if ((direction & ~priv->direction & TP_MEDIA_STREAM_DIRECTION_SEND) != 0)
    {
      /* We are requested to start sending, but... */
      if ((pending_send_mask
            & TP_MEDIA_STREAM_PENDING_LOCAL_SEND) != 0)
        {
          /* ... but we need to confirm this with the client.
           * Clear the sending bit and set the pending send flag. */
          direction &= ~(guint)TP_MEDIA_STREAM_DIRECTION_SEND;
          pending_send_flags |= TP_MEDIA_STREAM_PENDING_LOCAL_SEND;
        }
      if ((pending_send_mask
              & TP_MEDIA_STREAM_PENDING_REMOTE_SEND) != 0
          && (priv->pending_send_flags
              & TP_MEDIA_STREAM_PENDING_LOCAL_SEND) == 0)
        {
          g_assert ((priv_get_requested_direction (priv) & TP_MEDIA_STREAM_DIRECTION_SEND) == 0);

          /* ... but the caller wants to agree with the remote
           * end first. Block the stream handler from sending for now. */
          priv->pending_remote_receive = TRUE;
        }
    }
  if ((direction & ~priv->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE) != 0
      && (pending_send_mask
          & TP_MEDIA_STREAM_PENDING_REMOTE_SEND) != 0)
    {
      /* We're requested to start receiving, but the remote end did not
       * confirm if it will send. Set the pending send flag. */
      pending_send_flags |= TP_MEDIA_STREAM_PENDING_REMOTE_SEND;
    }

  if (priv->direction == direction
      && priv->pending_send_flags == pending_send_flags)
    return;

  old_sdp_direction = priv_get_requested_direction (priv);

  priv->direction = direction;
  priv->pending_send_flags = pending_send_flags;

  DEBUG("set direction %u, pending send flags %u", priv->direction, priv->pending_send_flags);

  g_signal_emit (stream, signals[SIG_DIRECTION_CHANGED], 0,
                 priv->direction, priv->pending_send_flags);

  if (priv->remote_media != NULL)
    priv_update_sending (stream, priv->direction);

  if (priv->native_cands_prepared
      && priv->native_codecs_prepared
      && priv_get_requested_direction (priv)
         != old_sdp_direction)
    priv_update_local_sdp (stream);
}

/*
 * Clears the pending send flag(s) present in @pending_send_mask.
 * If #TP_MEDIA_STREAM_PENDING_LOCAL_SEND is thus cleared,
 * enable the sending bit in the stream direction.
 * If @pending_send_mask has #TP_MEDIA_STREAM_PENDING_REMOTE_SEND flag set,
 * also start sending if agreed by the stream direction.
 */
void
tpsip_media_stream_apply_pending_direction (TpsipMediaStream *stream,
                                            guint pending_send_mask)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);
  guint flags;


  /* Don't apply pending send for new streams that haven't been negotiated */
  if (priv->remote_media == NULL)
    return;

  /* Remember the flags that got changes and then clear the set */
  flags = (priv->pending_send_flags & pending_send_mask);
  priv->pending_send_flags &= ~pending_send_mask;

  if (flags != 0)
    {
      if ((flags & TP_MEDIA_STREAM_PENDING_LOCAL_SEND) != 0)
        priv->direction |= TP_MEDIA_STREAM_DIRECTION_SEND;

      DEBUG("set direction %u, pending send flags %u", priv->direction, priv->pending_send_flags);

      g_signal_emit (stream, signals[SIG_DIRECTION_CHANGED], 0,
                     priv->direction, priv->pending_send_flags);
    }

  if ((pending_send_mask & TP_MEDIA_STREAM_PENDING_REMOTE_SEND) != 0)
    {
      priv->pending_remote_receive = FALSE;
      DEBUG("remote end ready to receive");
    }

  /* Always check to enable sending because the session could become accepted */
  priv_update_sending (stream, priv->direction);
}

TpMediaStreamDirection
tpsip_media_stream_get_requested_direction (TpsipMediaStream *self)
{
  return priv_get_requested_direction (TPSIP_MEDIA_STREAM_GET_PRIVATE (self));
}

/**
 * Returns true if the stream has a valid SDP description and
 * connection has been established with the stream engine.
 */
gboolean tpsip_media_stream_is_local_ready (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv;
  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);
  g_assert (priv->stream_sdp == NULL || priv->ready_received);
  return (priv->stream_sdp != NULL);
}

gboolean
tpsip_media_stream_is_codec_intersect_pending (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);
  return priv->codec_intersect_pending;
}

void
tpsip_media_stream_start_telephony_event (TpsipMediaStream *self, guchar event)
{
  tp_svc_media_stream_handler_emit_start_telephony_event (
        (TpSvcMediaStreamHandler *)self, event);
}

void
tpsip_media_stream_stop_telephony_event  (TpsipMediaStream *self)
{
  tp_svc_media_stream_handler_emit_stop_telephony_event (
        (TpSvcMediaStreamHandler *)self);
}

gboolean
tpsip_media_stream_request_hold_state (TpsipMediaStream *self, gboolean hold)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  if ((!priv->requested_hold_state) != (!hold))
    {
      priv->requested_hold_state = hold;
      tp_svc_media_stream_handler_emit_set_stream_held (self, hold);
      return TRUE;
    }
  return FALSE;
}

static void
priv_generate_sdp (TpsipMediaStream *self)
{
  TpsipMediaStreamPrivate *priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (self);

  if (priv->stream_sdp != NULL)
    return;

  priv_update_local_sdp (self);

  g_assert (priv->stream_sdp != NULL);

  g_signal_emit (self, signals[SIG_READY], 0);
}

/**
 * Notify StreamEngine of remote codecs.
 *
 * @pre Ready signal must be receiveid (priv->ready_received)
 */
static void push_remote_codecs (TpsipMediaStream *stream)
{
  TpsipMediaStreamPrivate *priv;
  GPtrArray *codecs;
  GHashTable *opt_params;
  GType codecs_type;
  GType codec_type;
  const sdp_media_t *sdpmedia;
  const sdp_rtpmap_t *rtpmap;
  gchar *ptime = NULL;
  gchar *max_ptime = NULL;

  DEBUG ("enter");

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  sdpmedia = priv->remote_media; 
  if (sdpmedia == NULL)
    {
      DEBUG("remote media description is not received yet");
      return;
    }

  if (!priv->ready_received)
    {
      DEBUG("the stream engine is not ready, SetRemoteCodecs is pending");
      priv->push_remote_codecs_pending = TRUE;
      return;
    }

  ptime = tpsip_sdp_get_string_attribute (sdpmedia->m_attributes, "ptime");
  if (ptime == NULL)
    {
      g_object_get (priv->session,
          "remote-ptime", &ptime,
          NULL);
    }
  max_ptime = tpsip_sdp_get_string_attribute (sdpmedia->m_attributes, "maxptime");
  if (max_ptime == NULL)
    {
      g_object_get (priv->session,
          "remote-max-ptime", &max_ptime,
          NULL);
    }

  codec_type = TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_CODEC;
  codecs_type = TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CODEC_LIST;

  codecs = dbus_g_type_specialized_construct (codecs_type);
  opt_params = g_hash_table_new_full (g_str_hash,
                                      g_str_equal,
                                      g_free,
                                      g_free);

  rtpmap = sdpmedia->m_rtpmaps;
  while (rtpmap)
    {
      GValue codec = { 0, };

      g_value_init (&codec, codec_type);
      g_value_take_boxed (&codec,
                          dbus_g_type_specialized_construct (codec_type));

      if (ptime != NULL)
        g_hash_table_insert (opt_params,
            g_strdup("ptime"), g_strdup (ptime));
      if (max_ptime != NULL)
        g_hash_table_insert (opt_params,
            g_strdup("maxptime"), g_strdup (max_ptime));

      tpsip_codec_param_parse (priv->media_type, rtpmap->rm_encoding,
          rtpmap->rm_fmtp, opt_params);

      /* RFC2327: see "m=" line definition 
       *  - note, 'encoding_params' is assumed to be channel
       *    count (i.e. channels in farsight) */

      dbus_g_type_struct_set (&codec,
                              /* payload type: */
                              0, rtpmap->rm_pt,
                              /* encoding name: */
                              1, rtpmap->rm_encoding,
                              /* media type */
                              2, (guint)priv->media_type,
                              /* clock-rate */
                              3, rtpmap->rm_rate,
                              /* number of supported channels: */
                              4, rtpmap->rm_params ? atoi(rtpmap->rm_params) : 0,
                              /* optional params: */
                              5, opt_params,
                              G_MAXUINT);

      g_hash_table_remove_all (opt_params);

      g_ptr_array_add (codecs, g_value_get_boxed (&codec));

      rtpmap = rtpmap->rm_next;
    }

  g_hash_table_destroy (opt_params);
  g_free (ptime);
  g_free (max_ptime);

  SESSION_DEBUG(priv->session, "passing %d remote codecs to stream engine",
                codecs->len);

  tp_svc_media_stream_handler_emit_set_remote_codecs (
        (TpSvcMediaStreamHandler *)stream, codecs);

  g_boxed_free (codecs_type, codecs);
}

static void push_remote_candidates (TpsipMediaStream *stream)
{
  TpsipMediaStreamPrivate *priv;
  GValue candidate = { 0 };
  GValue transport = { 0 };
  GValue transport_rtcp = { 0 };
  GPtrArray *candidates;
  GPtrArray *transports;
  GType candidate_type;
  GType candidates_type;
  GType transport_type;
  GType transports_type;
  const sdp_media_t *media;
  const sdp_connection_t *sdp_conn;
  gchar *candidate_id;
  guint port;

  DEBUG("enter");

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  media = priv->remote_media; 
  if (media == NULL)
    {
      DEBUG("remote media description is not received yet");
      return;
    }

  if (!priv->ready_received)
    {
      DEBUG("the stream engine is not ready, SetRemoteCandidateList is pending");
      priv->push_remote_cands_pending = TRUE;
      return;
    }

  /* use the address from SDP c-line as the only remote candidate */

  sdp_conn = sdp_media_connections (media);
  g_return_if_fail (sdp_conn != NULL);

  port = (guint) media->m_port;

  transports_type = TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_TRANSPORT_LIST;
  transports = dbus_g_type_specialized_construct (transports_type);

  transport_type = TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_TRANSPORT;
  g_value_init (&transport, transport_type);
  g_value_take_boxed (&transport,
                      dbus_g_type_specialized_construct (transport_type));
  dbus_g_type_struct_set (&transport,
                          0, 1,         /* component number */
                          1, sdp_conn->c_address,
                          2, port,
                          3, TP_MEDIA_STREAM_BASE_PROTO_UDP,
                          4, "RTP",
                          5, "AVP",
                          /* 6, 0.0f, */
                          7, TP_MEDIA_STREAM_TRANSPORT_TYPE_LOCAL,
                          /* 8, "", */
                          /* 9, "", */
                          G_MAXUINT);

  DEBUG("remote RTP address=<%s>, port=<%u>", sdp_conn->c_address, port);
  g_ptr_array_add (transports, g_value_get_boxed (&transport));

  if (!tpsip_sdp_rtcp_bandwidth_throttled (media->m_bandwidths))
    {
      gboolean session_rtcp_enabled = TRUE;
      g_object_get (priv->session,
                    "rtcp-enabled", &session_rtcp_enabled,
                    NULL);
      if (session_rtcp_enabled)
        {
          const sdp_attribute_t *rtcp_attr;
          const char *rtcp_address;
          guint rtcp_port;

          /* Get the port and optional address for RTCP accordingly to RFC 3605 */
          rtcp_address = sdp_conn->c_address;
          rtcp_attr = sdp_attribute_find (media->m_attributes, "rtcp");
          if (rtcp_attr == NULL || rtcp_attr->a_value == NULL)
            {
              rtcp_port = port + 1;
            }
          else
            {
              const char *rest;
              rtcp_port = (guint) g_ascii_strtoull (rtcp_attr->a_value,
                                                    (gchar **) &rest,
                                                    10);
              if (rtcp_port != 0
                  && (strncmp (rest, " IN IP4 ", 8) == 0
                      || strncmp (rest, " IN IP6 ", 8) == 0))
                rtcp_address = rest + 8;
            }

          g_value_init (&transport_rtcp, transport_type);
          g_value_take_boxed (&transport_rtcp,
                              dbus_g_type_specialized_construct (transport_type));
          dbus_g_type_struct_set (&transport_rtcp,
                                  0, 2,         /* component number */
                                  1, rtcp_address,
                                  2, rtcp_port,
                                  3, TP_MEDIA_STREAM_BASE_PROTO_UDP,
                                  4, "RTCP",
                                  5, "AVP",
                                  /* 6, 0.0f, */
                                  7, TP_MEDIA_STREAM_TRANSPORT_TYPE_LOCAL,
                                  /* 8, "", */
                                  /* 9, "", */
                                  G_MAXUINT);

          DEBUG("remote RTCP address=<%s>, port=<%u>", rtcp_address, rtcp_port);
          g_ptr_array_add (transports, g_value_get_boxed (&transport_rtcp));
        }
    }

  g_free (priv->remote_candidate_id);
  candidate_id = g_strdup_printf ("L%u", ++priv->remote_candidate_counter);
  priv->remote_candidate_id = candidate_id;

  candidate_type = TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE;
  g_value_init (&candidate, candidate_type);
  g_value_take_boxed (&candidate,
                      dbus_g_type_specialized_construct (candidate_type));
  dbus_g_type_struct_set (&candidate,
      0, candidate_id,
      1, transports,
      G_MAXUINT);

  candidates_type = TP_ARRAY_TYPE_MEDIA_STREAM_HANDLER_CANDIDATE_LIST;
  candidates = dbus_g_type_specialized_construct (candidates_type);
  g_ptr_array_add (candidates, g_value_get_boxed (&candidate));

  DEBUG("emitting SetRemoteCandidateList with %s", candidate_id);

  tp_svc_media_stream_handler_emit_set_remote_candidate_list (
          (TpSvcMediaStreamHandler *)stream, candidates);

  g_boxed_free (candidates_type, candidates);
  g_boxed_free (transports_type, transports);
}

static void
push_active_candidate_pair (TpsipMediaStream *stream)
{
  TpsipMediaStreamPrivate *priv;

  DEBUG("enter");

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  if (priv->ready_received
      && priv->native_candidate_id != NULL
      && priv->remote_candidate_id != NULL)
    {
      DEBUG("emitting SetActiveCandidatePair for %s-%s",
            priv->native_candidate_id, priv->remote_candidate_id);
      tp_svc_media_stream_handler_emit_set_active_candidate_pair (
                stream, priv->native_candidate_id, priv->remote_candidate_id);
    }
}

static const char* priv_media_type_to_str(guint media_type)
{
switch (media_type)
  {
  case TP_MEDIA_STREAM_TYPE_AUDIO: return "audio";
  case TP_MEDIA_STREAM_TYPE_VIDEO: return "video";
  default: g_assert_not_reached ();
    ;
  }
return "-";
}

static void
priv_append_rtpmaps (const GPtrArray *codecs, GString *mline, GString *alines)
{
  GValue codec = { 0, };
  gchar *co_name = NULL;
  guint co_id;
  guint co_type;
  guint co_clockrate;
  guint co_channels;
  GHashTable *co_params = NULL;
  guint i;

  g_value_init (&codec, TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_CODEC);

  for (i = 0; i < codecs->len; i++)
    {
      g_value_set_static_boxed (&codec, g_ptr_array_index (codecs, i));

      dbus_g_type_struct_get (&codec,
                              0, &co_id,
                              1, &co_name,
                              2, &co_type,
                              3, &co_clockrate,
                              4, &co_channels,
                              5, &co_params,
                              G_MAXUINT);

      /* g_return_if_fail (co_type == priv->media_type); */

      /* Add rtpmap entry to the a= lines */
      g_string_append_printf (alines,
                              "a=rtpmap:%u %s/%u",
                              co_id,
                              co_name,
                              co_clockrate);
      if (co_channels > 1)
        g_string_append_printf (alines, "/%u", co_channels);
      g_string_append (alines, "\r\n");

      /* Marshal parameters into the fmtp attribute */
      if (g_hash_table_size (co_params) != 0)
        {
          GString *fmtp_value;
          g_string_append_printf (alines, "a=fmtp:%u ", co_id);
          fmtp_value = g_string_new (NULL);
          tpsip_codec_param_format (co_type, co_name,
              co_params, fmtp_value);
          g_string_append (alines, fmtp_value->str);
          g_string_free (fmtp_value, TRUE);
          g_string_append (alines, "\r\n");
        }

      /* Add PT id to the m= line */
      g_string_append_printf (mline, " %u", co_id);

      g_free (co_name);
      co_name = NULL;
      g_hash_table_destroy (co_params);
      co_params = NULL;
    }
}

/**
* Refreshes the local SDP based on Farsight stream, and current
* object, state.
*/
static void
priv_update_local_sdp(TpsipMediaStream *stream)
{
  TpsipMediaStreamPrivate *priv;
  GString *mline;
  GString *alines;
  gchar *cline;
  GValue transport = { 0 };
  const GPtrArray *candidates;
  gchar *tr_addr = NULL;
  /* gchar *tr_user = NULL; */
  /* gchar *tr_pass = NULL; */
  gchar *tr_subtype = NULL;
  gchar *tr_profile = NULL;
  guint tr_port;
  guint tr_component;
  /* guint tr_type; */
  /* gdouble tr_pref; */
  guint rtcp_port = 0;
  gchar *rtcp_address = NULL;
  const gchar *dirline;
  int i;

  priv = TPSIP_MEDIA_STREAM_GET_PRIVATE (stream);

  candidates = g_value_get_boxed (&priv->native_candidates);

  g_value_init (&transport, TP_STRUCT_TYPE_MEDIA_STREAM_HANDLER_TRANSPORT);

  /* Find the preferred candidate, if defined,
   * else the last acceptable candidate */

  for (i = candidates->len - 1; i >= 0; --i)
    {
      GValueArray *candidate;
      const gchar *candidate_id;
      const GPtrArray *ca_tports;
      guint tr_proto = TP_MEDIA_STREAM_BASE_PROTO_UDP;
      guint j;

      candidate = g_ptr_array_index (candidates, i);
      candidate_id =
                g_value_get_string (g_value_array_get_nth (candidate, 0));
      ca_tports = g_value_get_boxed (g_value_array_get_nth (candidate, 1));

      if (ca_tports->len == 0)
        {
          WARNING ("candidate '%s' lists no transports, skipping", candidate_id);
          continue;
        }

      for (j = 0; j < ca_tports->len; j++)
        {
          g_value_set_static_boxed (&transport,
                                    g_ptr_array_index (ca_tports, j));
          dbus_g_type_struct_get (&transport,
                                  0, &tr_component,
                                  G_MAXUINT);
          switch (tr_component)
            {
            case 1:     /* RTP */
              dbus_g_type_struct_get (&transport,
                                      1, &tr_addr,
                                      2, &tr_port,
                                      3, &tr_proto,
                                      4, &tr_subtype,
                                      5, &tr_profile,
                                      /* 6, &tr_pref, */
                                      /* 7, &tr_type, */
                                      /* 8, &tr_user, */
                                      /* 9, &tr_pass, */
                                      G_MAXUINT);
              break;
            case 2:     /* RTCP */
              dbus_g_type_struct_get (&transport,
                                      1, &rtcp_address,
                                      2, &rtcp_port,
                                      G_MAXUINT);
              break;
            }
        }

      if (priv->native_candidate_id != NULL)
        {
          if (!strcmp (candidate_id, priv->native_candidate_id))
            break;
        }
      else if (tr_proto == TP_MEDIA_STREAM_BASE_PROTO_UDP)
        {
          g_free (priv->native_candidate_id);
          priv->native_candidate_id = g_strdup (candidate_id);
          break;
        }
    }
  g_return_if_fail (i >= 0);
  g_return_if_fail (tr_addr != NULL);
  g_return_if_fail (tr_subtype != NULL);
  g_return_if_fail (tr_profile != NULL);

  mline = g_string_new ("m=");
  g_string_append_printf (mline,
                          "%s %u %s/%s",
                          priv_media_type_to_str (priv->media_type),
                          tr_port,
                          tr_subtype,
                          tr_profile);

  cline = g_strdup_printf ("c=IN %s %s\r\n",
                           (strchr (tr_addr, ':') == NULL)? "IP4" : "IP6",
                           tr_addr);

  switch (priv_get_requested_direction (priv))
    {
    case TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL:
      dirline = "";
      break;
    case TP_MEDIA_STREAM_DIRECTION_SEND:
      dirline = "a=sendonly\r\n";
      break;
    case TP_MEDIA_STREAM_DIRECTION_RECEIVE:
      dirline = "a=recvonly\r\n";
      break;
    case TP_MEDIA_STREAM_DIRECTION_NONE:
      dirline = "a=inactive\r\n";
      break;
    default:
      g_assert_not_reached();
    }

  alines = g_string_new (dirline);

  if (rtcp_address != NULL)
    {
      /* Add RTCP attribute as per RFC 3605 */
      if (strcmp (rtcp_address, tr_addr) != 0)
        {
          g_string_append_printf (alines,
                                  "a=rtcp:%u IN %s %s\r\n",
                                  rtcp_port,
                                  (strchr (rtcp_address, ':') == NULL)
                                        ? "IP4" : "IP6",
                                  rtcp_address);
        }
      else if (rtcp_port != tr_port + 1)
        {
          g_string_append_printf (alines,
                                  "a=rtcp:%u\r\n",
                                  rtcp_port);
        }
    }

  priv_append_rtpmaps (g_value_get_boxed (&priv->native_codecs),
                       mline, alines);

  g_free(priv->stream_sdp);
  priv->stream_sdp = g_strconcat(mline->str, "\r\n",
                                 cline,
                                 alines->str,
                                 NULL);

  g_free (tr_addr);
  g_free (tr_profile);
  g_free (tr_subtype);
  /* g_free (tr_user); */
  /* g_free (tr_pass); */
  g_free (rtcp_address);

  g_string_free (mline, TRUE);
  g_free (cline);
  g_string_free (alines, TRUE);

  g_signal_emit (stream, signals[SIG_LOCAL_MEDIA_UPDATED], 0);
}

static void
stream_handler_iface_init (gpointer g_iface, gpointer iface_data)
{
  TpSvcMediaStreamHandlerClass *klass = (TpSvcMediaStreamHandlerClass *)g_iface;

#define IMPLEMENT(x) tp_svc_media_stream_handler_implement_##x (\
    klass, (tp_svc_media_stream_handler_##x##_impl) tpsip_media_stream_##x)
  IMPLEMENT(codec_choice);
  IMPLEMENT(error);
  IMPLEMENT(native_candidates_prepared);
  IMPLEMENT(new_active_candidate_pair);
  IMPLEMENT(new_native_candidate);
  IMPLEMENT(ready);
  IMPLEMENT(set_local_codecs);
  IMPLEMENT(codecs_updated);
  IMPLEMENT(stream_state);
  IMPLEMENT(supported_codecs);
  IMPLEMENT(hold_state);
  IMPLEMENT(unhold_failure);
#undef IMPLEMENT
}