From bba4cad50c6a9b724b247696a9368ef182b5df93 Mon Sep 17 00:00:00 2001
From: Boris Grozev <boris@jitsi.org>
Date: Sat, 27 Oct 2012 07:37:43 +0000
Subject: [PATCH] Refactors EncodingConfiguration to avoid it importing impl
 packages.

---
 .../codec/EncodingConfigurationImpl.java      | 129 ++++++++++++++++++
 .../neomedia/codec/EncodingConfiguration.java |  98 +------------
 2 files changed, 136 insertions(+), 91 deletions(-)

diff --git a/src/org/jitsi/impl/neomedia/codec/EncodingConfigurationImpl.java b/src/org/jitsi/impl/neomedia/codec/EncodingConfigurationImpl.java
index 627b9972..dbc69f90 100644
--- a/src/org/jitsi/impl/neomedia/codec/EncodingConfigurationImpl.java
+++ b/src/org/jitsi/impl/neomedia/codec/EncodingConfigurationImpl.java
@@ -6,10 +6,16 @@
  */
 package org.jitsi.impl.neomedia.codec;
 
+import java.util.*;
+
+import org.jitsi.impl.neomedia.*;
 import org.jitsi.impl.neomedia.format.*;
+import org.jitsi.service.neomedia.*;
 import org.jitsi.service.neomedia.codec.*;
+import org.jitsi.service.neomedia.format.*;
 import org.jitsi.util.*;
 
+
 /**
  * Configuration of encoding priorities.
  *
@@ -98,4 +104,127 @@ private void initializeFormatPreferences()
         // priority as it is not needed to order it with audio codecs
         setEncodingPreference(Constants.TELEPHONE_EVENT, 8000, 1);
     }
+
+    /**
+     * Sets <tt>pref</tt> as the preference associated with <tt>encoding</tt>.
+     * Use this method for both audio and video encodings and don't worry if
+     * preferences are equal since we rarely need to compare prefs of video
+     * encodings to those of audio encodings.
+     *
+     * @param encoding the SDP int of the encoding whose pref we're setting.
+     * @param clockRate clock rate
+     * @param pref a positive int indicating the preference for that encoding.
+     */
+    protected void setEncodingPreference(
+            String encoding, double clockRate,
+            int pref)
+    {
+        MediaFormat mediaFormat = null;
+
+        /*
+         * The key in encodingPreferences associated with a MediaFormat is
+         * currently composed of the encoding and the clockRate only so it makes
+         * sense to ignore the format parameters.
+         */
+        for (MediaFormat mf : MediaUtils.getMediaFormats(encoding))
+        {
+            if (mf.getClockRate() == clockRate)
+            {
+                mediaFormat = mf;
+                break;
+            }
+        }
+        if (mediaFormat != null)
+        {
+            encodingPreferences.put(
+                    getEncodingPreferenceKey(mediaFormat),
+                    pref);
+        }
+    }
+
+    /**
+     * Returns all the available encodings for a specific <tt>MediaType</tt>.
+     * This includes disabled ones (ones with priority 0).
+     *
+     * @param type the <tt>MediaType</tt> we would like to know the available
+     * encodings of
+     * @return array of <tt>MediaFormat</tt> supported for the
+     * <tt>MediaType</tt>
+     */
+    public MediaFormat[] getAllEncodings(MediaType type)
+    {
+        return MediaUtils.getMediaFormats(type);
+    }
+
+    /**
+     * Compares the two formats for order. Returns a negative integer, zero, or
+     * a positive integer as the first format has been assigned a preference
+     * higher, equal to, or greater than the one of the second.
+     *
+     * @param enc1 the first format to compare for preference.
+     * @param enc2 the second format to compare for preference
+     * @return a negative integer, zero, or a positive integer as the first
+     * format has been assigned a preference higher, equal to, or greater than
+     * the one of the second
+     */
+    protected int compareEncodingPreferences(MediaFormat enc1, MediaFormat enc2)
+    {
+        int res = getPriority(enc2) - getPriority(enc1);
+
+        /*
+         * If the encodings are with same priority, compare them by name. If we
+         * return equals, TreeSet will not add equal encodings.
+         */
+        if (res == 0)
+        {
+            res = enc1.getEncoding().compareToIgnoreCase(enc2.getEncoding());
+            /*
+             * There are formats with one and the same encoding (name) but
+             * different clock rates.
+             */
+            if (res == 0)
+            {
+                res = Double.compare(enc2.getClockRate(), enc1.getClockRate());
+                /*
+                 * And then again, there are formats (e.g. H.264) with one and
+                 * the same encoding (name) and clock rate but different format
+                 * parameters (e.g. packetization-mode).
+                 */
+                if (res == 0)
+                {
+                    // Try to preserve the order specified by MediaUtils.
+                    int index1;
+                    int index2;
+
+                    if (((index1 = MediaUtils.getMediaFormatIndex(enc1)) != -1)
+                            && ((index2 = MediaUtils.getMediaFormatIndex(enc2))
+                            != -1))
+                    {
+                        res = (index1 - index2);
+                    }
+
+                    if (res == 0)
+                    {
+                        /*
+                         * The format with more parameters will be considered
+                         * here to be the format with higher priority.
+                         */
+                        Map<String, String> fmtps1 = enc1.getFormatParameters();
+                        Map<String, String> fmtps2 = enc2.getFormatParameters();
+                        int fmtpCount1 = (fmtps1 == null) ? 0 : fmtps1.size();
+                        int fmtpCount2 = (fmtps2 == null) ? 0 : fmtps2.size();
+
+                        /*
+                         * TODO Even if the number of format parameters is
+                         * equal, the two formats may still be different.
+                         * Consider ordering by the values of the format
+                         * parameters as well.
+                         */
+                        res = (fmtpCount2 - fmtpCount1);
+                    }
+                }
+            }
+        }
+        return res;
+    }
 }
