📄 httpplayer.java
字号:
/* Stream-2-Stream - Peer to peer television and radio
* October 13, 2005 - This file has been modified from the original P2P-Radio source
* Project homepage: http://s2s.sourceforge.net/
* Copyright (C) 2005-2006 Jason Hooks
*/
/*
* P2P-Radio - Peer to peer streaming system
* Project homepage: http://p2p-radio.sourceforge.net/
* Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
*
* ---------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ---------------------------------------------------------------------------
*/
package p2pradio.players;
import stream2stream.network.*;
import p2pradio.*;
import p2pradio.logging.Logger;
import java.io.*;
import java.net.*;
/**
* Creates a server socket and waits for connections from a
* Shoutcast-/Icecast-compatible media player.
* <P>
* For every new connection a HttpPlayerServer is started.
*
* @author Michael Kaufmann
*/
//TODO - Add each HttpPlayerServer into a queue so they can be shut down
public class HttpPlayer extends Player
{
public static final String PLAYLIST_NAME = "/playlist.pls"; //$NON-NLS-1$
private Buffer buffer;
private ServerSocket socket;
private int port;
private long startTime; //Start Time (in milliseconds)
private long Time_Server_Has_Been_Running_At_Startup; //The time the server has been running when we start P2PRadio (in milliseconds)
//Will always be 0 if we are the server
private boolean shutdown;
private Peer peer;
/**
* Creates a Shoutcast Player.
*
* @param peer The peer which will give us info about the stream
*/
public HttpPlayer(Peer peer)
{
this(peer.getBuffer(), peer.getSocketAddress().getPort() + Peer.PLAYER_PORT_OFFSET);
startTime = peer.getStartTime();
Time_Server_Has_Been_Running_At_Startup = peer.Get_Time_Server_Has_Been_Running_At_Startup();
shutdown = false;
this.peer = peer;
}
/**
* Creates a Shoutcast Player.
*
* @param buffer The buffer that this player will use
* @param port The port number for the server socket
*/
public HttpPlayer(Buffer buffer, int port)
{
super(null, Messages.getString("HttpPlayer.THREAD_NAME")); //$NON-NLS-1$
this.buffer = buffer;
this.port = port;
}
/**
* Returns the MIME type that this player can handle: <code>audio/mpeg</code>.
*/
public String getMIMEType()
{
return "*/*"; //$NON-NLS-1$
}
public void run()
{
Logger.finer("HttpPlayer", "HttpPlayer.THREAD_RUNNING_PORT_NUMBER", new Integer(port)); //$NON-NLS-1$ //$NON-NLS-2$
try
{
try
{
if (peer.getXML().Get_Allow_Connections_From_Remote_Hosts())
{
socket = new ServerSocket(port);
}
else
{
socket = new ServerSocket(port, 0, InetAddress.getByName("127.0.0.1")); //$NON-NLS-1$
}
}
catch (IOException e)
{
Logger.fine("HttpPlayer", Messages.getString("COULD_NOT_CREATE_TCP_SERVERSOCKET"), e); //$NON-NLS-1$ //$NON-NLS-2$
return;
}
while (!shutdown)
{
Socket newsocket = null;
try
{
newsocket = socket.accept();
}
catch (IOException e)
{
if(shutdown)
return;
Logger.fine("HttpPlayer", "IO_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
continue;
}
if(shutdown)
return;
new HttpPlayerServer(this, new ListenBuffer(buffer, peer.getUI().getMetadata()), newsocket).start();
}
}
catch (Exception e)
{
Logger.severe("HttpPlayer", "INTERNAL_ERROR", e); //$NON-NLS-1$ //$NON-NLS-2$
}
finally
{
try {
socket.close();
} catch (IOException e) {
}
}
}
public String getHost()
{
return "localhost"; //$NON-NLS-1$
}
public int getPort()
{
return port;
}
public String getStreamURL()
{
// Einen Dateinamen erfinden
// Besonders wichtig f黵 NSV (Nullsoft Video), weil sonst das Videofenster nicht erscheint
String fileName = ""; //$NON-NLS-1$
if (buffer.getNewestMetadataPacket() != null)
{
String mimeType = peer.getUI().getMetadata().getContentType();
if (mimeType.equals("audio/mpeg")) //$NON-NLS-1$
{
fileName = "stream.mp3"; //$NON-NLS-1$
}
else if (mimeType.equals("video/nsv")) //$NON-NLS-1$ //$NON-NLS-2$
{
fileName = ";stream.nsv"; //$NON-NLS-1$
}
else if (mimeType.equals("application/ogg")) //$NON-NLS-1$
{
fileName = "stream.ogg"; //$NON-NLS-1$
}
}
return "http://" + getHost() + ":" + getPort() + "/" + fileName; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public String getPlaylistURL()
{
return "http://" + getHost() + ":" + getPort() + PLAYLIST_NAME; //$NON-NLS-1$ //$NON-NLS-2$
}
public long getStartTime()
{
return startTime;
}
public long Get_Time_Server_Has_Been_Running_At_Startup()
{
return Time_Server_Has_Been_Running_At_Startup;
}
public void shutdown()
{
shutdown = true;
if (socket != null)
{
try {
socket.close();
} catch (IOException e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -