Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectDisposedException when getting the Uri of an encrypted video #16

Closed
jamesqo opened this issue Sep 21, 2015 · 4 comments
Closed

ObjectDisposedException when getting the Uri of an encrypted video #16

jamesqo opened this issue Sep 21, 2015 · 4 comments

Comments

@jamesqo
Copy link
Collaborator

jamesqo commented Sep 21, 2015

This code will throw an ObjectDisposedException in libvideo 1.3.0:

string uri = "https://www.youtube.com/watch?v=NUsoVlDFqZg";
var video = YouTube.Default.GetVideo(uri);
string result = video.Uri; // right here

Why? The gist of it is: when we run GetVideo, we create a new HttpClient to visit YouTube. After getting the source code of the page, we dispose the HttpClient so we don't leak resources.

Next, when we try getting the Uri, we have to visit another webpage if the video is encrypted. To avoid wasting resources, we try to reuse the HttpClient we just used to go to YouTube. Except we just disposed it.

Now once we try to use the HttpClient again, it realizes it's disposed and throws an ObjectDisposedException.

Oops.


Potential fixes:

  1. Use VideoClient to visit the new webpage. While this would be neater and less code, it may not be a good solution because VideoClient is mainly intended for downloading the video binaries. The HTTP configurations (see bottom) you may want to make for a VideoClient may not apply for text-based files.

  2. Create a new HttpClient each time a URL is decrypted. While is one of the simpler solutions, this may not be such a good idea. Consider the following code:

var videos = YouTube.Default.GetAllVideos("...");
var uris = videos.Select(v => v.Uri);

Seems innocuous, right? Well, pretend the video is encrypted. If we chose this option, and the video had, say 7 URLs to decrypt, then 7 new HttpClients would be created. This is definitely not good for performance.

  1. Eagerly decrypt the URLs. Also a simple solution, but with big reprecussions. If we did this and the video is encrypted, then a simple line like YouTube.Default.GetVideos("...") will automatically decrypt all of the URLs. Basically, 7 HTTP requests. Even if we only needed 1, and didn't care about the other 6.

Alright, so those are the options I've come up with so far. At this moment I think 1) seems to be the best choice, it might not be so bad to share some HTTP configurations in VideoClient. If anyone else understood that, feel free to suggest any alternatives/give your opinion. I'll be fixing this issue by tomorrow evening.

@Qandil
Copy link

Qandil commented Sep 21, 2015

@jamesqo Wonderful!

@jamesqo
Copy link
Collaborator Author

jamesqo commented Sep 22, 2015

Fixed! 0246ce6. Try downloading v1.3.1.

@narqs
Copy link

narqs commented Sep 22, 2015

Hey just updated, it's working awsome thanks 👍

@jamesqo
Copy link
Collaborator Author

jamesqo commented Sep 22, 2015

@narqs Awesome, great to know! Contact me if you have any more issues 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants