title: libvlc - http stream error: local stream 1 error: Cancellation (0x8)
body: <p>I encounter a bug with <code>libvlc</code> where attempting to play video from YouTube result in error <code>http stream error: local stream 1 error: Cancellation (0x8)</code></p>
  <h1>Context</h1>
  <p><strong>C++ code:</strong></p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;vlc/vlc.h&gt;
  #include &lt;vlc/libvlc.h&gt;
  #include &lt;vlcpp/vlc.hpp&gt;
  
  int main() {
      const char *const arg[] = { &quot;--no-video&quot;, &quot;-vv&quot; };
      VLC::Instance instance = VLC::Instance(2, arg);
      
      const std::string mrl = &quot;link to youtube video here&quot;;
      VLC::Media media = VLC::Media(instance, mrl, VLC::Media::FromLocation);
      
      VLC::MediaPlayer player = VLC::MediaPlayer(instance);
      player.setMedia(media);
      
      player.play();
      
      std::this_thread::sleep_for( std::chrono::seconds(215) );
      
      return 0;
  }
  </code></pre>
  <p><strong>C++ log:</strong> <a href="https://gist.github.com/trofchik/39ebc5df6aeb4a82b464db74a48846d7" rel="nofollow noreferrer">https://gist.github.com/trofchik/39ebc5df6aeb4a82b464db74a48846d7</a></p>
  <p><strong>VLC player app log:</strong> <a href="https://gist.github.com/trofchik/8028fda99c08f22523800a03f26e168c" rel="nofollow noreferrer">https://gist.github.com/trofchik/8028fda99c08f22523800a03f26e168c</a>
  Note that I set 720p as prefered resolution for video to play due to some quirks of youtube api.</p>
  <p><strong>Python code</strong></p>
  <pre><code>#!/usr/bin/python3
  
  import pafy 
  import vlc
  import time
  import youtube_dl
  
  url = 'link to youtube video here'
  
  song = pafy.new(url)
  duration = song.length
  audiostreams = song.audiostreams
  best = song.getbest()
  play_url = best.url
  
  print(play_url)
  
  instance = vlc.Instance('--no-video')
  
  player = instance.media_player_new()
  media = instance.media_new(play_url)
  media.get_mrl()
  player.set_media(media)
  
  player.play()
  time.sleep(duration)
  </code></pre>
  <p><strong>Python log:</strong> <a href="https://gist.github.com/trofchik/68e797339853dd5607d8c7b3fcb1493a" rel="nofollow noreferrer">https://gist.github.com/trofchik/68e797339853dd5607d8c7b3fcb1493a</a></p>
  <p><strong>C++ log when using link generated by python script:</strong> <a href="https://gist.github.com/trofchik/000a32280ba177a3e8897eb25cc7b9e0" rel="nofollow noreferrer">https://gist.github.com/trofchik/000a32280ba177a3e8897eb25cc7b9e0</a></p>
  <p>I also replaced the code of lua script for youtube videos with code found on vlc's github page which you can find here: <a href="https://github.com/videolan/vlc/blob/master/share/lua/playlist/youtube.lua" rel="nofollow noreferrer">https://github.com/videolan/vlc/blob/master/share/lua/playlist/youtube.lua</a> This script apparently generates proper url for video playback.</p>
  <h1>Explanation</h1>
  <p>I've attempted to play video with 3 different ways.</p>
  <p>First is using libvlc with normal link to youtube video (e.g. youtube.com/watch?v=videoID) which gave me aforementioned error. (see C++ code and log)</p>
  <p>Second is playing it using VLC player app which worked fine. (see VLC player app log)</p>
  <p>Third is playing the video using <code>vlc-python</code> and <code>pafy</code> which also worked. (see python code and log)</p>
  <p>Fourth is playing it via <code>libvlc</code> but using a link generated by python code (stored in <code>play_url</code> variable) which worked. (see C++ code and C++ log when using python generated link) What's interesting is that in C++ log when using normal link at line 301 you can see that the link has a similar format to the one being generated by python code. The program still doesn't work. What's weird is that if I use a link that was generated by <code>libvlc</code> (one which I point to above and that starts with &quot;r1--&quot;) the program will work as intended.</p>
  <h1>What have I done so far and what do I want?</h1>
  <p>Most of what I've done I already described above. Besides that I've spent several hours comparing logs from all 4 methods I used to play the video but found nothing that could point me at fix. I've also searched for fix online. Many suggested to set max playback resolution to 720p which I did in case with VLC player app. I doubt that default resolution is the problem since I always run code with <code>--no-video</code> argument which disables video output and leaves only audio.</p>
  <p><strong>Edit</strong>: Tried running C++ code with <code>--prefered-resolution=720</code> argument. Didn't work. In fourth case described above nothing changed.</p>
  <p>I consider using python in conjunction with <code>libvlc</code> a last resort and rather figure out the problem with <code>lua</code> that is natively used by <code>libvlc</code>. Thus I want my <code>C++</code> code to work with normal YouTube links while not using <code>python</code>.</p>