You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTCSessionDescription.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import sdpTransform from 'sdp-transform';
  2. /**
  3. * RTCSessionDescription implementation.
  4. */
  5. export default class RTCSessionDescription {
  6. /**
  7. * RTCSessionDescription constructor.
  8. * @param {Object} [data]
  9. * @param {String} [data.type] - 'offer' / 'answer'.
  10. * @param {String} [data.sdp] - SDP string.
  11. * @param {Object} [data._sdpObject] - SDP object generated by the
  12. * sdp-transform library.
  13. */
  14. constructor(data) {
  15. // @type {String}
  16. this._sdp = null;
  17. // @type {Object}
  18. this._sdpObject = null;
  19. // @type {String}
  20. this._type = null;
  21. switch (data.type) {
  22. case 'offer':
  23. break;
  24. case 'answer':
  25. break;
  26. default:
  27. throw new TypeError(`invalid type "${data.type}"`);
  28. }
  29. this._type = data.type;
  30. if (typeof data.sdp === 'string') {
  31. this._sdp = data.sdp;
  32. try {
  33. this._sdpObject = sdpTransform.parse(data.sdp);
  34. } catch (error) {
  35. throw new Error(`invalid sdp: ${error}`);
  36. }
  37. } else if (typeof data._sdpObject === 'object') {
  38. this._sdpObject = data._sdpObject;
  39. try {
  40. this._sdp = sdpTransform.write(data._sdpObject);
  41. } catch (error) {
  42. throw new Error(`invalid sdp object: ${error}`);
  43. }
  44. } else {
  45. throw new TypeError('invalid sdp or _sdpObject');
  46. }
  47. }
  48. /**
  49. * Get sdp field.
  50. * @return {String}
  51. */
  52. get sdp() {
  53. return this._sdp;
  54. }
  55. /**
  56. * Set sdp field.
  57. * NOTE: This is not allowed per spec, but lib-jitsi-meet uses it.
  58. * @param {String} sdp
  59. */
  60. set sdp(sdp) {
  61. try {
  62. this._sdpObject = sdpTransform.parse(sdp);
  63. } catch (error) {
  64. throw new Error(`invalid sdp: ${error}`);
  65. }
  66. this._sdp = sdp;
  67. }
  68. /**
  69. * Gets the internal sdp object.
  70. * @return {Object}
  71. * @private
  72. */
  73. get sdpObject() {
  74. return this._sdpObject;
  75. }
  76. /**
  77. * Get type field.
  78. * @return {String}
  79. */
  80. get type() {
  81. return this._type;
  82. }
  83. /**
  84. * Returns an object with type and sdp fields.
  85. * @return {Object}
  86. */
  87. toJSON() {
  88. return {
  89. sdp: this._sdp,
  90. type: this._type
  91. };
  92. }
  93. }