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