Example #1
0
        public void ChangeCapturedDevices(AudioCaptureDevice audioDevice, VideoCaptureDevice videoDevice)
        {
            try
            {
                SelectedAudioDevice = audioDevice;
                SelectedVideoDevice = videoDevice;

                // Remember our initial capture state.
                bool wasCaptured = CaptureSource.State == CaptureState.Started;
                if (wasCaptured)
                {
                    CaptureSource.Stop();
                }

                CaptureSource.AudioCaptureDevice = audioDevice;
                CaptureSource.VideoCaptureDevice = videoDevice ?? CaptureSource.VideoCaptureDevice;
                ConfigureAudioCaptureDevice(CaptureSource.AudioCaptureDevice);
                ConfigureVideoCaptureDevice(CaptureSource.VideoCaptureDevice);

                // Restore capture state to how it originally was.
                if (wasCaptured)
                {
                    CaptureSelectedInputDevices();
                }
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Error updating captured devices");
                MessageService.ShowErrorHint(ex.Message);
            }
        }
Example #2
0
 protected override void GetSampleAsync(MediaStreamType mediaStreamType)
 {
     try
     {
         if (mediaStreamType == MediaStreamType.Video)
         {
             _sampleRequested = true;
         }
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex, "Get sample failed");
     }
 }
Example #3
0
 private void GetSample(object userState)
 {
     try
     {
         if (_sampleRequested && !_isClosed)
         {
             _sampleRequested = false;
             _videoController.GetNextVideoFrame(_ssrcId, ReportSample);
         }
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex, "Report sample failed");
     }
 }
Example #4
0
 private static void OnFrameSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     try
     {
         var newValue = e.NewValue as short[];
         var source   = d as AudioVisualizer;
         if (newValue != null && newValue.Length > 0 && source != null)
         {
             source.RenderVisualization(newValue);
         }
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex, "Render Visalization for new value failed");
     }
 }
 private void RemoteCamera_Loaded(object sender, RoutedEventArgs e)
 {
     try
     {
         if (_isLoaded)
         {
             return;
         }
         _isLoaded = true;
         mediaElement.BufferingTime = TimeSpan.FromMilliseconds(100);
         mediaElement.SetSource(_mediaStreamSource);
         mediaElement.Play();
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex);
     }
 }
Example #6
0
        public void SelectDevices()
        {
            try
            {
                var config = new ConfigureAudioVideo();
                config.AudioCaptureDevice = _captureSource == null?CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice() : _captureSource.AudioCaptureDevice;

                config.VideoCaptureDevice = null;
                config.Closed            += config_Closed;
                // For some reason, MaxWidth and MaxHeight doesn't work here.
                config.Width  = Math.Min(config.Width, Application.Current.Host.Content.ActualWidth);
                config.Height = Math.Min(config.Height, Application.Current.Host.Content.ActualHeight);
                config.Show();
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Showing configuration for AudioVideo failed");
            }
        }
        /// <summary>
        /// Callback method called after attempting to connect to the media server's control port.
        /// </summary>
        /// <param name="connectException">Any exception raised by the connection attempt.</param>
        protected virtual void HandleControlConnect(Exception connectException)
        {
            try
            {
                if (connectException == null)
                {
                    RegisterClientOnServer();
                }
                else
                {
                    string message = _controlClient != null?String.Format(MediaStrings.FailedToConnectToControlPort, _controlClient.Port, _controlClient.Host) : "Failed to connect to the media server and controlClient is null.";

                    FinalizeConnection(new Exception(message, connectException));
                }
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Error on handling media server connection");
                FinalizeConnection(ex);
            }
        }
Example #8
0
 public void Connect()
 {
     try
     {
         CurrentVisualState = VisualStates.Progress;
         _mediaDevices.Start();
         if (_mediaDevices.CaptureSource.State == CaptureState.Started)
         {
             OnCapturedStarted();
         }
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex);
         _messageService.ShowErrorHint(ex.Message);
     }
     finally
     {
         UpdatePanel();
     }
 }
Example #9
0
        public void RenderVisualization(short[] samples)
        {
            Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    // Copy the samples to the appropriate buffer.
                    Buffer.BlockCopy(samples, 0, _sampleBuffer, _sampleBufferPosition * sizeof(short), samples.Length * sizeof(short));
                    _sampleBufferPosition += samples.Length;

                    // Once there's enough data in the buffer to submit something to the visualizer,
                    // loop through the buffer, pulling a frame's worth of samples at a time, until there's no more room.
                    if (_sampleBufferPosition >= AudioVisualizerConstants.TransformBufferSize)
                    {
                        for (; _sampleBufferPosition - _sampleBufferStart >= AudioVisualizerConstants.TransformBufferSize; _sampleBufferStart += AudioVisualizerConstants.TransformBufferSize)
                        {
                            var samplesToRender = new short[AudioVisualizerConstants.TransformBufferSize];
                            Buffer.BlockCopy(_sampleBuffer, _sampleBufferStart * sizeof(short), samplesToRender, 0, AudioFormat.Default.BytesPerFrame * sizeof(short));
                            lock (_visualLock)
                            {
                                if (_currentRenderVisuals != null)
                                {
                                    _currentRenderVisuals(samplesToRender);
                                }
                            }
                        }

                        // When we're all done, move the unprocessed bytes back to the beginning of the buffer.
                        _sampleBufferPosition = _sampleBufferPosition - _sampleBufferStart;
                        Array.Copy(_sampleBuffer, _sampleBufferStart, _sampleBuffer, 0, _sampleBufferPosition);
                        _sampleBufferStart = 0;
                    }
                }
                catch (Exception ex)
                {
                    ClientLogger.ErrorException(ex, "Render visualization failed");
                }
            });
        }
