예제 #1
0
        void _sessionManager_PlaybackStop(object sender, PlaybackStopEventArgs e)
        {
            string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item.Id.ToString("N");

            if (playback_trackers.ContainsKey(key))
            {
                _logger.LogInformation("Playback stop tracker found, processing stop : {Key}", key);
                PlaybackTracker tracker   = playback_trackers[key];
                List <string>   event_log = tracker.ProcessStop(e);
                if (event_log.Count > 0)
                {
                    _logger.LogDebug("ProcessProgress : {Events}", string.Join("", event_log));
                }

                // if playback duration was long enough save the action
                if (tracker.TrackedPlaybackInfo != null)
                {
                    _logger.LogInformation("Saving playback tracking activity in DB");
                    _repository.UpdatePlaybackAction(tracker.TrackedPlaybackInfo);
                }
                else
                {
                    _logger.LogInformation("Playback stop but TrackedPlaybackInfo not found! not storing activity in DB");
                }

                // remove the playback tracer from the map as we no longer need it.
                playback_trackers.Remove(key);
            }
            else
            {
                _logger.LogInformation("Playback stop did not have a tracker : {Key}", key);
            }
        }
        void _sessionManager_PlaybackProgress(object sender, PlaybackProgressEventArgs e)
        {
            string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item.Id.ToString("N");

            if (playback_trackers != null && playback_trackers.ContainsKey(key))
            {
                try
                {
                    //_logger.Info("Playback progress tracker found, processing progress : " + key);
                    PlaybackTracker tracker = playback_trackers[key];
                    tracker.ProcessProgress(e);
                    if (tracker.TrackedPlaybackInfo != null)
                    {
                        _logger.Debug("Saving playback tracking activity in DB");
                        _repository.UpdatePlaybackAction(tracker.TrackedPlaybackInfo);
                    }
                }
                catch (Exception exp)
                {
                    playback_trackers.Remove(key);
                    throw new Exception("Error saving playback state: " + exp.Message);
                }
            }
            else
            {
                _logger.Debug("Playback progress did not have a tracker : " + key);
            }
        }
예제 #3
0
        void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
        {
            if (e.MediaInfo == null)
            {
                return;
            }

            if (e.Item != null && e.Item.IsThemeMedia)
            {
                // Don't report theme song or local trailer playback
                return;
            }

            if (e.Users.Count == 0)
            {
                return;
            }

            string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item?.Id.ToString("N");

            if (playback_trackers.ContainsKey(key))
            {
                _logger.LogInformation("Existing tracker found! : " + key);

                PlaybackTracker track = playback_trackers[key];
                if (track.TrackedPlaybackInfo != null)
                {
                    _logger.LogInformation("Saving existing playback tracking activity in DB");
                    List <string> event_log = new List <string>();
                    track.CalculateDuration(event_log);
                    if (event_log.Count > 0)
                    {
                        _logger.LogDebug("CalculateDuration : {Events}", string.Join("", event_log));
                    }
                    _repository.UpdatePlaybackAction(track.TrackedPlaybackInfo);
                }

                _logger.LogInformation("Removing existing tracker : " + key);
                playback_trackers.Remove(key);
            }

            _logger.LogInformation("Adding playback tracker : " + key);
            PlaybackTracker tracker = new PlaybackTracker(_loggerFactory.CreateLogger <PlaybackTracker>());

            tracker.ProcessStart(e);
            playback_trackers.Add(key, tracker);

            // start a task to report playback started
            _logger.LogInformation("Creating StartPlaybackTimer Task");
            Task.Run(() => StartPlaybackTimer(e));
        }
예제 #4
0
        void _sessionManager_PlaybackProgress(object sender, PlaybackProgressEventArgs e)
        {
            string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item.Id.ToString("N");

            if (playback_trackers != null && playback_trackers.ContainsKey(key))
            {
                try
                {
                    PlaybackTracker tracker = playback_trackers[key];
                    DateTime        now     = DateTime.Now;
                    if (now.Subtract(tracker.LastUpdated).TotalSeconds > 20) // update every 20 seconds
                    {
                        tracker.LastUpdated = now;
                        _logger.LogInformation("Processing playback tracker : {Key}", key);
                        List <string> event_log = tracker.ProcessProgress(e);
                        if (event_log.Count > 0)
                        {
                            _logger.LogDebug("ProcessProgress : {Events}", string.Join("", event_log));
                        }
                        if (tracker.TrackedPlaybackInfo != null)
                        {
                            _repository.UpdatePlaybackAction(tracker.TrackedPlaybackInfo);
                        }
                    }
                }
                catch (Exception exp)
                {
                    playback_trackers.Remove(key);
                    throw new Exception("Error saving playback state: " + exp.Message);
                }
            }
            else
            {
                _logger.LogDebug("Playback progress did not have a tracker : {Key}", key);
            }
        }
