home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 5.6 KB | 159 lines | [TEXT/CWIE] |
- /*
- File: Transmitter.java
-
- Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Change History (most recent first):
-
- */
-
-
- package com.apple.jens.radio;
-
- import java.io.*;
- import java.net.Socket;
-
-
- /** A "consumer" Thread that sends MP3 audio to a remote client
- by writing data from Buffers to Sockets.
- There is one of these per active client connection to the server.
-
- */
-
- public class Transmitter extends Thread {
-
- /** Creates a Transmitter, but doesn't start it running yet.
- @param dj The Station's DJ
- @param s The Socket to write to */
- public Transmitter( DJ dj, Socket s ) throws IOException {
- super(dj.getStation().getName()+"'s transmitter to "+s.getInetAddress().getHostName());
- LOG(1,"Starting connection");
- fDJ = dj;
- fStation = dj.getStation();
- fSocket = s;
- fOut = s.getOutputStream();
- }
-
-
- /** Writes the contents of a Buffer to the Socket. */
- private long sendBuffer( Buffer buf ) throws IOException, InterruptedException {
- LOG(2,"sending "+buf+"...");
- long millis = buf.writeTo(fOut); // will block for quite a while!
- fOut.flush();
- LOG(2,"finished sending "+buf);
- return millis;
- }
-
-
- /** Writes the HTTP-esque "reply" header that precedes the audio data. */
- public void writeHeader( ) throws IOException {
- Station station = fDJ.getStation();
- PrintStream ps = new PrintStream(fOut);
- ps.print("ICY 200 OK\r\n");
- ps.print("Server:"+Radio.getNameAndVersion()+"\r\n");
- ps.print("Content-type:audio/mpeg\r\n");
- ps.print("Content-length:5000000000\r\n"); // Yes, this is supposed to be hardcoded!
- if( station.getStationName() != null )
- ps.print("icy-name:"+station.getStationName()+"\r\n");
- if( station.getGenre() != null )
- ps.print("icy-genre:"+station.getGenre()+"\r\n");
- String url = fStation.getProperties().getProperty(Station.kPropHomePage);
- if( url != null )
- ps.print("icy-url:"+url+"\r\n\r\n");
- ps.flush();
- }
-
-
- public void run( ) {
- Throttle throttle = null;
-
- try{
- writeHeader();
-
- int i = fDJ.getFirstBufferIndex();
- while(true) {
- Buffer buf = fDJ.getFilledBuffer(i++);
- if( buf == null )
- break;
-
- if( throttle == null )
- throttle = new Throttle(getName()); // we've started streaming
-
- long millis = sendBuffer(buf); // Transmit the data! Will block for quite a while
-
- // Don't go faster than the audio bitrate or I'll overtake the DJ:
- //throttle.addTime( millis ); // Commented out; this may make Audion and Amp happier --jpa 7/14/00
- }
-
- }catch( IOException x ) {
- // This happens 'normally' whenever the Socket's closed from the other end.
- LOG(1,"Closed connection with "+fSocket.getInetAddress().getHostName());
- if( Radio.sLogLevel >= 2 )
- x.printStackTrace();
- }catch( InterruptedException x ) {
- LOG(1,"Thread interrupted!");
- } finally {
- fStation.transmitterStopped(this);
- }
-
- LOG(2,"Closing socket");
- try{
- fSocket.close();
- }catch( IOException x ) {
- System.err.println(this+": exception closing socket:");
- x.printStackTrace();
- }
- fSocket = null;
- }
-
-
- private void LOG( int level, String msg ) {
- Radio.LOG(level,getName(),msg);
- }
-
-
- // INSTANCE DATA:
-
-
- private final DJ fDJ;
- private final Station fStation;
- private Socket fSocket;
- private OutputStream fOut;
-
- }
-