media_session.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. // Use the draft-ietf-mmusic-sctp-sdp-03 obsolete syntax for SCTP
  101. // datachannels.
  102. // Default is true for backwards compatibility with clients that use
  103. // this internal interface.
  104. bool use_obsolete_sctp_sdp = true;
  105. };
  106. // Creates media session descriptions according to the supplied codecs and
  107. // other fields, as well as the supplied per-call options.
  108. // When creating answers, performs the appropriate negotiation
  109. // of the various fields to determine the proper result.
  110. class MediaSessionDescriptionFactory {
  111. public:
  112. // Simple constructor that does not set any configuration for the factory.
  113. // When using this constructor, the methods below can be used to set the
  114. // configuration.
  115. // The TransportDescriptionFactory and the UniqueRandomIdGenerator are not
  116. // owned by MediaSessionDescriptionFactory, so they must be kept alive by the
  117. // user of this class.
  118. MediaSessionDescriptionFactory(const TransportDescriptionFactory* factory,
  119. rtc::UniqueRandomIdGenerator* ssrc_generator);
  120. // This helper automatically sets up the factory to get its configuration
  121. // from the specified ChannelManager.
  122. MediaSessionDescriptionFactory(ChannelManager* cmanager,
  123. const TransportDescriptionFactory* factory,
  124. rtc::UniqueRandomIdGenerator* ssrc_generator);
  125. const AudioCodecs& audio_sendrecv_codecs() const;
  126. const AudioCodecs& audio_send_codecs() const;
  127. const AudioCodecs& audio_recv_codecs() const;
  128. void set_audio_codecs(const AudioCodecs& send_codecs,
  129. const AudioCodecs& recv_codecs);
  130. void set_audio_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
  131. audio_rtp_extensions_ = extensions;
  132. }
  133. RtpHeaderExtensions audio_rtp_header_extensions() const;
  134. const VideoCodecs& video_sendrecv_codecs() const;
  135. const VideoCodecs& video_send_codecs() const;
  136. const VideoCodecs& video_recv_codecs() const;
  137. void set_video_codecs(const VideoCodecs& send_codecs,
  138. const VideoCodecs& recv_codecs);
  139. void set_video_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
  140. video_rtp_extensions_ = extensions;
  141. }
  142. RtpHeaderExtensions video_rtp_header_extensions() const;
  143. const RtpDataCodecs& rtp_data_codecs() const { return rtp_data_codecs_; }
  144. void set_rtp_data_codecs(const RtpDataCodecs& codecs) {
  145. rtp_data_codecs_ = codecs;
  146. }
  147. SecurePolicy secure() const { return secure_; }
  148. void set_secure(SecurePolicy s) { secure_ = s; }
  149. void set_enable_encrypted_rtp_header_extensions(bool enable) {
  150. enable_encrypted_rtp_header_extensions_ = enable;
  151. }
  152. void set_is_unified_plan(bool is_unified_plan) {
  153. is_unified_plan_ = is_unified_plan;
  154. }
  155. std::unique_ptr<SessionDescription> CreateOffer(
  156. const MediaSessionOptions& options,
  157. const SessionDescription* current_description) const;
  158. std::unique_ptr<SessionDescription> CreateAnswer(
  159. const SessionDescription* offer,
  160. const MediaSessionOptions& options,
  161. const SessionDescription* current_description) const;
  162. private:
  163. const AudioCodecs& GetAudioCodecsForOffer(
  164. const webrtc::RtpTransceiverDirection& direction) const;
  165. const AudioCodecs& GetAudioCodecsForAnswer(
  166. const webrtc::RtpTransceiverDirection& offer,
  167. const webrtc::RtpTransceiverDirection& answer) const;
  168. const VideoCodecs& GetVideoCodecsForOffer(
  169. const webrtc::RtpTransceiverDirection& direction) const;
  170. const VideoCodecs& GetVideoCodecsForAnswer(
  171. const webrtc::RtpTransceiverDirection& offer,
  172. const webrtc::RtpTransceiverDirection& answer) const;
  173. void GetCodecsForOffer(
  174. const std::vector<const ContentInfo*>& current_active_contents,
  175. AudioCodecs* audio_codecs,
  176. VideoCodecs* video_codecs,
  177. RtpDataCodecs* rtp_data_codecs) const;
  178. void GetCodecsForAnswer(
  179. const std::vector<const ContentInfo*>& current_active_contents,
  180. const SessionDescription& remote_offer,
  181. AudioCodecs* audio_codecs,
  182. VideoCodecs* video_codecs,
  183. RtpDataCodecs* rtp_data_codecs) const;
  184. void GetRtpHdrExtsToOffer(
  185. const std::vector<const ContentInfo*>& current_active_contents,
  186. bool extmap_allow_mixed,
  187. RtpHeaderExtensions* audio_extensions,
  188. RtpHeaderExtensions* video_extensions) const;
  189. bool AddTransportOffer(const std::string& content_name,
  190. const TransportOptions& transport_options,
  191. const SessionDescription* current_desc,
  192. SessionDescription* offer,
  193. IceCredentialsIterator* ice_credentials) const;
  194. std::unique_ptr<TransportDescription> CreateTransportAnswer(
  195. const std::string& content_name,
  196. const SessionDescription* offer_desc,
  197. const TransportOptions& transport_options,
  198. const SessionDescription* current_desc,
  199. bool require_transport_attributes,
  200. IceCredentialsIterator* ice_credentials) const;
  201. bool AddTransportAnswer(const std::string& content_name,
  202. const TransportDescription& transport_desc,
  203. SessionDescription* answer_desc) const;
  204. // Helpers for adding media contents to the SessionDescription. Returns true
  205. // it succeeds or the media content is not needed, or false if there is any
  206. // error.
  207. bool AddAudioContentForOffer(
  208. const MediaDescriptionOptions& media_description_options,
  209. const MediaSessionOptions& session_options,
  210. const ContentInfo* current_content,
  211. const SessionDescription* current_description,
  212. const RtpHeaderExtensions& audio_rtp_extensions,
  213. const AudioCodecs& audio_codecs,
  214. StreamParamsVec* current_streams,
  215. SessionDescription* desc,
  216. IceCredentialsIterator* ice_credentials) const;
  217. bool AddVideoContentForOffer(
  218. const MediaDescriptionOptions& media_description_options,
  219. const MediaSessionOptions& session_options,
  220. const ContentInfo* current_content,
  221. const SessionDescription* current_description,
  222. const RtpHeaderExtensions& video_rtp_extensions,
  223. const VideoCodecs& video_codecs,
  224. StreamParamsVec* current_streams,
  225. SessionDescription* desc,
  226. IceCredentialsIterator* ice_credentials) const;
  227. bool AddSctpDataContentForOffer(
  228. const MediaDescriptionOptions& media_description_options,
  229. const MediaSessionOptions& session_options,
  230. const ContentInfo* current_content,
  231. const SessionDescription* current_description,
  232. StreamParamsVec* current_streams,
  233. SessionDescription* desc,
  234. IceCredentialsIterator* ice_credentials) const;
  235. bool AddRtpDataContentForOffer(
  236. const MediaDescriptionOptions& media_description_options,
  237. const MediaSessionOptions& session_options,
  238. const ContentInfo* current_content,
  239. const SessionDescription* current_description,
  240. const RtpDataCodecs& rtp_data_codecs,
  241. StreamParamsVec* current_streams,
  242. SessionDescription* desc,
  243. IceCredentialsIterator* ice_credentials) const;
  244. // This function calls either AddRtpDataContentForOffer or
  245. // AddSctpDataContentForOffer depending on protocol.
  246. // The codecs argument is ignored for SCTP.
  247. bool AddDataContentForOffer(
  248. const MediaDescriptionOptions& media_description_options,
  249. const MediaSessionOptions& session_options,
  250. const ContentInfo* current_content,
  251. const SessionDescription* current_description,
  252. const RtpDataCodecs& rtp_data_codecs,
  253. StreamParamsVec* current_streams,
  254. SessionDescription* desc,
  255. IceCredentialsIterator* ice_credentials) const;
  256. bool AddAudioContentForAnswer(
  257. const MediaDescriptionOptions& media_description_options,
  258. const MediaSessionOptions& session_options,
  259. const ContentInfo* offer_content,
  260. const SessionDescription* offer_description,
  261. const ContentInfo* current_content,
  262. const SessionDescription* current_description,
  263. const TransportInfo* bundle_transport,
  264. const AudioCodecs& audio_codecs,
  265. StreamParamsVec* current_streams,
  266. SessionDescription* answer,
  267. IceCredentialsIterator* ice_credentials) const;
  268. bool AddVideoContentForAnswer(
  269. const MediaDescriptionOptions& media_description_options,
  270. const MediaSessionOptions& session_options,
  271. const ContentInfo* offer_content,
  272. const SessionDescription* offer_description,
  273. const ContentInfo* current_content,
  274. const SessionDescription* current_description,
  275. const TransportInfo* bundle_transport,
  276. const VideoCodecs& video_codecs,
  277. StreamParamsVec* current_streams,
  278. SessionDescription* answer,
  279. IceCredentialsIterator* ice_credentials) const;
  280. bool AddDataContentForAnswer(
  281. const MediaDescriptionOptions& media_description_options,
  282. const MediaSessionOptions& session_options,
  283. const ContentInfo* offer_content,
  284. const SessionDescription* offer_description,
  285. const ContentInfo* current_content,
  286. const SessionDescription* current_description,
  287. const TransportInfo* bundle_transport,
  288. const RtpDataCodecs& rtp_data_codecs,
  289. StreamParamsVec* current_streams,
  290. SessionDescription* answer,
  291. IceCredentialsIterator* ice_credentials) const;
  292. void ComputeAudioCodecsIntersectionAndUnion();
  293. void ComputeVideoCodecsIntersectionAndUnion();
  294. bool is_unified_plan_ = false;
  295. AudioCodecs audio_send_codecs_;
  296. AudioCodecs audio_recv_codecs_;
  297. // Intersection of send and recv.
  298. AudioCodecs audio_sendrecv_codecs_;
  299. // Union of send and recv.
  300. AudioCodecs all_audio_codecs_;
  301. RtpHeaderExtensions audio_rtp_extensions_;
  302. VideoCodecs video_send_codecs_;
  303. VideoCodecs video_recv_codecs_;
  304. // Intersection of send and recv.
  305. VideoCodecs video_sendrecv_codecs_;
  306. // Union of send and recv.
  307. VideoCodecs all_video_codecs_;
  308. RtpHeaderExtensions video_rtp_extensions_;
  309. RtpDataCodecs rtp_data_codecs_;
  310. // This object is not owned by the channel so it must outlive it.
  311. rtc::UniqueRandomIdGenerator* const ssrc_generator_;
  312. bool enable_encrypted_rtp_header_extensions_ = false;
  313. // TODO(zhihuang): Rename secure_ to sdec_policy_; rename the related getter
  314. // and setter.
  315. SecurePolicy secure_ = SEC_DISABLED;
  316. const TransportDescriptionFactory* transport_desc_factory_;
  317. };
  318. // Convenience functions.
  319. bool IsMediaContent(const ContentInfo* content);
  320. bool IsAudioContent(const ContentInfo* content);
  321. bool IsVideoContent(const ContentInfo* content);
  322. bool IsDataContent(const ContentInfo* content);
  323. const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
  324. MediaType media_type);
  325. const ContentInfo* GetFirstAudioContent(const ContentInfos& contents);
  326. const ContentInfo* GetFirstVideoContent(const ContentInfos& contents);
  327. const ContentInfo* GetFirstDataContent(const ContentInfos& contents);
  328. const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
  329. MediaType media_type);
  330. const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc);
  331. const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc);
  332. const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc);
  333. const AudioContentDescription* GetFirstAudioContentDescription(
  334. const SessionDescription* sdesc);
  335. const VideoContentDescription* GetFirstVideoContentDescription(
  336. const SessionDescription* sdesc);
  337. const RtpDataContentDescription* GetFirstRtpDataContentDescription(
  338. const SessionDescription* sdesc);
  339. const SctpDataContentDescription* GetFirstSctpDataContentDescription(
  340. const SessionDescription* sdesc);
  341. // Non-const versions of the above functions.
  342. // Useful when modifying an existing description.
  343. ContentInfo* GetFirstMediaContent(ContentInfos* contents, MediaType media_type);
  344. ContentInfo* GetFirstAudioContent(ContentInfos* contents);
  345. ContentInfo* GetFirstVideoContent(ContentInfos* contents);
  346. ContentInfo* GetFirstDataContent(ContentInfos* contents);
  347. ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
  348. MediaType media_type);
  349. ContentInfo* GetFirstAudioContent(SessionDescription* sdesc);
  350. ContentInfo* GetFirstVideoContent(SessionDescription* sdesc);
  351. ContentInfo* GetFirstDataContent(SessionDescription* sdesc);
  352. AudioContentDescription* GetFirstAudioContentDescription(
  353. SessionDescription* sdesc);
  354. VideoContentDescription* GetFirstVideoContentDescription(
  355. SessionDescription* sdesc);
  356. RtpDataContentDescription* GetFirstRtpDataContentDescription(
  357. SessionDescription* sdesc);
  358. SctpDataContentDescription* GetFirstSctpDataContentDescription(
  359. SessionDescription* sdesc);
  360. // Helper functions to return crypto suites used for SDES.
  361. void GetSupportedAudioSdesCryptoSuites(
  362. const webrtc::CryptoOptions& crypto_options,
  363. std::vector<int>* crypto_suites);
  364. void GetSupportedVideoSdesCryptoSuites(
  365. const webrtc::CryptoOptions& crypto_options,
  366. std::vector<int>* crypto_suites);
  367. void GetSupportedDataSdesCryptoSuites(
  368. const webrtc::CryptoOptions& crypto_options,
  369. std::vector<int>* crypto_suites);
  370. void GetSupportedAudioSdesCryptoSuiteNames(
  371. const webrtc::CryptoOptions& crypto_options,
  372. std::vector<std::string>* crypto_suite_names);
  373. void GetSupportedVideoSdesCryptoSuiteNames(
  374. const webrtc::CryptoOptions& crypto_options,
  375. std::vector<std::string>* crypto_suite_names);
  376. void GetSupportedDataSdesCryptoSuiteNames(
  377. const webrtc::CryptoOptions& crypto_options,
  378. std::vector<std::string>* crypto_suite_names);
  379. } // namespace cricket
  380. #endif // PC_MEDIA_SESSION_H_