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