Select and Monitor Bitrate

The bitrate of Smooth Streaming media automatically adjusts to the bandwidth capacity of the network and client processors. Applications can monitor the bitrate and apply limits that are based on commercial or security requirements.

This example uses the events PlaybackTrackChanged and ManifestReady to demonstrate a simple performance monitor and implementation of bandwidth limits according to customer account privileges.

Bitrate changes while a stream is playing raise the PlaybackTrackChanged event. Applications can monitor the bitrate in the delegate that handles the event. The following implementation reads the new bitrate from the TrackChangedEventArgs and prints it to a text block.

    void SmoothPlayer_PlaybackTrackChanged(object sender, TrackChangedEventArgs e)
    {
        OutputText.Text = "Current bitrate: " + e.NewTrack.Bitrate.ToString();
    }

In some applications it is useful to set the bitrate rather than allowing the rate to adjust automatically according to bandwidth capacity. For example, an application might limit high performance bitrates to customers who pay for premium service.

The following example limits the bitrate for customers who do not have premium accounts. First it gets the StreamInfo object for the video stream. Next the method AvailableTracks gets the available tracks of the video stream. The highRate variable allows only bitrates less than this number to users with non-premium accounts. (Addition of 1 tick to the highRate variable simplifies the lambda expression in the next step.)

The lambda expression re-initializes a list of type TrackInfo that contains only the bitrates that are less than highRate. The method SelectTracks assigns usable tracks to the lower bitrates.

    // Switch for track selection.
    Boolean PremiumAccount = false;
    void SmoothPlayer_ManifestReady(object sender, EventArgs e)
    {
        if (!PremiumAccount)
        {
            foreach (SegmentInfo segment in SmoothPlayer.ManifestInfo.Segments)
            {
                IList<StreamInfo> streamInfoList = segment.AvailableStreams;
                foreach (StreamInfo stream in streamInfoList)
                {
                    if (stream.Type == MediaStreamType.Video)
                    {
                        // Limit bitrate to 866000.
                        ulong highRate = 866000 + 1;
                        List<TrackInfo> tracks = new List<TrackInfo>();
   
                        tracks = stream.AvailableTracks.ToList<TrackInfo>();
                        IList<TrackInfo> allowedTracks = tracks.Where((ti) => ti.Bitrate < highRate).ToList();
                        stream.SelectTracks(allowedTracks, false);
                    }
                }
            }
        }
    }

Show: