We have developed PeerStreaming, an on-demand peer-to-peer (P2P) media streaming system. The behavior of a PeerStreaming client is very much like an ordinary media player: it streams and plays whatever media the user desires, starting at whatever point he/she desires. Under the hood, however, the media is delivered from multiple peers to the client. The serving peers can be the media server, but more commonly they are the clients that have received the media in a previous streaming session. Leveraging the network and storage resources of the P2P network and retrieving a major part of the media from nearby peers, PeerStreaming greatly improves streaming media quality, reduces server load, and eases traffic on the network. We describe the design and implementation of the PeerStreaming system in this paper, with a focus on a number of key components. They include: (1) peer discovery and content location, (2) scalable coded and randomly accessible media format, (3) receiver-driven streaming, (4) digital rights management, and (5) architecture for the media rendering, caching and serving system.