private void UnregisterMediaPlugin(IMediaPlugin mediaPlugin)
        {
            var adaptiveMediaPlugin = mediaPlugin as IAdaptiveMediaPlugin;

            _adMarkerManager.Reset();
            _captionManager.Reset();
            _timelineMarkerManager.Reset();

            mediaPlugin.CurrentStateChanged -= MediaPlugin_CurrentStateChanged;
            mediaPlugin.MediaOpened -= MediaPlugin_MediaOpened;
            mediaPlugin.MediaFailed -= MediaPlugin_MediaFailed;
            mediaPlugin.MediaEnded -= MediaPlugin_MediaEnded;
            mediaPlugin.LogReady -= Plugin_LogReady;
            mediaPlugin.MarkerReached -= MediaPlugin_MarkerReached;
            mediaPlugin.BufferingProgressChanged -= MediaPlugin_BufferingProgressChanged;
            mediaPlugin.DownloadProgressChanged -= MediaPlugin_DownloadProgressChanged;
            mediaPlugin.PluginLoaded -= MediaPlugIn_PluginLoaded;
            mediaPlugin.PluginLoadFailed -= MediaPlugIn_PluginLoadFailed;
            mediaPlugin.PlaybackRateChanged -= MediaPlugin_PlaybackRateChanged;
            mediaPlugin.AdClickThrough -= MediaPlugin_AdClickThrough;
            mediaPlugin.AdProgressUpdated -= MediaPlugin_AdProgressUpdated;
            mediaPlugin.AdStateChanged -= mediaPlugin_AdStateChanged;
            mediaPlugin.AdError -= MediaPlugin_AdError;
            mediaPlugin.SeekCompleted -= MediaPlugin_SeekCompleted;

            if (adaptiveMediaPlugin != null)
            {
                ActiveHeuristicsPlugin.IfNotNull(i => i.UnregisterPlugin(adaptiveMediaPlugin));
                adaptiveMediaPlugin.ManifestReady -= MediaPlugin_ManifestReady;
                adaptiveMediaPlugin.StreamSelected -= MediaPlugin_StreamSelected;
                adaptiveMediaPlugin.StreamSelectionFailed -= MediaPlugin_StreamSelectionFailed;
                adaptiveMediaPlugin.StreamDataAdded -= MediaPlugin_StreamDataAdded;
                adaptiveMediaPlugin.DownloadStreamDataFailed -= MediaPlugin_DownloadStreamDataFailed;
                adaptiveMediaPlugin.DownloadStreamDataCompleted -= MediaPlugin_DownloadStreamDataCompleted;
                adaptiveMediaPlugin.AdaptiveStreamingErrorOccurred -= MediaPlugin_AdaptiveStreamingErrorOccurred;
                adaptiveMediaPlugin.StreamUnselected -= MediaPlugin_StreamUnselected;
                adaptiveMediaPlugin.StreamDataRemoved -= MediaPlugin_StreamDataRemoved;
                //adaptiveMediaPlugin.VideoPlaybackTrackChanged -= MediaPlugin_VideoPlaybackTrackChanged;
                adaptiveMediaPlugin.VideoDownloadTrackChanged -= MediaPlugin_VideoDownloadTrackChanged;
            }
            if (mediaPlugin is IVariableBitrateMediaPlugin)
            {
                ((IVariableBitrateMediaPlugin)mediaPlugin).VideoPlaybackBitrateChanged -= MediaPlugin_VideoPlaybackBitrateChanged;
            }

            if (PlaySpeedManager != null)
            {
                PlaySpeedManager.Dispose();
                PlaySpeedManager = null;
            }

            if (_retryMonitor != null)
            {
                _retryMonitor.RetryAttemptFailed -= RetryMonitor_RetryAttemptFailed;
                _retryMonitor.RetryCancelled -= RetryMonitor_RetryCancelled;
                _retryMonitor.RetryFailed -= RetryMonitor_RetryFailed;
                _retryMonitor.RetryStarted -= RetryMonitor_RetryStarted;
                _retryMonitor.RetrySuccessful -= RetryMonitor_RetrySuccessful;
                _retryMonitor.Dispose();
                _retryMonitor = null;
            }

            if (mediaPlugin.IsLoaded)
            {
                mediaPlugin.Unload();
            }

            MediaPresenterElement.IfNotNull(i => i.Content = null);
        }
        private void RegisterMediaPlugin(IMediaPlugin mediaPlugin)
        {
            var adaptiveMediaPlugin = mediaPlugin as IAdaptiveMediaPlugin;

            mediaPlugin.CurrentStateChanged += MediaPlugin_CurrentStateChanged;
            mediaPlugin.MediaOpened += MediaPlugin_MediaOpened;
            mediaPlugin.MediaFailed += MediaPlugin_MediaFailed;
            mediaPlugin.MediaEnded += MediaPlugin_MediaEnded;
            mediaPlugin.LogReady += Plugin_LogReady;
            mediaPlugin.MarkerReached += MediaPlugin_MarkerReached;
            mediaPlugin.BufferingProgressChanged += MediaPlugin_BufferingProgressChanged;
            mediaPlugin.DownloadProgressChanged += MediaPlugin_DownloadProgressChanged;
            mediaPlugin.PluginLoaded += MediaPlugIn_PluginLoaded;
            mediaPlugin.PluginLoadFailed += MediaPlugIn_PluginLoadFailed;
            mediaPlugin.PlaybackRateChanged += MediaPlugin_PlaybackRateChanged;
            mediaPlugin.AdClickThrough += MediaPlugin_AdClickThrough;
            mediaPlugin.AdProgressUpdated += MediaPlugin_AdProgressUpdated;
            mediaPlugin.AdStateChanged += mediaPlugin_AdStateChanged;
            mediaPlugin.AdError += MediaPlugin_AdError;
            mediaPlugin.SeekCompleted += MediaPlugin_SeekCompleted;


            if (adaptiveMediaPlugin != null)
            {
                adaptiveMediaPlugin.ManifestReady += MediaPlugin_ManifestReady;
                adaptiveMediaPlugin.StreamSelected += MediaPlugin_StreamSelected;
                adaptiveMediaPlugin.StreamUnselected += MediaPlugin_StreamUnselected;
                adaptiveMediaPlugin.StreamSelectionFailed += MediaPlugin_StreamSelectionFailed;
                adaptiveMediaPlugin.StreamDataAdded += MediaPlugin_StreamDataAdded;
                adaptiveMediaPlugin.StreamDataRemoved += MediaPlugin_StreamDataRemoved;
                adaptiveMediaPlugin.DownloadStreamDataFailed += MediaPlugin_DownloadStreamDataFailed;
                adaptiveMediaPlugin.DownloadStreamDataCompleted += MediaPlugin_DownloadStreamDataCompleted;
                adaptiveMediaPlugin.AdaptiveStreamingErrorOccurred += MediaPlugin_AdaptiveStreamingErrorOccurred;
                //adaptiveMediaPlugin.VideoPlaybackTrackChanged += MediaPlugin_VideoPlaybackTrackChanged;
                adaptiveMediaPlugin.VideoDownloadTrackChanged += MediaPlugin_VideoDownloadTrackChanged;
            }
            if (mediaPlugin is IVariableBitrateMediaPlugin)
            {
                ((IVariableBitrateMediaPlugin)mediaPlugin).VideoPlaybackBitrateChanged += MediaPlugin_VideoPlaybackBitrateChanged;
            }

            PlaySpeedManager = new PlaySpeedManager(mediaPlugin);
            InitPlaySpeedManager();

            _retryMonitor = new RetryMonitor
            {
                MediaPlugin = mediaPlugin
            };

            _retryMonitor.RetryAttemptFailed += RetryMonitor_RetryAttemptFailed;
            _retryMonitor.RetryCancelled += RetryMonitor_RetryCancelled;
            _retryMonitor.RetryFailed += RetryMonitor_RetryFailed;
            _retryMonitor.RetryStarted += RetryMonitor_RetryStarted;
            _retryMonitor.RetrySuccessful += RetryMonitor_RetrySuccessful;

            MediaPluginRegistered.IfNotNull(i => i(this, new CustomEventArgs<IMediaPlugin>(mediaPlugin)));
        }
 private void RetryMonitor_RetryCancelled(RetryMonitor retryMonitor)
 {
     SendLogEntry(message: SilverlightMediaFrameworkResources.RetryCancelledLogMessage, type: KnownLogEntryTypes.RetryCancelled);
     RetryState = RetryStateEnum.NotRetrying;
     ResetRetryInformation();
     RetryCancelled.IfNotNull(i => i(this, EventArgs.Empty));
 }
 private void RetryMonitor_RetryAttemptFailed(RetryMonitor retryMonitor, Exception error)
 {
     string logMessage = string.Format(SilverlightMediaFrameworkResources.RetryAttemptFailedLogMessage,
                                       error.Message);
     SendLogEntry(KnownLogEntryTypes.RetryAttemptFailed, LogLevel.Warning, logMessage);
     RetryAttemptFailed.IfNotNull(i => i(this, new CustomEventArgs<Exception>(error)));
 }
 private void RetryMonitor_RetryFailed(RetryMonitor retryMonitor)
 {
     SendLogEntry(KnownLogEntryTypes.RetryFailed, LogLevel.Warning, SilverlightMediaFrameworkResources.RetryFailedLogMessage);
     RetryState = RetryStateEnum.RetryFailed;
     RetryFailed.IfNotNull(i => i(this, EventArgs.Empty));
     TryNextPlaylistItem();
 }