예제 #5
0
        public async Task StartPlaybackTimer(PlaybackProgressEventArgs e)
        {
            _logger.LogInformation("StartPlaybackTimer : Entered");
            await Task.Delay(20000);

            try
            {
                var session = _sessionManager.GetSession(e.DeviceId, e.ClientName, "");
                if (session != null)
                {
                    string event_playing_id = e.Item.Id.ToString("N");

                    string event_user_id     = e.Users[0].Id.ToString("N");
                    long   event_user_id_int = e.Users[0].InternalId;

                    string session_playing_id = "";
                    if (session.NowPlayingItem != null)
                    {
                        session_playing_id = session.NowPlayingItem.Id.ToString("N");
                    }
                    string session_user_id = "";

                    _logger.LogInformation("session.RemoteEndPoint : {RemoteEndPoint}", session.RemoteEndPoint);

                    if (session.UserId != Guid.Empty)
                    {
                        session_user_id = session.UserId.ToString("N");
                    }

                    string play_method = "na";
                    if (session.PlayState != null && session.PlayState.PlayMethod != null)
                    {
                        play_method = session.PlayState.PlayMethod.Value.ToString();
                    }
                    if (session.PlayState != null && session.PlayState.PlayMethod == MediaBrowser.Model.Session.PlayMethod.Transcode)
                    {
                        if (session.TranscodingInfo != null)
                        {
                            string video_codec = "direct";
                            if (session.TranscodingInfo.IsVideoDirect == false)
                            {
                                video_codec = session.TranscodingInfo.VideoCodec;
                            }
                            string audio_codec = "direct";
                            if (session.TranscodingInfo.IsAudioDirect == false)
                            {
                                audio_codec = session.TranscodingInfo.AudioCodec;
                            }
                            play_method += " (v:" + video_codec + " a:" + audio_codec + ")";
                        }
                    }

                    string item_name = GetItemName(e.Item);
                    string item_id   = e.Item.Id.ToString("N");
                    string item_type = e.MediaInfo.Type;

                    _logger.LogInformation("StartPlaybackTimer : event_playing_id     = {EventPlayingId}", event_playing_id);
                    _logger.LogInformation("StartPlaybackTimer : event_user_id        = {EventUserId}", event_user_id);
                    _logger.LogInformation("StartPlaybackTimer : event_user_id_int    = {EventUserIdInternal}", event_user_id_int);
                    _logger.LogInformation("StartPlaybackTimer : session_playing_id   = {SessionPlayingId}", session_playing_id);
                    _logger.LogInformation("StartPlaybackTimer : session_user_id      = {SessionUserId}", session_user_id);
                    _logger.LogInformation("StartPlaybackTimer : play_method          = {PlayMethod}", play_method);
                    _logger.LogInformation("StartPlaybackTimer : e.ClientName         = {ClientName}", e.ClientName);
                    _logger.LogInformation("StartPlaybackTimer : e.DeviceName         = {DeviceName}", e.DeviceName);
                    _logger.LogInformation("StartPlaybackTimer : ItemName             = {ItemName}", item_name);
                    _logger.LogInformation("StartPlaybackTimer : ItemId               = {ItemId}", item_id);
                    _logger.LogInformation("StartPlaybackTimer : ItemType             = {ItemType}", item_type);

                    PlaybackInfo play_info = new PlaybackInfo
                    {
                        Id             = Guid.NewGuid().ToString("N"),
                        Date           = DateTime.Now,
                        ClientName     = e.ClientName,
                        DeviceName     = e.DeviceName,
                        PlaybackMethod = play_method,
                        UserId         = event_user_id,
                        ItemId         = item_id,
                        ItemName       = item_name,
                        ItemType       = item_type
                    };

                    if (event_playing_id == session_playing_id && event_user_id == session_user_id)
                    {
                        _logger.LogInformation("StartPlaybackTimer : All matches, playback registered");

                        // update tracker with playback info
                        string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item.Id.ToString("N");
                        if (playback_trackers.ContainsKey(key))
                        {
                            _logger.LogInformation("Playback tracker found, adding playback info : {Key}", key);
                            PlaybackTracker tracker = playback_trackers[key];
                            tracker.TrackedPlaybackInfo = play_info;

                            _logger.LogInformation("Saving playback tracking activity in DB");
                            _repository.AddPlaybackAction(tracker.TrackedPlaybackInfo);
                        }
                        else
                        {
                            _logger.LogInformation("Playback trackler not found : {Key}", key);
                        }
                    }
                    else
                    {
                        _logger.LogInformation("StartPlaybackTimer : Details do not match for play item");
                    }
                }
                else
                {
                    _logger.LogInformation("StartPlaybackTimer : session Not Found");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "StartPlaybackTimer : Unexpected error occurred");
            }
            _logger.LogInformation("StartPlaybackTimer : Exited");
        }
        public async System.Threading.Tasks.Task StartPlaybackTimer(PlaybackProgressEventArgs e)
        {
            _logger.Info("StartPlaybackTimer : Entered");
            await System.Threading.Tasks.Task.Delay(20000);

            try
            {
                var session = _sessionManager.GetSession(e.DeviceId, e.ClientName, "");
                if (session != null)
                {
                    string event_playing_id = e.Item.Id.ToString("N");
                    string event_user_id    = e.Users[0].Id.ToString("N");

                    string session_playing_id = "";
                    if (session.NowPlayingItem != null)
                    {
                        session_playing_id = session.NowPlayingItem.Id.ToString("N");
                    }
                    string session_user_id = "";

                    _logger.Info("session.RemoteEndPoint : " + session.RemoteEndPoint);

                    if (session.UserId != null)
                    {
                        session_user_id = ((Guid)session.UserId).ToString("N");
                    }

                    string play_method = "na";
                    if (session.PlayState != null && session.PlayState.PlayMethod != null)
                    {
                        play_method = session.PlayState.PlayMethod.Value.ToString();
                    }
                    if (session.PlayState != null && session.PlayState.PlayMethod == MediaBrowser.Model.Session.PlayMethod.Transcode)
                    {
                        if (session.TranscodingInfo != null)
                        {
                            string video_codec = "direct";
                            if (session.TranscodingInfo.IsVideoDirect == false)
                            {
                                video_codec = session.TranscodingInfo.VideoCodec;
                            }
                            string audio_codec = "direct";
                            if (session.TranscodingInfo.IsAudioDirect == false)
                            {
                                audio_codec = session.TranscodingInfo.AudioCodec;
                            }
                            play_method += " (v:" + video_codec + " a:" + audio_codec + ")";
                        }
                    }

                    string item_name = GetItemName(e.Item);
                    string item_id   = e.Item.Id.ToString("N");
                    string item_type = e.MediaInfo.Type;

                    _logger.Info("StartPlaybackTimer : event_playing_id   = " + event_playing_id);
                    _logger.Info("StartPlaybackTimer : event_user_id      = " + event_user_id);
                    _logger.Info("StartPlaybackTimer : session_playing_id = " + session_playing_id);
                    _logger.Info("StartPlaybackTimer : session_user_id    = " + session_user_id);
                    _logger.Info("StartPlaybackTimer : play_method        = " + play_method);
                    _logger.Info("StartPlaybackTimer : e.ClientName       = " + e.ClientName);
                    _logger.Info("StartPlaybackTimer : e.DeviceName       = " + e.DeviceName);
                    _logger.Info("StartPlaybackTimer : ItemName           = " + item_name);
                    _logger.Info("StartPlaybackTimer : ItemId             = " + item_id);
                    _logger.Info("StartPlaybackTimer : ItemType           = " + item_type);

                    PlaybackInfo play_info = new PlaybackInfo();
                    play_info.Id             = Guid.NewGuid().ToString("N");
                    play_info.Date           = DateTime.Now;
                    play_info.ClientName     = e.ClientName;
                    play_info.DeviceName     = e.DeviceName;
                    play_info.PlaybackMethod = play_method;
                    play_info.UserId         = event_user_id;
                    play_info.ItemId         = item_id;
                    play_info.ItemName       = item_name;
                    play_info.ItemType       = item_type;

                    if (event_playing_id == session_playing_id && event_user_id == session_user_id)
                    {
                        _logger.Info("StartPlaybackTimer : All matches, playback registered");

                        // update tracker with playback info
                        string key = e.DeviceId + "-" + e.Users[0].Id.ToString("N") + "-" + e.Item.Id.ToString("N");
                        if (playback_trackers.ContainsKey(key))
                        {
                            _logger.Info("Playback tracker found, adding playback info : " + key);
                            PlaybackTracker tracker = playback_trackers[key];
                            tracker.TrackedPlaybackInfo = play_info;

                            _logger.Info("Saving playback tracking activity in DB");
                            _repository.AddPlaybackAction(tracker.TrackedPlaybackInfo);
                        }
                        else
                        {
                            _logger.Info("Playback trackler not found : " + key);
                        }
                    }
                    else
                    {
                        _logger.Info("StartPlaybackTimer : Details do not match for play item");
                    }
                }
                else
                {
                    _logger.Info("StartPlaybackTimer : session Not Found");
                }
            }
            catch (Exception exception)
            {
                _logger.Info("StartPlaybackTimer : Error = " + exception.Message + "\n" + exception.StackTrace);
            }
            _logger.Info("StartPlaybackTimer : Exited");
        }