QXmppJingleIq.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  6. *
  7. * Source:
  8. * http://code.google.com/p/qxmpp
  9. *
  10. * This file is a part of QXmpp library.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. */
  23. #include <QDomElement>
  24. #include "QXmppConstants.h"
  25. #include "QXmppJingleIq.h"
  26. #include "QXmppUtils.h"
  27. static const char* ns_jingle_rtp_info = "urn:xmpp:jingle:apps:rtp:info:1";
  28. static const char* jingle_actions[] = {
  29. "content-accept",
  30. "content-add",
  31. "content-modify",
  32. "content-reject",
  33. "content-remove",
  34. "description-info",
  35. "security-info",
  36. "session-accept",
  37. "session-info",
  38. "session-initiate",
  39. "session-terminate",
  40. "transport-accept",
  41. "transport-info",
  42. "transport-reject",
  43. "transport-replace",
  44. };
  45. static const char* jingle_reasons[] = {
  46. "",
  47. "alternative-session",
  48. "busy",
  49. "cancel",
  50. "connectivity-error",
  51. "decline",
  52. "expired",
  53. "failed-application",
  54. "failed-transport",
  55. "general-error",
  56. "gone",
  57. "incompatible-parameters",
  58. "media-error",
  59. "security-error",
  60. "success",
  61. "timeout",
  62. "unsupported-applications",
  63. "unsupported-transports",
  64. };
  65. QXmppJingleIq::Content::Content()
  66. {
  67. }
  68. QString QXmppJingleIq::Content::creator() const
  69. {
  70. return m_creator;
  71. }
  72. void QXmppJingleIq::Content::setCreator(const QString &creator)
  73. {
  74. m_creator = creator;
  75. }
  76. QString QXmppJingleIq::Content::name() const
  77. {
  78. return m_name;
  79. }
  80. void QXmppJingleIq::Content::setName(const QString &name)
  81. {
  82. m_name = name;
  83. }
  84. QString QXmppJingleIq::Content::senders() const
  85. {
  86. return m_senders;
  87. }
  88. void QXmppJingleIq::Content::setSenders(const QString &senders)
  89. {
  90. m_senders = senders;
  91. }
  92. QString QXmppJingleIq::Content::descriptionMedia() const
  93. {
  94. return m_descriptionMedia;
  95. }
  96. void QXmppJingleIq::Content::setDescriptionMedia(const QString &media)
  97. {
  98. m_descriptionMedia = media;
  99. }
  100. void QXmppJingleIq::Content::addPayloadType(const QXmppJinglePayloadType &payload)
  101. {
  102. m_descriptionType = ns_jingle_rtp;
  103. m_payloadTypes << payload;
  104. }
  105. QList<QXmppJinglePayloadType> QXmppJingleIq::Content::payloadTypes() const
  106. {
  107. return m_payloadTypes;
  108. }
  109. void QXmppJingleIq::Content::setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes)
  110. {
  111. m_descriptionType = payloadTypes.isEmpty() ? QString() : ns_jingle_rtp;
  112. m_payloadTypes = payloadTypes;
  113. }
  114. void QXmppJingleIq::Content::addTransportCandidate(const QXmppJingleCandidate &candidate)
  115. {
  116. m_transportType = ns_jingle_ice_udp;
  117. m_transportCandidates << candidate;
  118. }
  119. QList<QXmppJingleCandidate> QXmppJingleIq::Content::transportCandidates() const
  120. {
  121. return m_transportCandidates;
  122. }
  123. QString QXmppJingleIq::Content::transportUser() const
  124. {
  125. return m_transportUser;
  126. }
  127. void QXmppJingleIq::Content::setTransportUser(const QString &user)
  128. {
  129. m_transportUser = user;
  130. }
  131. QString QXmppJingleIq::Content::transportPassword() const
  132. {
  133. return m_transportPassword;
  134. }
  135. void QXmppJingleIq::Content::setTransportPassword(const QString &password)
  136. {
  137. m_transportPassword = password;
  138. }
  139. /// \cond
  140. void QXmppJingleIq::Content::parse(const QDomElement &element)
  141. {
  142. m_creator = element.attribute("creator");
  143. m_disposition = element.attribute("disposition");
  144. m_name = element.attribute("name");
  145. m_senders = element.attribute("senders");
  146. // description
  147. QDomElement descriptionElement = element.firstChildElement("description");
  148. m_descriptionType = descriptionElement.namespaceURI();
  149. m_descriptionMedia = descriptionElement.attribute("media");
  150. QDomElement child = descriptionElement.firstChildElement("payload-type");
  151. while (!child.isNull())
  152. {
  153. QXmppJinglePayloadType payload;
  154. payload.parse(child);
  155. m_payloadTypes << payload;
  156. child = child.nextSiblingElement("payload-type");
  157. }
  158. // transport
  159. QDomElement transportElement = element.firstChildElement("transport");
  160. m_transportType = transportElement.namespaceURI();
  161. m_transportUser = transportElement.attribute("ufrag");
  162. m_transportPassword = transportElement.attribute("pwd");
  163. child = transportElement.firstChildElement("candidate");
  164. while (!child.isNull())
  165. {
  166. QXmppJingleCandidate candidate;
  167. candidate.parse(child);
  168. m_transportCandidates << candidate;
  169. child = child.nextSiblingElement("candidate");
  170. }
  171. }
  172. void QXmppJingleIq::Content::toXml(QXmlStreamWriter *writer) const
  173. {
  174. if (m_creator.isEmpty() || m_name.isEmpty())
  175. return;
  176. writer->writeStartElement("content");
  177. helperToXmlAddAttribute(writer, "creator", m_creator);
  178. helperToXmlAddAttribute(writer, "disposition", m_disposition);
  179. helperToXmlAddAttribute(writer, "name", m_name);
  180. helperToXmlAddAttribute(writer, "senders", m_senders);
  181. // description
  182. if (!m_descriptionType.isEmpty() || !m_payloadTypes.isEmpty())
  183. {
  184. writer->writeStartElement("description");
  185. writer->writeAttribute("xmlns", m_descriptionType);
  186. helperToXmlAddAttribute(writer, "media", m_descriptionMedia);
  187. foreach (const QXmppJinglePayloadType &payload, m_payloadTypes)
  188. payload.toXml(writer);
  189. writer->writeEndElement();
  190. }
  191. // transport
  192. if (!m_transportType.isEmpty() || !m_transportCandidates.isEmpty())
  193. {
  194. writer->writeStartElement("transport");
  195. writer->writeAttribute("xmlns", m_transportType);
  196. helperToXmlAddAttribute(writer, "ufrag", m_transportUser);
  197. helperToXmlAddAttribute(writer, "pwd", m_transportPassword);
  198. foreach (const QXmppJingleCandidate &candidate, m_transportCandidates)
  199. candidate.toXml(writer);
  200. writer->writeEndElement();
  201. }
  202. writer->writeEndElement();
  203. }
  204. /// \endcond
  205. QXmppJingleIq::Reason::Reason()
  206. : m_type(None)
  207. {
  208. }
  209. QString QXmppJingleIq::Reason::text() const
  210. {
  211. return m_text;
  212. }
  213. void QXmppJingleIq::Reason::setText(const QString &text)
  214. {
  215. m_text = text;
  216. }
  217. QXmppJingleIq::Reason::Type QXmppJingleIq::Reason::type() const
  218. {
  219. return m_type;
  220. }
  221. void QXmppJingleIq::Reason::setType(QXmppJingleIq::Reason::Type type)
  222. {
  223. m_type = type;
  224. }
  225. /// \cond
  226. void QXmppJingleIq::Reason::parse(const QDomElement &element)
  227. {
  228. m_text = element.firstChildElement("text").text();
  229. for (int i = AlternativeSession; i <= UnsupportedTransports; i++)
  230. {
  231. if (!element.firstChildElement(jingle_reasons[i]).isNull())
  232. {
  233. m_type = static_cast<Type>(i);
  234. break;
  235. }
  236. }
  237. }
  238. void QXmppJingleIq::Reason::toXml(QXmlStreamWriter *writer) const
  239. {
  240. if (m_type < AlternativeSession || m_type > UnsupportedTransports)
  241. return;
  242. writer->writeStartElement("reason");
  243. if (!m_text.isEmpty())
  244. helperToXmlAddTextElement(writer, "text", m_text);
  245. writer->writeEmptyElement(jingle_reasons[m_type]);
  246. writer->writeEndElement();
  247. }
  248. /// \endcond
  249. /// Constructs a QXmppJingleIq.
  250. QXmppJingleIq::QXmppJingleIq()
  251. : m_ringing(false)
  252. {
  253. }
  254. /// Returns the Jingle IQ's action.
  255. QXmppJingleIq::Action QXmppJingleIq::action() const
  256. {
  257. return m_action;
  258. }
  259. /// Sets the Jingle IQ's action.
  260. ///
  261. /// \param action
  262. void QXmppJingleIq::setAction(QXmppJingleIq::Action action)
  263. {
  264. m_action = action;
  265. }
  266. /// Returns the session initiator.
  267. QString QXmppJingleIq::initiator() const
  268. {
  269. return m_initiator;
  270. }
  271. /// Sets the session initiator.
  272. ///
  273. /// \param initiator
  274. void QXmppJingleIq::setInitiator(const QString &initiator)
  275. {
  276. m_initiator = initiator;
  277. }
  278. /// Returns the session responder.
  279. QString QXmppJingleIq::responder() const
  280. {
  281. return m_responder;
  282. }
  283. /// Sets the session responder.
  284. ///
  285. /// \param responder
  286. void QXmppJingleIq::setResponder(const QString &responder)
  287. {
  288. m_responder = responder;
  289. }
  290. /// Returns the session ID.
  291. QString QXmppJingleIq::sid() const
  292. {
  293. return m_sid;
  294. }
  295. /// Sets the session ID.
  296. ///
  297. /// \param sid
  298. void QXmppJingleIq::setSid(const QString &sid)
  299. {
  300. m_sid = sid;
  301. }
  302. /// Returns true if the call is ringing.
  303. bool QXmppJingleIq::ringing() const
  304. {
  305. return m_ringing;
  306. }
  307. /// Set to true if the call is ringing.
  308. ///
  309. /// \param ringing
  310. void QXmppJingleIq::setRinging(bool ringing)
  311. {
  312. m_ringing = ringing;
  313. }
  314. /// \cond
  315. bool QXmppJingleIq::isJingleIq(const QDomElement &element)
  316. {
  317. QDomElement jingleElement = element.firstChildElement("jingle");
  318. return (jingleElement.namespaceURI() == ns_jingle);
  319. }
  320. void QXmppJingleIq::parseElementFromChild(const QDomElement &element)
  321. {
  322. QDomElement jingleElement = element.firstChildElement("jingle");
  323. const QString action = jingleElement.attribute("action");
  324. for (int i = ContentAccept; i <= TransportReplace; i++)
  325. {
  326. if (action == jingle_actions[i])
  327. {
  328. m_action = static_cast<Action>(i);
  329. break;
  330. }
  331. }
  332. m_initiator = jingleElement.attribute("initiator");
  333. m_responder = jingleElement.attribute("responder");
  334. m_sid = jingleElement.attribute("sid");
  335. // content
  336. QDomElement contentElement = jingleElement.firstChildElement("content");
  337. m_content.parse(contentElement);
  338. QDomElement reasonElement = jingleElement.firstChildElement("reason");
  339. m_reason.parse(reasonElement);
  340. // ringing
  341. QDomElement ringingElement = jingleElement.firstChildElement("ringing");
  342. m_ringing = (ringingElement.namespaceURI() == ns_jingle_rtp_info);
  343. }
  344. void QXmppJingleIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
  345. {
  346. writer->writeStartElement("jingle");
  347. writer->writeAttribute("xmlns", ns_jingle);
  348. helperToXmlAddAttribute(writer, "action", jingle_actions[m_action]);
  349. helperToXmlAddAttribute(writer, "initiator", m_initiator);
  350. helperToXmlAddAttribute(writer, "responder", m_responder);
  351. helperToXmlAddAttribute(writer, "sid", m_sid);
  352. m_content.toXml(writer);
  353. m_reason.toXml(writer);
  354. // ringing
  355. if (m_ringing)
  356. {
  357. writer->writeStartElement("ringing");
  358. writer->writeAttribute("xmlns", ns_jingle_rtp_info);
  359. writer->writeEndElement();
  360. }
  361. writer->writeEndElement();
  362. }
  363. /// \endcond
  364. QXmppJingleCandidate::QXmppJingleCandidate()
  365. : m_component(0),
  366. m_foundation(0),
  367. m_generation(0),
  368. m_network(0),
  369. m_port(0),
  370. m_priority(0),
  371. m_type(HostType)
  372. {
  373. }
  374. /// Returns the candidate's component ID.
  375. int QXmppJingleCandidate::component() const
  376. {
  377. return m_component;
  378. }
  379. /// Sets the candidates's component ID.
  380. ///
  381. /// \param component
  382. void QXmppJingleCandidate::setComponent(int component)
  383. {
  384. m_component = component;
  385. }
  386. /// Returns the candidate's foundation.
  387. int QXmppJingleCandidate::foundation() const
  388. {
  389. return m_foundation;
  390. }
  391. /// Sets the candidate's foundation.
  392. ///
  393. /// \param foundation
  394. void QXmppJingleCandidate::setFoundation(int foundation)
  395. {
  396. m_foundation = foundation;
  397. }
  398. /// Returns the candidate's host address.
  399. ///
  400. QHostAddress QXmppJingleCandidate::host() const
  401. {
  402. return m_host;
  403. }
  404. /// Sets the candidate's host address.
  405. ///
  406. /// \param host
  407. void QXmppJingleCandidate::setHost(const QHostAddress &host)
  408. {
  409. m_host = host;
  410. }
  411. /// Returns the candidate's unique identifier.
  412. ///
  413. QString QXmppJingleCandidate::id() const
  414. {
  415. return m_id;
  416. }
  417. /// Sets the candidate's unique identifier.
  418. ///
  419. /// \param id
  420. void QXmppJingleCandidate::setId(const QString &id)
  421. {
  422. m_id = id;
  423. }
  424. /// Returns the network index (starting at 0) the candidate is on.
  425. ///
  426. int QXmppJingleCandidate::network() const
  427. {
  428. return m_network;
  429. }
  430. /// Sets the network index (starting at 0) the candidate is on.
  431. ///
  432. /// \param network
  433. void QXmppJingleCandidate::setNetwork(int network)
  434. {
  435. m_network = network;
  436. }
  437. /// Returns the candidate's port number.
  438. ///
  439. quint16 QXmppJingleCandidate::port() const
  440. {
  441. return m_port;
  442. }
  443. /// Sets the candidate's port number.
  444. ///
  445. /// \param port
  446. void QXmppJingleCandidate::setPort(quint16 port)
  447. {
  448. m_port = port;
  449. }
  450. /// Returns the candidate's priority.
  451. ///
  452. int QXmppJingleCandidate::priority() const
  453. {
  454. return m_priority;
  455. }
  456. /// Sets the candidate's priority.
  457. ///
  458. /// \param priority
  459. void QXmppJingleCandidate::setPriority(int priority)
  460. {
  461. m_priority = priority;
  462. }
  463. /// Returns the candidate's protocol (e.g. "udp").
  464. ///
  465. QString QXmppJingleCandidate::protocol() const
  466. {
  467. return m_protocol;
  468. }
  469. /// Sets the candidate's protocol (e.g. "udp").
  470. ///
  471. /// \param protocol
  472. void QXmppJingleCandidate::setProtocol(const QString &protocol)
  473. {
  474. m_protocol = protocol;
  475. }
  476. /// Returns the candidate type (e.g. "host").
  477. ///
  478. QXmppJingleCandidate::Type QXmppJingleCandidate::type() const
  479. {
  480. return m_type;
  481. }
  482. /// Sets the candidate type (e.g. "host").
  483. ///
  484. /// \param type
  485. void QXmppJingleCandidate::setType(QXmppJingleCandidate::Type type)
  486. {
  487. m_type = type;
  488. }
  489. /// Returns true if the host address or port are empty.
  490. ///
  491. bool QXmppJingleCandidate::isNull() const
  492. {
  493. return m_host.isNull() || !m_port;
  494. }
  495. /// \cond
  496. void QXmppJingleCandidate::parse(const QDomElement &element)
  497. {
  498. m_component = element.attribute("component").toInt();
  499. m_foundation = element.attribute("foundation").toInt();
  500. m_generation = element.attribute("generation").toInt();
  501. m_host = QHostAddress(element.attribute("ip"));
  502. m_id = element.attribute("id");
  503. m_network = element.attribute("network").toInt();
  504. m_port = element.attribute("port").toInt();
  505. m_priority = element.attribute("priority").toInt();
  506. m_protocol = element.attribute("protocol");
  507. m_type = typeFromString(element.attribute("type"));
  508. }
  509. void QXmppJingleCandidate::toXml(QXmlStreamWriter *writer) const
  510. {
  511. writer->writeStartElement("candidate");
  512. helperToXmlAddAttribute(writer, "component", QString::number(m_component));
  513. helperToXmlAddAttribute(writer, "foundation", QString::number(m_foundation));
  514. helperToXmlAddAttribute(writer, "generation", QString::number(m_generation));
  515. helperToXmlAddAttribute(writer, "id", m_id);
  516. helperToXmlAddAttribute(writer, "ip", m_host.toString());
  517. helperToXmlAddAttribute(writer, "network", QString::number(m_network));
  518. helperToXmlAddAttribute(writer, "port", QString::number(m_port));
  519. helperToXmlAddAttribute(writer, "priority", QString::number(m_priority));
  520. helperToXmlAddAttribute(writer, "protocol", m_protocol);
  521. helperToXmlAddAttribute(writer, "type", typeToString(m_type));
  522. writer->writeEndElement();
  523. }
  524. QXmppJingleCandidate::Type QXmppJingleCandidate::typeFromString(const QString &typeStr, bool *ok)
  525. {
  526. QXmppJingleCandidate::Type type;
  527. if (typeStr == "host")
  528. type = HostType;
  529. else if (typeStr == "prflx")
  530. type = PeerReflexiveType;
  531. else if (typeStr == "srflx")
  532. type = ServerReflexiveType;
  533. else if (typeStr == "relay")
  534. type = RelayedType;
  535. else {
  536. qWarning() << "Unknown candidate type" << typeStr;
  537. if (ok)
  538. *ok = false;
  539. return HostType;
  540. }
  541. if (ok)
  542. *ok = true;
  543. return type;
  544. }
  545. QString QXmppJingleCandidate::typeToString(QXmppJingleCandidate::Type type)
  546. {
  547. QString typeStr;
  548. switch (type)
  549. {
  550. case HostType:
  551. typeStr = "host";
  552. break;
  553. case PeerReflexiveType:
  554. typeStr = "prflx";
  555. break;
  556. case ServerReflexiveType:
  557. typeStr = "srflx";
  558. break;
  559. case RelayedType:
  560. typeStr = "relay";
  561. break;
  562. }
  563. return typeStr;
  564. }
  565. /// \endcond
  566. QXmppJinglePayloadType::QXmppJinglePayloadType()
  567. : m_channels(1),
  568. m_clockrate(0),
  569. m_id(0),
  570. m_maxptime(0),
  571. m_ptime(0)
  572. {
  573. }
  574. /// Returns the number of channels (e.g. 1 for mono, 2 for stereo).
  575. ///
  576. unsigned char QXmppJinglePayloadType::channels() const
  577. {
  578. return m_channels;
  579. }
  580. /// Sets the number of channels (e.g. 1 for mono, 2 for stereo).
  581. ///
  582. /// \param channels
  583. void QXmppJinglePayloadType::setChannels(unsigned char channels)
  584. {
  585. m_channels = channels;
  586. }
  587. /// Returns the clockrate in Hz, i.e. the number of samples per second.
  588. ///
  589. unsigned int QXmppJinglePayloadType::clockrate() const
  590. {
  591. return m_clockrate;
  592. }
  593. /// Sets the clockrate in Hz, i.e. the number of samples per second.
  594. ///
  595. /// \param clockrate
  596. void QXmppJinglePayloadType::setClockrate(unsigned int clockrate)
  597. {
  598. m_clockrate = clockrate;
  599. }
  600. /// Returns the payload type identifier.
  601. ///
  602. unsigned char QXmppJinglePayloadType::id() const
  603. {
  604. return m_id;
  605. }
  606. /// Sets the payload type identifier.
  607. ///
  608. void QXmppJinglePayloadType::setId(unsigned char id)
  609. {
  610. Q_ASSERT(id <= 127);
  611. m_id = id;
  612. }
  613. /// Returns the maximum packet time in milliseconds.
  614. ///
  615. unsigned int QXmppJinglePayloadType::maxptime() const
  616. {
  617. return m_maxptime;
  618. }
  619. /// Sets the maximum packet type in milliseconds.
  620. ///
  621. /// \param maxptime
  622. void QXmppJinglePayloadType::setMaxptime(unsigned int maxptime)
  623. {
  624. m_maxptime = maxptime;
  625. }
  626. /// Returns the payload type name.
  627. ///
  628. QString QXmppJinglePayloadType::name() const
  629. {
  630. return m_name;
  631. }
  632. /// Sets the payload type name.
  633. ///
  634. /// \param name
  635. void QXmppJinglePayloadType::setName(const QString &name)
  636. {
  637. m_name = name;
  638. }
  639. /// Returns the payload parameters.
  640. QMap<QString,QString> QXmppJinglePayloadType::parameters() const
  641. {
  642. return m_parameters;
  643. }
  644. /// Sets the payload parameters.
  645. void QXmppJinglePayloadType::setParameters(const QMap<QString, QString> &parameters)
  646. {
  647. m_parameters = parameters;
  648. }
  649. /// Returns the packet time in milliseconds (20 by default).
  650. ///
  651. unsigned int QXmppJinglePayloadType::ptime() const
  652. {
  653. return m_ptime ? m_ptime : 20;
  654. }
  655. /// Sets the packet time in milliseconds (20 by default).
  656. ///
  657. /// \param ptime
  658. void QXmppJinglePayloadType::setPtime(unsigned int ptime)
  659. {
  660. m_ptime = ptime;
  661. }
  662. /// \cond
  663. void QXmppJinglePayloadType::parse(const QDomElement &element)
  664. {
  665. m_id = element.attribute("id").toInt();
  666. m_name = element.attribute("name");
  667. m_channels = element.attribute("channels").toInt();
  668. if (!m_channels)
  669. m_channels = 1;
  670. m_clockrate = element.attribute("clockrate").toInt();
  671. m_maxptime = element.attribute("maxptime").toInt();
  672. m_ptime = element.attribute("ptime").toInt();
  673. QDomElement child = element.firstChildElement("parameter");
  674. while (!child.isNull()) {
  675. m_parameters.insert(child.attribute("name"), child.attribute("value"));
  676. child = child.nextSiblingElement("parameter");
  677. }
  678. }
  679. void QXmppJinglePayloadType::toXml(QXmlStreamWriter *writer) const
  680. {
  681. writer->writeStartElement("payload-type");
  682. helperToXmlAddAttribute(writer, "id", QString::number(m_id));
  683. helperToXmlAddAttribute(writer, "name", m_name);
  684. if (m_channels > 1)
  685. helperToXmlAddAttribute(writer, "channels", QString::number(m_channels));
  686. if (m_clockrate > 0)
  687. helperToXmlAddAttribute(writer, "clockrate", QString::number(m_clockrate));
  688. if (m_maxptime > 0)
  689. helperToXmlAddAttribute(writer, "maxptime", QString::number(m_maxptime));
  690. if (m_ptime > 0)
  691. helperToXmlAddAttribute(writer, "ptime", QString::number(m_ptime));
  692. foreach (const QString &key, m_parameters.keys()) {
  693. writer->writeStartElement("parameter");
  694. writer->writeAttribute("name", key);
  695. writer->writeAttribute("value", m_parameters.value(key));
  696. writer->writeEndElement();
  697. }
  698. writer->writeEndElement();
  699. }
  700. /// \endcond
  701. /// Returns true if this QXmppJinglePayloadType and \a other refer to the same payload type.
  702. ///
  703. /// \param other
  704. bool QXmppJinglePayloadType::operator==(const QXmppJinglePayloadType &other) const
  705. {
  706. // FIXME : what to do with m_ptime and m_maxptime?
  707. if (m_id <= 95)
  708. return other.m_id == m_id && other.m_clockrate == m_clockrate;
  709. else
  710. return other.m_channels == m_channels &&
  711. other.m_clockrate == m_clockrate &&
  712. other.m_name.toLower() == m_name.toLower();
  713. }