Bug 1142105 - Part 2: Move some stuff from JsepCodecDescription into /sdp r=mt
authorByron Campen [:bwc] <docfaraday@gmail.com>
Thu, 23 Jul 2015 16:55:08 -0500
changeset 286815 bf7d6315fd6b616ebec2e0ae7febeeaf53168b4f
parent 286814 b65c048414f701e3526f13719ca97f15951eeefb
child 286816 07fc7b5295783a9cf45f44c825ae510b16c8cb74
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmt
bugs1142105
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1142105 - Part 2: Move some stuff from JsepCodecDescription into /sdp r=mt
media/webrtc/signaling/src/jsep/JsepCodecDescription.h
media/webrtc/signaling/src/jsep/JsepSessionImpl.cpp
media/webrtc/signaling/src/sdp/SdpHelper.cpp
media/webrtc/signaling/src/sdp/SdpHelper.h
media/webrtc/signaling/src/sdp/SdpMediaSection.h
--- a/media/webrtc/signaling/src/jsep/JsepCodecDescription.h
+++ b/media/webrtc/signaling/src/jsep/JsepCodecDescription.h
@@ -2,18 +2,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef _JSEPCODECDESCRIPTION_H_
 #define _JSEPCODECDESCRIPTION_H_
 
 #include <iostream>
 #include <string>
-#include <string.h>
 #include "signaling/src/sdp/SdpMediaSection.h"
