Advertisement
Guest User

FFMpeg Playback

a guest
Feb 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. private void DoStart()
  2.         {
  3.             var vss = Source;
  4.             if (!_modeAudio)
  5.                 vss = Tokenise(vss);
  6.  
  7.             AVDictionary* options = null;
  8.             if (_inputFormat == null)
  9.             {
  10.                 ffmpeg.av_dict_set(&options, "analyzeduration", _analyzeDuration.ToString(), 0);
  11.  
  12.  
  13.  
  14.                 string prefix = vss.ToLower().Substring(0, vss.IndexOf(":", StringComparison.Ordinal));
  15.                 switch (prefix)
  16.                 {
  17.                     case "http":
  18.                     case "mmsh":
  19.                     case "mms":
  20.                         ffmpeg.av_dict_set(&options, "timeout", _timeout.ToString(CultureInfo.InvariantCulture), 0);
  21.                         ffmpeg.av_dict_set(&options, "stimeout", (_timeout * 1000).ToString(CultureInfo.InvariantCulture), 0);
  22.  
  23.                         if (_cookies != "")
  24.                         {
  25.                             ffmpeg.av_dict_set(&options, "cookies", _cookies, 0);
  26.                         }
  27.  
  28.                         if (_headers != "")
  29.                         {
  30.                             ffmpeg.av_dict_set(&options, "headers", _headers, 0);
  31.                         }
  32.                         if (_userAgent != "")
  33.                         {
  34.                             ffmpeg.av_dict_set(&options, "user-agent", _userAgent, 0);
  35.                         }
  36.                         break;
  37.                     case "tcp":
  38.                     case "udp":
  39.                     case "rtp":
  40.                     case "sdp":
  41.                     case "mmst":
  42.                     case "ftp":
  43.                         ffmpeg.av_dict_set(&options, "timeout", _timeout.ToString(CultureInfo.InvariantCulture), 0);
  44.                         break;
  45.                     case "rtsp":
  46.                     case "rtmp":
  47.                         ffmpeg.av_dict_set(&options, "stimeout", (_timeout * 1000).ToString(CultureInfo.InvariantCulture), 0);
  48.                         if (_userAgent != "")
  49.                         {
  50.                             ffmpeg.av_dict_set(&options, "user-agent", _userAgent, 0);
  51.                         }
  52.                         break;
  53.                 }
  54.  
  55.                 ffmpeg.av_dict_set(&options, "rtsp_transport", _rtsPmode, 0);
  56.             }
  57.  
  58.             ffmpeg.av_dict_set(&options, "rtbufsize", "10000000", 0);
  59.  
  60.             var lo = _options.Split(Environment.NewLine.ToCharArray());
  61.             foreach (var nv in lo)
  62.             {
  63.                 if (!string.IsNullOrEmpty(nv))
  64.                 {
  65.                     var i = nv.IndexOf('=');
  66.                     if (i > -1)
  67.                     {
  68.                         var n = nv.Substring(0, i).Trim();
  69.                         var v = nv.Substring(i + 1).Trim();
  70.                         if (!string.IsNullOrEmpty(n) && !string.IsNullOrEmpty(v))
  71.                         {
  72.                             int j;
  73.                             if (int.TryParse(v, out j))
  74.                             {
  75.                                 ffmpeg.av_dict_set_int(&options, n, j, 0);
  76.                             }
  77.                             else
  78.                             {
  79.                                 ffmpeg.av_dict_set(&options, n, v, 0);
  80.                             }
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.  
  86.  
  87.             _stopReadingFrames = false;
  88.             try
  89.             {
  90.                 Program.FfmpegMutex.WaitOne();
  91.                 var pFormatContext = ffmpeg.avformat_alloc_context();
  92.                 _lastPacket = DateTime.UtcNow;
  93.  
  94.  
  95.                 _interruptCallback = InterruptCb;
  96.                 _interruptCallbackAddress = Marshal.GetFunctionPointerForDelegate(_interruptCallback);
  97.  
  98.                 pFormatContext->interrupt_callback.callback = _interruptCallbackAddress;
  99.                 pFormatContext->interrupt_callback.opaque = null;
  100.  
  101.  
  102.                 if (ffmpeg.avformat_open_input(&pFormatContext, vss, _inputFormat, &options) != 0)
  103.                 {
  104.                     throw new ApplicationException(@"Could not open source");
  105.                 }
  106.                 _formatContext = pFormatContext;
  107.  
  108.  
  109.                 SetupFormat();
  110.             }
  111.             catch (Exception ex)
  112.             {
  113.                 ErrorHandler?.Invoke(ex.Message);
  114.                 _res = ReasonToFinishPlaying.VideoSourceError;
  115.                 PlayingFinished?.Invoke(this, new PlayingFinishedEventArgs(_res));
  116.                 AudioFinished?.Invoke(this, new PlayingFinishedEventArgs(_res));
  117.  
  118.             }
  119.             finally
  120.             {
  121.                 try
  122.                 {
  123.                     Program.FfmpegMutex.ReleaseMutex();
  124.                 }
  125.                 catch
  126.                 {
  127.  
  128.                 }
  129.             }
  130.             _starting = false;
  131.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement