Skip to content

Commit

Permalink
Added limit to the maximum speed at which it sends. No more opening t…
Browse files Browse the repository at this point in the history
…he stream as download and getting 500MB in a few seconds.
  • Loading branch information
Banane9 committed Jan 18, 2014
1 parent 0e21e9d commit e82e021
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
7 changes: 6 additions & 1 deletion PiStrom/Config/StreamInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ public class StreamInfo
public string Genre { get; set; }

/// <summary>
/// The Interval (in bytes) in which meta information about the stream is sent. Also the size of the byte buffer.
/// Gets or sets the interval (in bytes) in which meta information about the stream is sent. Also the size of the byte buffer.
/// </summary>
public int MetaInt { get; set; }

/// <summary>
/// Gets or sets the maximum rate at which data is sent. In bytes per second.
/// </summary>
public int TargetByteRate { get; set; }

/// <summary>
/// Gets or sets the Music that is played on the stream.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions PiStrom/Config/StreamInfo.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<xs:element name="Name" type="xs:string" />
<xs:element name="Genre" type="xs:string" />
<xs:element name="MetaInt" type="xs:unsignedInt" />
<xs:element name="TargetByteRate" type="xs:unsignedInt" />
<xs:element name="Music">
<xs:complexType>
<xs:sequence>
Expand Down
15 changes: 14 additions & 1 deletion PiStrom/MusicStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class MusicStream

private Random random = new Random();

private int delay;

public MusicStream(string configPath)
{
XmlReader reader = XmlReader.Create(configPath);
Expand All @@ -43,12 +45,14 @@ public MusicStream(string configPath)

fileBuffer = new byte[StreamInfo.MetaInt];

delay = (int)(((double)StreamInfo.MetaInt / (double)StreamInfo.TargetByteRate) * 1000d);

Running = false;
}

public void AddClient(Socket client, bool metaInfo)
{
string responseHeader = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nServer: PiStrøm\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nConnection: close\r\n" + (metaInfo ? "icy-metaint:" + StreamInfo.MetaInt + "\r\nicy-name:" + StreamInfo.Name + "\r\nicy-genre:" + StreamInfo.Genre + "\r\n" : "") + "\r\n"; //icy-url:http://localhost:1337\r\n type: audio/mpeg
string responseHeader = "HTTP/1.1 200 OK\r\nContent-Type: audio/mpeg\r\nServer: PiStrøm\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nConnection: close\r\n" + (metaInfo ? "icy-metaint:" + StreamInfo.MetaInt + "\r\nicy-name:" + StreamInfo.Name + "\r\nicy-genre:" + StreamInfo.Genre + "\r\n" : "") + "\r\n"; //icy-url:http://localhost:1337\r\n type: audio/mpeg
client.Send(Encoding.UTF8.GetBytes(responseHeader));
clients.Add(client, metaInfo);
}
Expand All @@ -57,6 +61,8 @@ public void Run(CancellationToken cancellationToken)
{
Running = true;

DateTime lastSend = DateTime.Now.AddDays(-1);

if (fileStream == null)
setNewSourceFile();

Expand All @@ -73,6 +79,13 @@ public void Run(CancellationToken cancellationToken)

List<Socket> remove = new List<Socket>();

int sinceLastSend = (int)(DateTime.Now - lastSend).TotalMilliseconds;
if (sinceLastSend < delay)
{
Thread.Sleep(delay - sinceLastSend);
}
lastSend = DateTime.Now;

Parallel.ForEach(clients, client =>
{
try
Expand Down
1 change: 1 addition & 0 deletions PiStrom/Streams/Electro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Name>Electro Stream</Name>
<Genre>Electro</Genre>
<MetaInt>32768</MetaInt>
<TargetByteRate>24000</TargetByteRate>
<Music FileType="mp3">
<TimeSpan From="02:00" Till="21:40">
<Folder>C:\Users\Banane\Music\Binärpilot</Folder>
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ This folder contains the informations for the individual streams that will be av
<Name>Electro Stream</Name>
<Genre>Electro</Genre>
<MetaInt>32768</MetaInt>
<TargetByteRate>24000</TargetByteRate>
<Music FileType="mp3">
<TimeSpan From="02:00" Till="21:40">
<Folder>C:\Users\Banane\Music\Binärpilot</Folder>
Expand All @@ -86,6 +87,8 @@ While it is a bit more complex than `PiStrom.xml`, it's easy to understand as we

* `<MetaInt>` is the interval (in bytes) at which MetaInfo will be embedded into the Stream. Also defines the size of the buffer into which is read from the file and from which is written into the Stream.

* `<TargetByteRate>` is the maximum rate at which data is sent. In bytes per second. To get the value from the kbit/s of the music, multiply by 8000. For example 192kbit/s would require 24000 byte/s.

Now, the values for Name and Genre only matter if the Client requests `Ice-MetaInt: 1` as a HTTP header in the request. The MetaInfo will also only be sent if that was requested, but it still defines the buffer size.

On, to the `<Music>` tag:
Expand Down

0 comments on commit e82e021

Please sign in to comment.