001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingleold.mediaimpl.multi;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smackx.jingleold.JingleSession;
023import org.jivesoftware.smackx.jingleold.media.JingleMediaManager;
024import org.jivesoftware.smackx.jingleold.media.JingleMediaSession;
025import org.jivesoftware.smackx.jingleold.media.PayloadType;
026import org.jivesoftware.smackx.jingleold.nat.JingleTransportManager;
027import org.jivesoftware.smackx.jingleold.nat.TransportCandidate;
028
029/**
030 * Implements a MultiMediaManager using other JingleMediaManager implementations.
031 * It supports every Codec that JingleMediaManagers added has.
032 *
033 * @author Thiago Camargo
034 */
035
036public class MultiMediaManager extends JingleMediaManager {
037
038    public static final String MEDIA_NAME = "Multi";
039
040    private static final List<JingleMediaManager> managers = new ArrayList<>();
041
042    private PayloadType preferredPayloadType = null;
043
044    public MultiMediaManager(JingleTransportManager transportManager) {
045        super(transportManager);
046    }
047
048    public void addMediaManager(JingleMediaManager manager) {
049        managers.add(manager);
050    }
051
052    public void removeMediaManager(JingleMediaManager manager) {
053        managers.remove(manager);
054    }
055
056    /**
057     * Return all supported Payloads for this Manager.
058     *
059     * @return The Payload List
060     */
061    @Override
062    public List<PayloadType> getPayloads() {
063        List<PayloadType> list = new ArrayList<>();
064        if (preferredPayloadType != null) list.add(preferredPayloadType);
065        for (JingleMediaManager manager : managers) {
066            for (PayloadType payloadType : manager.getPayloads()) {
067                if (!list.contains(payloadType) && !payloadType.equals(preferredPayloadType))
068                    list.add(payloadType);
069            }
070        }
071        return list;
072    }
073
074    /**
075     * Returns a new JingleMediaSession.
076     *
077     * @param payloadType payloadType
078     * @param remote      remote Candidate
079     * @param local       local Candidate
080     * @return JingleMediaSession JingleMediaSession
081     */
082    @Override
083    public JingleMediaSession createMediaSession(PayloadType payloadType, final TransportCandidate remote, final TransportCandidate local, final JingleSession jingleSession) {
084        for (JingleMediaManager manager : managers) {
085            if (manager.getPayloads().contains(payloadType)) {
086                return manager.createMediaSession(payloadType, remote, local, jingleSession);
087            }
088        }
089        return null;
090    }
091
092    @Override
093    public PayloadType getPreferredPayloadType() {
094        if (preferredPayloadType != null) return preferredPayloadType;
095        return super.getPreferredPayloadType();
096    }
097
098    public void setPreferredPayloadType(PayloadType preferredPayloadType) {
099        this.preferredPayloadType = preferredPayloadType;
100    }
101
102    @Override
103    public  String getName() {
104        return MEDIA_NAME;
105    }
106}