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 org.jivesoftware.smackx.jingleold.JingleSession;
020import org.jivesoftware.smackx.jingleold.media.JingleMediaManager;
021import org.jivesoftware.smackx.jingleold.media.JingleMediaSession;
022import org.jivesoftware.smackx.jingleold.media.PayloadType;
023import org.jivesoftware.smackx.jingleold.nat.JingleTransportManager;
024import org.jivesoftware.smackx.jingleold.nat.TransportCandidate;
025
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * Implements a MultiMediaManager using other JingleMediaManager implementations.
031 * It supports every Codecs 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 List<JingleMediaManager> managers = new ArrayList<JingleMediaManager>();
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    public List<PayloadType> getPayloads() {
062        List<PayloadType> list = new ArrayList<PayloadType>();
063        if (preferredPayloadType != null) list.add(preferredPayloadType);
064        for (JingleMediaManager manager : managers) {
065            for (PayloadType payloadType : manager.getPayloads()) {
066                if (!list.contains(payloadType) && !payloadType.equals(preferredPayloadType))
067                    list.add(payloadType);
068            }
069        }
070        return list;
071    }
072
073    /**
074     * Returns a new JingleMediaSession
075     *
076     * @param payloadType payloadType
077     * @param remote      remote Candidate
078     * @param local       local Candidate
079     * @return JingleMediaSession JingleMediaSession
080     */
081    public JingleMediaSession createMediaSession(PayloadType payloadType, final TransportCandidate remote, final TransportCandidate local, final JingleSession jingleSession) {
082        for (JingleMediaManager manager : managers) {
083            if (manager.getPayloads().contains(payloadType)) {
084                return manager.createMediaSession(payloadType, remote, local, jingleSession);
085            }
086        }
087        return null;
088    }
089
090    public PayloadType getPreferredPayloadType() {
091        if (preferredPayloadType != null) return preferredPayloadType;
092        return super.getPreferredPayloadType();
093    }
094
095    public void setPreferredPayloadType(PayloadType preferredPayloadType) {
096        this.preferredPayloadType = preferredPayloadType;
097    }
098    
099    public  String getName() {
100        return MEDIA_NAME;
101    }
102}