Esempio n. 1
0
        public static IMediaMetadata ReadMetadata(this IStorageFile file, string format, out IEnumerable <AttachedPicture> pictures)
        {
            IMediaEncoder encoder = MediaEncoderExtension.EncoderByMediaType(format);

            pictures = null;

            if (encoder != null)
            {
                if (encoder.Init(file, true))
                {
                    pictures = encoder.AttachedPictures;
                    return(encoder.GetMetadata());
                }
            }
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads media packets passing delegate to read packets using different logic.
        /// </summary>
        /// <param name="sessionKey">Streaming session key.</param>
        /// <param name="mediaId">Media identifier.</param>
        /// <param name="funcReadPackets">Delegate function to read packets.</param>
        /// <returns></returns>
        protected AudioPackets ReadMediaPackets(string sessionKey, string songId, Func <IMediaEncoder, AudioPackets> funcReadPackets)
        {
            Guid gSongId, sessionId;

            if (!Guid.TryParse(songId, out gSongId) || !Guid.TryParse(sessionKey, out sessionId))
            {
                return(null);
            }

            var mediaFile = (from s in _mediaDataContext.Get <Song>()
                             where s.Id == gSongId && s.Media.IsArchived == false
                             select s.Media).FirstOrDefault();

            if (mediaFile == null)
            {
                return(null);
            }

            Guid playingMediaId = Guid.Empty;
            int  playingAtMSec  = 0;

            if (!_userSession.GetSessionInfo(sessionKey, out playingMediaId, out playingAtMSec))
            {
                throw new InvalidOperationException("Session not exists !");
            }

            string       encoderStateJson = string.Empty, mediaFileStateJson = string.Empty;
            AudioPackets packets     = null;
            int          currentMSec = 0;

            // Check media of state json.
            if (playingMediaId == gSongId)
            {
                mediaFileStateJson = _cacheService.ReadFileState <string>(sessionKey);
                encoderStateJson   = _cacheService.ReadEncoderState <string>(sessionKey);

                if (string.IsNullOrEmpty(encoderStateJson))
                {
                    encoderStateJson = ReadMediaFileEncoderStateJson(mediaFile, out mediaFileStateJson);
                    _cacheService.SaveEncoderState(sessionKey, encoderStateJson);
                }
            }
            else
            {
                encoderStateJson = ReadMediaFileEncoderStateJson(mediaFile, out mediaFileStateJson);
                _cacheService.SaveEncoderState(sessionKey, encoderStateJson);
                _cacheService.SaveFileState(sessionKey, mediaFileStateJson);
            }

            using (var media = string.IsNullOrEmpty(mediaFileStateJson) ?
                               _storage.Open(mediaFile.Url) :
                               _storage.OpenAndRestoreState(mediaFile.Url, mediaFileStateJson))
            {
                IMediaEncoder encoder = MediaEncoderExtension.EncoderByMediaType(mediaFile.Format);
                if (encoder == null)
                {
                    throw new InvalidOperationException($"Unable to instantiate encoder by format = '{mediaFile.Format}'!");
                }

                if (encoder.Init(media, false, encoderStateJson))
                {
                    packets = funcReadPackets.Invoke(encoder);
                    if (packets != null)
                    {
                        // if(encoder.SaveStateIntoJson(out encoderStateJson))
                        // {
                        //     // Save encoder state in cache.
                        //     SaveEncoderStateInCache(sessionKey, encoderStateJson);
                        // }

                        currentMSec = (int)encoder.CurrentMs;
                    }
                }

                // Save media file state in cache.
                if (string.IsNullOrEmpty(mediaFileStateJson))
                {
                    _cacheService.SaveFileState(sessionKey, media.SaveStateAsJson());
                }

                // ???
                //encoder.Dispose();
            }

            // Update playing session info.
            _userSession.UpdateSessionInfo(sessionKey, gSongId, currentMSec);
            return(packets);
        }