aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/device/DirectShowAuto.java
blob: 972a87012ae3e7407d2d7b517333dab8d94ce455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.neomedia.device;

import javax.media.*;

import net.java.sip.communicator.impl.neomedia.codec.*;
import net.java.sip.communicator.impl.neomedia.codec.video.*;
import net.java.sip.communicator.impl.neomedia.directshow.*;
import net.java.sip.communicator.impl.neomedia.jmfext.media.protocol.directshow.*;
import net.java.sip.communicator.util.*;

/**
 * Discovers and registers DirectShow video capture devices with JMF.
 *
 * @author Sebastien Vincent
 */
public class DirectShowAuto
{
    /**
     * The <tt>Logger</tt>.
     */
    private static final Logger logger = Logger.getLogger(DirectShowAuto.class);

    /**
     * The protocol of the <tt>MediaLocator</tt>s identifying QuickTime/QTKit
     * capture devices.
     */
    public static final String LOCATOR_PROTOCOL = "directshow";

    /**
     * Constructor. Discover and register DirectShow capture devices
     * with JMF.
     *
     * @throws Exception if anything goes wrong while discovering and
     * registering DirectShow capture defines with JMF
     */
    public DirectShowAuto() throws Exception
    {
        if(logger.isInfoEnabled())
            logger.info("Start detecting DirectShow capture devices");

        DSManager manager = DSManager.getInstance();
        DSCaptureDevice devices[] = null;
        boolean captureDeviceInfoIsAdded = false;

        /* get devices */
        devices = manager.getCaptureDevices();

        if(devices == null || devices.length == 0)
        {
            throw new Exception("no devices!");
        }

        for(int i = 0 ; i < devices.length ; i++)
        {
            DSFormat fmt = devices[i].getFormat();
            long pixelFormat = fmt.getPixelFormat();
            Format format = null;
            int ffmpegPixFmt = (int)DataSource.getFFmpegPixFmt(pixelFormat);

            if(ffmpegPixFmt != FFmpeg.PIX_FMT_NONE)
            {
                format = new AVFrameFormat(ffmpegPixFmt, (int)pixelFormat);
            }
            else
            {
                logger.warn("No support for this webcam: " +
                        devices[i].getName() + "(no format supported)");
                continue;
            }

            CaptureDeviceInfo device
                = new CaptureDeviceInfo(devices[i].getName(),
                        new MediaLocator(LOCATOR_PROTOCOL + ':' + devices[i].
                            getName()),
                        new Format[]
                        {
                            format,
                        });

            if(logger.isInfoEnabled())
                logger.info("Found[" + i + "]: " + device.getName());

            CaptureDeviceManager.addDevice(device);
            captureDeviceInfoIsAdded = true;
        }

        if (captureDeviceInfoIsAdded)
            CaptureDeviceManager.commit();

        devices = null;
        DSManager.dispose();

        if(logger.isInfoEnabled())
            logger.info("Finish detecting DirectShow capture devices");
    }
}