Exemple #1
0
  public static Map getCodec(SessionDescriptor offers) {

    Logger logger = Logger.getLogger(Codecs.class.getCanonicalName());
    if (offers == null) {
      logger.warning("offers == null");
      return null;
    }

    MediaDescriptor mAudio = offers.getMediaDescriptor("audio");
    if (mAudio == null) {
      logger.warning("offer doesn't contain m=audio");
      logger.info(offers.toString());
      return null;
    }

    MediaField m = mAudio.getMedia();
    if (m == null) {
      logger.warning("media field invalid");
      return null;
    }

    String proto = m.getTransport();
    // see http://tools.ietf.org/html/rfc4566#page-22, paragraph 5.14, <fmt> description
    if (proto.equals("RTP/AVP") || proto.equals("RTP/SAVP") || proto.equals("RTP/SAVPF")) {
      Vector<String> formats = m.getFormatList();
      Vector<String> names = new Vector<String>(formats.size());
      Vector<Integer> numbers = new Vector<Integer>(formats.size());
      Vector<Codec> codecmap = new Vector<Codec>(formats.size());

      // add all avail formats with empty names
      for (String fmt : formats) {
        try {
          int number = Integer.parseInt(fmt);
          numbers.add(number);
          names.add("");
          codecmap.add(null);
        } catch (NumberFormatException e) {
          // continue ... remote sent bogus rtp setting
        }
      }
      ;
      logger.info("got " + numbers.size() + " format numbers");

      // if we have attrs for format -> set name
      Vector<AttributeField> attrs = offers.getMediaDescriptor("audio").getAttributes("rtpmap");
      logger.info("got " + attrs.size() + " rtpmap attributes");
      for (AttributeField a : attrs) {
        String s = a.getValue();
        // skip over "rtpmap:"
        s = s.substring(7, s.indexOf("/"));
        int i = s.indexOf(" ");
        try {
          String name = s.substring(i + 1);
          int number = Integer.parseInt(s.substring(0, i));
          int index = numbers.indexOf(number);
          logger.info("format offered " + index + ", " + name);
          if (index >= 0) names.set(index, name.toLowerCase());
        } catch (NumberFormatException e) {
          // continue ... remote sent bogus rtp setting
        }
      }

      Codec codec = null;
      int index = formats.size() + 1;
      logger.info("number of local codecs = " + codecs.size());
      for (Codec c : codecs) {
        logger.info("checking " + c.userName() + ", valid = " + c.isValid());
        if (!c.isValid()) continue;

        // search current codec in offers by name
        int i = names.indexOf(c.userName().toLowerCase());
        if (i >= 0) {
          logger.info("adding codec " + c.userName() + " by name");
          codecmap.set(i, c);
          if ((codec == null) || (i < index)) {
            codec = c;
            index = i;
            continue;
          }
        }

        // search current codec in offers by number
        i = numbers.indexOf(c.number());
        if (i >= 0) {
          if (names.elementAt(i).equals("")) {
            logger.info("adding codec " + c.userName() + " by number");
            codecmap.set(i, c);
            if ((codec == null) || (i < index)) {
              // fmt number has no attr with name
              codec = c;
              index = i;
              continue;
            }
          }
        }
      }
      if (codec != null) return new Map(numbers.elementAt(index), codec, numbers, codecmap);
      else {
        // no codec found ... we can't talk
        logger.warning("didn't find any recognised codec");
        return null;
      }
    } else {
      /*formats of other protocols not supported yet*/
      logger.warning("can't handle protocol: " + proto);
      return null;
    }
  }