From 12ce35afa003e5d5cef83fdac31b29b5d4747f2a Mon Sep 17 00:00:00 2001
From: Emil Ivov <emcho@jitsi.org>
Date: Tue, 15 Jan 2013 01:28:07 +0000
Subject: [PATCH] Moves format matching from AsbtractMediaStream into
 MediaFormat and MediaFormatImpl

---
 .../format/MediaFormatFactoryImpl.java        |  3 +-
 .../impl/neomedia/format/MediaFormatImpl.java | 83 +++++++++++++++++++
 .../service/neomedia/AbstractMediaStream.java | 58 -------------
 .../service/neomedia/format/MediaFormat.java  | 43 ++++++++++
 4 files changed, 127 insertions(+), 60 deletions(-)

diff --git a/src/org/jitsi/impl/neomedia/format/MediaFormatFactoryImpl.java b/src/org/jitsi/impl/neomedia/format/MediaFormatFactoryImpl.java
index f9d93d23..3d50966d 100644
--- a/src/org/jitsi/impl/neomedia/format/MediaFormatFactoryImpl.java
+++ b/src/org/jitsi/impl/neomedia/format/MediaFormatFactoryImpl.java
@@ -173,8 +173,7 @@ private MediaFormat createMediaFormat(
              * clockRate. We just want to make sure that the channels and the
              * format parameters match.
              */
-            if (AbstractMediaStream.matches(
-                    format,
+            if (format.matches(
                     format.getMediaType(),
                     format.getEncoding(), format.getClockRate(), channels,
                     fmtps))
diff --git a/src/org/jitsi/impl/neomedia/format/MediaFormatImpl.java b/src/org/jitsi/impl/neomedia/format/MediaFormatImpl.java
index b79507f2..29dc9cee 100644
--- a/src/org/jitsi/impl/neomedia/format/MediaFormatImpl.java
+++ b/src/org/jitsi/impl/neomedia/format/MediaFormatImpl.java
@@ -608,4 +608,87 @@ public String toString()
 
         return str.toString();
     }
+
+    /**
+     * Determines whether this <tt>MediaFormat</tt> matches properties of a
+     * specific <tt>MediaFormat</tt>, such as <tt>mediaType</tt>,
+     * <tt>encoding</tt>, <tt>clockRate</tt> and <tt>channels</tt> for
+     * <tt>MediaFormat</tt>s with <tt>mediaType</tt> equal to
+     * {@link MediaType#AUDIO}.
+     *
+     * @param format the {@link MediaFormat} whose properties we'd like to
+     * examine and compare with ours.
+     */
+    public boolean matches(MediaFormat format)
+    {
+        if(format == null)
+            return false;
+
+        MediaType mediaType = format.getMediaType();
+        String encoding = format.getEncoding();
+        double clockRate = format.getClockRate();
+        int channels = MediaType.AUDIO.equals(mediaType)
+                ? ((AudioMediaFormat) format).getChannels()
+                : MediaFormatFactory.CHANNELS_NOT_SPECIFIED;
+        Map<String, String> formatParameters = format.getFormatParameters();
+
+        return matches(
+            mediaType, encoding, clockRate, channels, formatParameters);
+    }
+
+    /**
+     * Determines whether this <tt>MediaFormat</tt> has specific values
+     * for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
+     * <tt>clockRate</tt> and <tt>channels</tt> for <tt>MediaFormat</tt>s with
+     * <tt>mediaType</tt> equal to {@link MediaType#AUDIO}.
+     *
+     * @param mediaType the type we expect {@link MediaFormat} to have
+     * @param encoding the encoding we are looking for.
+     * @param clockRate the clock rate that we'd like the format to have.
+     * @param channels the number of channels that expect to find in this format
+     * @param formatParameters the format parameters expected to match these of
+     * the specified <tt>format</tt>
+     * @return <tt>true</tt> if the specified <tt>format</tt> has specific
+     * values for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
+     * <tt>clockRate</tt> and <tt>channels</tt>; otherwise, <tt>false</tt>
+     */
+    public boolean matches(MediaType mediaType,
+                           String encoding,
+                           double clockRate,
+                           int channels,
+                           Map<String, String> formatParameters)
+    {
+        // mediaType
+        // encoding
+        if (!getMediaType().equals(mediaType)
+                || !getEncoding().equals(encoding))
+            return false;
+
+        // clockRate
+        if (clockRate != MediaFormatFactory.CLOCK_RATE_NOT_SPECIFIED)
+        {
+            double formatClockRate = getClockRate();
+
+            if ((formatClockRate != MediaFormatFactory.CLOCK_RATE_NOT_SPECIFIED)
+                    && (formatClockRate != clockRate))
+                return false;
+        }
+
+        // channels
+        if (MediaType.AUDIO.equals(mediaType))
+        {
+            if (channels == MediaFormatFactory.CHANNELS_NOT_SPECIFIED)
+                channels = 1;
+
+            int formatChannels = ((AudioMediaFormat) this).getChannels();
+
+            if (formatChannels == MediaFormatFactory.CHANNELS_NOT_SPECIFIED)
+                formatChannels = 1;
+            if (formatChannels != channels)
+                return false;
+        }
+
+        // formatParameters
+        return formatParametersMatch(formatParameters);
+    }
 }
diff --git a/src/org/jitsi/service/neomedia/AbstractMediaStream.java b/src/org/jitsi/service/neomedia/AbstractMediaStream.java
index c766a485..4cb77264 100644
--- a/src/org/jitsi/service/neomedia/AbstractMediaStream.java
+++ b/src/org/jitsi/service/neomedia/AbstractMediaStream.java
@@ -83,64 +83,6 @@ public String getName()
         return name;
     }
 
