QXmppRtpChannel.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef QXMPPRTPCHANNEL_H
  24. #define QXMPPRTPCHANNEL_H
  25. #include <QIODevice>
  26. #include <QSize>
  27. #include "QXmppJingleIq.h"
  28. #include "QXmppLogger.h"
  29. class QXmppCodec;
  30. class QXmppJinglePayloadType;
  31. class QXmppRtpAudioChannelPrivate;
  32. class QXmppRtpVideoChannelPrivate;
  33. /// \brief The QXmppRtpPacket class represents an RTP packet.
  34. ///
  35. class QXMPP_EXPORT QXmppRtpPacket
  36. {
  37. public:
  38. bool decode(const QByteArray &ba);
  39. QByteArray encode() const;
  40. QString toString() const;
  41. /// RTP version.
  42. quint8 version;
  43. /// Marker flag.
  44. bool marker;
  45. /// Payload type.
  46. quint8 type;
  47. /// Synchronization source.
  48. quint32 ssrc;
  49. /// Contributing sources.
  50. QList<quint32> csrc;
  51. /// Sequence number.
  52. quint16 sequence;
  53. /// Timestamp.
  54. quint32 stamp;
  55. /// Raw payload data.
  56. QByteArray payload;
  57. };
  58. class QXMPP_EXPORT QXmppRtpChannel
  59. {
  60. public:
  61. QXmppRtpChannel();
  62. /// Closes the RTP channel.
  63. virtual void close() = 0;
  64. /// Returns the mode in which the channel has been opened.
  65. virtual QIODevice::OpenMode openMode() const = 0;
  66. QList<QXmppJinglePayloadType> localPayloadTypes();
  67. void setRemotePayloadTypes(const QList<QXmppJinglePayloadType> &remotePayloadTypes);
  68. protected:
  69. /// \cond
  70. virtual void payloadTypesChanged() = 0;
  71. QList<QXmppJinglePayloadType> m_incomingPayloadTypes;
  72. QList<QXmppJinglePayloadType> m_outgoingPayloadTypes;
  73. bool m_outgoingPayloadNumbered;
  74. /// \endcond
  75. };
  76. /// \brief The QXmppRtpAudioChannel class represents an RTP audio channel to a remote party.
  77. ///
  78. /// It acts as a QIODevice so that you can read / write audio samples, for
  79. /// instance using a QAudioOutput and a QAudioInput.
  80. ///
  81. /// \note THIS API IS NOT FINALIZED YET
  82. class QXMPP_EXPORT QXmppRtpAudioChannel : public QIODevice, public QXmppRtpChannel
  83. {
  84. Q_OBJECT
  85. Q_ENUMS(Tone)
  86. public:
  87. /// This enum is used to describe a DTMF tone.
  88. enum Tone {
  89. Tone_0 = 0, ///< Tone for the 0 key.
  90. Tone_1, ///< Tone for the 1 key.
  91. Tone_2, ///< Tone for the 2 key.
  92. Tone_3, ///< Tone for the 3 key.
  93. Tone_4, ///< Tone for the 4 key.
  94. Tone_5, ///< Tone for the 5 key.
  95. Tone_6, ///< Tone for the 6 key.
  96. Tone_7, ///< Tone for the 7 key.
  97. Tone_8, ///< Tone for the 8 key.
  98. Tone_9, ///< Tone for the 9 key.
  99. Tone_Star, ///< Tone for the * key.
  100. Tone_Pound, ///< Tone for the # key.
  101. Tone_A, ///< Tone for the A key.
  102. Tone_B, ///< Tone for the B key.
  103. Tone_C, ///< Tone for the C key.
  104. Tone_D ///< Tone for the D key.
  105. };
  106. QXmppRtpAudioChannel(QObject *parent = 0);
  107. ~QXmppRtpAudioChannel();
  108. qint64 bytesAvailable() const;
  109. void close();
  110. bool isSequential() const;
  111. QIODevice::OpenMode openMode() const;
  112. QXmppJinglePayloadType payloadType() const;
  113. qint64 pos() const;
  114. bool seek(qint64 pos);
  115. signals:
  116. /// \brief This signal is emitted when a datagram needs to be sent.
  117. void sendDatagram(const QByteArray &ba);
  118. /// \brief This signal is emitted to send logging messages.
  119. void logMessage(QXmppLogger::MessageType type, const QString &msg);
  120. public slots:
  121. void datagramReceived(const QByteArray &ba);
  122. void startTone(QXmppRtpAudioChannel::Tone tone);
  123. void stopTone(QXmppRtpAudioChannel::Tone tone);
  124. protected:
  125. /// \cond
  126. void debug(const QString &message)
  127. {
  128. emit logMessage(QXmppLogger::DebugMessage, qxmpp_loggable_trace(message));
  129. }
  130. void warning(const QString &message)
  131. {
  132. emit logMessage(QXmppLogger::WarningMessage, qxmpp_loggable_trace(message));
  133. }
  134. void logReceived(const QString &message)
  135. {
  136. emit logMessage(QXmppLogger::ReceivedMessage, qxmpp_loggable_trace(message));
  137. }
  138. void logSent(const QString &message)
  139. {
  140. emit logMessage(QXmppLogger::SentMessage, qxmpp_loggable_trace(message));
  141. }
  142. void payloadTypesChanged();
  143. qint64 readData(char * data, qint64 maxSize);
  144. qint64 writeData(const char * data, qint64 maxSize);
  145. /// \endcond
  146. private slots:
  147. void emitSignals();
  148. void writeDatagram();
  149. private:
  150. friend class QXmppRtpAudioChannelPrivate;
  151. QXmppRtpAudioChannelPrivate * d;
  152. };
  153. /// \brief The QXmppVideoFrame class provides a representation of a frame of video data.
  154. ///
  155. /// \note THIS API IS NOT FINALIZED YET
  156. class QXMPP_EXPORT QXmppVideoFrame
  157. {
  158. public:
  159. /// This enum describes a pixel format.
  160. enum PixelFormat {
  161. Format_Invalid = 0, ///< The frame is invalid.
  162. Format_RGB32 = 3, ///< The frame stored using a 32-bit RGB format (0xffRRGGBB).
  163. Format_RGB24 = 4, ///< The frame is stored using a 24-bit RGB format (8-8-8).
  164. Format_YUV420P = 18, ///< The frame is stored using an 8-bit per component planar
  165. ///< YUV format with the U and V planes horizontally and
  166. ///< vertically sub-sampled, i.e. the height and width of the
  167. ///< U and V planes are half that of the Y plane.
  168. Format_UYVY = 20, ///< The frame is stored using an 8-bit per component packed
  169. ///< YUV format with the U and V planes horizontally
  170. ///< sub-sampled (U-Y-V-Y), i.e. two horizontally adjacent
  171. ///< pixels are stored as a 32-bit macropixel which has a Y
  172. ///< value for each pixel and common U and V values.
  173. Format_YUYV = 21, ///< The frame is stored using an 8-bit per component packed
  174. ///< YUV format with the U and V planes horizontally
  175. ///< sub-sampled (Y-U-Y-V), i.e. two horizontally adjacent
  176. ///< pixels are stored as a 32-bit macropixel which has a Y
  177. ///< value for each pixel and common U and V values.
  178. };
  179. QXmppVideoFrame();
  180. QXmppVideoFrame(int bytes, const QSize &size, int bytesPerLine, PixelFormat format);
  181. uchar *bits();
  182. const uchar *bits() const;
  183. int bytesPerLine() const;
  184. int height() const;
  185. bool isValid() const;
  186. int mappedBytes() const;
  187. PixelFormat pixelFormat() const;
  188. QSize size() const;
  189. int width() const;
  190. private:
  191. int m_bytesPerLine;
  192. QByteArray m_data;
  193. int m_height;
  194. int m_mappedBytes;
  195. PixelFormat m_pixelFormat;
  196. int m_width;
  197. };
  198. class QXMPP_EXPORT QXmppVideoFormat
  199. {
  200. public:
  201. int frameHeight() const {
  202. return m_frameSize.height();
  203. }
  204. int frameWidth() const {
  205. return m_frameSize.width();
  206. }
  207. qreal frameRate() const {
  208. return m_frameRate;
  209. }
  210. void setFrameRate(qreal frameRate) {
  211. m_frameRate = frameRate;
  212. }
  213. QSize frameSize() const {
  214. return m_frameSize;
  215. }
  216. void setFrameSize(const QSize &frameSize) {
  217. m_frameSize = frameSize;
  218. }
  219. QXmppVideoFrame::PixelFormat pixelFormat() const {
  220. return m_pixelFormat;
  221. }
  222. void setPixelFormat(QXmppVideoFrame::PixelFormat pixelFormat) {
  223. m_pixelFormat = pixelFormat;
  224. }
  225. private:
  226. qreal m_frameRate;
  227. QSize m_frameSize;
  228. QXmppVideoFrame::PixelFormat m_pixelFormat;
  229. };
  230. /// \brief The QXmppRtpVideoChannel class represents an RTP video channel to a remote party.
  231. ///
  232. /// \note THIS API IS NOT FINALIZED YET
  233. class QXMPP_EXPORT QXmppRtpVideoChannel : public QXmppLoggable, public QXmppRtpChannel
  234. {
  235. Q_OBJECT
  236. public:
  237. QXmppRtpVideoChannel(QObject *parent = 0);
  238. ~QXmppRtpVideoChannel();
  239. void close();
  240. QIODevice::OpenMode openMode() const;
  241. // incoming stream
  242. QXmppVideoFormat decoderFormat() const;
  243. QList<QXmppVideoFrame> readFrames();
  244. // outgoing stream
  245. QXmppVideoFormat encoderFormat() const;
  246. void setEncoderFormat(const QXmppVideoFormat &format);
  247. void writeFrame(const QXmppVideoFrame &frame);
  248. signals:
  249. /// \brief This signal is emitted when a datagram needs to be sent.
  250. void sendDatagram(const QByteArray &ba);
  251. public slots:
  252. void datagramReceived(const QByteArray &ba);
  253. protected:
  254. /// \cond
  255. void payloadTypesChanged();
  256. /// \endcond
  257. private:
  258. friend class QXmppRtpVideoChannelPrivate;
  259. QXmppRtpVideoChannelPrivate * d;
  260. };
  261. #endif