+#include "signaling/src/sdp/SdpHelper.h"
 #include "nsCRT.h"
 
 namespace mozilla {
 
 #define JSEP_CODEC_CLONE(T)                                                    \
   virtual JsepCodecDescription* Clone() const override                         \
   {                                                                            \
     return new T(*this);                                                       \
@@ -37,57 +37,36 @@ struct JsepCodecDescription {
   {
   }
   virtual ~JsepCodecDescription() {}
 
   virtual JsepCodecDescription* Clone() const = 0;
   virtual void AddFmtps(SdpFmtpAttributeList& fmtp) const = 0;
   virtual void AddRtcpFbs(SdpRtcpFbAttributeList& rtcpfb) const = 0;
 
-  // TODO(bug 1142105): This probably should be a helper function in
-  // /sdp
-  static bool
-  GetPtAsInt(const std::string& ptString, uint16_t* ptOutparam)
-  {
-    char* end;
-    unsigned long pt = strtoul(ptString.c_str(), &end, 10);
-    size_t length = static_cast<size_t>(end - ptString.c_str());
-    if ((pt > UINT16_MAX) || (length != ptString.size())) {
-      return false;
-    }
-    *ptOutparam = pt;
-    return true;
-  }
-
   bool
   GetPtAsInt(uint16_t* ptOutparam) const
   {
-    return GetPtAsInt(mDefaultPt, ptOutparam);
+    return SdpHelper::GetPtAsInt(mDefaultPt, ptOutparam);
   }
 
   virtual bool
   Matches(const std::string& fmt, const SdpMediaSection& remoteMsection) const
   {
-    auto& attrs = remoteMsection.GetAttributeList();
-    if (!attrs.HasAttribute(SdpAttribute::kRtpmapAttribute)) {
+    if (mType != remoteMsection.GetMediaType()) {
       return false;
     }
 
-    const SdpRtpmapAttributeList& rtpmap = attrs.GetRtpmap();
-    if (!rtpmap.HasEntry(fmt)) {
-      return false;
-    }
+    const SdpRtpmapAttributeList::Rtpmap* entry(remoteMsection.FindRtpmap(fmt));
 
-    const SdpRtpmapAttributeList::Rtpmap& entry = rtpmap.GetEntry(fmt);
-
-    if (mType == remoteMsection.GetMediaType()
-        && !nsCRT::strcasecmp(mName.c_str(), entry.name.c_str())
-        && (mClock == entry.clock)
-        && (mChannels == entry.channels)) {
-      return ParametersMatch(entry.pt, remoteMsection);
+    if (entry
+        && !nsCRT::strcasecmp(mName.c_str(), entry->name.c_str())
+        && (mClock == entry->clock)
+        && (mChannels == entry->channels)) {
+      return ParametersMatch(fmt, remoteMsection);
     }
     return false;
   }
 
   virtual bool
   ParametersMatch(const std::string& fmt,
                   const SdpMediaSection& remoteMsection) const
   {
@@ -95,35 +74,16 @@ struct JsepCodecDescription {
   }
 
   virtual bool
   Negotiate(const SdpMediaSection& remoteMsection)
   {
     return true;
   }
 
-  // TODO(bug 1142105): This probably should be a helper function in
-  // /sdp
-  static const SdpFmtpAttributeList::Parameters*
-  FindParameters(const std::string& pt,
-                 const mozilla::SdpMediaSection& remoteMsection)
-  {
-    const SdpAttributeList& attrs = remoteMsection.GetAttributeList();
-
-    if (attrs.HasAttribute(SdpAttribute::kFmtpAttribute)) {
-      const SdpFmtpAttributeList& fmtps = attrs.GetFmtp();
-      for (auto i = fmtps.mFmtps.begin(); i != fmtps.mFmtps.end(); ++i) {
-        if (i->format == pt && i->parameters) {
-          return i->parameters.get();
-        }
-      }
-    }
-    return nullptr;
-  }
-
   virtual bool LoadSendParameters(
       const mozilla::SdpMediaSection& remoteMsection)
   {
     return true;
   }
 
   virtual bool LoadRecvParameters(
       const mozilla::SdpMediaSection& remoteMsection)
@@ -302,17 +262,17 @@ struct JsepVideoCodecDescription : publi
   }
 
   SdpFmtpAttributeList::H264Parameters
   GetH264Parameters(const std::string& pt,
                     const SdpMediaSection& msection) const
   {
     // Will contain defaults if nothing else
     SdpFmtpAttributeList::H264Parameters result;
-    auto* params = FindParameters(pt, msection);
+    auto* params = msection.FindFmtp(pt);
 
     if (params && params->codec_type == SdpRtpmapAttributeList::kH264) {
       result =
         static_cast<const SdpFmtpAttributeList::H264Parameters&>(*params);
     }
 
     return result;
   }
@@ -323,59 +283,34 @@ struct JsepVideoCodecDescription : publi
   {
     SdpRtpmapAttributeList::CodecType expectedType(
         mName == "VP8" ?
         SdpRtpmapAttributeList::kVP8 :
         SdpRtpmapAttributeList::kVP9);
 
     // Will contain defaults if nothing else
     SdpFmtpAttributeList::VP8Parameters result(expectedType);
-    auto* params = FindParameters(pt, msection);
+    auto* params = msection.FindFmtp(pt);
 
     if (params && params->codec_type == expectedType) {
       result =
         static_cast<const SdpFmtpAttributeList::VP8Parameters&>(*params);
     }
 
     return result;
   }
 
-  static bool
-  HasRtcpFb(const SdpMediaSection& msection,
-            const std::string& pt,
-            SdpRtcpFbAttributeList::Type type,
-            const std::string& subType)
-  {
-    const SdpAttributeList& attrs(msection.GetAttributeList());
-
-    if (!attrs.HasAttribute(SdpAttribute::kRtcpFbAttribute)) {
-      return false;
-    }
-
-    for (auto& rtcpfb : attrs.GetRtcpFb().mFeedbacks) {
-      if (rtcpfb.type == type) {
-        if (rtcpfb.pt == "*" || rtcpfb.pt == pt) {
-          if (rtcpfb.parameter == subType) {
-            return true;
-          }
-        }
-      }
-    }
-
-    return false;
-  }
-
   void
   NegotiateRtcpFb(const SdpMediaSection& remoteMsection,
                   SdpRtcpFbAttributeList::Type type,
                   std::vector<std::string>* supportedTypes)
   {
     std::vector<std::string> temp;
     for (auto& subType : *supportedTypes) {
-      if (HasRtcpFb(remoteMsection, mDefaultPt, type, subType)) {
+      if (remoteMsection.HasRtcpFb(mDefaultPt, type, subType)) {
         temp.push_back(subType);
       }
     }
     *supportedTypes = temp;
   }
 
   void
   NegotiateRtcpFb(const SdpMediaSection& remote)
@@ -686,30 +621,24 @@ struct JsepApplicationCodecDescription :
 
   JSEP_CODEC_CLONE(JsepApplicationCodecDescription)
 
   // Override, uses sctpmap instead of rtpmap
   virtual bool
   Matches(const std::string& fmt,
           const SdpMediaSection& remoteMsection) const override
   {
-    auto& attrs = remoteMsection.GetAttributeList();
-    if (!attrs.HasAttribute(SdpAttribute::kSctpmapAttribute)) {
+    if (mType != remoteMsection.GetMediaType()) {
       return false;
     }
 
-    const SdpSctpmapAttributeList& sctpmap = attrs.GetSctpmap();
-    if (!sctpmap.HasEntry(fmt)) {
-      return false;
-    }
+    const SdpSctpmapAttributeList::Sctpmap* entry(
+        remoteMsection.FindSctpmap(fmt));
 
-    const SdpSctpmapAttributeList::Sctpmap& entry = sctpmap.GetEntry(fmt);
-
-    if (mType == remoteMsection.GetMediaType() &&
-        !nsCRT::strcasecmp(mName.c_str(), entry.name.c_str())) {
+    if (entry && !nsCRT::strcasecmp(mName.c_str(), entry->name.c_str())) {
       return true;
     }
     return false;
   }
 };
 
 } // namespace mozilla
 
--- a/media/webrtc/signaling/src/jsep/JsepSessionImpl.cpp
+++ b/media/webrtc/signaling/src/jsep/JsepSessionImpl.cpp
@@ -1757,17 +1757,17 @@ JsepSessionImpl::ParseSdp(const std::str
                      "\"holdconn\" at level "
                      << i);
       return NS_ERROR_INVALID_ARG;
     }
 
     auto& formats = parsed->GetMediaSection(i).GetFormats();
     for (auto f = formats.begin(); f != formats.end(); ++f) {
       uint16_t pt;
-      if (!JsepCodecDescription::GetPtAsInt(*f, &pt)) {
+      if (!SdpHelper::GetPtAsInt(*f, &pt)) {
         JSEP_SET_ERROR("Payload type \""
                        << *f << "\" is not a 16-bit unsigned int at level "
                        << i);
         return NS_ERROR_INVALID_ARG;
       }
     }
 
     std::string streamId;
--- a/media/webrtc/signaling/src/sdp/SdpHelper.cpp
+++ b/media/webrtc/signaling/src/sdp/SdpHelper.cpp
@@ -5,18 +5,19 @@
 #include "signaling/src/sdp/SdpHelper.h"
 
 #include "signaling/src/sdp/Sdp.h"
 #include "signaling/src/sdp/SdpMediaSection.h"
 #include "logging.h"
 
 #include "nsDebug.h"
 #include "nsError.h"
-#include "nspr/prprf.h"
+#include "prprf.h"
 
+#include <string.h>
 #include <set>
 
 namespace mozilla {
 MOZ_MTLOG_MODULE("sdp")
 
 #define SDP_SET_ERROR(error)                                                   \
   do {                                                                         \
     std::ostringstream os;                                                     \
@@ -585,11 +586,24 @@ SdpHelper::appendSdpParseErrors(
   std::ostringstream os;
   for (auto i = aErrors.begin(); i != aErrors.end(); ++i) {
     os << "SDP Parse Error on line " << i->first << ": " + i->second
        << std::endl;
   }
   *aErrorString += os.str();
 }
 
+/* static */ bool
+SdpHelper::GetPtAsInt(const std::string& ptString, uint16_t* ptOutparam)
+{
+  char* end;
+  unsigned long pt = strtoul(ptString.c_str(), &end, 10);
+  size_t length = static_cast<size_t>(end - ptString.c_str());
+  if ((pt > UINT16_MAX) || (length != ptString.size())) {
+    return false;
+  }
+  *ptOutparam = pt;
+  return true;
+}
+
 } // namespace mozilla
 
 
--- a/media/webrtc/signaling/src/sdp/SdpHelper.h
+++ b/media/webrtc/signaling/src/sdp/SdpHelper.h
@@ -1,15 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef _SDPHELPER_H_
 #define _SDPHELPER_H_
 
+#include "nsError.h"
+
 #include "signaling/src/sdp/SdpMediaSection.h"
 #include "signaling/src/sdp/SdpAttribute.h"
 
 #include "m_cpp_utils.h"
 
 #include <string>
 #include <map>
 #include <vector>
@@ -83,16 +85,18 @@ class SdpHelper {
                               SdpMediaSection* dest);
     bool HasRtcp(SdpMediaSection::Protocol proto) const;
     SdpMediaSection::Protocol GetProtocolForMediaType(
         SdpMediaSection::MediaType type);
     void appendSdpParseErrors(
           const std::vector<std::pair<size_t, std::string> >& aErrors,
           std::string* aErrorString);
 
+    static bool GetPtAsInt(const std::string& ptString, uint16_t* ptOutparam);
+
   private:
     std::string& mLastError;
 
     DISALLOW_COPY_ASSIGN(SdpHelper);
 };
 } // namespace mozilla
 
 #endif // _SDPHELPER_H_
--- a/media/webrtc/signaling/src/sdp/SdpMediaSection.h
+++ b/media/webrtc/signaling/src/sdp/SdpMediaSection.h
@@ -143,16 +143,90 @@ public:
     }
   }
 
   inline void SetDirection(SdpDirectionAttribute::Direction direction)
   {
     GetAttributeList().SetAttribute(new SdpDirectionAttribute(direction));
   }
 
+  const SdpFmtpAttributeList::Parameters*
+  FindFmtp(const std::string& pt) const
+  {
+    const SdpAttributeList& attrs = GetAttributeList();
+
+    if (attrs.HasAttribute(SdpAttribute::kFmtpAttribute)) {
+      const SdpFmtpAttributeList& fmtps = attrs.GetFmtp();
+      for (auto i = fmtps.mFmtps.begin(); i != fmtps.mFmtps.end(); ++i) {
+        if (i->format == pt && i->parameters) {
+          return i->parameters.get();
+        }
+      }
+    }
+    return nullptr;
+  }
+
+  const SdpRtpmapAttributeList::Rtpmap*
+  FindRtpmap(const std::string& pt) const
+  {
+    auto& attrs = GetAttributeList();
+    if (!attrs.HasAttribute(SdpAttribute::kRtpmapAttribute)) {
+      return nullptr;
+    }
+
+    const SdpRtpmapAttributeList& rtpmap = attrs.GetRtpmap();
+    if (!rtpmap.HasEntry(pt)) {
+      return nullptr;
+    }
+
+    return &rtpmap.GetEntry(pt);
+  }
+
+  const SdpSctpmapAttributeList::Sctpmap*
+  FindSctpmap(const std::string& pt) const
+  {
+    auto& attrs = GetAttributeList();
+    if (!attrs.HasAttribute(SdpAttribute::kSctpmapAttribute)) {
+      return nullptr;
+    }
+
+    const SdpSctpmapAttributeList& sctpmap = attrs.GetSctpmap();
+    if (!sctpmap.HasEntry(pt)) {
+      return nullptr;
+    }
+
+    return &sctpmap.GetEntry(pt);
+  }
+
+  bool
+  HasRtcpFb(const std::string& pt,
+            SdpRtcpFbAttributeList::Type type,
+            const std::string& subType) const
+  {
+    const SdpAttributeList& attrs(GetAttributeList());
+
+    if (!attrs.HasAttribute(SdpAttribute::kRtcpFbAttribute)) {
+      return false;
+    }
+
+    for (auto& rtcpfb : attrs.GetRtcpFb().mFeedbacks) {
+      if (rtcpfb.type == type) {
+        if (rtcpfb.pt == "*" || rtcpfb.pt == pt) {
+          if (rtcpfb.parameter == subType) {
+            return true;
+          }
+        }
+      }
+    }
+
+    return false;
+  }
+
+
+
 private:
   size_t mLevel;
 };
 
 inline std::ostream& operator<<(std::ostream& os, const SdpMediaSection& ms)
 {
   ms.Serialize(os);
   return os;