private byte[] DownloadFileInBlocks(string fileName)
        {
            CloudBlobContainer cloudBlobContainer = GetContainerReference();
            CloudBlockBlob     blob = cloudBlobContainer.GetBlockBlobReference(Path.GetFileName(fileName));

            int blockSize = 1024 * 1024; // 1 MB block size

            blob.FetchAttributes();
            long fileSize = blob.Properties.Length;

            byte[] blobContents = new byte[fileSize];
            int    position     = 0;

            while (fileSize > 0)
            {
                int blockLength = (int)Math.Min(blockSize, fileSize);

                blob.DownloadRangeToByteArray(blobContents, position, position, blockLength);

                position += blockLength;
                fileSize -= blockSize;
            }

            return(blobContents);
        }
        public List <VideoMediaBuffer> GetVideoMediaBuffers(long currentTick)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            // 1. Downlaod _nbSecondToLoad seconds of video content from the storage account
            long bufferSize = _frameSize * _videoFormat.FrameRate * _nbSecondToLoad;

            byte[] bytesToRead = new byte[bufferSize];
            var    nbByteRead  = _videoBlob.DownloadRangeToByteArray(bytesToRead, 0, _videoOffset, bytesToRead.Length, null, null);

            //2. Extract each video frame in a VideoMediaBuffer object
            List <VideoMediaBuffer> videoMediaBuffers = new List <VideoMediaBuffer>();

            long referenceTime = currentTick;

            for (int index = 0; index < nbByteRead; index += _frameSize)
            {
                IntPtr unmanagedBuffer = Marshal.AllocHGlobal(_frameSize);
                Marshal.Copy(bytesToRead, index, unmanagedBuffer, _frameSize);
                referenceTime += _frameDurationInTicks;

                var videoSendBuffer = new VideoSendBuffer(unmanagedBuffer, (uint)_frameSize, _videoFormat, referenceTime);
                videoMediaBuffers.Add(videoSendBuffer);

                _videoOffset += _frameSize;
            }
            Log.Info(new CallerInfo(), LogContext.FrontEnd, $"Loading {_nbSecondToLoad}s video took {watch.ElapsedMilliseconds}ms ({_frameSize * _videoFormat.FrameRate * _nbSecondToLoad} bytes)");

            watch.Stop();

            return(videoMediaBuffers);
        }
Exemple #3
0
        public static GenericBufferedStream ToStream(this CloudBlockBlob blob)
        {
            Func <long, byte[], int> filler = (remoteOffset, buffer) =>
            {
                var read = blob.DownloadRangeToByteArray(buffer, 0, remoteOffset, buffer.Length);
                return(read);
            };

            return(new GenericBufferedStream(blob.Properties.Length, filler));
        }
        public List <Code> GenerateCodesFromCloudFile(long[] offset)
        {
            var cFile = new CloudBlockBlob(CloudFile);
            var codes = new List <Code>();

            for (var i = offset[0]; i < offset[1]; i += 4)
            {
                var bytes = new byte[4];
                cFile.DownloadRangeToByteArray(bytes, index: 0, blobOffset: i, length: 4);
                var seedValue = BitConverter.ToInt32(bytes, 0);
                var code      = new Code()
                {
                    SeedValue = seedValue
                };
                codes.Add(code);
            }
            return(codes);
        }
        public List <AudioMediaBuffer> GetAudioMediaBuffers(long currentTick)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            // 1. Downlaod _nbSecondToLoad seconds of audio content from the storage account
            long bufferSize = 16000 * 2 * _nbSecondToLoad; // Pcm16K is 16000 samples per seconds, each sample is 2 bytes

            byte[] bytesToRead = new byte[bufferSize];
            var    nbByteRead  = _audioBlob.DownloadRangeToByteArray(bytesToRead, 0, _audioOffset, bytesToRead.Length, null, null);

            //2. Extract each audio sample in a AudioMediaBuffer object
            List <AudioMediaBuffer> audioMediaBuffers = new List <AudioMediaBuffer>();

            int  audioBufferSize = (int)(16000 * 2 * 0.02); // the Real-time media platform expects audio buffer duration of 20ms
            long referenceTime   = currentTick;

            for (int index = 0; index < nbByteRead; index += audioBufferSize)
            {
                IntPtr unmanagedBuffer = Marshal.AllocHGlobal(audioBufferSize);
                Marshal.Copy(bytesToRead, index, unmanagedBuffer, audioBufferSize);
                // 10000 ticks in a ms
                referenceTime += 20 * 10000;

                var audioBuffer = new AudioSendBuffer(unmanagedBuffer, audioBufferSize, _audioFormat, referenceTime);
                audioMediaBuffers.Add(audioBuffer);

                _audioOffset += audioBufferSize;
            }
            Log.Info(new CallerInfo(), LogContext.FrontEnd, $"Loading {_nbSecondToLoad}s audio took {watch.ElapsedMilliseconds}ms ({16000 * 2 * _nbSecondToLoad} bytes)");

            watch.Stop();

            return(audioMediaBuffers);
        }