-    /**
-     * Determines whether a specific <tt>MediaFormat</tt> has specific values
-     * for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
-     * <tt>clockRate</tt> and <tt>channels</tt> for <tt>MediaFormat</tt>s with
-     * <tt>mediaType</tt> equal to {@link MediaType#AUDIO}.
-     *
-     * @param format the {@link MediaFormat} whose properties we'd like to
-     * examine
-     * @param mediaType the type we expect {@link MediaFormat} to have
-     * @param encoding the encoding we are looking for.
-     * @param clockRate the clock rate that we'd like the format to have.
-     * @param channels the number of channels that expect to find in this format
-     * @param formatParameters the format parameters expected to match these of
-     * the specified <tt>format</tt>
-     * @return <tt>true</tt> if the specified <tt>format</tt> has specific
-     * values for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
-     * <tt>clockRate</tt> and <tt>channels</tt>; otherwise, <tt>false</tt>
-     */
-    public static boolean matches(
-            MediaFormat format,
-            MediaType mediaType,
-            String encoding, double clockRate,  int channels,
-            Map<String, String> formatParameters)
-    {
-        // mediaType
-        // encoding
-        if (!format.getMediaType().equals(mediaType)
-                || !format.getEncoding().equals(encoding))
-            return false;
-
-        // clockRate
-        if (clockRate != MediaFormatFactory.CLOCK_RATE_NOT_SPECIFIED)
-        {
-            double formatClockRate = format.getClockRate();
-
-            if ((formatClockRate != MediaFormatFactory.CLOCK_RATE_NOT_SPECIFIED)
-                    && (formatClockRate != clockRate))
-                return false;
-        }
-
-        // channels
-        if (MediaType.AUDIO.equals(mediaType))
-        {
-            if (channels == MediaFormatFactory.CHANNELS_NOT_SPECIFIED)
-                channels = 1;
-
-            int formatChannels = ((AudioMediaFormat) format).getChannels();
-
-            if (formatChannels == MediaFormatFactory.CHANNELS_NOT_SPECIFIED)
-                formatChannels = 1;
-            if (formatChannels != channels)
-                return false;
-        }
-
-        // formatParameters
-        return format.formatParametersMatch(formatParameters);
-    }
-
     /**
      * Removes the specified <tt>PropertyChangeListener</tt> from this stream so
      * that it won't receive further property change events.
diff --git a/src/org/jitsi/service/neomedia/format/MediaFormat.java b/src/org/jitsi/service/neomedia/format/MediaFormat.java
index 4d7a34c5..2da0c165 100644
--- a/src/org/jitsi/service/neomedia/format/MediaFormat.java
+++ b/src/org/jitsi/service/neomedia/format/MediaFormat.java
@@ -171,4 +171,47 @@ public interface MediaFormat
      */
     @Override
     public String toString();
+
+    /**
+     * Determines whether this <tt>MediaFormat</tt> matches properties of a
+     * specific <tt>MediaFormat</tt>, such as <tt>mediaType</tt>,
+     * <tt>encoding</tt>, <tt>clockRate</tt> and <tt>channels</tt> for
+     * <tt>MediaFormat</tt>s with <tt>mediaType</tt> equal to
+     * {@link MediaType#AUDIO}.
+     *
+     * @param format the {@link MediaFormat} whose properties we'd like to
+     * examine
+     * @param mediaType the type we expect {@link MediaFormat} to have
+     * @param encoding the encoding we are looking for.
+     * @param clockRate the clock rate that we'd like the format to have.
+     * @param channels the number of channels that expect to find in this format
+     * @param formatParameters the format parameters expected to match these of
+     * the specified <tt>format</tt>
+     * @return <tt>true</tt> if the specified <tt>format</tt> has specific
+     * values for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
+     * <tt>clockRate</tt> and <tt>channels</tt>; otherwise, <tt>false</tt>
+     */
+    public boolean matches(MediaFormat format);
+
+    /**
+     * Determines whether this <tt>MediaFormat</tt> has specific values
+     * for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
+     * <tt>clockRate</tt> and <tt>channels</tt> for <tt>MediaFormat</tt>s with
+     * <tt>mediaType</tt> equal to {@link MediaType#AUDIO}.
+     *
+     * @param mediaType the type we expect {@link MediaFormat} to have
+     * @param encoding the encoding we are looking for.
+     * @param clockRate the clock rate that we'd like the format to have.
+     * @param channels the number of channels that expect to find in this format
+     * @param formatParameters the format parameters expected to match these of
+     * the specified <tt>format</tt>
+     * @return <tt>true</tt> if the specified <tt>format</tt> has specific
+     * values for its properties <tt>mediaType</tt>, <tt>encoding</tt>,
+     * <tt>clockRate</tt> and <tt>channels</tt>; otherwise, <tt>false</tt>
+     */
+    public boolean matches(MediaType mediaType,
+                           String encoding,
+                           double clockRate,
+                           int channels,
+                           Map<String, String> formatParameters);
 }
-- 
GitLab