Unable to seek/replay video (.ogv) files served from http server [C#] - c#

ISSUE:
Video player (native player in webkit based browser) is not giving option to replay and I cannot seek video while playing it. Once played, I have to refresh page to play it again.
If I give a web url for ogv video in my html page, same player works fine (replays and seek is successful).
Hence I suspect issue with my algorithm of serving files (especially .ogv files).
What I am doing:
I have an HTTP fileserver (completely in C#, using .Net framework classes - HttpListenerContext, HttpListenerRequest, HttpListenerResponse, etc). This server is serving files (of all types and extensions - text, video, audio, images) to clients. All files are available on server which are requested by clients.
Algorithm to serve files from server:
Server gets the file name from URL.
Reads the complete file in a buffer(byte[] array) (using File.ReadAllBytes(file_path_on_server)),
Array is assigned to HttpListenerResponse.OutputStream to return the file (contents) to client.
Corresponding HttpListenerResponse.ContentType (MimeType) and HttpListenerResponse.ContentLength64 is assigned (size of array).
Response header is added as "no-cache".
HttpListenerResponse.ContentEncoding is UTF8.
Close response object to send it to client.
This works fine and files are reaching clients for processing including video files (.ogv) but video is not seekable/replayable in player.
Already tried
Tried removing assignment of HttpListenerResponse.ContentEncoding to UTF8.

It seems that you server returns the file the same way as if it's just downloaded, so seek/replay most likely will not be available until the whole file is loaded. If you want to support true video streaming you need another server-side implementation. Live Streaming within IIS Media Services may help you to support Apple HLS Streaming or similar (depending on your player), also i saw somewhere an IIS extension/module that supports http pseudo streaming with seeking for some video formats.

Related

How to load Dicom image from server (path contains https://)

How to load Dicom image from server (path contains https://)
Am using fo-Diocm library.
var image = new DicomImage(filePath);
if filePath is from local directory its working fine.
if filePath is from server (like https://example.com/filepath.dcm), its throwing 'Dicom.DicomFileException' saying that specified path value is not correct.
What is the right way to load DicomImage from server ??
AFAIK: Not at all. Obtaining DICOM Files through Web API is referred to as WADO (Web Access to DICOM Objects), and there is an open feature request in fo-dicom to support this, but it has been assigned quite a high priority.
However, the URL https://example.com/filepath.dcm does not adhere to WADO encoding, so maybe you cannot use a DICOM communication protocol to obtain the image (or you do not want to). But in this case, your task is simply "downloading a file from an URL" and not related to DICOM at all. After the download is completed, you have it stored locally, and can go on with local file access as you are used to.
The "missing link": How to download a file from an URL

efficiently pass files from webserver to file server

i have multiple web server and one central file server inside my data center.
and all my Web server store the user uploaded files into central internal file server.
i would like to know what is the best way to pass the file from web server to file server in this case?
as suggested i try to add more details to question:
the solution i came up was:
after receiving files from user at web server, i should just do an Http Post to the file server. but i think there is some thing wrong with this because it causes large files to be entirely loaded into memory twice: (once at web server and once at file server)
Is your file server just another windows/linux server or is it a NAS device. I can suggest you number of approaches based on your requirement. The question is why d you want to use HTTP protocol when you have much better way to transfer files between servers.
HTTP protocol is best when you send text data as HTTP itself is based
on text.From the client side to Server side HTTP is used as that is
the only available option for you by our browsers .But
between your servers ,I feel you should use SMB protocol(am assuming
you are using windows as it is tagged for IIS) to move data.It will
be orders of magnitude faster as much more efficient to transfer the same data over SMB vs
HTTP.
And for SMB protocol,you do not have to write any code or complex scripts to do this.As provided by one of the answers above,you can just issue a simple copy command and it will happen for you.
So just summarizing the options for you (based on my preference)
Let the files get upload to some location on the each IIS web server e.g C:\temp\UploadedFiles . You can write a simple 2-3 line powershell script which will copy the files from this C:\temp\UploadedFiles to \FileServer\Files\UserID\\uploaded.file .This same powershell script can delete the file once it is moved to the other server successfully.
E.g script can be this simple and easy to make it as windows scheduled task
$Destination = "\\FileServer\Files\UserID\<FILEGUID>\"
New-Item -ItemType directory -Path $Destination -Force
Copy-Item -Path $Source\*.* -Destination $Destination -Force
This script can be modified to suit your needs to delete the files if it is done :)
In the Asp.net application ,you can directly save the file to network location.So in the SaveAs call,you can give the network path itself. This you have to make sure this network share is accessible for the IIS worker process and also has write permission.Also in my understanding asp.net gets the file saved to temporary location first (you do not have control on this if you are using the asp.net HttpPostedFileBase or FormCollection ). More details here
You can even run this in an async so that your requests will not be blocked
if (FileUpload1.HasFile)
// Call to save the file.
FileUpload1.SaveAs("\\networkshare\filename");
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas(v=vs.110).aspx
3.Save the file the current way to local directory and then use HTTP POST. This is worst design possible as you are first going to read the contents and then transfer it as chunked to other server where you have to setup another webservice which recieves the file.The you have to read the file from request stream and again save it to your location. Am not sure if you need to do this.
let me know if you need more details on any of the listed method.
Or you just write it to a folder on the webservers, and create a scheduled task that moves the files to the file server every x minutes (e.g. via robocopy). This also makes sure your webservers are not reliant on your file server.
Assuming that you have an HttpPostedFileBase then the best way is just to call the .SaveAs() method.
You need the UNC path to the file server and that is it. The simplest version would look something like this:
public void SaveFile(HttpPostedFileBase inputFile) {
var saveDirectory = #"\\fileshare\application\directory";
var savePath = Path.Combine(saveDirectory, inputFile.FileName);
inputFile.SaveAs(savePath);
}
However, this is simplistic in the extreme. Take a look at the OWASP Guidance on Unrestricted File Uploads. File uploads can be the source of many vulnerabilities in your application.
You also need to make sure that the web application has access to the file share. Take a look at this answer
Creating a file on network location in asp.net
for more info. Generally the best solution is to run the application pool with a special identity which is only used to access the folder.
the solution i came up was: after receiving files from user at web server, i should just do an Http Post to the file server. but i think there is some thing wrong with this because it causes large files to be entirely loaded into memory twice: (once at web server and once at file server)
I would suggest not posting the file at once - it's then full in memory, which is not needed.
You could post the file in chunks, by using ajax. When a chunk receives at your server, just add it to the file.
With the File Reader API, you could read the file in chunks in Javascript.
Something like this:
/** upload file in chunks */
function upload(file) {
var chunkSize = 8000;
var start = 0;
while (start < file.size) {
var chunk = file.slice(start, start + chunkSize);
var xhr = new XMLHttpRequest();
xhr.onload = function () {
//check if all chunks are and then send filename or send in in the first/last request.
};
xhr.open("POST", "/FileUpload", true);
xhr.send(chunk);
start = end;
}
}
It can be implemented in different ways. If you are storing files in files server as files in file system. And all of your servers inside the same virtual network
Then will be better to create shared folder on your file server and once you received files at web server, just save this file in this shared folder directly on file server.
Here the instructions how to create shared folders: https://technet.microsoft.com/en-us/library/cc770880(v=ws.11).aspx
Just map a drive
I take it you have a means of saving the uploaded file on the web server's local filesystem. The question pertains to moving the file from the web server (which is probably one of many load-balanced nodes) to a central file system all web servers can access it.
The solution to this is remarkably simple.
Let's say you are currently saving the files some folder, say c:\uploadedfiles. The path to uploadedfiles is stored in your web.config.
Take the following steps:
Sign on as the service account under which your web site executes
Map a persistent network drive to the desired location, e.g. from command line:
NET USE f: \\MyFileServer\MyFileShare /user:SomeUserName password
Modify your web.config and change c:\uploadedfiles to f:\
Ta da, all done.
Just make sure the drive mapping is persistent, and make sure you use a user with adequate permissions, and voila.

How to download big video files using WebClient class

I'm a newbie and I'm developing a windows application. I need to download a video file from my site and that's my issue here. I had designed a custom down-loader, through which I can download images, text files from my site. But I wasn't able download videos from my site. Could anyone please help me out..?
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri("http://mysitename.com/Videos/vid.mp4"), "c:\\movie.mp4");
I don't want to download by means of response content dispatch because my client wants me to download through custom browser.. so please let me know solutions from you experts.. thank you
I have tried to download a video file with WebClient and it works. My setup is as below:
I have a virtualdirectory(Video) in defaultwebsite (IIS) which has this video file.
I just use the below code to download the video file to C drive:
var client = new WebClient();
Uri address = new Uri("http://localhost/Video/wildlife.wmv");
client.DownloadFileAsync(address, #"c:\video.wmv");
Also note since you are downloading in Async fashion, wait for about a min for the operation to complete for the full file to be downloaded. Initially it shows 0 bytes but based on the size it takes some time to complete it.
UPDATE: If your server doesnt have the file mime type specified then just add to the collection of mime types that IIS can serve and you can download the file without any problem.
When adding MIME type the following values to be used are (for your scenario):
File Extension: .mp4
MIME Type: video/mp4
To add mime types in IIS follow these links:
For IIS 4,5
For IIS 6
For IIS 7
This sounds more like a server issue, but if you are doubting your code, you may want to try download sync (I have had some issues in the past downloading async). Another way is to use the WebRequest class. If this server is very remote, try pinging beforehand. I think that you should also check to make sure the file is on the server, and if the file is really big, you should check to see if the file finished uploading.

How To Do a Server To Server File Transfer without any user interaction?

In my scenario, users are able to upload zip files to a.example.com
I would love to create a "daemon" which in specified time intervals will move-transfer any zip files uploaded by the users from a.example.com to b.example.com
From the info i gathered so far,
The daemon will be an .ashx generic handler.
The daemon will be triggered at the specified time intervals via a plesk cron job
The daemon (thanks to SLaks) will consist of two FtpWebRequest's (One for reading and one for writing).
So the question is how could i implement step 3?
Do i have to read into to a memory() array the whole file and try to write that in b.example.com ?
How could i write the info i read to b.example.com?
Could i perform reading and writing of the file at the same time?
No i am not asking for the full code, i just can figure out, how could i perform reading and writing on the fly, without user interaction.
I mean i could download the file locally from a.example.com and upload it at b.example.com but that is not the point.
Here is another solution:
Let ASP.Net in server A receive the file as a regular file upload and store it in directory XXX
Have a windows service in server A that checks directory XXX for new files.
Let the window service upload the file to server B using HttpWebRequest
Let server B receive the file using a regular ASP.Net file upload page.
Links:
File upload example (ASP.Net): http://msdn.microsoft.com/en-us/library/aa479405.aspx
Building a windows service: http://www.codeproject.com/KB/system/WindowsService.aspx
Uploading files using HttpWebRequest: Upload files with HTTPWebrequest (multipart/form-data)
Problems you gotto solve:
How to determine which files to upload to server B. I would use Directory.GetFiles in a Timer to find new files instead of using a FileSystemWatcher. You need to be able to check if a file have been uploaded previously (delete it, rename it, check DB or whatever suits your needs).
Authentication on server B, so that only you can upload files to it.
To answer your questions - yes you can read and write the files at the same time.
You can open an FTPWebRequest to ServerA and a FTPWebRequest to ServerB. On the FTPWebRequest to serverA you would request the file, and get the ResponseStream. Once you have the ResponseStream, you would read a chunk of bytes at a time, and write that chunck of bytes to the serverB RequestStream.
The only memory you would be using would be the byte[] buffer in your read/write loop. Just keep in mind though that the underlying implementation of FTPWebRequest will download the complete FTP file before returning the response stream.
Similarly, you cannot send your FTPWebRequest to upload the new file until all bytes have been written. In effect, the operations will happen synchronously. You will call GetResponse which won't return until the full file is available, and only then can you 'upload' the new file.
References:
FTPWebRequest
Something you have to take into consideration is that a long running web requests (your .ashx generic handler) may be killed when the AppDomain refreshes. Therefore you have to implement some sort of atomic transaction logic in your code, and you should handle sudden disconnects and incomplete FTP transfers if you go that way.
Did you have a look at Windows Azure before? This cloud platform supports distributed file system, and has built-in atomic transactions. Plus it scales nicely, should your service grow fast.
I would make it pretty simple. The client program uploads the file to server A. This can be done very easily in C# with an FtpWebRequest.
http://msdn.microsoft.com/en-us/library/ms229715.aspx
I would then have a service on server A that monitors the directory where files are uploaded. When a file is uploaded to that directory or on certain intervals it simply copies files over to server B. Again this can be done via Ftp or other means if they're on the same network.
you need some listener on the target domain, ftp server running there, and on the client side you will use System.Net.WebClient and UploadFile or UploadFileAsync to send the file. is that what you are asking?
It sounds like you don't really need a webservice or handler. All you need is a program that will, at regular intervals, open up an FTP connection to the other server and move the files. This can be done by any .NET program with the System.WebClient library, doesn't have to be a "web app". This other program could be a service, which could handle its own timing, or a simple app run by your cron job. If you need this to go two ways, for instance if the two servers are mirrors, you simply have the same app on the second box doing the same thing to upload files over to the first.
If both machines are in the same domain, couldn't you just do file replication at the OS level?
DFS
set up keys if you are using linux based systems:
http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html
Once you have the keys working, you can copy the file from system A to system B by writing regular shell scripts that would not need any user interactions.

Proxying Videos locally with a TCP Socket

I've been really interested in adding support for video podcasts to Media Browser.
I would like users to be able to navigate through the available video podcasts and stream them from the internets. That's really easy cause media player etc.. will happily play a file that lives in the cloud.
The problem is that I want cache these files locally so subsequent viewings of the same episode will not involve streaming and instead will play the local file.
So... I was thinking, why not host an HttpListener and as media player asks it for bits of the file, have the HttpListener download and store it locally. Next time a user plays the file we will already have portions of the file locally.
Does anyone know of example code that uses HttpListener for proxying?
EDIT
The idea would be only to proxy simple streamable content like MP3 or Mov.
The bounty will go to an actual implementation.
Here is the API I would like:
// will proxy a uri on the local port, if cacheFile exists it will resume the
// download from cacheFile.
// while the file is downloading it will be name cacheFile.partial, after the
// download is complete the file will be renamed to cacheFile.
// Example usage: ProxyFile("http://media.railscasts.com/videos/176_searchlogic.mov", 8000, #"c:\downloads\railscasts\176_searchlogic.mov")
//
// Directly after this call http://localhost:8000 will be the proxy stream, it will be playable locally.
void ProxyUri(Uri uri, int port, string cacheFile)
Edit 2
HttpListener is looking pretty unpromising I will probably need to do the work at a TCP socket level as HttpListeners seem to require the program runs as admin which is going to be really tricky.
I hadn't done anything with HttpListener before, so I thought this would be a nice little exercise to bring myself up to speed with it - and so it proved. I implemented it as a single ProxyListener class whose constructor takes the parameters of the ProxyUri function you specified. Once you obtain an instance, you start it listening (and potentially downloading) by calling its Start method. When you're done with it, call Cleanup.
There are one or two rough edges but basically it works as per your question. To test it, I built it up as a console application with a Program class which accepts input lines consisting of (uri, port, filename), space-separated, creates the ProxyListener instances and starts them. You can run this console application, type in a suitable line, and the downloader will start (printing out progress to console). Simultaneously you can e.g. fire up IE and fetch the file from the specified port, and you will be able to download it while the downloader is still working. The "uploader" progress will be printed to console, too.
I'm having a bit of trouble pasting it in here, maybe due to size (it's not that big, but bigger than the snippets you normally see here - the ProxyListener class is a tad under 200 lines). Does it sound interesting? If so, I'll post it to a pastebin and update this answer with a link.
Update: Posted as a gist.
Note that you will need Administrator privileges to run the program, since HttpListener requires this.
Update 2: Under certain circumstances, it is not necessary to have admin privileges to run HttpListener. See this link and this one. The idea is, if you can reserve an URL namespace during installation time, then the user does not have to have admin privileges if listening against that namespace.
Streaming was not designed to be saved, and also these protocols are very custom and very complex to implement, streaming sessions do lots of validation and synchronization which will be extremely difficult to imitate. Of course it is not an impossible task, but its fairly big task to do. Only other way is to read and save it as local media file, and use that as a reference. Because you can use windows media encoder to read stream and write stream data as local file, but it still may not allow you to do copy protected data.
Did you consider using HTTP proxy with caching features?
Like:
Apache httpd with mod_proxy and mod_cache
Squid
See also Web Cache # wikipedia
If you want your application to have such web cache component, I suggest you look for Web Cache implementation in .Net, and not code it from scratch.

Categories

Resources