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.

410 lines
17 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // Types and classes used in media session descriptions.
  11. #ifndef PC_MEDIA_SESSION_H_
  12. #define PC_MEDIA_SESSION_H_
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "api/media_types.h"
  18. #include "media/base/media_constants.h"
  19. #include "media/base/media_engine.h" // For DataChannelType
  20. #include "p2p/base/ice_credentials_iterator.h"
  21. #include "p2p/base/transport_description_factory.h"
  22. #include "pc/jsep_transport.h"
  23. #include "pc/media_protocol_names.h"
  24. #include "pc/session_description.h"
  25. #include "rtc_base/unique_id_generator.h"
  26. namespace cricket {
  27. class ChannelManager;
  28. // Default RTCP CNAME for unit tests.
  29. const char kDefaultRtcpCname[] = "DefaultRtcpCname";
  30. // Options for an RtpSender contained with an media description/"m=" section.
  31. // Note: Spec-compliant Simulcast and legacy simulcast are mutually exclusive.
  32. struct SenderOptions {
  33. std::string track_id;
  34. std::vector<std::string> stream_ids;
  35. // Use RIDs and Simulcast Layers to indicate spec-compliant Simulcast.
  36. std::vector<RidDescription> rids;
  37. SimulcastLayerList simulcast_layers;
  38. // Use |num_sim_layers| to indicate legacy simulcast.
  39. int num_sim_layers;
  40. };
  41. // Options for an individual media description/"m=" section.
  42. struct MediaDescriptionOptions {
  43. MediaDescriptionOptions(MediaType type,
  44. const std::string& mid,
  45. webrtc::RtpTransceiverDirection direction,
  46. bool stopped)
  47. : type(type), mid(mid), direction(direction), stopped(stopped) {}
  48. // TODO(deadbeef): When we don't support Plan B, there will only be one
  49. // sender per media description and this can be simplified.
  50. void AddAudioSender(const std::string& track_id,
  51. const std::vector<std::string>& stream_ids);
  52. void AddVideoSender(const std::string& track_id,
  53. const std::vector<std::string>& stream_ids,
  54. const std::vector<RidDescription>& rids,
  55. const SimulcastLayerList& simulcast_layers,
  56. int num_sim_layers);
  57. // Internally just uses sender_options.
  58. void AddRtpDataChannel(const std::string& track_id,
  59. const std::string& stream_id);
  60. MediaType type;
  61. std::string mid;
  62. webrtc::RtpTransceiverDirection direction;
  63. bool stopped;
  64. TransportOptions transport_options;
  65. // Note: There's no equivalent "RtpReceiverOptions" because only send
  66. // stream information goes in the local descriptions.
  67. std::vector<SenderOptions> sender_options;
  68. std::vector<webrtc::RtpCodecCapability> codec_preferences;
  69. absl::optional<std::string> alt_protocol;
  70. private:
  71. // Doesn't DCHECK on |type|.
  72. void AddSenderInternal(const std::string& track_id,
  73. const std::vector<std::string>& stream_ids,
  74. const std::vector<RidDescription>& rids,
  75. const SimulcastLayerList& simulcast_layers,
  76. int num_sim_layers);
  77. };
  78. // Provides a mechanism for describing how m= sections should be generated.
  79. // The m= section with index X will use media_description_options[X]. There
  80. // must be an option for each existing section if creating an answer, or a
  81. // subsequent offer.
  82. struct MediaSessionOptions {
  83. MediaSessionOptions() {}
  84. bool has_audio() const { return HasMediaDescription(MEDIA_TYPE_AUDIO); }
  85. bool has_video() const { return HasMediaDescription(MEDIA_TYPE_VIDEO); }
  86. bool has_data() const { return HasMediaDescription(MEDIA_TYPE_DATA); }
  87. bool HasMediaDescription(MediaType type) const;
  88. DataChannelType data_channel_type = DCT_NONE;
  89. bool vad_enabled = true; // When disabled, removes all CN codecs from SDP.
  90. bool rtcp_mux_enabled = true;
  91. bool bundle_enabled = false;
  92. bool offer_extmap_allow_mixed = false;
  93. bool raw_packetization_for_video = false;
  94. std::string rtcp_cname = kDefaultRtcpCname;
  95. webrtc::CryptoOptions crypto_options;
  96. // List of media description options in the same order that the media
  97. // descriptions will be generated.
  98. std::vector<MediaDescriptionOptions> media_description_options;
  99. std::vector<IceParameters> pooled_ice_credentials;
  100. // An optional media transport settings.
  101. // In the future we may consider using a vector here, to indicate multiple
  102. // supported transports.
  103. absl::optional<cricket::SessionDescription::MediaTransportSetting>
  104. media_transport_settings;
  105. // Use the draft-ietf-mmusic-sctp-sdp-03 obsolete syntax for SCTP
  106. // datachannels.
  107. // Default is true for backwards compatibility with clients that use
  108. // this internal interface.
  109. bool use_obsolete_sctp_sdp = true;
  110. };
  111. // Creates media session descriptions according to the supplied codecs and
  112. // other fields, as well as the supplied per-call options.
  113. // When creating answers, performs the appropriate negotiation
  114. // of the various fields to determine the proper result.
  115. class MediaSessionDescriptionFactory {
  116. public:
  117. // Simple constructor that does not set any configuration for the factory.
  118. // When using this constructor, the methods below can be used to set the
  119. // configuration.
  120. // The TransportDescriptionFactory and the UniqueRandomIdGenerator are not
  121. // owned by MediaSessionDescriptionFactory, so they must be kept alive by the
  122. // user of this class.
  123. MediaSessionDescriptionFactory(const TransportDescriptionFactory* factory,
  124. rtc::UniqueRandomIdGenerator* ssrc_generator);
  125. // This helper automatically sets up the factory to get its configuration
  126. // from the specified ChannelManager.
  127. MediaSessionDescriptionFactory(ChannelManager* cmanager,
  128. const TransportDescriptionFactory* factory,
  129. rtc::UniqueRandomIdGenerator* ssrc_generator);
  130. const AudioCodecs& audio_sendrecv_codecs() const;
  131. const AudioCodecs& audio_send_codecs() const;
  132. const AudioCodecs& audio_recv_codecs() const;
  133. void set_audio_codecs(const AudioCodecs& send_codecs,
  134. const AudioCodecs& recv_codecs);
  135. void set_audio_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
  136. audio_rtp_extensions_ = extensions;
  137. }
  138. RtpHeaderExtensions audio_rtp_header_extensions() const;
  139. const VideoCodecs& video_codecs() const { return video_codecs_; }
  140. void set_video_codecs(const VideoCodecs& codecs) { video_codecs_ = codecs; }
  141. void set_video_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
  142. video_rtp_extensions_ = extensions;
  143. }
  144. RtpHeaderExtensions video_rtp_header_extensions() const;
  145. const RtpDataCodecs& rtp_data_codecs() const { return rtp_data_codecs_; }
  146. void set_rtp_data_codecs(const RtpDataCodecs& codecs) {
  147. rtp_data_codecs_ = codecs;
  148. }
  149. SecurePolicy secure() const { return secure_; }
  150. void set_secure(SecurePolicy s) { secure_ = s; }
  151. void set_enable_encrypted_rtp_header_extensions(bool enable) {
  152. enable_encrypted_rtp_header_extensions_ = enable;
  153. }
  154. void set_is_unified_plan(bool is_unified_plan) {
  155. is_unified_plan_ = is_unified_plan;
  156. }
  157. std::unique_ptr<SessionDescription> CreateOffer(
  158. const MediaSessionOptions& options,
  159. const SessionDescription* current_description) const;
  160. std::unique_ptr<SessionDescription> CreateAnswer(
  161. const SessionDescription* offer,
  162. const MediaSessionOptions& options,
  163. const SessionDescription* current_description) const;
  164. private:
  165. const AudioCodecs& GetAudioCodecsForOffer(
  166. const webrtc::RtpTransceiverDirection& direction) const;
  167. const AudioCodecs& GetAudioCodecsForAnswer(
  168. const webrtc::RtpTransceiverDirection& offer,
  169. const webrtc::RtpTransceiverDirection& answer) const;
  170. void GetCodecsForOffer(
  171. const std::vector<const ContentInfo*>& current_active_contents,
  172. AudioCodecs* audio_codecs,
  173. VideoCodecs* video_codecs,
  174. RtpDataCodecs* rtp_data_codecs) const;
  175. void GetCodecsForAnswer(
  176. const std::vector<const ContentInfo*>& current_active_contents,
  177. const SessionDescription& remote_offer,
  178. AudioCodecs* audio_codecs,
  179. VideoCodecs* video_codecs,
  180. RtpDataCodecs* rtp_data_codecs) const;
  181. void GetRtpHdrExtsToOffer(
  182. const std::vector<const ContentInfo*>& current_active_contents,
  183. bool extmap_allow_mixed,
  184. RtpHeaderExtensions* audio_extensions,
  185. RtpHeaderExtensions* video_extensions) const;
  186. bool AddTransportOffer(const std::string& content_name,
  187. const TransportOptions& transport_options,
  188. const SessionDescription* current_desc,
  189. SessionDescription* offer,
  190. IceCredentialsIterator* ice_credentials) const;
  191. std::unique_ptr<TransportDescription> CreateTransportAnswer(
  192. const std::string& content_name,
  193. const SessionDescription* offer_desc,
  194. const TransportOptions& transport_options,
  195. const SessionDescription* current_desc,
  196. bool require_transport_attributes,
  197. IceCredentialsIterator* ice_credentials) const;
  198. bool AddTransportAnswer(const std::string& content_name,
  199. const TransportDescription& transport_desc,
  200. SessionDescription* answer_desc) const;
  201. // Helpers for adding media contents to the SessionDescription. Returns true
  202. // it succeeds or the media content is not needed, or false if there is any
  203. // error.
  204. bool AddAudioContentForOffer(
  205. const MediaDescriptionOptions& media_description_options,
  206. const MediaSessionOptions& session_options,
  207. const ContentInfo* current_content,
  208. const SessionDescription* current_description,
  209. const RtpHeaderExtensions& audio_rtp_extensions,
  210. const AudioCodecs& audio_codecs,
  211. StreamParamsVec* current_streams,
  212. SessionDescription* desc,
  213. IceCredentialsIterator* ice_credentials) const;
  214. bool AddVideoContentForOffer(
  215. const MediaDescriptionOptions& media_description_options,
  216. const MediaSessionOptions& session_options,
  217. const ContentInfo* current_content,
  218. const SessionDescription* current_description,
  219. const RtpHeaderExtensions& video_rtp_extensions,
  220. const VideoCodecs& video_codecs,
  221. StreamParamsVec* current_streams,
  222. SessionDescription* desc,
  223. IceCredentialsIterator* ice_credentials) const;
  224. bool AddSctpDataContentForOffer(
  225. const MediaDescriptionOptions& media_description_options,
  226. const MediaSessionOptions& session_options,
  227. const ContentInfo* current_content,
  228. const SessionDescription* current_description,
  229. StreamParamsVec* current_streams,
  230. SessionDescription* desc,
  231. IceCredentialsIterator* ice_credentials) const;
  232. bool AddRtpDataContentForOffer(
  233. const MediaDescriptionOptions& media_description_options,
  234. const MediaSessionOptions& session_options,
  235. const ContentInfo* current_content,
  236. const SessionDescription* current_description,
  237. const RtpDataCodecs& rtp_data_codecs,
  238. StreamParamsVec* current_streams,
  239. SessionDescription* desc,
  240. IceCredentialsIterator* ice_credentials) const;
  241. // This function calls either AddRtpDataContentForOffer or
  242. // AddSctpDataContentForOffer depending on protocol.
  243. // The codecs argument is ignored for SCTP.
  244. bool AddDataContentForOffer(
  245. const MediaDescriptionOptions& media_description_options,
  246. const MediaSessionOptions& session_options,
  247. const ContentInfo* current_content,
  248. const SessionDescription* current_description,
  249. const RtpDataCodecs& rtp_data_codecs,
  250. StreamParamsVec* current_streams,
  251. SessionDescription* desc,
  252. IceCredentialsIterator* ice_credentials) const;
  253. bool AddAudioContentForAnswer(
  254. const MediaDescriptionOptions& media_description_options,
  255. const MediaSessionOptions& session_options,
  256. const ContentInfo* offer_content,
  257. const SessionDescription* offer_description,
  258. const ContentInfo* current_content,
  259. const SessionDescription* current_description,
  260. const TransportInfo* bundle_transport,
  261. const AudioCodecs& audio_codecs,
  262. StreamParamsVec* current_streams,
  263. SessionDescription* answer,
  264. IceCredentialsIterator* ice_credentials) const;
  265. bool AddVideoContentForAnswer(
  266. const MediaDescriptionOptions& media_description_options,
  267. const MediaSessionOptions& session_options,
  268. const ContentInfo* offer_content,
  269. const SessionDescription* offer_description,
  270. const ContentInfo* current_content,
  271. const SessionDescription* current_description,
  272. const TransportInfo* bundle_transport,
  273. const VideoCodecs& video_codecs,
  274. StreamParamsVec* current_streams,
  275. SessionDescription* answer,
  276. IceCredentialsIterator* ice_credentials) const;
  277. bool AddDataContentForAnswer(
  278. const MediaDescriptionOptions& media_description_options,
  279. const MediaSessionOptions& session_options,
  280. const ContentInfo* offer_content,
  281. const SessionDescription* offer_description,
  282. const ContentInfo* current_content,
  283. const SessionDescription* current_description,
  284. const TransportInfo* bundle_transport,
  285. const RtpDataCodecs& rtp_data_codecs,
  286. StreamParamsVec* current_streams,
  287. SessionDescription* answer,
  288. IceCredentialsIterator* ice_credentials) const;
  289. void ComputeAudioCodecsIntersectionAndUnion();
  290. bool is_unified_plan_ = false;
  291. AudioCodecs audio_send_codecs_;
  292. AudioCodecs audio_recv_codecs_;
  293. // Intersection of send and recv.
  294. AudioCodecs audio_sendrecv_codecs_;
  295. // Union of send and recv.
  296. AudioCodecs all_audio_codecs_;
  297. RtpHeaderExtensions audio_rtp_extensions_;
  298. VideoCodecs video_codecs_;
  299. RtpHeaderExtensions video_rtp_extensions_;
  300. RtpDataCodecs rtp_data_codecs_;
  301. // This object is not owned by the channel so it must outlive it.
  302. rtc::UniqueRandomIdGenerator* const ssrc_generator_;
  303. bool enable_encrypted_rtp_header_extensions_ = false;
  304. // TODO(zhihuang): Rename secure_ to sdec_policy_; rename the related getter
  305. // and setter.
  306. SecurePolicy secure_ = SEC_DISABLED;
  307. const TransportDescriptionFactory* transport_desc_factory_;
  308. };
  309. // Convenience functions.
  310. bool IsMediaContent(const ContentInfo* content);
  311. bool IsAudioContent(const ContentInfo* content);
  312. bool IsVideoContent(const ContentInfo* content);
  313. bool IsDataContent(const ContentInfo* content);
  314. const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
  315. MediaType media_type);
  316. const ContentInfo* GetFirstAudioContent(const ContentInfos& contents);
  317. const ContentInfo* GetFirstVideoContent(const ContentInfos& contents);
  318. const ContentInfo* GetFirstDataContent(const ContentInfos& contents);
  319. const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
  320. MediaType media_type);
  321. const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc);
  322. const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc);
  323. const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc);
  324. const AudioContentDescription* GetFirstAudioContentDescription(
  325. const SessionDescription* sdesc);
  326. const VideoContentDescription* GetFirstVideoContentDescription(
  327. const SessionDescription* sdesc);
  328. const RtpDataContentDescription* GetFirstRtpDataContentDescription(
  329. const SessionDescription* sdesc);
  330. const SctpDataContentDescription* GetFirstSctpDataContentDescription(
  331. const SessionDescription* sdesc);
  332. // Non-const versions of the above functions.
  333. // Useful when modifying an existing description.
  334. ContentInfo* GetFirstMediaContent(ContentInfos* contents, MediaType media_type);
  335. ContentInfo* GetFirstAudioContent(ContentInfos* contents);
  336. ContentInfo* GetFirstVideoContent(ContentInfos* contents);
  337. ContentInfo* GetFirstDataContent(ContentInfos* contents);
  338. ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
  339. MediaType media_type);
  340. ContentInfo* GetFirstAudioContent(SessionDescription* sdesc);
  341. ContentInfo* GetFirstVideoContent(SessionDescription* sdesc);
  342. ContentInfo* GetFirstDataContent(SessionDescription* sdesc);
  343. AudioContentDescription* GetFirstAudioContentDescription(
  344. SessionDescription* sdesc);
  345. VideoContentDescription* GetFirstVideoContentDescription(
  346. SessionDescription* sdesc);
  347. RtpDataContentDescription* GetFirstRtpDataContentDescription(
  348. SessionDescription* sdesc);
  349. SctpDataContentDescription* GetFirstSctpDataContentDescription(
  350. SessionDescription* sdesc);
  351. // Helper functions to return crypto suites used for SDES.
  352. void GetSupportedAudioSdesCryptoSuites(
  353. const webrtc::CryptoOptions& crypto_options,
  354. std::vector<int>* crypto_suites);
  355. void GetSupportedVideoSdesCryptoSuites(
  356. const webrtc::CryptoOptions& crypto_options,
  357. std::vector<int>* crypto_suites);
  358. void GetSupportedDataSdesCryptoSuites(
  359. const webrtc::CryptoOptions& crypto_options,
  360. std::vector<int>* crypto_suites);
  361. void GetSupportedAudioSdesCryptoSuiteNames(
  362. const webrtc::CryptoOptions& crypto_options,
  363. std::vector<std::string>* crypto_suite_names);
  364. void GetSupportedVideoSdesCryptoSuiteNames(
  365. const webrtc::CryptoOptions& crypto_options,
  366. std::vector<std::string>* crypto_suite_names);
  367. void GetSupportedDataSdesCryptoSuiteNames(
  368. const webrtc::CryptoOptions& crypto_options,
  369. std::vector<std::string>* crypto_suite_names);
  370. } // namespace cricket
  371. #endif // PC_MEDIA_SESSION_H_