Beispiel #1
0
        public override void OnHandshake(Session session)
        {
            base.OnHandshake(session);

            if (m_MediaServer.IsWorking())
            {
                session.UserData = m_MediaServer;
                m_MediaServer.ValidateClient(session);
            }
        }
Beispiel #2
0
        public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo)
        {
            try
            {
                if (mediaInfo != null && mediaInfo.Length > 0)
                {
                    foreach (var channel in channels)
                    {
                        channel.SetWelcomeText(mediaInfo);
                        channel.Process(new BufferData(channel.GetWelcomeText()));
                    }
                }

                // PcmHandler will ignore MediaServer.InputBufferSize
                int inputBufferSize = mediaServer.OutputBufferSize > 0 ? mediaServer.OutputBufferSize : 1;

                try
                {
                    while (mediaServer.IsWorking())
                    {
                        int    realSize = 0;
                        byte[] data     = new byte[inputBufferSize];
                        try
                        {
                            realSize = inputStream.Read(data, 0, data.Length);
                        }
                        catch (Exception ex)
                        {
                            realSize = 0;
                            mediaServer.Logger.Error("PCM input error: " + ex.Message);
                        }

                        if (realSize <= 0)
                        {
                            break;
                        }

                        foreach (var channel in channels)
                        {
                            channel.Process(new BufferData(data, realSize));
                        }
                    }
                }
                catch (Exception ex)
                {
                    mediaServer.Logger.Error("PCM input error: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                mediaServer.Logger.Error("PCM input process error in thread: " + ex.Message);
            }
        }
Beispiel #3
0
        public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo)
        {
            try
            {
                var    channel    = channels.First();
                string sourceName = channel.ChannelName;

                if (sourceName.Length > 0)
                {
                    Int16 width  = VIDEO_DEFAULT_WIDTH;
                    Int16 height = VIDEO_DEFAULT_HEIGHT;

                    string vinfo = "";

                    // new jsmepg would not need "header" message anymore

                    /*
                     * lock (m_Headers)
                     * {
                     *  if (m_Headers.ContainsKey(sourceName)) m_Headers.Remove(sourceName); // refresh it
                     *
                     *  if (mediaInfo.Length > 0)
                     *  {
                     *      try
                     *      {
                     *          var mpegInfo = mediaInfo.Replace("/", "");
                     *          if (mpegInfo.Contains('(') && mpegInfo.Contains(')'))
                     *              mpegInfo = mpegInfo.Substring(mpegInfo.IndexOf('(') + 1,
                     *                  mpegInfo.IndexOf(')') - mpegInfo.IndexOf('(') - 1);
                     *
                     *          vinfo = mpegInfo;
                     *          var parts = (vinfo.Split('@')[0]).Split('x');
                     *
                     *          if (parts.Length > 0) width = Convert.ToInt16(parts[0]);
                     *          if (parts.Length > 1) height = Convert.ToInt16(parts[1]);
                     *      }
                     *      catch
                     *      {
                     *          width = VIDEO_DEFAULT_WIDTH;
                     *          height = VIDEO_DEFAULT_HEIGHT;
                     *      }
                     *  }
                     *
                     *  byte[] header = GenHeader(width, height);
                     *  m_Headers.Add(sourceName, header);
                     * }
                     */

                    if (mediaInfo.Length > 0)
                    {
                        try
                        {
                            var mpegInfo = mediaInfo.Replace("/", "");
                            if (mpegInfo.Contains('(') && mpegInfo.Contains(')'))
                            {
                                mpegInfo = mpegInfo.Substring(mpegInfo.IndexOf('(') + 1,
                                                              mpegInfo.IndexOf(')') - mpegInfo.IndexOf('(') - 1);
                            }

                            vinfo = mpegInfo;
                            var parts = (vinfo.Split('@')[0]).Split('x');

                            if (parts.Length > 0)
                            {
                                width = Convert.ToInt16(parts[0]);
                            }
                            if (parts.Length > 1)
                            {
                                height = Convert.ToInt16(parts[1]);
                            }
                        }
                        catch
                        {
                            width  = VIDEO_DEFAULT_WIDTH;
                            height = VIDEO_DEFAULT_HEIGHT;
                        }
                    }

                    if (vinfo.Length <= 0)
                    {
                        vinfo = width + "x" + height;
                    }
                }

                // new jsmepg would not need "header" message anymore
                //channel.SetWelcomeData(GetWelcomeData(sourceName));

                int inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1;

                while (mediaServer.IsWorking())
                {
                    int    realSize = 0;
                    byte[] data     = new byte[inputBufferSize]; // MpegHandler will ignore MediaServer.OutputBufferSize
                    try
                    {
                        realSize = inputStream.Read(data, 0, data.Length);
                    }
                    catch (Exception ex)
                    {
                        realSize = 0;
                        mediaServer.Logger.Error("MPEG1 input error: " + ex.Message);
                    }

                    if (realSize <= 0)
                    {
                        break;
                    }

                    channel.Process(new BufferData(data, realSize));
                }

                if (sourceName.Length > 0)
                {
                    lock (m_Headers)
                    {
                        if (m_Headers.ContainsKey(sourceName))
                        {
                            m_Headers.Remove(sourceName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                mediaServer.Logger.Error("MPEG1 input process error in thread: " + ex.Message);
            }
        }
Beispiel #4
0
        public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo)
        {
            try
            {
                if (mediaInfo != null && mediaInfo.Length > 0)
                {
                    foreach (var channel in channels)
                    {
                        channel.SetWelcomeText(mediaInfo);
                        channel.Process(new BufferData(channel.GetWelcomeText()));
                    }
                }

                bool foundFirstMagicTag = false;

                List <byte> headerData = new List <byte>();

                List <List <byte> > currentChunks = new List <List <byte> >();

                int totalBytes = 0;

                int currentByte = 0;

                List <byte> cache = new List <byte>();

                int    inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1;
                byte[] inputBuffer     = new byte[inputBufferSize];

                int  currentSize = 0;
                bool foundError  = false;

                try
                {
                    while (mediaServer.IsWorking())
                    {
                        currentSize = inputStream.Read(inputBuffer, 0, inputBuffer.Length);
                        if (currentSize <= 0)
                        {
                            break;
                        }
                        foundError = false;

                        for (var currentIdx = 0; currentIdx < currentSize; currentIdx++)
                        {
                            currentByte = inputBuffer[currentIdx];

                            bool foundTag = false;

                            if (currentByte == MAGIC_TAG.Last() && cache.Count >= MAGIC_TAG_LEN - 1)
                            {
                                foundTag = true;
                                for (var i = 1; i <= MAGIC_TAG_LEN - 1; i++)
                                {
                                    if (cache[cache.Count - i] != MAGIC_TAG[MAGIC_TAG_LEN - 1 - i])
                                    {
                                        foundTag = false;
                                        break;
                                    }
                                }
                                if (foundTag)
                                {
                                    cache.RemoveRange(cache.Count - (MAGIC_TAG_LEN - 1), MAGIC_TAG_LEN - 1);
                                }
                            }

                            if (foundTag)
                            {
                                if (!foundFirstMagicTag)
                                {
                                    foundFirstMagicTag = true;
                                }
                                else
                                {
                                    currentChunks.Add(new List <byte>(cache));
                                    totalBytes += cache.Count;
                                    if (totalBytes >= mediaServer.OutputBufferSize)
                                    {
                                        if (headerData.Count <= 0 && currentChunks.Count > 1)
                                        {
                                            headerData.AddRange(currentChunks[0]);
                                            headerData.AddRange(currentChunks[1]);
                                            currentChunks.RemoveRange(0, 2);
                                        }
                                        byte[] data = GenAudioData(headerData, currentChunks);
                                        foreach (var channel in channels)
                                        {
                                            channel.Process(new BufferData(data, data.Length));
                                        }

                                        currentChunks.Clear();
                                        totalBytes = 0;
                                    }
                                }

                                cache.Clear();
                                cache.AddRange(MAGIC_TAG);
                            }
                            else
                            {
                                cache.Add((byte)currentByte);
                            }

                            if (cache.Count > MAX_CACHE_SIZE)
                            {
                                mediaServer.Logger.Error("Exceeded max cache size when read OGG input.");
                                foundError = true;
                                break;
                            }
                        }

                        if (foundError)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    mediaServer.Logger.Error("OGG input error: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                mediaServer.Logger.Error("OGG input process error in thread: " + ex.Message);
            }
        }
        public void HandleInput(IMediaServer mediaServer, List <MediaChannel> channels, Stream inputStream, string mediaInfo)
        {
            try
            {
                if (mediaInfo != null && mediaInfo.Length > 0)
                {
                    foreach (var channel in channels)
                    {
                        channel.SetWelcomeText(mediaInfo);
                        channel.Process(new BufferData(channel.GetWelcomeText()));
                    }
                }

                bool foundFirstSyncWord = false;

                List <byte> tagPart    = new List <byte>();
                List <byte> headerPart = new List <byte>();

                List <List <byte> > currentChunks = new List <List <byte> >();

                int totalBytes = 0;

                int currentByte = 0;
                int lastByte    = 0;

                List <byte> cache = new List <byte>();

                int    inputBufferSize = mediaServer.InputBufferSize > 0 ? mediaServer.InputBufferSize : 1;
                byte[] inputBuffer     = new byte[inputBufferSize];

                int  currentSize = 0;
                bool foundError  = false;

                try
                {
                    while (mediaServer.IsWorking())
                    {
                        currentSize = inputStream.Read(inputBuffer, 0, inputBuffer.Length);
                        if (currentSize <= 0)
                        {
                            break;
                        }
                        foundError = false;

                        for (var currentIdx = 0; currentIdx < currentSize; currentIdx++)
                        {
                            currentByte = inputBuffer[currentIdx];

                            if (currentByte == SECOND_SYNC_BYTE && lastByte == FIRST_SYNC_BYTE)
                            {
                                if (!foundFirstSyncWord)
                                {
                                    foundFirstSyncWord = true;
                                    tagPart.AddRange(cache);

                                    cache.Clear();
                                    cache.Add(FIRST_SYNC_BYTE);
                                    cache.Add(SECOND_SYNC_BYTE);
                                }
                                else
                                {
                                    currentChunks.Add(new List <byte>(cache));
                                    totalBytes += cache.Count;
                                    if (totalBytes >= mediaServer.OutputBufferSize)
                                    {
                                        byte[] data = GenAudioData(tagPart, headerPart, currentChunks);
                                        foreach (var channel in channels)
                                        {
                                            channel.Process(new BufferData(data, data.Length));
                                        }

                                        currentChunks.Clear();
                                        totalBytes = 0;
                                    }

                                    cache.Clear();
                                    cache.Add(FIRST_SYNC_BYTE);
                                    cache.Add(SECOND_SYNC_BYTE);
                                }

                                lastByte = currentByte;
                                continue;
                            }

                            if (lastByte == FIRST_SYNC_BYTE)
                            {
                                cache.Add(FIRST_SYNC_BYTE);
                            }
                            if (currentByte != FIRST_SYNC_BYTE)
                            {
                                cache.Add((byte)currentByte);
                            }
                            lastByte = currentByte;

                            if (cache.Count > MAX_CACHE_SIZE)
                            {
                                mediaServer.Logger.Error("Exceeded max cache size when read MP3 input.");
                                foundError = true;
                                break;
                            }
                        }

                        if (foundError)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    mediaServer.Logger.Error("MP3 input error: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                mediaServer.Logger.Error("MP3 input process error in thread: " + ex.Message);
            }
        }