Example #1
0
        public WTVStreamingVideoResult StartStreamer(WTVStreamingVideoRequest strq)
        {
            int newStreamerID = -1;

            // Too many streamers?
            if (mediaStreamers.Count > MAXIMUM_STREAMERS)
            {
                Functions.WriteLineToLogFile("DSStreamingManager: too many streamers (" + mediaStreamers.Count.ToString() + " streamers are running, which is above the maximum of " + MAXIMUM_STREAMERS.ToString() + ")");
                return(new WTVStreamingVideoResult(DSStreamResultCodes.ErrorTooManyStreamers));
            }

            // For now, some overrides and assumptions
            if (!File.Exists(strq.FileName))
            {
                Functions.WriteLineToLogFile("WebSvc Start Streaming FAIL: File not found: " + strq.FileName);
                return(new WTVStreamingVideoResult(DSStreamResultCodes.ErrorFileNotFound));
            }

            try
            {
                DSStreamer mediaStreamer = new DSStreamer();
                mediaStreamer                        = new FatAttitude.WTVTranscoder.DSStreamer();
                mediaStreamer.Finished              += new EventHandler <DSTranscoderBase.ConversionEndedEventArgs>(mediaStreamer_Finished);
                mediaStreamer.ConversionCompleted   += new EventHandler(mediaStreamer_ConversionCompleted);
                mediaStreamer.DebugMessageGenerated += new EventHandler <DSTranscoderBase.DebugMessageEventArgs>(mediaStreamer_DebugMessageGenerated);
                Functions.WriteLineToLogFile("DSStreamingManager: DSStreamer object created.");

                // Which port should we use?
                int portToTry = GetNextFreePort();

                // Try streaming  (Async)
                Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "DSStreamingManager: Attempting to stream using port " + portToTry.ToString());
                WTVStreamingVideoResult streamResult = mediaStreamer.StreamWithFileAndPort(strq, portToTry, false, true);

                if (streamResult.ResultCode == DSStreamResultCodes.OK)
                {
                    // Add to local streamers
                    newStreamerID    = AddNewStreamer(mediaStreamer);
                    mediaStreamer.ID = newStreamerID;

                    // Add streamer ID to result code too
                    streamResult.StreamerID = newStreamerID.ToString();
                }
                // Return
                return(streamResult);
            }
            catch (Exception e)
            {
                Functions.WriteLineToLogFile("Exception setting up mediaStreaming object:");
                Functions.WriteExceptionToLogFile(e);
                return(new WTVStreamingVideoResult(DSStreamResultCodes.ErrorExceptionOccurred, "Error setting up mediastreaming object: " + e.Message + " (see server log for more details)"));
            }
        }
        public WTVStreamingVideoResult StartStreamer(WTVStreamingVideoRequest strq)
        {
            int newStreamerID = -1;

            // Too many streamers?
            if (mediaStreamers.Count > MAXIMUM_STREAMERS)
            {
                Functions.WriteLineToLogFile("DSStreamingManager: too many streamers (" + mediaStreamers.Count.ToString() + " streamers are running, which is above the maximum of " + MAXIMUM_STREAMERS.ToString() + ")" );
                return new WTVStreamingVideoResult(DSStreamResultCodes.ErrorTooManyStreamers);
            }

            // For now, some overrides and assumptions
            if (!File.Exists(strq.FileName))
            {
                Functions.WriteLineToLogFile("WebSvc Start Streaming FAIL: File not found: " + strq.FileName);
                return new WTVStreamingVideoResult(DSStreamResultCodes.ErrorFileNotFound);
            }

            try
            {
                DSStreamer mediaStreamer = new DSStreamer();
                mediaStreamer = new FatAttitude.WTVTranscoder.DSStreamer();
                mediaStreamer.Finished += new EventHandler<DSTranscoderBase.ConversionEndedEventArgs>(mediaStreamer_Finished);
                mediaStreamer.ConversionCompleted += new EventHandler(mediaStreamer_ConversionCompleted);
                mediaStreamer.DebugMessageGenerated += new EventHandler<DSTranscoderBase.DebugMessageEventArgs>(mediaStreamer_DebugMessageGenerated);
                Functions.WriteLineToLogFile("DSStreamingManager: DSStreamer object created.");

                // Which port should we use?
                int portToTry = GetNextFreePort();

                // Try streaming  (Async)
                Functions.WriteLineToLogFileIfSetting(Settings.Default.DebugStreaming, "DSStreamingManager: Attempting to stream using port " + portToTry.ToString());
                WTVStreamingVideoResult streamResult = mediaStreamer.StreamWithFileAndPort(strq, portToTry, false, true);

                if (streamResult.ResultCode == DSStreamResultCodes.OK)
                {
                    // Add to local streamers
                    newStreamerID = AddNewStreamer(mediaStreamer);
                    mediaStreamer.ID = newStreamerID;

                    // Add streamer ID to result code too
                    streamResult.StreamerID = newStreamerID.ToString();
                }
                // Return
                return streamResult;
            }
            catch (Exception e)
            {
                Functions.WriteLineToLogFile("Exception setting up mediaStreaming object:");
                Functions.WriteExceptionToLogFile(e);
                return new WTVStreamingVideoResult(DSStreamResultCodes.ErrorExceptionOccurred, "Error setting up mediastreaming object: " + e.Message + " (see server log for more details)");
            }
        }
        void RemoveStreamer(DSStreamer ms)
        {
            if (ms != null)
            {
                CleanupDSStreamerFiles(ms);
                mediaStreamers.Remove(ms.ID);

                try
                {
                    ms.Dispose();
                    ms = null;
                }
                catch (Exception ex)
                {
                    Functions.WriteLineToLogFile("Couldn't dispose DSStreamer:");
                    Functions.WriteExceptionToLogFile(ex);
                }

            }

            // Power options
            SetPowerOptions();
        }
        void CleanupDSStreamerFiles(DSStreamer ms)
        {
            if (string.IsNullOrEmpty(ms.StreamingRequest.FileName)) return;

            // Cleanup file
            string fileName = ms.StreamingRequest.FileName + ".wmv";
            if (File.Exists(fileName))
            {
                if (Settings.Default.DebugAdvanced)
                    Functions.WriteLineToLogFile("Cleaning up old streaming file " + fileName);
                try
                {
                    File.Delete(fileName);
                }
                catch { }
            }
        }
        int AddNewStreamer(DSStreamer newStreamer)
        {
            int newID ;
            do
            {
                Random r = new Random();
                newID = r.Next(10000,99999);
            }
            while (mediaStreamers.ContainsKey(newID));

            mediaStreamers.Add(newID, newStreamer);

            // Power options
            SetPowerOptions();

            return newID;
        }