aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/codec/video/h264/ConfigurationPanel.java
blob: 1381d210de0b55e6be6c3e9bcbe1c63dbed8022a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.neomedia.codec.video.h264;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.impl.neomedia.*;
import net.java.sip.communicator.plugin.desktoputil.*;

import org.jitsi.impl.neomedia.codec.video.h264.*;
import org.jitsi.service.configuration.*;
import org.jitsi.service.neomedia.control.*;
import org.jitsi.service.resources.*;

/**
 * Implements the H.264 configuration form/panel.
 *
 * @author Lyubomir Marinov
 * @author Damian Minkov
 */
public class ConfigurationPanel
    extends TransparentPanel
{
    /**
     * Serial version UID.
     */
    private final static long serialVersionUID = 0L;

    /**
     * Initializer a new <tt>ConfigurationPanel</tt> instance.
     */
    public ConfigurationPanel()
    {
        // Create the UI components.
        super(new BorderLayout());

        TransparentPanel contentPanel
            = new TransparentPanel(new GridBagLayout());
        add(contentPanel, BorderLayout.NORTH);

        ResourceManagementService r = NeomediaActivator.getResources();
        GridBagConstraints cnstrnts = new GridBagConstraints();

        cnstrnts.anchor = GridBagConstraints.FIRST_LINE_START;
        cnstrnts.fill = GridBagConstraints.HORIZONTAL;

        Component defaultProfileLabel
            = createLineWrapLabel(
                    r.getI18NString(
                            "impl.neomedia.configform.H264.defaultProfile"));
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 0;
        cnstrnts.weightx = 1;
        contentPanel.add(defaultProfileLabel, cnstrnts);

        JComboBox defaultProfileComboBox = new JComboBox();
        defaultProfileComboBox.setEditable(false);
        defaultProfileComboBox.addItem(
                new NameValuePair(
                        r.getI18NString(
                                "impl.neomedia.configform.H264.defaultProfile."
                                    + JNIEncoder.BASELINE_PROFILE),
                        JNIEncoder.BASELINE_PROFILE));
        defaultProfileComboBox.addItem(
                new NameValuePair(
                        r.getI18NString(
                                "impl.neomedia.configform.H264.defaultProfile."
                                    + JNIEncoder.MAIN_PROFILE),
                        JNIEncoder.MAIN_PROFILE));
        defaultProfileComboBox.addItem(
                new NameValuePair(
                        r.getI18NString(
                                "impl.neomedia.configform.H264.defaultProfile."
                                    + JNIEncoder.HIGH_PROFILE),
                        JNIEncoder.HIGH_PROFILE));
        cnstrnts.gridx = 1;
        cnstrnts.gridy = 0;
        cnstrnts.weightx = 0;
        contentPanel.add(defaultProfileComboBox, cnstrnts);

        Component preferredKeyFrameRequesterLabel
            = createLineWrapLabel(
                    r.getI18NString(
                            "impl.neomedia.configform.H264"
                                + ".preferredKeyFrameRequester"));
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 1;
        cnstrnts.weightx = 1;
        contentPanel.add(preferredKeyFrameRequesterLabel, cnstrnts);

        JComboBox preferredKeyFrameRequesterComboBox = new JComboBox();
        preferredKeyFrameRequesterComboBox.setEditable(false);
        preferredKeyFrameRequesterComboBox.addItem(
                new NameValuePair(
                        r.getI18NString(
                                "impl.neomedia.configform.H264"
                                    + ".preferredKeyFrameRequester."
                                    + KeyFrameControl.KeyFrameRequester.RTCP),
                        KeyFrameControl.KeyFrameRequester.RTCP));
        preferredKeyFrameRequesterComboBox.addItem(
                new NameValuePair(
                        r.getI18NString(
                                "impl.neomedia.configform.H264"
                                    + ".preferredKeyFrameRequester."
                                    + KeyFrameControl.KeyFrameRequester.SIGNALING),
                        KeyFrameControl.KeyFrameRequester.SIGNALING));
        cnstrnts.gridx = 1;
        cnstrnts.gridy = 1;
        cnstrnts.weightx = 0;
        contentPanel.add(
                preferredKeyFrameRequesterComboBox,
                cnstrnts);

        Component presetLabel
            = createLineWrapLabel(
                    r.getI18NString("impl.neomedia.configform.H264.preset"));
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 2;
        cnstrnts.weightx = 1;
        contentPanel.add(presetLabel, cnstrnts);

        JComboBox presetComboBox = new JComboBox();
        presetComboBox.setEditable(false);
        for (String preset : JNIEncoder.AVAILABLE_PRESETS)
            presetComboBox.addItem(new NameValuePair(preset, preset));
        cnstrnts.gridx = 1;
        cnstrnts.gridy = 2;
        cnstrnts.weightx = 0;
        contentPanel.add(presetComboBox, cnstrnts);

        JCheckBox defaultIntraRefreshCheckBox
            = new SIPCommCheckBox(
                    r.getI18NString(
                            "impl.neomedia.configform.H264"
                                + ".defaultIntraRefresh"));
        cnstrnts.gridwidth = GridBagConstraints.REMAINDER;
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 3;
        cnstrnts.weightx = 1;
        contentPanel.add(defaultIntraRefreshCheckBox, cnstrnts);
        cnstrnts.gridwidth = 1;

        Component keyintLabel
            = createLineWrapLabel(
                    r.getI18NString("impl.neomedia.configform.H264.keyint"));
        cnstrnts.gridx = 0;
        cnstrnts.gridy = 4;
        cnstrnts.weightx = 1;
        contentPanel.add(keyintLabel, cnstrnts);

        JSpinner keyintSpinner
            = new JSpinner(
                    new SpinnerNumberModel(
                            JNIEncoder.DEFAULT_KEYINT,
                            1, JNIEncoder.X264_KEYINT_MAX_INFINITE,
                            JNIEncoder.DEFAULT_FRAME_RATE));
        cnstrnts.gridx = 1;
        cnstrnts.gridy = 4;
        cnstrnts.weightx = 0;
        contentPanel.add(keyintSpinner, cnstrnts);

        // Load the values from the ConfigurationService into the UI components.
        ConfigurationService cfg = NeomediaActivator.getConfigurationService();

        setSelectedNameValuePair(
                defaultProfileComboBox,
                cfg.getString(
                        JNIEncoder.DEFAULT_PROFILE_PNAME,
                        JNIEncoder.DEFAULT_DEFAULT_PROFILE));
        addActionListener(
                defaultProfileComboBox,
                JNIEncoder.DEFAULT_PROFILE_PNAME);

        setSelectedNameValuePair(
                preferredKeyFrameRequesterComboBox,
                cfg.getString(
                        KeyFrameControl.KeyFrameRequester.PREFERRED_PNAME,
                        KeyFrameControl.KeyFrameRequester.DEFAULT_PREFERRED));
        addActionListener(
                preferredKeyFrameRequesterComboBox,
                KeyFrameControl.KeyFrameRequester.PREFERRED_PNAME);

        setSelectedNameValuePair(
                presetComboBox,
                cfg.getString(
                        JNIEncoder.PRESET_PNAME,
                        JNIEncoder.DEFAULT_PRESET));
        addActionListener(presetComboBox, JNIEncoder.PRESET_PNAME);

        defaultIntraRefreshCheckBox.setSelected(
                cfg.getBoolean(
                        JNIEncoder.DEFAULT_INTRA_REFRESH_PNAME,
                        JNIEncoder.DEFAULT_DEFAULT_INTRA_REFRESH));
        defaultIntraRefreshCheckBox.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        JCheckBox checkBox = (JCheckBox) e.getSource();

                        NeomediaActivator
                            .getConfigurationService()
                                .setProperty(
                                        JNIEncoder.DEFAULT_INTRA_REFRESH_PNAME,
                                        Boolean.toString(
                                                checkBox.isSelected()));
                    }
                });

        keyintSpinner.setValue(
                cfg.getInt(JNIEncoder.KEYINT_PNAME, JNIEncoder.DEFAULT_KEYINT));
        keyintSpinner.addChangeListener(
                new ChangeListener()
                {
                    public void stateChanged(ChangeEvent e)
                    {
                        JSpinner spinner = (JSpinner) e.getSource();
                        SpinnerNumberModel model
                            = (SpinnerNumberModel) spinner.getModel();
                        int value = model.getNumber().intValue();

                        NeomediaActivator
                            .getConfigurationService()
                                .setProperty(
                                        JNIEncoder.KEYINT_PNAME,
                                        Integer.toString(value));                        
                    }
                });
    }

    /**
     * Adds an <tt>ActionListener</tt> to a specific <tt>JComboBox</tt>
     * populated with <tt>NameValuePair</tt>s which sets the value of a specific
     * <tt>ConfigurationService</tt> property to the <tt>value</tt> of the
     * selected <tt>NameValuePair</tt> of the <tt>comboBox</tt>.
     *
     * @param comboBox the <tt>JComboBox</tt> to add an <tt>ActionListener</tt>
     * to
     * @param property the name of the <tt>ConfigurationService</tt> property
     * to set the value of
     */
    private void addActionListener(
            final JComboBox comboBox,
            final String property)
    {
        comboBox.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        NameValuePair nameValuePair
                            = (NameValuePair) comboBox.getSelectedItem();

                        if (nameValuePair != null)
                        {
                            NeomediaActivator.getConfigurationService()
                                    .setProperty(property, nameValuePair.value);
                        }
                    }
                });
    }

    /**
     * Initializes a new <tt>Component</tt> instance which is to display a
     * specific text in the fashion of <tt>JLabel</tt> and with line wrapping.
     *
     * @param text the text to be displayed by the new instance
     * @return a new <tt>Component</tt> instance which displays the specified
     * <tt>text</tt> in the fashion of <tt>JLabel</tt> and with line wrapping
     */
    private Component createLineWrapLabel(String text)
    {
        JTextArea textArea = new JTextArea();

        textArea.setEditable(false);
        textArea.setFocusable(false);
        textArea.setLineWrap(true);
        textArea.setOpaque(false);
        textArea.setWrapStyleWord(true);

        textArea.setText(text);

        return textArea;
    }

    /**
     * Sets the selected item in a specific <tt>JComboBox</tt> populated with
     * <tt>NameValuePair</tt>s to the one which has a specific <tt>value</tt>.
     *
     * @param comboBox the <tt>JComboBox</tt> to set the selected item of
     * @param value the value of the <tt>NameValuePair</tt> to set as the
     * selected item of <tt>comboBox</tt>
     */
    private void setSelectedNameValuePair(JComboBox comboBox, String value)
    {
        for (int i = 0, count = comboBox.getItemCount(); i < count; i++)
        {
            NameValuePair nameValuePair = (NameValuePair) comboBox.getItemAt(i);

            if (nameValuePair.value.equals(value))
            {
                comboBox.setSelectedIndex(i);
                break;
            }
        }
    }

    /**
     * Represents a <tt>String</tt> value which has a human-readable name
     * associated with it for display purposes.
     */
    private static class NameValuePair
    {
        /**
         * The human-readable name of this <tt>NameValuePair</tt>.
         */
        public final String name;

        /**
         * The <tt>String</tt> value represented by this <tt>NameValuePair</tt>.
         */
        public final String value;

        /**
         * Initializes a new <tt>NameValuePair</tt> which is to represent a
         * specific <tt>String</tt> <tt>value</tt> which is to be displayed to
         * the user as <tt>name</tt>.
         *
         * @param name the human-readable name of the new instance
         * @param value the <tt>String</tt> value to be represented by the new
         * instance
         */
        public NameValuePair(String name, String value)
        {
            this.name = name;
            this.value = value;
        }

        /**
         * Returns a human-readable representation of this <tt>Object</tt> i.e.
         * the name of this <tt>NameValuePair</tt>.
         *
         * @return a human-readable representation of this <tt>Object</tt> i.e.
         * the name of this <tt>NameValuePair</tt>
         */
        @Override
        public String toString()
        {
            return name;
        }
    }
}