diff --git a/src/org/jitsi/service/neomedia/codec/EncodingConfiguration.java b/src/org/jitsi/service/neomedia/codec/EncodingConfiguration.java
index f90d3d83..1bf2f33a 100644
--- a/src/org/jitsi/service/neomedia/codec/EncodingConfiguration.java
+++ b/src/org/jitsi/service/neomedia/codec/EncodingConfiguration.java
@@ -8,7 +8,6 @@
 
 import java.util.*;
 
-import org.jitsi.impl.neomedia.*;
 import org.jitsi.service.neomedia.*;
 import org.jitsi.service.neomedia.format.*;
 import org.jitsi.util.*;
@@ -21,7 +20,7 @@
  * @author Lyubomir Marinov
  * @author Boris Grozev
  */
-public class EncodingConfiguration
+public abstract class EncodingConfiguration
 {
     /**
      * The <tt>Logger</tt> used by this <tt>EncodingConfiguration</tt> instance
@@ -111,32 +110,9 @@ private Set<MediaFormat> updateSupportedEncodings(MediaType type)
      * @param clockRate clock rate
      * @param pref a positive int indicating the preference for that encoding.
      */
-    protected void setEncodingPreference(
+    protected abstract void setEncodingPreference(
             String encoding, double clockRate,
-            int pref)
-    {
-        MediaFormat mediaFormat = null;
-
-        /*
-         * The key in encodingPreferences associated with a MediaFormat is
-         * currently composed of the encoding and the clockRate only so it makes
-         * sense to ignore the format parameters.
-         */
-        for (MediaFormat mf : MediaUtils.getMediaFormats(encoding))
-        {
-            if (mf.getClockRate() == clockRate)
-            {
-                mediaFormat = mf;
-                break;
-            }
-        }
-        if (mediaFormat != null)
-        {
-            encodingPreferences.put(
-                    getEncodingPreferenceKey(mediaFormat),
-                    pref);
-        }
-    }
+            int pref);
 
     /**
      * Sets <tt>priority</tt> as the preference associated with
@@ -195,10 +171,7 @@ public int getPriority(MediaFormat encoding)
      * @return array of <tt>MediaFormat</tt> supported for the
      * <tt>MediaType</tt>
      */
-    public MediaFormat[] getAllEncodings(MediaType type)
-    {
-        return MediaUtils.getMediaFormats(type);
-    }
+    public abstract MediaFormat[] getAllEncodings(MediaType type);
 
     /**
      * Returns the supported <tt>MediaFormat</tt>s i.e. the enabled available
@@ -228,7 +201,7 @@ public MediaFormat[] getEnabledEncodings(MediaType type)
             supportedEncodings = supportedVideoEncodings;
             break;
         default:
-            return MediaUtils.EMPTY_MEDIA_FORMATS;
+            return new MediaFormat[0];
         }
 
         return
@@ -247,66 +220,9 @@ public MediaFormat[] getEnabledEncodings(MediaType type)
      * format has been assigned a preference higher, equal to, or greater than
      * the one of the second
      */
-    private int compareEncodingPreferences(MediaFormat enc1, MediaFormat enc2)
-    {
-        int res = getPriority(enc2) - getPriority(enc1);
+    protected abstract
+            int compareEncodingPreferences(MediaFormat enc1, MediaFormat enc2);
 
-        /*
-         * If the encodings are with same priority, compare them by name. If we
-         * return equals, TreeSet will not add equal encodings.
-         */
-        if (res == 0)
-        {
-            res = enc1.getEncoding().compareToIgnoreCase(enc2.getEncoding());
-            /*
-             * There are formats with one and the same encoding (name) but
-             * different clock rates.
-             */
-            if (res == 0)
-            {
-                res = Double.compare(enc2.getClockRate(), enc1.getClockRate());
-                /*
-                 * And then again, there are formats (e.g. H.264) with one and
-                 * the same encoding (name) and clock rate but different format
-                 * parameters (e.g. packetization-mode).
-                 */
-                if (res == 0)
-                {
-                    // Try to preserve the order specified by MediaUtils.
-                    int index1;
-                    int index2;
-
-                    if (((index1 = MediaUtils.getMediaFormatIndex(enc1)) != -1)
-                            && ((index2 = MediaUtils.getMediaFormatIndex(enc2))
-                                    != -1))
-                    {
-                        res = (index1 - index2);
-                    }
-
-                    if (res == 0)
-                    {
-                        /*
-                         * The format with more parameters will be considered
-                         * here to be the format with higher priority.
-                         */
-                        Map<String, String> fmtps1 = enc1.getFormatParameters();
-                        Map<String, String> fmtps2 = enc2.getFormatParameters();
-                        int fmtpCount1 = (fmtps1 == null) ? 0 : fmtps1.size();
-                        int fmtpCount2 = (fmtps2 == null) ? 0 : fmtps2.size();
-
-                        /*
-                         * TODO Even if the number of format parameters is
-                         * equal, the two formats may still be different.
-                         * Consider ordering by the values of the format
-                         * parameters as well.
-                         */
-                        res = (fmtpCount2 - fmtpCount1);
-                    }
-                }
-            }
-        }
-        return res;
-    }
 
     /**
      * Gets the key in {@link #encodingPreferences} which is associated with the
-- 
GitLab