Example #10
0
 /// <summary>
 /// Saves the selected video capture source back to the local config file after it's successfully captured data.
 /// </summary>
 private void SaveVideoCaptureSource()
 {
     Deployment.Current.Dispatcher.BeginInvoke(() =>
     {
         try
         {
             if (CaptureSource != null && _mediaDeviceConfig != null)
             {
                 if (CaptureSource.VideoCaptureDevice != null)
                 {
                     _mediaDeviceConfig.VideoCaptureDeviceFriendlyName = CaptureSource.VideoCaptureDevice.FriendlyName;
                 }
                 var helper = new ConfigurationHelper <MediaDeviceConfig>();
                 helper.SaveConfigurationObject(_mediaDeviceConfig, MediaDeviceConfig.DefaultFileName);
             }
         }
         catch (Exception ex)
         {
             ClientLogger.ErrorException(ex, "Unable to save video capture source");
         }
     });
 }
Example #11
0
 void config_Closed(object sender, EventArgs e)
 {
     try
     {
         var config = (ConfigureAudioVideo)sender;
         if (config.DialogResult.HasValue && config.DialogResult.Value)
         {
             if (_captureSource != null)
             {
                 if (_captureSource.AudioCaptureDevice != config.AudioCaptureDevice || _captureSource.VideoCaptureDevice != config.VideoCaptureDevice)
                 {
                     _captureSource.AudioCaptureDevice = config.AudioCaptureDevice;
                     CreateAudioContexts();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex, "Closing windows of configuration AudioVideo failed");
     }
 }
Example #12
0
        protected void ProcessVideoQueue(object threadState)
        {
            // The idea here is that we wait for the ProcessRtpData thread to tell us that we've got something to do.
            // Any chunks that arrive before the current chunk is decoded and processed will be discarded.
            var videoThreadData = (VideoThreadData)threadState;

            while (videoThreadData.ResetEvent.WaitOne() && _isActive)
            {
                try
                {
                    videoThreadData.ResetEvent.Reset();
                    while (videoThreadData.VideoChunkQueue.Count > 0)
                    {
                        Chunk chunk = null;
                        try
                        {
                            lock (videoThreadData.VideoChunkQueue)
                            {
                                chunk = videoThreadData.VideoChunkQueue.Dequeue();
                            }
                            if (chunk != null)
                            {
                                videoThreadData.Decoder.DecodeChunk(chunk.Payload, videoThreadData.SsrcId);
                            }
                        }
                        finally
                        {
                            _videoChunkPool.Recycle(chunk);
                        }
                    }
                    UpdateRemoteCameraStatus(videoThreadData);
                }
                catch (Exception ex)
                {
                    ClientLogger.ErrorException(ex, "Process video queue failed");
                }
            }
        }
        /// <summary>
        /// Callback method called after sending registration data to the media server over control port (typically 4521).
        /// </summary>
        /// <param name="data">The byte array containing the data sent by the media server</param>
        /// <param name="offset">The offset into the byte array where the actual data starts</param>
        /// <param name="length">The length of the data sent by the media server</param>
        protected virtual void HandleControlData(byte[] data, int offset, int length)
        {
            if (_disposed)
            {
                return;
            }
            try
            {
                if (_expectingControlData)
                {
                    _expectingControlData = false;
                    string message = Encoding.UTF8.GetString(data, offset, length);                     // .Split('\0')[0];
                    _commandSet.ParseResult(message);
                    if (CheckSuccess(_commandSet))
                    {
                        _rtpClient.Connect(HandleRtpConnect, HandleRtpData);                         // -> HandleRtpConnect
                    }
                    else
                    {
                        string errorMessage = MediaStrings.FailedToRegister;
#if DEBUG
                        errorMessage += Environment.NewLine + "Server = " + _rtpClient.Host + "; Port = " + _rtpClient.Port;
#endif
                        FinalizeConnection(new Exception(errorMessage));
                    }
                }
                else
                {
                    ClientLogger.Error("Unexpectedly received control data.");
                }
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Handle control data failed");
                FinalizeConnection(ex);
            }
        }
Example #14
0
 public T GetConfigurationObject(string fileName)
 {
     try
     {
         string filePath = ConfigPrefix + fileName;
         if (!IsolatedStorageFile.GetUserStoreForSite().FileExists(filePath))
         {
             return(new T());
         }
         using (var stream = new IsolatedStorageFileStream(ConfigPrefix + fileName, FileMode.Open, IsolatedStorageFile.GetUserStoreForSite()))
         {
             var serializer = new XmlSerializer(typeof(T));
             return(serializer.Deserialize(stream) as T);
         }
     }
     catch (FileNotFoundException ex)
     {
         ClientLogger.DebugException(ex, "The configuration file {0} was not found", fileName);
         return(new T());
     }
     catch (IsolatedStorageException ex)
     {
         ClientLogger.ErrorException(ex, "The IsolatedStorage subsystem threw an error retrieving file {0}", fileName);
         return(new T());
     }
     catch (InvalidOperationException ex)
     {
         ClientLogger.DebugException(ex, "InvalidOperationException error getting configuration file {0}", fileName);
         return(new T());
     }
     catch (Exception ex)
     {
         ClientLogger.ErrorException(ex, "Unexpected error getting configuration file {0}", fileName);
         throw;
     }
 }
Example #15
0
        public void Initialize(MediaElement mediaElement, string userId, string roomId)
        {
            _mediaElement = mediaElement;
            _userId       = userId;
            _roomId       = roomId;
            try
            {
                InitializeMedia();
                UpdatePanel();
                ConnectToMediaServer();
                InitializeConnectTimer();

                // This may not succeed (for instance, if the user hasn't authorized the Alanta client to access his webcam)
                // but if it does, it's the best way, since it spins up the webcam automatically. If it fails, it will fail silently.
                // Note: we need to call BeginInvoke here, to avoid showing the "Access for camera and microphone" popup:
                // because we get here when a user on configuration screen has clicked "Leave a message"
                Dispatcher.BeginInvoke(Connect);
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex);
                _messageService.ShowErrorMessage(ex.Message);
            }
        }
Example #16
0
        private static void SaveConfigurationObject(T config, string fileName, bool lastRetry)
        {
            try
            {
                using (var stream = new IsolatedStorageFileStream(ConfigPrefix + fileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForSite()))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    serializer.Serialize(stream, config);
                }
            }
            catch (IsolatedStorageException ex)
            {
                if (!lastRetry)
                {
                    // Ask user to increase IsolatedStorage and on approve try again save data.
                    // Note: in catch we can't show window for increase storage, user should click first on some UI button.
                    AppMessagingAdapter.Instance.TryIncreaseIsolateStorage(isStorageIncreased =>
                    {
                        if (isStorageIncreased)
                        {
                            SaveConfigurationObject(config, fileName, true);
                        }
                    });

                    ClientLogger.ErrorException(ex, "Error on saving configuration file {0}", fileName);
                }
                else
                {
                    ClientLogger.ErrorException(ex, "Occured error on saving data in IsolatedStorage after user increased Storage");
                }
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Unknown error on saving configuration");
            }
        }
        protected void HandleRtpData(byte[] buffer, int offset, int length)
        {
            if (_disposed)
            {
                return;
            }
            List <RtpPacketData> packets = null;

            try
            {
                // ks 8/23/11 - TODO: Investigate whether we can get rid of this memcpy().
                // I think we can do it by passing the dataReceiveBuffer to the NetClient class,
                // and just keeping better track of what's been processed and what hasn't.
                // Kind of like how we handle it in the media server.
                lock (_dataReceiveBuffer)
                {
                    // Copy the data in the network receive buffer to the packet receive buffer.
                    int dataLength = _dataReceiveBuffer.CurrentOffset + length;
                    _dataReceiveBuffer.TryWriteBytes(buffer, offset, length);
                    _dataReceiveBuffer.DataOffset    = 0;
                    _dataReceiveBuffer.CurrentOffset = 0;
                    _dataReceiveBuffer.DataLength    = dataLength;

                    // Pull the packets out of the packet receive buffer.
                    packets = RtpPacketData.GetPacketsFromData(_dataReceiveBuffer, _rtpPacketDataListPool, _rtpPacketDataPool);
                }
                #region Packet Processor
                foreach (var packet in packets)
                {
                    try
                    {
                        switch (packet.PayloadType)
                        {
                        case RtpPayloadType.Audio:
                            if (AudioPacketHandler != null)
                            {
                                AudioPacketHandler(packet);
                            }
                            break;

                        case RtpPayloadType.VideoFromServer:
                            if (VideoPacketHandler != null)
                            {
                                VideoPacketHandler(packet);
                            }
                            break;

                        default:
                            ClientLogger.Debug("Unexpected packetBuffer type {0}", packet.PayloadType);
                            break;
                        }
                    }
                    finally
                    {
                        _rtpPacketDataPool.Recycle(packet);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ClientLogger.ErrorException(ex, "Error processing RTP data");
            }
            finally
            {
                _rtpPacketDataListPool.Recycle(packets);
            }
        }