sdp_utils.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "pch.h"
  2. #include "../common/comm.h"
  3. #include "sdp_utils.h"
  4. #include "api/jsep_session_description.h"
  5. #include "pc/session_description.h"
  6. #include "pc/webrtc_sdp.h"
  7. std::string SdpForceCodecs(const std::string& message,
  8. const std::string& audio_codec_name,
  9. const std::string& video_codec_name) {
  10. // Deserialize the SDP message
  11. webrtc::JsepSessionDescription jdesc(webrtc::SdpType::kOffer);
  12. webrtc::SdpParseError error;
  13. if (!webrtc::SdpDeserialize(message, &jdesc, &error)) {
  14. RTC_LOG(LS_WARNING)
  15. << "Failed to deserialize SDP message to force codecs. Error line "
  16. << error.line << ": " << error.description;
  17. return message;
  18. }
  19. // Remove codecs not wanted
  20. {
  21. // Loop over the session contents to find the audio and video ones
  22. const cricket::SessionDescription* const desc = jdesc.description();
  23. const cricket::ContentInfos& contents = desc->contents();
  24. for (auto&& content : contents) {
  25. cricket::MediaContentDescription* media_desc = (cricket::MediaContentDescription*)content.media_description();
  26. switch (media_desc->type()) {
  27. case cricket::MediaType::MEDIA_TYPE_AUDIO:
  28. // Only try modify the audio codecs if asked for
  29. if (!audio_codec_name.empty()) {
  30. cricket::AudioContentDescription* const audio_desc =
  31. media_desc->as_audio();
  32. const std::vector<cricket::AudioCodec>& codecs =
  33. audio_desc->codecs();
  34. auto it = std::find_if(
  35. codecs.begin(), codecs.end(),
  36. [&audio_codec_name](const cricket::AudioCodec& codec) {
  37. return (codec.name == audio_codec_name);
  38. });
  39. if (it == codecs.end()) {
  40. break;
  41. }
  42. std::vector<cricket::AudioCodec> new_codecs;
  43. new_codecs.push_back(*it);
  44. audio_desc->set_codecs(new_codecs);
  45. }
  46. break;
  47. case cricket::MediaType::MEDIA_TYPE_VIDEO:
  48. // Only try modify the audio codecs if asked for
  49. if (!video_codec_name.empty()) {
  50. cricket::VideoContentDescription* const video_desc =
  51. media_desc->as_video();
  52. const std::vector<cricket::VideoCodec>& codecs =
  53. video_desc->codecs();
  54. auto it = std::find_if(
  55. codecs.begin(), codecs.end(),
  56. [&video_codec_name](const cricket::VideoCodec& codec) {
  57. return (codec.name == video_codec_name);
  58. });
  59. if (it == codecs.end()) {
  60. break;
  61. }
  62. std::vector<cricket::VideoCodec> new_codecs;
  63. new_codecs.push_back(*it);
  64. video_desc->set_codecs(new_codecs);
  65. }
  66. break;
  67. case cricket::MediaType::MEDIA_TYPE_DATA:
  68. continue;
  69. }
  70. }
  71. }
  72. // Re-serialize the SDP modified message
  73. return webrtc::SdpSerialize(jdesc);
  74. }