jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftAudioPusher.java
author serb
Mon, 15 Aug 2016 20:08:55 +0300
changeset 40444 afabcfc2f3ef
parent 37550 c8252b8fea3d
permissions -rw-r--r--
8163949: Cleanup of classes which are related to JavaSound Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     4
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2655
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2655
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    10
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    15
 * accompanied this code).
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    16
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2655
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2655
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2655
diff changeset
    23
 * questions.
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    24
 */
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37550
diff changeset
    25
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    26
package com.sun.media.sound;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    27
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    28
import java.io.IOException;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    29
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    30
import javax.sound.sampled.AudioInputStream;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    31
import javax.sound.sampled.SourceDataLine;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    32
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    33
/**
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    34
 * This is a processor object that writes into SourceDataLine
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    35
 *
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    36
 * @author Karl Helgason
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    37
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    38
public final class SoftAudioPusher implements Runnable {
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    39
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    40
    private volatile boolean active = false;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    41
    private SourceDataLine sourceDataLine = null;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    42
    private Thread audiothread;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    43
    private final AudioInputStream ais;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    44
    private final byte[] buffer;
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    45
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    46
    public SoftAudioPusher(SourceDataLine sourceDataLine, AudioInputStream ais,
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    47
            int workbuffersizer) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    48
        this.ais = ais;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    49
        this.buffer = new byte[workbuffersizer];
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    50
        this.sourceDataLine = sourceDataLine;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    51
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    52
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    53
    public synchronized void start() {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    54
        if (active)
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    55
            return;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    56
        active = true;
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    57
        audiothread = new Thread(null, this, "AudioPusher", 0, false);
2655
a69fc05ff63d 6821030: Merge OpenJDK Gervill with upstream sources, Q1CY2009
kalli
parents: 1846
diff changeset
    58
        audiothread.setDaemon(true);
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    59
        audiothread.setPriority(Thread.MAX_PRIORITY);
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    60
        audiothread.start();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    61
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    62
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    63
    public synchronized void stop() {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    64
        if (!active)
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    65
            return;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    66
        active = false;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    67
        try {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    68
            audiothread.join();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    69
        } catch (InterruptedException e) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    70
            //e.printStackTrace();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    71
        }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    72
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    73
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37550
diff changeset
    74
    @Override
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    75
    public void run() {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    76
        byte[] buffer = SoftAudioPusher.this.buffer;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    77
        AudioInputStream ais = SoftAudioPusher.this.ais;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    78
        SourceDataLine sourceDataLine = SoftAudioPusher.this.sourceDataLine;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    79
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    80
        try {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    81
            while (active) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    82
                // Read from audio source
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    83
                int count = ais.read(buffer);
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    84
                if(count < 0) break;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    85
                // Write byte buffer to source output
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    86
                sourceDataLine.write(buffer, 0, count);
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    87
            }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    88
        } catch (IOException e) {
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    89
            active = false;
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    90
            //e.printStackTrace();
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    91
        }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    92
    }
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents:
diff changeset
    93
}