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.sshare;
018
019import java.awt.Rectangle;
020import java.awt.event.WindowAdapter;
021import java.awt.event.WindowEvent;
022import java.io.IOException;
023import java.net.DatagramSocket;
024import java.net.InetAddress;
025import java.net.ServerSocket;
026import java.net.UnknownHostException;
027import java.util.logging.Level;
028import java.util.logging.Logger;
029
030import javax.swing.JFrame;
031import javax.swing.JPanel;
032
033import org.jivesoftware.smackx.jingleold.JingleSession;
034import org.jivesoftware.smackx.jingleold.media.JingleMediaSession;
035import org.jivesoftware.smackx.jingleold.media.PayloadType;
036import org.jivesoftware.smackx.jingleold.mediaimpl.sshare.api.ImageDecoder;
037import org.jivesoftware.smackx.jingleold.mediaimpl.sshare.api.ImageEncoder;
038import org.jivesoftware.smackx.jingleold.mediaimpl.sshare.api.ImageReceiver;
039import org.jivesoftware.smackx.jingleold.mediaimpl.sshare.api.ImageTransmitter;
040import org.jivesoftware.smackx.jingleold.nat.TransportCandidate;
041
042/**
043 * This Class implements a complete JingleMediaSession.
044 * It sould be used to transmit and receive captured images from the Display.
045 * This Class should be automaticly controlled by JingleSession.
046 * For better NAT Traversal support this implementation don't support only receive or only transmit.
047 * To receive you MUST transmit. So the only implemented and functionally methods are startTransmit() and stopTransmit()
048 *
049 * @author Thiago Camargo
050 */
051public class ScreenShareSession extends JingleMediaSession {
052
053    private static final Logger LOGGER = Logger.getLogger(ScreenShareSession.class.getName());
054
055    private ImageTransmitter transmitter = null;
056    private ImageReceiver receiver = null;
057    private int width = 600;
058    private int height = 600;
059
060    /**
061     * Creates a org.jivesoftware.jingleaudio.jmf.AudioMediaSession with defined payload type, remote and local candidates.
062     *
063     * @param payloadType Payload of the jmf
064     * @param remote      the remote information. The candidate that the jmf will be sent to.
065     * @param local       the local information. The candidate that will receive the jmf
066     * @param locator     media locator
067     */
068    public ScreenShareSession(final PayloadType payloadType, final TransportCandidate remote, final TransportCandidate local,
069            final String locator, JingleSession jingleSession) {
070        super(payloadType, remote, local, "Screen", jingleSession);
071        initialize();
072    }
073
074    /**
075     * Initialize the screen share channels.
076     */
077    @Override
078    public void initialize() {
079
080        JingleSession session = getJingleSession();
081        if ((session != null) && (session.getInitiator().equals(session.getConnection().getUser()))) {
082            // If the initiator of the jingle session is us then we transmit a screen share.
083            try {
084                InetAddress remote = InetAddress.getByName(getRemote().getIp());
085                transmitter = new ImageTransmitter(new DatagramSocket(getLocal().getPort()), remote, getRemote().getPort(),
086                        new Rectangle(0, 0, width, height));
087            } catch (Exception e) {
088                LOGGER.log(Level.WARNING, "exception", e);
089            }
090
091        } else {
092            // Otherwise we receive a screen share.
093            JFrame window = new JFrame();
094            JPanel jp = new JPanel();
095            window.add(jp);
096
097            window.setLocation(0, 0);
098            window.setSize(600, 600);
099
100            window.addWindowListener(new WindowAdapter() {
101                @Override
102                public void windowClosed(WindowEvent e) {
103                    receiver.stop();
104                }
105            });
106
107            try {
108                receiver = new ImageReceiver(InetAddress.getByName("0.0.0.0"), getRemote().getPort(), getLocal().getPort(), width,
109                        height);
110                LOGGER.fine("Receiving on:" + receiver.getLocalPort());
111            } catch (UnknownHostException e) {
112                LOGGER.log(Level.WARNING, "exception", e);
113            }
114
115            jp.add(receiver);
116            receiver.setVisible(true);
117            window.setAlwaysOnTop(true);
118            window.setVisible(true);
119        }
120    }
121
122    /**
123     * Starts transmission and for NAT Traversal reasons start receiving also.
124     */
125    @Override
126    public void startTrasmit() {
127        new Thread(transmitter).start();
128    }
129
130    /**
131     * Set transmit activity. If the active is true, the instance should trasmit.
132     * If it is set to false, the instance should pause transmit.
133     *
134     * @param active active state
135     */
136    @Override
137    public void setTrasmit(boolean active) {
138        transmitter.setTransmit(true);
139    }
140
141    /**
142     * For NAT Reasons this method does nothing. Use startTransmit() to start transmit and receive jmf
143     */
144    @Override
145    public void startReceive() {
146        // Do nothing
147    }
148
149    /**
150     * Stops transmission and for NAT Traversal reasons stop receiving also.
151     */
152    @Override
153    public void stopTrasmit() {
154        if (transmitter != null) {
155            transmitter.stop();
156        }
157    }
158
159    /**
160     * For NAT Reasons this method does nothing. Use startTransmit() to start transmit and receive jmf
161     */
162    @Override
163    public void stopReceive() {
164        if (receiver != null) {
165            receiver.stop();
166        }
167    }
168
169    /**
170     * Obtain a free port we can use.
171     *
172     * @return A free port number.
173     */
174    protected int getFreePort() {
175        ServerSocket ss;
176        int freePort = 0;
177
178        for (int i = 0; i < 10; i++) {
179            freePort = (int) (10000 + Math.round(Math.random() * 10000));
180            freePort = freePort % 2 == 0 ? freePort : freePort + 1;
181            try {
182                ss = new ServerSocket(freePort);
183                freePort = ss.getLocalPort();
184                ss.close();
185                return freePort;
186            } catch (IOException e) {
187                LOGGER.log(Level.WARNING, "exception", e);
188            }
189        }
190        try {
191            ss = new ServerSocket(0);
192            freePort = ss.getLocalPort();
193            ss.close();
194        } catch (IOException e) {
195            LOGGER.log(Level.WARNING, "exception", e);
196        }
197        return freePort;
198    }
199
200    public void setEncoder(ImageEncoder encoder) {
201        if (encoder != null) {
202            this.transmitter.setEncoder(encoder);
203        }
204    }
205
206    public void setDecoder(ImageDecoder decoder) {
207        if (decoder != null) {
208            this.receiver.setDecoder(decoder);
209        }
210    }
211}