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.jmf;
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.
036 * It supports GSM and G723 codices.
037 * <i>This API only currently works on windows and Mac.</i>
038 *
039 * @author Thiago Camargo
040 */
041public class JmfMediaManager extends JingleMediaManager {
042
043    private static final Logger LOGGER = Logger.getLogger(JmfMediaManager.class.getName());
044
045    public static final String MEDIA_NAME = "JMF";
046
047
048    private List<PayloadType> payloads = new ArrayList<>();
049    private String mediaLocator = null;
050
051    /**
052     * Creates a Media Manager instance.
053     *
054     * @param transportManager the transport manger.
055     */
056    public JmfMediaManager(JingleTransportManager transportManager) {
057        super(transportManager);
058        setupPayloads();
059    }
060
061    /**
062     * Creates a Media Manager instance.
063     *
064     * @param mediaLocator Media Locator
065     * @param transportManager the transport manger.
066     */
067    public JmfMediaManager(String mediaLocator, JingleTransportManager transportManager) {
068        super(transportManager);
069        this.mediaLocator = mediaLocator;
070        setupPayloads();
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 TODO javadoc me please
080     */
081    @Override
082    public JingleMediaSession createMediaSession(final PayloadType payloadType, final TransportCandidate remote, final TransportCandidate local, final JingleSession jingleSession) {
083        return new AudioMediaSession(payloadType, remote, local, mediaLocator, jingleSession);
084    }
085
086    /**
087     * Setup API supported Payloads
088     */
089    private void setupPayloads() {
090        payloads.add(new PayloadType.Audio(3, "gsm"));
091        payloads.add(new PayloadType.Audio(4, "g723"));
092        payloads.add(new PayloadType.Audio(0, "PCMU", 16000));
093    }
094
095    /**
096     * Return all supported Payloads for this Manager.
097     *
098     * @return The Payload List
099     */
100    @Override
101    public List<PayloadType> getPayloads() {
102        return payloads;
103    }
104
105    /**
106     * Return the media locator or null if not defined.
107     *
108     * @return media locator
109     */
110    public String getMediaLocator() {
111        return mediaLocator;
112    }
113
114    /**
115     * Set the media locator.
116     *
117     * @param mediaLocator media locator or null to use default
118     */
119    public void setMediaLocator(String mediaLocator) {
120        this.mediaLocator = mediaLocator;
121    }
122
123    /**
124     * Runs JMFInit the first time the application is started so that capture
125     * devices are properly detected and initialized by JMF.
126     */
127    public static void setupJMF() {
128        // .jmf is the place where we store the jmf.properties file used
129        // by JMF. if the directory does not exist or it does not contain
130        // a jmf.properties file. or if the jmf.properties file has 0 length
131        // then this is the first time we're running and should continue to
132        // with JMFInit
133        String homeDir = System.getProperty("user.home");
134        File jmfDir = new File(homeDir, ".jmf");
135        String classpath = System.getProperty("java.class.path");
136        classpath += System.getProperty("path.separator")
137                + jmfDir.getAbsolutePath();
138        System.setProperty("java.class.path", classpath);
139
140        if (!jmfDir.exists())
141            jmfDir.mkdir();
142
143        File jmfProperties = new File(jmfDir, "jmf.properties");
144
145        if (!jmfProperties.exists()) {
146            try {
147                jmfProperties.createNewFile();
148            }
149            catch (IOException ex) {
150                LOGGER.log(Level.FINE, "Failed to create jmf.properties", ex);
151            }
152        }
153
154        // if we're running on linux checkout that libjmutil.so is where it
155        // should be and put it there.
156        runLinuxPreInstall();
157
158        // if (jmfProperties.length() == 0) {
159        new JMFInit(null, false);
160        // }
161
162    }
163
164    private static void runLinuxPreInstall() {
165        // @TODO Implement Linux Pre-Install
166    }
167
168    @Override
169    public  String getName() {
170        return MEDIA_NAME;
171    }
172}