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.jingle.mediaimpl.jspeex;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.logging.Logger;
024
025import org.jivesoftware.smackx.jingle.JingleSession;
026import org.jivesoftware.smackx.jingle.media.JingleMediaManager;
027import org.jivesoftware.smackx.jingle.media.JingleMediaSession;
028import org.jivesoftware.smackx.jingle.media.PayloadType;
029import org.jivesoftware.smackx.jingle.mediaimpl.JMFInit;
030import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
031import org.jivesoftware.smackx.jingle.nat.TransportCandidate;
032
033/**
034 * Implements a jingleMediaManager using JMF based API and JSpeex.
035 * It supports Speex codec.
036 * <i>This API only currently works on windows.</i>
037 *
038 * @author Thiago Camargo
039 */
040public class SpeexMediaManager extends JingleMediaManager {
041
042        private static final Logger LOGGER = Logger.getLogger(SpeexMediaManager.class.getName());
043
044        public static final String MEDIA_NAME = "Speex";
045
046    private List<PayloadType> payloads = new ArrayList<PayloadType>();
047
048    public SpeexMediaManager(JingleTransportManager transportManager) {
049        super(transportManager);
050        setupPayloads();
051        setupJMF();
052    }
053
054    /**
055     * Returns a new jingleMediaSession
056     *
057     * @param payloadType payloadType
058     * @param remote      remote Candidate
059     * @param local       local Candidate
060     * @return JingleMediaSession
061     */
062    public JingleMediaSession createMediaSession(PayloadType payloadType, final TransportCandidate remote, final TransportCandidate local, final JingleSession jingleSession) {
063        return new AudioMediaSession(payloadType, remote, local, null,null);
064    }
065
066    /**
067     * Setup API supported Payloads
068     */
069    private void setupPayloads() {
070        payloads.add(new PayloadType.Audio(15, "speex"));
071    }
072
073    /**
074     * Return all supported Payloads for this Manager
075     *
076     * @return The Payload List
077     */
078    public List<PayloadType> getPayloads() {
079        return payloads;
080    }
081
082    /**
083     * Runs JMFInit the first time the application is started so that capture
084     * devices are properly detected and initialized by JMF.
085     */
086    public static void setupJMF() {
087        // .jmf is the place where we store the jmf.properties file used
088        // by JMF. if the directory does not exist or it does not contain
089        // a jmf.properties file. or if the jmf.properties file has 0 length
090        // then this is the first time we're running and should continue to
091        // with JMFInit
092        String homeDir = System.getProperty("user.home");
093        File jmfDir = new File(homeDir, ".jmf");
094        String classpath = System.getProperty("java.class.path");
095        classpath += System.getProperty("path.separator")
096                + jmfDir.getAbsolutePath();
097        System.setProperty("java.class.path", classpath);
098
099        if (!jmfDir.exists())
100            jmfDir.mkdir();
101
102        File jmfProperties = new File(jmfDir, "jmf.properties");
103
104        if (!jmfProperties.exists()) {
105            try {
106                jmfProperties.createNewFile();
107            }
108            catch (IOException ex) {
109                LOGGER.fine("Failed to create jmf.properties");
110                ex.printStackTrace();
111            }
112        }
113
114        // if we're running on linux checkout that libjmutil.so is where it
115        // should be and put it there.
116        runLinuxPreInstall();
117
118        if (jmfProperties.length() == 0) {
119            new JMFInit(null, false);
120        }
121
122    }
123
124    private static void runLinuxPreInstall() {
125        // @TODO Implement Linux Pre-Install
126    }
127    
128    public String getName() {
129        return MEDIA_NAME;
130    }
131}