media_session.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions;
  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. const VideoCodecs& video_sendrecv_codecs() const;
  131. const VideoCodecs& video_send_codecs() const;
  132. const VideoCodecs& video_recv_codecs() const;
  133. void set_video_codecs(const VideoCodecs& send_codecs,
  134. const VideoCodecs& recv_codecs);
  135. RtpHeaderExtensions filtered_rtp_header_extensions(
  136. RtpHeaderExtensions extensions) const;
  137. const RtpDataCodecs& rtp_data_codecs() const { return rtp_data_codecs_; }
  138. void set_rtp_data_codecs(const RtpDataCodecs& codecs) {
  139. rtp_data_codecs_ = codecs;
  140. }
  141. SecurePolicy secure() const { return secure_; }
  142. void set_secure(SecurePolicy s) { secure_ = s; }
  143. void set_enable_encrypted_rtp_header_extensions(bool enable) {
  144. enable_encrypted_rtp_header_extensions_ = enable;
  145. }
  146. void set_is_unified_plan(bool is_unified_plan) {
  147. is_unified_plan_ = is_unified_plan;
  148. }
  149. std::unique_ptr<SessionDescription> CreateOffer(
  150. const MediaSessionOptions& options,
  151. const SessionDescription* current_description) const;
  152. std::unique_ptr<SessionDescription> CreateAnswer(
  153. const SessionDescription* offer,
  154. const MediaSessionOptions& options,
  155. const SessionDescription* current_description) const;
  156. private:
  157. struct AudioVideoRtpHeaderExtensions {
  158. RtpHeaderExtensions audio;
  159. RtpHeaderExtensions video;
  160. };
  161. const AudioCodecs& GetAudioCodecsForOffer(
  162. const webrtc::RtpTransceiverDirection& direction) const;
  163. const AudioCodecs& GetAudioCodecsForAnswer(
  164. const webrtc::RtpTransceiverDirection& offer,
  165. const webrtc::RtpTransceiverDirection& answer) const;
  166. const VideoCodecs& GetVideoCodecsForOffer(
  167. const webrtc::RtpTransceiverDirection& direction) const;
  168. const VideoCodecs& GetVideoCodecsForAnswer(
  169. const webrtc::RtpTransceiverDirection& offer,
  170. const webrtc::RtpTransceiverDirection& answer) const;
  171. void GetCodecsForOffer(
  172. const std::vector<const ContentInfo*>& current_active_contents,
  173. AudioCodecs* audio_codecs,
  174. VideoCodecs* video_codecs,
  175. RtpDataCodecs* rtp_data_codecs) const;
  176. void GetCodecsForAnswer(
  177. const std::vector<const ContentInfo*>& current_active_contents,
  178. const SessionDescription& remote_offer,
  179. AudioCodecs* audio_codecs,
  180. VideoCodecs* video_codecs,
  181. RtpDataCodecs* rtp_data_codecs) const;
  182. AudioVideoRtpHeaderExtensions GetOfferedRtpHeaderExtensionsWithIds(
  183. const std::vector<const ContentInfo*>& current_active_contents,
  184. bool extmap_allow_mixed,
  185. const std::vector<MediaDescriptionOptions>& media_description_options)
  186. const;
  187. bool AddTransportOffer(const std::string& content_name,
  188. const TransportOptions& transport_options,
  189. const SessionDescription* current_desc,
  190. SessionDescription* offer,
  191. IceCredentialsIterator* ice_credentials) const;
  192. std::unique_ptr<TransportDescription> CreateTransportAnswer(
  193. const std::string& content_name,
  194. const SessionDescription* offer_desc,
  195. const TransportOptions& transport_options,
  196. const SessionDescription* current_desc,
  197. bool require_transport_attributes,
  198. IceCredentialsIterator* ice_credentials) const;
  199. bool AddTransportAnswer(const std::string& content_name,
  200. const TransportDescription& transport_desc,
  201. SessionDescription* answer_desc) const;
  202. // Helpers for adding media contents to the SessionDescription. Returns true
  203. // it succeeds or the media content is not needed, or false if there is any
  204. // error.
  205. bool AddAudioContentForOffer(
  206. const MediaDescriptionOptions& media_description_options,
  207. const MediaSessionOptions& session_options,
  208. const ContentInfo* current_content,
  209. const SessionDescription* current_description,
  210. const RtpHeaderExtensions& audio_rtp_extensions,
  211. const AudioCodecs& audio_codecs,
  212. StreamParamsVec* current_streams,
  213. SessionDescription* desc,
  214. IceCredentialsIterator* ice_credentials) const;
  215. bool AddVideoContentForOffer(
  216. const MediaDescriptionOptions& media_description_options,
  217. const MediaSessionOptions& session_options,
  218. const ContentInfo* current_content,
  219. const SessionDescription* current_description,
  220. const RtpHeaderExtensions& video_rtp_extensions,
  221. const VideoCodecs& video_codecs,
  222. StreamParamsVec* current_streams,
  223. SessionDescription* desc,
  224. IceCredentialsIterator* ice_credentials) const;
  225. bool AddSctpDataContentForOffer(
  226. const MediaDescriptionOptions& media_description_options,
  227. const MediaSessionOptions& session_options,
  228. const ContentInfo* current_content,
  229. const SessionDescription* current_description,
  230. StreamParamsVec* current_streams,
  231. SessionDescription* desc,
  232. IceCredentialsIterator* ice_credentials) const;
  233. bool AddRtpDataContentForOffer(
  234. const MediaDescriptionOptions& media_description_options,
  235. const MediaSessionOptions& session_options,
  236. const ContentInfo* current_content,
  237. const SessionDescription* current_description,
  238. const RtpDataCodecs& rtp_data_codecs,
  239. StreamParamsVec* current_streams,
  240. SessionDescription* desc,
  241. IceCredentialsIterator* ice_credentials) const;
  242. // This function calls either AddRtpDataContentForOffer or
  243. // AddSctpDataContentForOffer depending on protocol.
  244. // The codecs argument is ignored for SCTP.
  245. bool AddDataContentForOffer(
  246. const MediaDescriptionOptions& media_description_options,
  247. const MediaSessionOptions& session_options,
  248. const ContentInfo* current_content,
  249. const SessionDescription* current_description,
  250. const RtpDataCodecs& rtp_data_codecs,
  251. StreamParamsVec* current_streams,
  252. SessionDescription* desc,
  253. IceCredentialsIterator* ice_credentials) const;
  254. bool AddAudioContentForAnswer(
  255. const MediaDescriptionOptions& media_description_options,
  256. const MediaSessionOptions& session_options,
  257. const ContentInfo* offer_content,
  258. const SessionDescription* offer_description,
  259. const ContentInfo* current_content,
  260. const SessionDescription* current_description,
  261. const TransportInfo* bundle_transport,
  262. const AudioCodecs& audio_codecs,
  263. const RtpHeaderExtensions& default_audio_rtp_header_extensions,
  264. StreamParamsVec* current_streams,
  265. SessionDescription* answer,
  266. IceCredentialsIterator* ice_credentials) const;
  267. bool AddVideoContentForAnswer(
  268. const MediaDescriptionOptions& media_description_options,
  269. const MediaSessionOptions& session_options,
  270. const ContentInfo* offer_content,
  271. const SessionDescription* offer_description,
  272. const ContentInfo* current_content,
  273. const SessionDescription* current_description,
  274. const TransportInfo* bundle_transport,
  275. const VideoCodecs& video_codecs,
  276. const RtpHeaderExtensions& default_video_rtp_header_extensions,
  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. VideoCodecs video_send_codecs_;
  302. VideoCodecs video_recv_codecs_;
  303. // Intersection of send and recv.
  304. VideoCodecs video_sendrecv_codecs_;
  305. // Union of send and recv.
  306. VideoCodecs all_video_codecs_;
  307. RtpDataCodecs rtp_data_codecs_;
  308. // This object is not owned by the channel so it must outlive it.
  309. rtc::UniqueRandomIdGenerator* const ssrc_generator_;
  310. bool enable_encrypted_rtp_header_extensions_ = false;
  311. // TODO(zhihuang): Rename secure_ to sdec_policy_; rename the related getter
  312. // and setter.
  313. SecurePolicy secure_ = SEC_DISABLED;
  314. const TransportDescriptionFactory* transport_desc_factory_;
  315. };
  316. // Convenience functions.
  317. bool IsMediaContent(const ContentInfo* content);
  318. bool IsAudioContent(const ContentInfo* content);
  319. bool IsVideoContent(const ContentInfo* content);
  320. bool IsDataContent(const ContentInfo* content);
  321. const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
  322. MediaType media_type);
  323. const ContentInfo* GetFirstAudioContent(const ContentInfos& contents);
  324. const ContentInfo* GetFirstVideoContent(const ContentInfos& contents);
  325. const ContentInfo* GetFirstDataContent(const ContentInfos& contents);
  326. const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
  327. MediaType media_type);
  328. const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc);
  329. const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc);
  330. const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc);
  331. const AudioContentDescription* GetFirstAudioContentDescription(
  332. const SessionDescription* sdesc);
  333. const VideoContentDescription* GetFirstVideoContentDescription(
  334. const SessionDescription* sdesc);
  335. const RtpDataContentDescription* GetFirstRtpDataContentDescription(
  336. const SessionDescription* sdesc);
  337. const SctpDataContentDescription* GetFirstSctpDataContentDescription(
  338. const SessionDescription* sdesc);
  339. // Non-const versions of the above functions.
  340. // Useful when modifying an existing description.
  341. ContentInfo* GetFirstMediaContent(ContentInfos* contents, MediaType media_type);
  342. ContentInfo* GetFirstAudioContent(ContentInfos* contents);
  343. ContentInfo* GetFirstVideoContent(ContentInfos* contents);
  344. ContentInfo* GetFirstDataContent(ContentInfos* contents);
  345. ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
  346. MediaType media_type);
  347. ContentInfo* GetFirstAudioContent(SessionDescription* sdesc);
  348. ContentInfo* GetFirstVideoContent(SessionDescription* sdesc);
  349. ContentInfo* GetFirstDataContent(SessionDescription* sdesc);
  350. AudioContentDescription* GetFirstAudioContentDescription(
  351. SessionDescription* sdesc);
  352. VideoContentDescription* GetFirstVideoContentDescription(
  353. SessionDescription* sdesc);
  354. RtpDataContentDescription* GetFirstRtpDataContentDescription(
  355. SessionDescription* sdesc);
  356. SctpDataContentDescription* GetFirstSctpDataContentDescription(
  357. SessionDescription* sdesc);
  358. // Helper functions to return crypto suites used for SDES.
  359. void GetSupportedAudioSdesCryptoSuites(
  360. const webrtc::CryptoOptions& crypto_options,
  361. std::vector<int>* crypto_suites);
  362. void GetSupportedVideoSdesCryptoSuites(
  363. const webrtc::CryptoOptions& crypto_options,
  364. std::vector<int>* crypto_suites);
  365. void GetSupportedDataSdesCryptoSuites(
  366. const webrtc::CryptoOptions& crypto_options,
  367. std::vector<int>* crypto_suites);
  368. void GetSupportedAudioSdesCryptoSuiteNames(
  369. const webrtc::CryptoOptions& crypto_options,
  370. std::vector<std::string>* crypto_suite_names);
  371. void GetSupportedVideoSdesCryptoSuiteNames(
  372. const webrtc::CryptoOptions& crypto_options,
  373. std::vector<std::string>* crypto_suite_names);
  374. void GetSupportedDataSdesCryptoSuiteNames(
  375. const webrtc::CryptoOptions& crypto_options,
  376. std::vector<std::string>* crypto_suite_names);
  377. } // namespace cricket
  378. #endif // PC_MEDIA_SESSION_H_