Esempio n. 1
0
        static void Main(string[] args)
        {
            string mp3FilePath = args[0];

            var sw = new Stopwatch();

            sw.Start();

            string stateJsonInit = string.Empty;

            // Read json state.
            using (var fs = File.OpenRead($"{mp3FilePath}.json"))
            {
                byte[] arrJson = new byte[fs.Length];
                fs.Read(arrJson, 0, arrJson.Length);
                stateJsonInit = System.Text.ASCIIEncoding.UTF8.GetString(arrJson, 0, arrJson.Length);
            }

            using (IStorage storage = GoogleDriveStorageFactory.Create("", "", ""))
            //using(IStorage storage = new LocalStorage(""))
            {
                using (var file = storage.Open("/Wovenwar/Honor is Dead/World on Fire.mp3"))
                //using(var file = storage.Open("World on Fire.mp3"))
                {
                    using (var encoder = MediaEncoderExtension.EncoderByMediaType("mp3"))
                    {
                        if (encoder.Init(file, false, stateJsonInit))
                        {
                            var packets = encoder.ReadPackets(100, 50);

                            // // Save state json
                            // string stateJson;
                            // encoder.SaveStateIntoJson(true, out stateJson);

                            // // Save json state.
                            // using(var fs = File.OpenWrite($"{mp3FilePath}.json"))
                            // {
                            //     byte[] arrJson = System.Text.ASCIIEncoding.UTF8.GetBytes(stateJson);
                            //     fs.Write(arrJson, 0, arrJson.Length);
                            // }

                            sw.Stop();

                            Console.WriteLine($"Elapsed {sw.ElapsedMilliseconds}ms");
                        }
                    }
                }
            }
        }
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);
        }