Setting up multiple audio streams using Icecast in PHP requires a combination of server-side and client-side code. Below, I’ll provide a simple example of how you can set up a basic Icecast server with multiple streams and a PHP script to stream audio from the microphone using the Icecast source client, ices.
Step 1: Install Icecast
First, make sure you have Icecast installed on your server. You can follow the official Icecast documentation for installation: Icecast Installation
Step 2: Configure Icecast
Edit the Icecast configuration file (usually located at /etc/icecast2/icecast.xml
on Linux systems) to include multiple mount points:
<!-- /etc/icecast2/icecast.xml -->
<icecast>
<!-- Other Icecast configuration settings -->
<mount>
<mount-name>/stream1</mount-name>
<username>source</username>
<password>hackme</password>
<max-listeners>10</max-listeners>
<!-- Other mount-specific settings -->
</mount>
<mount>
<mount-name>/stream2</mount-name>
<username>source</username>
<password>hackme</password>
<max-listeners>10</max-listeners>
<!-- Other mount-specific settings -->
</mount>
<!-- Add more mount points as needed -->
<!-- Other Icecast configuration settings -->
</icecast>
Step 3: Install Ices
Ices is the source client for Icecast. Install it on your server:
sudo apt-get install ices2
Step 4: Create a PHP script to stream audio from the microphone
Create a PHP script (stream_mic.php
) to execute the ices command and stream audio from the microphone:
<?php
// stream_mic.php
$stream1Command = "ices /path/to/ices-config-stream1.xml";
$stream2Command = "ices /path/to/ices-config-stream2.xml";
// Execute commands in the background
exec($stream1Command . " > /dev/null 2>&1 &");
exec($stream2Command . " > /dev/null 2>&1 &");
echo "Audio streams started.";
?>
Step 5: Create Ices Configuration Files
Create separate Ices configuration files (ices-config-stream1.xml
and ices-config-stream2.xml
) for each stream:
<!-- ices-config-stream1.xml -->
<ices>
<background>1</background>
<consolelog>0</consolelog>
<stream>
<metadata>
<name>Stream 1</name>
<genre>Various</genre>
<description>First audio stream from the microphone</description>
</metadata>
<input>
<module>oss</module>
<device>/dev/dsp</device>
<samplerate>44100</samplerate>
<channels>2</channels>
<type>mp3</type>
</input>
<instance>
<hostname>localhost</hostname>
<port>8000</port>
<password>hackme</password>
<mount>/stream1</mount>
</instance>
</stream>
</ices>
Create a similar configuration file for the second stream (ices-config-stream2.xml
) with appropriate settings.
Step 6: Run the PHP script
Run the PHP script to start streaming audio from the microphone to multiple Icecast streams:
php stream_mic.php
Ensure that the Icecast server is running, and you should be able to listen to the streams at:
- http://your-server:8000/stream1
- http://your-server:8000/stream2
Adjust the configuration files and PHP script as needed based on your requirements and server setup.