Author Topic: Icecast:disconnecting source due to socket timeout  (Read 877 times)

alexeyZ

  • Posts: 18
I have simple BASS icecast broadcasting application. When it starts it is ok and all works fine, but after some time(3/4 hours), sometimes earlier, sometimes later the broadcasting stops and in the icecast log I see the following error:disconnecting source due to socket timeout.
Here my code:
Code: [Select]
        public IcecastInfo Start()
        {
            Logger.Info("IcecastService Started");

            if(!Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
            {
                throw new Exception("BASS_Init failed");
            }

            var song = _rotationLogicService.GetSong();

            if(song == null)
            {
                throw new Exception("Song is empty");
            }

            if (!File.Exists(song.FileName))
            {
                throw new Exception($"Song doesnt exists:{song.FileName}");
            }

            _recHandle = Bass.BASS_StreamCreateFile(song.FileName, 0, 0, BASSFlag.BASS_DEFAULT);

            _lame = new EncoderLAME(_recHandle);
            _lame.InputFile = null;
            _lame.OutputFile = null;
            _lame.LAME_Bitrate = (int)BaseEncoder.BITRATE.kbps_128;
            _lame.LAME_Mode = EncoderLAME.LAMEMode.Default;
            _lame.LAME_TargetSampleRate = (int)BaseEncoder.SAMPLERATE.Hz_44100;
            _lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality;


            var serverAddress = ConfigurationManager.AppSettings["serverAddress"];
            var serverPort = ConfigurationManager.AppSettings["serverPort"];
            var serverMountPoint = ConfigurationManager.AppSettings["serverMountPoint"];
            var serverLogin = ConfigurationManager.AppSettings["serverLogin"];
            var serverPassword = ConfigurationManager.AppSettings["serverPassword"];

            _icecast = new ICEcast(_lame);
            _icecast.ServerAddress = serverAddress;
            _icecast.ServerPort = Convert.ToInt32(serverPort);
            _icecast.MountPoint = serverMountPoint;
           
            _icecast.PublicFlag = true;
            _icecast.AdminPassword = serverPassword;
            _icecast.AdminUsername = serverLogin;
            _icecast.Password = serverPassword;
            _broadCast = new BroadCast(_icecast);
            _broadCast.Notification += new BroadCastEventHandler(OnBroadCast_Notification);

            Bass.BASS_ChannelSetAttribute(_recHandle, BASSAttribute.BASS_ATTRIB_VOL, 0f);
            _mySync = new SYNCPROC(MetaSync);
            Bass.BASS_ChannelSetSync(_recHandle, BASSSync.BASS_SYNC_END, 0, _mySync, IntPtr.Zero);

            Bass.BASS_ChannelPlay(_recHandle, false);
            _songsRepository.AddHistoryInfo(new Domain.Common.HistoryInfo
            {
                SondId = song.IdSong,
                Album = song.AlbumName,
                BandName = song.ArtistName,
                SongTitle = song.SongTitle
            });

            var songTitle = song.ArtistName + " - " + song.SongTitle;
            _icecast.UpdateTitle(songTitle, string.Empty);
           
            _broadCast.AutoConnect();

            return new IcecastInfo
            {
                Bitrate = _lame.LAME_Bitrate.ToString(),
                MountPoint = _icecast.MountPoint,
                ServerPort = _icecast.ServerPort.ToString(),
                SongTitle = songTitle,
                StationName = _icecast.StreamName
            };

        }


        private void MetaSync(int handle, int channel, int data, IntPtr user)
        {
            SwitchToTheNextSong();
        }

        private void SwitchToTheNextSong()
        {
            int totalCount;
            var queueSongs =_queuesRepository.Get(new GridParams { Page = 1, PageSize = 1, IsDescOrderBy = true }, out totalCount);
            Song song;
            if(queueSongs.Count != 0)
            {
                var qSong = queueSongs[0];
                song = new Song
                {
                    AlbumName = qSong.Album,
                    ArtistName = qSong.Artist,
                    SongTitle = qSong.Title,
                    IdSong = qSong.IdSong,
                    FileName = qSong.FileName
                };
                _queuesRepository.Remove(qSong.IdQueue);
            }
            else
            {
                song = _rotationLogicService.GetSong();
            }

            Bass.BASS_StreamFree(_recHandle);           
            _recHandle = Bass.BASS_StreamCreateFile(song.FileName, 0, 0, BASSFlag.BASS_DEFAULT);
            _mySync = new SYNCPROC(MetaSync);
            Bass.BASS_ChannelSetAttribute(_recHandle, BASSAttribute.BASS_ATTRIB_VOL, 0f);
            Bass.BASS_ChannelSetSync(_recHandle, BASSSync.BASS_SYNC_END, 0, _mySync, IntPtr.Zero);
            Bass.BASS_ChannelPlay(_recHandle, false);
            _lame.ChannelHandle = _recHandle;
            _songsRepository.AddHistoryInfo(new Domain.Common.HistoryInfo
            {
                SondId = song.IdSong,
                Album = song.AlbumName,
                BandName = song.ArtistName,
                SongTitle = song.SongTitle
            });
            var songTitle = song.ArtistName + " - " + song.SongTitle;
            _icecast.UpdateTitle(songTitle, string.Empty);
            OnSongChanged?.Invoke(songTitle);
        }


I also tried to change timeout options in the icecast config file(source-timeout, client-timeout etc.) but this didn't help.
Repository DB queries are fast and can't produce the timeout also all audio files on the local/network disks and available all the time.
How I can fix or avoid this error?

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #1 on: 24 Jun '19 - 16:27 »
To perhaps narrow down the cause, please see if you can reproduce the problem with the pre-compiled CAST.EXE example that's included in the BASSenc package (C\BIN folder).

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #2 on: 25 Jun '19 - 13:30 »
I run my app on the VPS without sound device and when I tried to run CAST.EXE, I had the following error: Can't initialize device(error code:23)

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #3 on: 25 Jun '19 - 15:19 »
OK. The CAST example won't work in that case as it requires a recording device.

Getting back to your app, the timeout could be caused by the encoder/caster not being fed any data for a while (resulting in the Icecast server receiving nothing). Please try adding some logging of the BASS_ENCODE_COUNT_IN and BASS_ENCODE_COUNT_CAST counters (from BASS_Encode_GetCount), eg. at 5 second intervals, and check if they're still advancing when (and just before) the problem happens.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #4 on: 27 Jun '19 - 17:53 »
When I tried to add logging for this statistic, I always have -1 value for both parameters(BASS_ENCODE_COUNT_IN/BASS_ENCODE_COUNT_CAST), also the brodcasting works fine, music playing but both of the parameters equals to -1. Probably I did something wrong, here my code:
Code: [Select]
        public IcecastInfo Start()
        {
            Logger.Info("IcecastService Started");

            if (!Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
            {
                throw new Exception("BASS_Init failed");
            }

            var song = _rotationLogicService.GetSong();

            if(song == null)
            {
                throw new Exception("Song is empty");
            }

            if (!File.Exists(song.FileName))
            {
                throw new Exception($"Song doesnt exists:{song.FileName}");
            }

            _recHandle = Bass.BASS_StreamCreateFile(song.FileName, 0, 0, BASSFlag.BASS_DEFAULT);

            _lame = new EncoderLAME(_recHandle);
            _lame.InputFile = null;
            _lame.OutputFile = null;
            _lame.LAME_Bitrate = (int)BaseEncoder.BITRATE.kbps_128;
            _lame.LAME_Mode = EncoderLAME.LAMEMode.Default;
            _lame.LAME_TargetSampleRate = (int)BaseEncoder.SAMPLERATE.Hz_44100;
            _lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality;


            var serverAddress = ConfigurationManager.AppSettings["serverAddress"];
            var serverPort = ConfigurationManager.AppSettings["serverPort"];
            var serverMountPoint = ConfigurationManager.AppSettings["serverMountPoint"];
            var serverLogin = ConfigurationManager.AppSettings["serverLogin"];
            var serverPassword = ConfigurationManager.AppSettings["serverPassword"];

            _icecast = new ICEcast(_lame);
            _icecast.ServerAddress = serverAddress;
            _icecast.ServerPort = Convert.ToInt32(serverPort);
            _icecast.MountPoint = serverMountPoint;
            _icecast.PublicFlag = true;
            _icecast.AdminPassword = serverPassword;
            _icecast.AdminUsername = serverLogin;
            _icecast.Password = serverPassword;
            _broadCast = new BroadCast(_icecast);
            _broadCast.Notification += new BroadCastEventHandler(OnBroadCast_Notification);

            Bass.BASS_ChannelSetAttribute(_recHandle, BASSAttribute.BASS_ATTRIB_VOL, 0f);
            _mySync = new SYNCPROC(MetaSync);
            Bass.BASS_ChannelSetSync(_recHandle, BASSSync.BASS_SYNC_END, 0, _mySync, IntPtr.Zero);

            Bass.BASS_ChannelPlay(_recHandle, false);
            _songsRepository.AddHistoryInfo(new Domain.Common.HistoryInfo
            {
                SondId = song.IdSong,
                Album = song.AlbumName,
                BandName = song.ArtistName,
                SongTitle = song.SongTitle
            });

            var songTitle = song.ArtistName + " - " + song.SongTitle;
            _icecast.UpdateTitle(songTitle, string.Empty);
           
            _broadCast.AutoConnect();

            Timer timer = new Timer(5000);
            timer.Elapsed += timer_Elapsed;
            timer.Start();

            return new IcecastInfo
            {
                Bitrate = _lame.LAME_Bitrate.ToString(),
                MountPoint = _icecast.MountPoint,
                ServerPort = _icecast.ServerPort.ToString(),
                SongTitle = songTitle,
                StationName = _icecast.StreamName
            };
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var countIn = BassEnc.BASS_Encode_GetCount(_recHandle, BASSEncodeCount.BASS_ENCODE_COUNT_IN);
            var countCast = BassEnc.BASS_Encode_GetCount(_recHandle, BASSEncodeCount.BASS_ENCODE_COUNT_CAST);
            var tst = Bass.BASS_ErrorGetCode();
            Logger.Info($"DATETIME:{DateTime.Now}, BASS_ENCODE_COUNT_IN:{countIn}, BASS_ENCODE_COUNT_CAST:{countCast}");
        }

Bass.BASS_ErrorGetCode method returns BASS_ERROR_HANDLE.
« Last Edit: 27 Jun '19 - 17:57 by alexeyZ »

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #5 on: 27 Jun '19 - 18:00 »
I think you would use "_lame.EncoderHandle" (for the BASSenc encoder handle) in the BASS_Encode_GetCount calls.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #6 on: 27 Jun '19 - 18:24 »
Yes, with _lame.EncoderHandle I started to receive values, will try to run my app with this logging several hours

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #7 on: 29 Jun '19 - 07:59 »
Hi, after my app worked a few hours and stopped, I found the following in the log:
Code: [Select]
2019-06-27 13:51:08,640 [12] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:6/27/2019 1:51:08 PM, BASS_ENCODE_COUNT_IN:2050687520, BASS_ENCODE_COUNT_CAST:185999360


2019-06-27 13:51:13,646 [14] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:6/27/2019 1:51:13 PM, BASS_ENCODE_COUNT_IN:2051569520, BASS_ENCODE_COUNT_CAST:186081280


2019-06-27 13:51:18,485 [9] INFO  TBMR.BLL.Services.RotationLogicService -
Song:Happy Days-;SongCount:5590;Index:1031;RuleStep:4;Category:2


2019-06-27 13:51:18,653 [12] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:6/27/2019 1:51:18 PM, BASS_ENCODE_COUNT_IN:2052306828, BASS_ENCODE_COUNT_CAST:186146816


2019-06-27 13:51:23,761 [13] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:6/27/2019 1:51:23 PM, BASS_ENCODE_COUNT_IN:2052306828, BASS_ENCODE_COUNT_CAST:186146816


2019-06-27 13:51:28,761 [12] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:6/27/2019 1:51:28 PM, BASS_ENCODE_COUNT_IN:2052306828, BASS_ENCODE_COUNT_CAST:186146816

This values for COUNT_IN/COUNT_CAST started to be non-changeable when the broadcasting switched to a new song. I found this song filename and when I tried to play this file via my app it is always stopped to broadcasting. I thought that this is an encoder problem, but when I tried to play this file using SAM Broadcaster + LAME encoder it is ok

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #8 on: 1 Jul '19 - 12:33 »
If the encoder counts are stopping, that explains why the server connection times out (due to lack of activity). Is BASS_StreamCreateFile failing on that file, ie. returning 0? If not, it may be that the file has a different sample format (rate and channel count) to the other files, which means it can't be fed to the same encoder (without resampling). You can use BASS_ChannelGetInfo to check that.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #9 on: 5 Jul '19 - 11:02 »
I checked the info by BASS_ChannelGetInfo method of the corrupted file, and this file has a higher sample rate than encoder("MP3, 48000Hz, Stereo, 16bit"). In this situation, what is the best way to fix this, can we do resampling "on the fly"?

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #10 on: 5 Jul '19 - 12:43 »
You can use the BASSmix add-on for resampling. You would create a mixer with the wanted sample rate (eg. 44100) and then plug the sources into it one after another. For example, something like this:

Code: [Select]
mixer = BASS_Mixer_StreamCreate(44100, 2, BASS_MIXER_END); // create a stereo 44100Hz mixer
BASS_ChannelSetSync(mixer, BASS_SYNC_END | BASS_SYNC_MIXTIME, 0, EndSyncProc, 0); // set a mixtime END sync on the mixer
BASS_Mixer_StreamAddChannel(mixer, firstsource, 0); // plug in the 1st source
BASS_ChannelPlay(mixer, 0); // start the mixer

...

void CALLBACK EndSyncProc(HSYNC handle, DWORD channel, DWORD data, void *user)
{
if (nextsource) { // got another source to play
BASS_Mixer_StreamAddChannel(mixer, nextsource, 0); // add it to the mixer
BASS_ChannelSetPosition(mixer, 0, BASS_POS_BYTE); // reset the mixer to continue playing (it's ended)
}
}

Please see the documentation for details on the mentioned functions. Note that the mixer sources need to be decoding channels, ie. have the BASS_STREAM_DECODE flag set. If you want to resample a single file, you can skip the END sync "nextsource" stuff.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #11 on: 5 Jul '19 - 13:34 »
Thanks for your advice, will try this approach

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #12 on: 5 Aug '19 - 14:56 »
I postponed for some time my radio project, but now I am starting to work with this project again. I changed the logic to use a bass mixer and locally it is working fine, but on the production environment I have VPS hosting which haven't sound device. I changed initialization to Bass.BASS_Init( 0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero) but after that I had the following app logs:
Code: [Select]
2019-08-05 16:27:48,782 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-05 16:27:49,171 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:3;RuleStep:0;Category:12


2019-08-05 16:27:50,990 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast failed connect


2019-08-05 16:27:56,003 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:27:56 PM, BASS_ENCODE_COUNT_IN:-1, BASS_ENCODE_COUNT_CAST:-1


2019-08-05 16:28:01,010 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:28:01 PM, BASS_ENCODE_COUNT_IN:-1, BASS_ENCODE_COUNT_CAST:-1


Also, when I connected via RDP to VPS hosting PC, I can init sound device only for this connection session time(looks like using a sound card from connection PC) and for this situation I used Bass.BASS_Init( -1, 44100, ....) initialization. But, broadcaster played just one song and after that stopped to work, here logs of this situation:

Code: [Select]
2019-08-05 16:24:52,127 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-05 16:25:00,870 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:1;RuleStep:0;Category:12


2019-08-05 16:25:02,721 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast connected


2019-08-05 16:25:07,728 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:07 PM, BASS_ENCODE_COUNT_IN:885464, BASS_ENCODE_COUNT_CAST:77824


2019-08-05 16:25:12,812 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:12 PM, BASS_ENCODE_COUNT_IN:1730420, BASS_ENCODE_COUNT_CAST:155648


2019-08-05 16:25:17,826 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:17 PM, BASS_ENCODE_COUNT_IN:2610656, BASS_ENCODE_COUNT_CAST:233472


2019-08-05 16:25:20,771 [9] INFO  TBMR.BLL.Services.RotationLogicService -
Song:Marduk-Stone Stands It's Silent Vigil;SongCount:5924;Index:1613;RuleStep:1;Category:2


2019-08-05 16:25:22,838 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:22 PM, BASS_ENCODE_COUNT_IN:3056948, BASS_ENCODE_COUNT_CAST:274432


2019-08-05 16:25:27,839 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:27 PM, BASS_ENCODE_COUNT_IN:3067532, BASS_ENCODE_COUNT_CAST:274432


2019-08-05 16:25:32,840 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:32 PM, BASS_ENCODE_COUNT_IN:3067532, BASS_ENCODE_COUNT_CAST:274432


2019-08-05 16:25:37,853 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:25:37 PM, BASS_ENCODE_COUNT_IN:3067532, BASS_ENCODE_COUNT_CAST:274432

Can anyone help to solve this?

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #13 on: 5 Aug '19 - 15:42 »
Code: [Select]
2019-08-05 16:27:56,003 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/5/2019 4:27:56 PM, BASS_ENCODE_COUNT_IN:-1, BASS_ENCODE_COUNT_CAST:-1

That looks like a BASS_Encode_GetCount call failed, probably because the provided handle is invalid; it might be 0 due to an earlier BASS_Encode_Start call (or add-on equivalent) failing. Are you checking/logging the BASS and BASSenc function return values? If not, please try that to find out what calls are failing (and use BASS_ErrorGetCode to get an error code for any that are).

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #14 on: 6 Aug '19 - 11:26 »
I added some logging info and I have the following:
Code: [Select]
2019-08-06 13:09:04,656 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-06 13:09:05,235 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:3;RuleStep:0;Category:12


2019-08-06 13:09:07,113 [1] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:L:\music\Jingle.mp3


2019-08-06 13:09:07,369 [1] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483647


2019-08-06 13:09:07,371 [1] INFO  TBMR.BLL.Services.IcecastService -
_mixer:0


2019-08-06 13:09:07,665 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast failed connect, error:BASS_ERROR_HANDLE


2019-08-06 13:09:12,779 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 1:09:12 PM, BASS_ENCODE_COUNT_IN:-1, BASS_ENCODE_COUNT_CAST:-1

Also, here is my code with BASS mixer approach:

Code: [Select]
        public IcecastInfo Start()
        {
            Logger.Info("IcecastService Started");
            var serverSilentOutputMode = Convert.ToBoolean(ConfigurationManager.AppSettings["serverSilentOutputMode"]);
           
            if (!Bass.BASS_Init(serverSilentOutputMode ? 0 : -1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
            {
                throw new Exception("BASS_Init failed");
            }

            var song = _rotationLogicService.GetSong();

            if (song == null)
            {
                throw new Exception("Song is empty");
            }

            if (!File.Exists(song.FileName))
            {
                throw new Exception($"Song doesnt exists:{song.FileName}");
            }

            _mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_MIXER_END);

            _lame = new EncoderLAME(_mixer);
            _lame.InputFile = null;
            _lame.OutputFile = null;
            _lame.LAME_Bitrate = (int)BaseEncoder.BITRATE.kbps_128;
            _lame.LAME_Mode = EncoderLAME.LAMEMode.Default;
            _lame.LAME_TargetSampleRate = (int)BaseEncoder.SAMPLERATE.Hz_44100;
            _lame.LAME_Quality = EncoderLAME.LAMEQuality.Quality;


            var serverAddress = ConfigurationManager.AppSettings["serverAddress"];
            var serverPort = ConfigurationManager.AppSettings["serverPort"];
            var serverMountPoint = ConfigurationManager.AppSettings["serverMountPoint"];
            var serverLogin = ConfigurationManager.AppSettings["serverLogin"];
            var serverPassword = ConfigurationManager.AppSettings["serverPassword"];

            _icecast = new ICEcast(_lame);
            _icecast.ServerAddress = serverAddress;
            _icecast.ServerPort = Convert.ToInt32(serverPort);
            _icecast.MountPoint = serverMountPoint;
         
            _icecast.PublicFlag = true;
            _icecast.AdminPassword = serverPassword;
            _icecast.AdminUsername = serverLogin;
            _icecast.Password = serverPassword;
            _broadCast = new BroadCast(_icecast);
            _broadCast.Notification += new BroadCastEventHandler(OnBroadCast_Notification);

            InitRecHandle(song.FileName);

            Logger.Info($"_recHandle:{_recHandle}");

            _mySync = new SYNCPROC(MetaSync);
            Bass.BASS_ChannelSetAttribute(_mixer, BASSAttribute.BASS_ATTRIB_VOL, 0f);
            Bass.BASS_ChannelSetSync(_mixer, BASSSync.BASS_SYNC_END | BASSSync.BASS_SYNC_MIXTIME, 0, _mySync, IntPtr.Zero);
            BassMix.BASS_Mixer_StreamAddChannel(_mixer, _recHandle, 0);
            Bass.BASS_ChannelPlay(_mixer, false);

            Logger.Info($"_mixer:{_mixer}");

            _songsRepository.AddHistoryInfo(new HistoryInfo
            {
                SondId = song.IdSong,
                Album = song.AlbumName,
                BandName = song.ArtistName,
                SongTitle = song.SongTitle
            });

            var songTitle = song.ArtistName + " - " + song.SongTitle;
            _icecast.UpdateTitle(songTitle, string.Empty);
           
            _broadCast.AutoConnect();

            Timer timer = new Timer(5000);
            timer.Elapsed += timer_Elapsed;
            timer.Start();


            return new IcecastInfo
            {
                Bitrate = _lame.LAME_Bitrate.ToString(),
                MountPoint = _icecast.MountPoint,
                ServerPort = _icecast.ServerPort.ToString(),
                SongTitle = songTitle,
                StationName = _icecast.StreamName
            };

        }

        private void InitRecHandle(string fileName)
        {
            Logger.Info($"InitRecHandle, fileName:{fileName}");

            _recHandle = Bass.BASS_StreamCreateFile(fileName, 0, 0,
                BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);

            if (File.Exists(_tempCopiedFile))
            {
                File.Delete(_tempCopiedFile);
            }

            if (_recHandle == 0)
            {
                Logger.Info($"InitRecHandle, copy");
                File.Copy(fileName, _tempCopiedFile);
                _recHandle = Bass.BASS_StreamCreateFile(_tempCopiedFile, 0, 0,
                    BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);
            }
        }

        private void OnBroadCast_Notification(object sender, BroadCastEventArgs e)
        {
            // Note: this method might be called from another thread (non UI thread)!
            if (_broadCast == null)
                return;
            if (_broadCast.IsConnected)
            {
                Logger.Info("BroadCast connected");
            }
            else
            {
                var error = Bass.BASS_ErrorGetCode();
                Logger.Info($"BroadCast failed connect, error:{error}");
            }
        }

       void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var countIn = BassEnc.BASS_Encode_GetCount(_lame.EncoderHandle, BASSEncodeCount.BASS_ENCODE_COUNT_IN);
            var countCast = BassEnc.BASS_Encode_GetCount(_lame.EncoderHandle, BASSEncodeCount.BASS_ENCODE_COUNT_CAST);
            var tst = Bass.BASS_ErrorGetCode();
            Logger.Info($"DATETIME:{DateTime.Now}, BASS_ENCODE_COUNT_IN:{countIn}, BASS_ENCODE_COUNT_CAST:{countCast}");
        }


I am using silent mode set to true(following init function - Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #15 on: 6 Aug '19 - 15:17 »
That looks like you may be using an old BASS version that doesn't support playback on the "No Sound" device (it was added in 2.4.14). You can use BASS_GetVersion to check that.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #16 on: 6 Aug '19 - 16:27 »
Yes, it was older bass DLL, I fixed version before but looks like lost these changes, now the app is working in the silent mode, but played just one song and stopped after that. Here my log :
Code: [Select]
2019-08-06 18:16:27,360 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-06 18:16:27,879 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:0;RuleStep:0;Category:12


2019-08-06 18:16:29,699 [1] INFO  TBMR.BLL.Services.IcecastService -
Bass version:33820160


2019-08-06 18:16:29,715 [1] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:L:\music\Jingle.mp3


2019-08-06 18:16:29,973 [1] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483645


2019-08-06 18:16:29,978 [1] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647


2019-08-06 18:16:30,753 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast connected


2019-08-06 18:16:35,781 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:16:35 PM, BASS_ENCODE_COUNT_IN:883764, BASS_ENCODE_COUNT_CAST:77824


2019-08-06 18:16:40,795 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:16:40 PM, BASS_ENCODE_COUNT_IN:1765764, BASS_ENCODE_COUNT_CAST:155648


2019-08-06 18:16:45,806 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:16:45 PM, BASS_ENCODE_COUNT_IN:2646000, BASS_ENCODE_COUNT_CAST:237568


2019-08-06 18:16:49,124 [9] INFO  TBMR.BLL.Services.RotationLogicService -
Song:Lucifugum-The Mysterious Garden(Hymn To A Black Art);SongCount:5924;Index:1498;RuleStep:1;Category:2


2019-08-06 18:16:50,136 [9] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:J:\Music\Lucifugum\2001 - Instinct Prevelence\03 - The Mysterious Garden(Hymn To The Black Art).mp3


2019-08-06 18:16:50,807 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:16:50 PM, BASS_ENCODE_COUNT_IN:3176740, BASS_ENCODE_COUNT_CAST:286720


2019-08-06 18:16:51,654 [9] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483640


2019-08-06 18:16:51,655 [9] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647


2019-08-06 18:16:55,961 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:16:55 PM, BASS_ENCODE_COUNT_IN:3192840, BASS_ENCODE_COUNT_CAST:286720


2019-08-06 18:17:00,963 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:17:00 PM, BASS_ENCODE_COUNT_IN:3192840, BASS_ENCODE_COUNT_CAST:286720

2019-08-06 18:17:05,978 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:17:05 PM, BASS_ENCODE_COUNT_IN:3192840, BASS_ENCODE_COUNT_CAST:286720

2019-08-06 18:17:10,979 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/6/2019 6:17:10 PM, BASS_ENCODE_COUNT_IN:3192840, BASS_ENCODE_COUNT_CAST:286720

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #17 on: 6 Aug '19 - 17:03 »
When and how are you starting the next song? Check that you're successfully adding it to the mixer.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #18 on: 7 Aug '19 - 16:53 »
I created the mixer in the following way:
Code: [Select]
            _mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_MIXER_END);
            _mySync = new SYNCPROC(MetaSync);
            Bass.BASS_ChannelSetAttribute(_mixer, BASSAttribute.BASS_ATTRIB_VOL, 0f);
            Bass.BASS_ChannelSetSync(_mixer, BASSSync.BASS_SYNC_END | BASSSync.BASS_SYNC_MIXTIME, 0, _mySync, IntPtr.Zero);
            BassMix.BASS_Mixer_StreamAddChannel(_mixer, _recHandle, 0);
            Bass.BASS_ChannelPlay(_mixer, false);

And the following code I am using for the next song switch:
Code: [Select]
        private void MetaSync(int handle, int channel, int data, IntPtr user)
        {
            SwitchToTheNextSong();
        }

        private void SwitchToTheNextSong()
        {
            int totalCount;
            var queueSongs =_queuesRepository.Get(new GridParams { Page = 1, PageSize = 1, IsDescOrderBy = true }, out totalCount);
            Song song;
            if(queueSongs.Count != 0)
            {
                var qSong = queueSongs[0];
                song = new Song
                {
                    AlbumName = qSong.Album,
                    ArtistName = qSong.Artist,
                    SongTitle = qSong.Title,
                    IdSong = qSong.IdSong,
                    FileName = qSong.FileName
                };
                _queuesRepository.Remove(qSong.IdQueue);
            }
            else
            {
                song = _rotationLogicService.GetSong();
            }

            if (!File.Exists(song.FileName))
            {
                SwitchToTheNextSong();
            }

            Bass.BASS_StreamFree(_recHandle);

            InitRecHandle(song.FileName);

            Logger.Info($"_recHandle:{_recHandle}");

            BassMix.BASS_Mixer_StreamAddChannel(_mixer, _recHandle, 0); // add it to the mixer
            Bass.BASS_ChannelSetPosition(_mixer, 0, BASSMode.BASS_POS_BYTE); // reset the mixer to continue playing (it's ended)

            Logger.Info($"_mixer:{_mixer}");

            _songsRepository.AddHistoryInfo(new Domain.Common.HistoryInfo
            {
                SondId = song.IdSong,
                Album = song.AlbumName,
                BandName = song.ArtistName,
                SongTitle = song.SongTitle
            });
            var songTitle = song.ArtistName + " - " + song.SongTitle;
            _icecast.UpdateTitle(songTitle, string.Empty);
            OnSongChanged?.Invoke(songTitle);
        }

        private void InitRecHandle(string fileName)
        {
            Logger.Info($"InitRecHandle, fileName:{fileName}");

            _recHandle = Bass.BASS_StreamCreateFile(fileName, 0, 0,
                BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);

            if (File.Exists(_tempCopiedFile))
            {
                File.Delete(_tempCopiedFile);
            }

            if (_recHandle == 0)
            {
                Logger.Info($"InitRecHandle, copy");
                File.Copy(fileName, _tempCopiedFile);
                _recHandle = Bass.BASS_StreamCreateFile(_tempCopiedFile, 0, 0,
                    BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT);
            }
        }






Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #19 on: 7 Aug '19 - 17:24 »
That code seems fine. To narrow down what the problem is, please try adding BASS_Encode_IsActive to your BASS_ENCODE_COUNT logging, and also log the BASS_Mixer_StreamAddChannel and BASS_ChannelSetPosition call results in SwitchToTheNextSong and a BASS_ChannelIsActive call on the mixer after that too.

Also confirm what BASS/BASSmix/BASSenc versions you are using, with BASS_GetVersion and BASS_Mixer_GetVersion and BASS_Encode_GetVersion.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #20 on: 22 Aug '19 - 16:38 »
I added all necessary log modifications, and now I have the following:
First of all, data about versions:
Code: [Select]
2019-08-22 18:23:36,568 [1] INFO  TBMR.BLL.Services.IcecastService -
Bass version:33820160, mixVersion:33818880, encVersion:33820160

And the full log:
Code: [Select]
2019-08-22 18:23:34,399 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-22 18:23:34,966 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:0;RuleStep:0;Category:12


2019-08-22 18:23:36,568 [1] INFO  TBMR.BLL.Services.IcecastService -
Bass version:33820160, mixVersion:33818880, encVersion:33820160


2019-08-22 18:23:36,578 [1] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:L:\music\radio\Jingle.mp3


2019-08-22 18:23:37,807 [1] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483645


2019-08-22 18:23:37,811 [1] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647

2019-08-22 18:23:38,067 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast connected


2019-08-22 18:23:43,090 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:23:43 PM, BASS_ENCODE_COUNT_IN:876580, BASS_ENCODE_COUNT_CAST:77824, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-22 18:23:48,105 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:23:48 PM, BASS_ENCODE_COUNT_IN:1760344, BASS_ENCODE_COUNT_CAST:155648, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-22 18:23:53,125 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:23:53 PM, BASS_ENCODE_COUNT_IN:2644108, BASS_ENCODE_COUNT_CAST:237568, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-22 18:23:57,341 [10] INFO  TBMR.BLL.Services.RotationLogicService -
Song:Maniac Butcher-Sileny Reznik(Live);SongCount:5950;Index:1582;RuleStep:1;Category:2


2019-08-22 18:23:58,143 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:23:58 PM, BASS_ENCODE_COUNT_IN:3270104, BASS_ENCODE_COUNT_CAST:294912, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-22 18:23:58,710 [10] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:J:\Music\Maniac Butcher\2004 - Krvestreb - Ltd.Ed\07-maniac_butcher-sileny_reznik(live)-ice.mp3


2019-08-22 18:24:00,350 [10] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483640


2019-08-22 18:24:00,351 [10] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647, BASS_Mixer_StreamAddChannelResult:True, BASS_ChannelSetPositionResult:True, BASS_ChannelIsActiveResult:BASS_ACTIVE_STOPPED


2019-08-22 18:24:03,144 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:24:03 PM, BASS_ENCODE_COUNT_IN:3280912, BASS_ENCODE_COUNT_CAST:294912, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-22 18:24:08,144 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:24:08 PM, BASS_ENCODE_COUNT_IN:3280912, BASS_ENCODE_COUNT_CAST:294912, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED


2019-08-22 18:24:13,157 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:24:13 PM, BASS_ENCODE_COUNT_IN:3280912, BASS_ENCODE_COUNT_CAST:294912, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED


2019-08-22 18:24:18,172 [9] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/22/2019 6:24:18 PM, BASS_ENCODE_COUNT_IN:3280912, BASS_ENCODE_COUNT_CAST:294912, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED

After song was changed, we have BASS_ChannelIsActiveResult:BASS_ACTIVE_STOPPED and BASS_Encode_IsActive:BASS_ACTIVE_STOPPED values

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #21 on: 22 Aug '19 - 17:51 »
That looks like the encoder stopped. If you use BASS_Encode_SetNotify on the encoder to request notifications, what notification do you receive?

You could also try using BASSenc directly (instead of the BASS.Net wrapper classes) and see if that makes any difference. Something like this:

Code: [Select]
_lame = BASS_Encode_MP3_Start(_mixer, "-b 128", 0, NULL, NULL);
BASS_Encode_CastInit(_lame, ...);

Please see the documentation for parameter details.

alexeyZ

  • Posts: 18
Re: Icecast:disconnecting source due to socket timeout
« Reply #22 on: 29 Aug '19 - 15:17 »
I added BASS_Encode_SetNotify logging and have just one info about this when song was changed(NotifyProc:BASS_ENCODE_NOTIFY_CAST):
Code: [Select]
2019-08-29 17:00:28,039 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Started


2019-08-29 17:00:28,385 [1] INFO  TBMR.BLL.Services.RotationLogicService -
Song:radio-com;SongCount:5;Index:3;RuleStep:0;Category:12


2019-08-29 17:00:29,123 [1] INFO  TBMR.BLL.Services.IcecastService -
Bass version:33820160, mixVersion:33818880, encVersion:33820160


2019-08-29 17:00:29,130 [1] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:L:\music\radio\Jingle.mp3


2019-08-29 17:00:29,409 [1] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483645


2019-08-29 17:00:29,413 [1] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647


2019-08-29 17:00:29,663 [1] INFO  TBMR.BLL.Services.IcecastService -
BroadCast connected


2019-08-29 17:00:34,681 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:34 PM, BASS_ENCODE_COUNT_IN:887164, BASS_ENCODE_COUNT_CAST:77824, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-29 17:00:39,676 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:39 PM, BASS_ENCODE_COUNT_IN:1769164, BASS_ENCODE_COUNT_CAST:155648, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-29 17:00:44,710 [5] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:44 PM, BASS_ENCODE_COUNT_IN:2651164, BASS_ENCODE_COUNT_CAST:237568, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-29 17:00:46,403 [13] INFO  TBMR.BLL.Services.RotationLogicService -
Song:Abbath-Root of the Mountain;SongCount:5979;Index:3150;RuleStep:1;Category:2


2019-08-29 17:00:48,571 [13] INFO  TBMR.BLL.Services.IcecastService -
InitRecHandle, fileName:k:\music\Abbath\Abbath (Limited Edition) (2016)\07. Root of the Mountain.mp3


2019-08-29 17:00:49,721 [12] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:49 PM, BASS_ENCODE_COUNT_IN:2910680, BASS_ENCODE_COUNT_CAST:262144, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-29 17:00:50,165 [13] INFO  TBMR.BLL.Services.IcecastService -
_recHandle:-2147483640


2019-08-29 17:00:50,165 [13] INFO  TBMR.BLL.Services.IcecastService -
_mixer:-2147483647, BASS_Mixer_StreamAddChannelResult:True, BASS_ChannelSetPositionResult:True, BASS_ChannelIsActiveResult:BASS_ACTIVE_STOPPED


2019-08-29 17:00:54,723 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:54 PM, BASS_ENCODE_COUNT_IN:2921056, BASS_ENCODE_COUNT_CAST:262144, BASS_Encode_IsActive:BASS_ACTIVE_PLAYING


2019-08-29 17:00:59,723 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:00:59 PM, BASS_ENCODE_COUNT_IN:2921056, BASS_ENCODE_COUNT_CAST:262144, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED


2019-08-29 17:00:59,727 [10] INFO  TBMR.BLL.Services.IcecastService -
NotifyProc:BASS_ENCODE_NOTIFY_CAST


2019-08-29 17:01:04,725 [8] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:01:04 PM, BASS_ENCODE_COUNT_IN:2921056, BASS_ENCODE_COUNT_CAST:262144, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED


2019-08-29 17:01:09,743 [12] INFO  TBMR.BLL.Services.IcecastService -
DATETIME:8/29/2019 5:01:09 PM, BASS_ENCODE_COUNT_IN:2921056, BASS_ENCODE_COUNT_CAST:262144, BASS_Encode_IsActive:BASS_ACTIVE_STOPPED


2019-08-29 17:01:09,795 [1] INFO  TBMR.BLL.Services.IcecastService -
IcecastService Stopped

Also, about using BASSenc directly - this can be achieved using P/Invoke mechanism? Do you have some example where using BASSenc directly?

Ian @ un4seen

  • Administrator
  • Posts: 21991
Re: Icecast:disconnecting source due to socket timeout
« Reply #23 on: 29 Aug '19 - 16:03 »
BASS_ENCODE_NOTIFY_CAST indicates that the Icecast server closed the connection. One possible reason for that to happen is that it timed-out due to no data being sent, which could be due to the encoder's source not being processed (ie. the mixer isn't playing). I see in your log that the BASS_ENCODE_COUNT_CAST appears to be stuck on 262144 for at least 10 seconds before the BASS_ENCODE_NOTIFY_CAST. Please try logging the result of every BASS/BASSenc call to see if any are failing.

You can use BASS.Net's BassEnc class to access the BASSenc functions directly (BassEnc_Mp3 class for BASSenc_MP3).