I call this project NetFM. I hope the name doesn't sound stupid and nobody will drag me to court over trademark violations.
This project was made for submission as my semester 6 project for B.Tech Computer Science. It provides the basic framework for implementing Internet Broadcasting. I hope that the person who is reading this knows about Internet Broadcasting and the basics of network programming. I will be using SHOUTcast and Winamp as examples to explain about Internet Broadcasting. I was inspired by Winamp TV/Radio services (SHOUTcast) when I selected this topic.
Internet Broadcasting is your normal TV & Radio but on the Internet. The medium of transmission is the internetwork, LANs or WANs, instead of air/cable. And that ladies and gentlemen is the biggest trouble since most of us do not have sufficient bandwidth to receive real-time audio/video!
And also because the Internet is a packet-based network and was never made for transmission of real-time multimedia. Thus it is very difficult to achieve the same levels of quality as conventional TV transmission.
Probably you will experience problems in understanding what I am trying to say and since I have 3 programs in this project you may experience troubles in trying out the programs. I will do my best to keep matters as simple as possible. Please forgive me if you find my article difficult to follow.
During the early stages of this project (before programming started) I read a lot about audio formats, network programming( especially UDP) and Multicasting. Please checkout Multicasting, it will help you to better understand this project.
Using the code
Please read the project report...it will help you a lot . I have attached only the necessary pages from the report. Please contact me if you would like to read the entire report.
Before running the programs copy the file "textnewsbroadcastfile.txt" which I have attached in Download source & resources to your "c" drive. It provides news updates along with the audio stream.
How to use the programs
Ok its very easy to use the programs since I have made a "settings" button for all the 3 programs and once you have made the settings its as easy as riding a bicycle.
I advise that the first time when you run the programs keep the default settings. e.g.- "loopback" for IP address in the programs and run all 3 programs on the same computer ( don't worry...its a network program...will work anywhere on this planet...wherever TCP/IP is available). If you can get them working on a computer try it out on a LAN, or the Internet.
The 3 programs
1. StationServer. It is a server program very much like winamp.com , it keeps track of channels (which are essentially broadcast programs) and provides the Receiver programs with the list of all channels whenever requested. Only one instance of this program is needed for implementing the broadcast network.
A channel is identified by the IP address of the computer on which the broadcast program is running and the TCP port on which it receives connection requests... The StationServer program does no more than receive details from the broadcast programs and add them after some processing to the
ListBox that you can see in the above image.
Each channel has a descriptive name about its content (e.g. World News) , socket address to contact for listening to the broadcast (e.g. 127.0.0.1:1046) , number of active listeners and total number of listeners the broadcast program can support (e.g. 2/10) .
I am using Multiple Unicasting here, since Multicasting requires the support of "Multicast Routers" which are few in number over the internet.
Maximum Listeners= (Bandwidth available to Broadcast program) / (Rate of transmission).
This project uses MP3 file format. So transmission rate=bitrate of the MP3 file.
All other programs... the Mediaserver (broadcast program) and Receiver connect to the StationServer. You can run any number of Mediaservers and Receivers but only one StationServer.
The StationServer acts as the central server for this broadcast network.
2. Mediaserver .This is the broadcast program. On startup it informs the StationServer about its port details, content, maximum number of receivers. It must be loaded with a playlist file...I have provided an example in Download source & resources. The StationServer IP and Port must be set to that of the StationServer program. Otherwise no receiver can connect to this channel.
The broadcast program "plays" i.e. move the file pointer at the bitrate of the MP3 file. Please note that there is no audio output in the broadcast program. You must run a receiver to checkout that things are working properly.
Receivers get the list of channels from the StationServer and connect to a selected Mediaserver. On connection to the Mediaserver the receiver is add to the "Receiver List" and an update is sent to the StationServer.
In the broadcast program the MP3 file is loaded in memory and at intervals of 1 second packets of fixed size(=bitrate of MP3 file) , are sent to the Receiver over UDP. You can see the "Track Progress" bar that displays the status of current file. When a song ends, a new one is loaded from the playlist. You can also select a song from the playlist and press the "Load Selected Track" to manually stop transmission of current song and load a new file.
There are separate threads for audio broadcast and news broadcast. The source for the news broadcast is the file "c:\textnewsbroadcastfile.txt" in which the first entry is the number of news items followed by news snippets separated by "#" symbols.........
I am not including any code in the explanation of the working since the code is very simple and you need to run the programs a couple of times to understand how it works. Once you do that, checkout the source files, I have given as many comments as required, & the variables have been named such that they are easy to understand. Probably you will find my way of programming rather unconventional Most importantly, this article is already too big !!
3. Receiver . This program connects to the StationServer and obtains the list of channels on air. Then you can select any channel and the Receiver will try to connect to that particular channel.
The receiver will be added to the list of the broadcast program and it will receive data packets over UDP containing both audio content and news items.
The news items are display in the "NEWS Bar", the audio packets are buffered for playback, the buffer size can be set in the "settings" . Please be patient while the program buffers. You can see the buffer progress on the interface. After the first buffering you can listen to the audio being transmitted by the broadcast program (Mediaserver).
I use MCI for the playback of audio stream. The packets coming over the network are buffered and when the buffer limit is reached, written to disk and then MCI is used for playback. I write the buffer to disk only because I didn't find any way to implement playback from memory. Though I have kept the disk access to a minimum , it would still be a big improvement if any of you can suggest me some method to achieve playback of the memory buffer. The writing to disk alternates between 2 files. while one is written to, the other is played.
When you disconnect from a broadcast, the Mediaserver is contacted and its list updated, the StationServer is updated and, the receiver can now select another channel to listen to.
I made a jpeg image to explain how control and data flows...please note that I have depicted only a subset of the project working....
Important Points while trying the Programs
- You must copy the resource file "textnewsbroadcastfile.txt" to the "c" drive...it looks something like this...
number of news items
news item 1
news item 2
The MediaServer uses this file for news broadcast..( "c:\textnewsbroadcastfile.txt") you can modify it as required.....but keep the format otherwise the MediaServer program will get struck/crash/whatever.
Make playlist before you start the MediaServer program...the program accepts only filenames with extension "*.srp" or "*.m3u"...
"*.srp" is of the form...
E:\MP3\jukebox\Linkin Park\Linkin Park - Papercut.mp3
E:\MP3\jukebox\Linkin Park\Linkin Park - Pushing Me Away.mp3
E:\MP3\jukebox\Linkin Park\Linkin Park - Somewhere I Belong.mp3
E:\MP3\jukebox\Linkin Park\Linkin Park - By Myself.mp3
E:\MP3\jukebox\Linkin Park\linkin park - crawling.mp3
or use the Winamp playlist "*.m3u" provided it contains only the full pathnames and no other entries. Just make sure that the full file pathname is in the playlist.
- The StationServer should be started first. Followed by Mediaservers and Receivers. (any number of them) you can run all these on the same computer...now to get audio output you must connect to some channel
- The MediaServer makes the assumption that all playlist entries are CBR MP3s, no support for VBR files.
- Please click on the "Disconnect" button before you exit the Receiver program, otherwise the temporary files remain on the hard disk.
Right now I can't think of any more precautions to take If you have trouble in running the programs please contact me. I will rewrite the article as required.
Points of Interest
In the Mediaserver I don't use a linked list to add/remove clients from the broadcast. Instead I use the
ListBox itself to do all the work of adding, searching, removing receivers from the broadcast list.
Addstring to add receiver,
FindStringExact to search for an entry in the list and
DeleteString to remove the receivers from the broadcast list.
For the temporary files in the receiver program I use the UDP port numbers of the receiver program as filename, this helps in running as many receiver programs as you would like to on the same computer without them conflicting.
Multiple Unicasting consumes a lot of bandwidth, thus from an economical point of view, this project is damn costly to implement on large scale. Small scale implementation using low bitrate MP3's news, talk-shows with 8-16kbps can be easily implemented at colleges and small businesses. On LANs, since bandwidth is no problem, it can be easily used.
I will try to implement Multicasting and Video transmission and extent this to other file formats (broadcastable formats like MP3), if any of you would like to build up on this project please let me know, I would like to join if that's ok with you.
The improvements that are possible-
- Video Transmission
- Addition of more file formats
- Implementation of a "broadcast codec"
- Peer-to-peer relay broadcasting (based on the concept of peer-to-peer file sharing)
- Playback from memory (i.e. no temporary disk storage)
You have problems?
- Probably you started the programs in the wrong order.
- OR The "c:\textnewsbroadcastfile.txt" file is not where it should be.
- OR If you have big breaks in audio playback probably the MP3 file is a VBR one
- OR The MediaServer program made an error in determining the bitrate of the file.
- OR You have firewall enabled that might be blocking the UDP packets.
Can't think of anything else that might go wrong , so mail me if something terrible happens.
"If something can go wrong, it will go wrong!" - heard that somewhere.
That's all folks, bye bye.