Ejemplo n.º 1
0
        private async Task <bool> CheckDeviceAccessAsync(bool audio, ChatRecordMode mode)
        {
            // For some reason, as far as I understood, CurrentStatus is always Unspecified on Xbox
            if (string.Equals(AnalyticsInfo.VersionInfo.DeviceFamily, "Windows.Xbox"))
            {
                return(true);
            }

            var access = DeviceAccessInformation.CreateFromDeviceClass(audio ? DeviceClass.AudioCapture : DeviceClass.VideoCapture);

            if (access.CurrentStatus == DeviceAccessStatus.Unspecified)
            {
                MediaCapture capture = null;
                try
                {
                    capture = new MediaCapture();
                    var settings = new MediaCaptureInitializationSettings();
                    settings.StreamingCaptureMode = mode == ChatRecordMode.Video
                        ? StreamingCaptureMode.AudioAndVideo
                        : StreamingCaptureMode.Audio;
                    await capture.InitializeAsync(settings);
                }
                catch { }
                finally
                {
                    if (capture != null)
                    {
                        capture.Dispose();
                        capture = null;
                    }
                }

                return(false);
            }
            else if (access.CurrentStatus != DeviceAccessStatus.Allowed)
            {
                var message = audio
                    ? mode == ChatRecordMode.Voice
                    ? Strings.Resources.PermissionNoAudio
                    : Strings.Resources.PermissionNoAudioVideo
                    : Strings.Resources.PermissionNoCamera;

                this.BeginOnUIThread(async() =>
                {
                    var confirm = await MessagePopup.ShowAsync(message, Strings.Resources.AppName, Strings.Resources.PermissionOpenSettings, Strings.Resources.OK);
                    if (confirm == ContentDialogResult.Primary)
                    {
                        await Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app"));
                    }
                });

                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
            private async void Send(DialogViewModel viewModel, ChatRecordMode mode, StorageFile file, bool mirroring, int duration)
            {
                if (mode == ChatRecordMode.Video)
                {
                    var props = await file.Properties.GetVideoPropertiesAsync();

                    var width  = props.GetWidth();
                    var height = props.GetHeight();
                    var x      = 0d;
                    var y      = 0d;

                    if (width > height)
                    {
                        x     = (width - height) / 2;
                        width = height;
                    }
                    else if (height > width)
                    {
                        y      = (height - width) / 2;
                        height = width;
                    }

                    var transform = new VideoTransformEffectDefinition();
                    transform.CropRectangle = new Rect(x, y, width, height);
                    transform.OutputSize    = new Size(240, 240);
                    transform.Mirror        = mirroring ? MediaMirroringOptions.Horizontal : MediaMirroringOptions.None;

                    var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
                    profile.Video.Width   = 240;
                    profile.Video.Height  = 240;
                    profile.Video.Bitrate = 300000;

                    try
                    {
                        viewModel.Dispatcher.Dispatch(async() =>
                        {
                            await viewModel.SendVideoNoteAsync(file, profile, transform);
                        });
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        viewModel.Dispatcher.Dispatch(async() =>
                        {
                            await viewModel.SendVoiceNoteAsync(file, duration, null);
                        });
                    }
                    catch { }
                }
            }
Ejemplo n.º 3
0
        private async Task <bool> CheckAccessAsync(ChatRecordMode mode)
        {
            var audioPermission = await CheckDeviceAccessAsync(true, mode);

            if (audioPermission == false)
            {
                return(false);
            }

            if (mode == ChatRecordMode.Video)
            {
                var videoPermission = await CheckDeviceAccessAsync(false, ChatRecordMode.Video);

                if (videoPermission == false)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        private void OnRelease()
        {
            if (_recordingLocked)
            {
                Logger.Debug(Target.Recording, "Recording is locked, abort");
                return;
            }
            if (_recordAudioVideoRunnableStarted)
            {
                Logger.Debug(Target.Recording, "Timer should still tick, change mode to: " + (Mode == ChatRecordMode.Video ? ChatRecordMode.Voice : ChatRecordMode.Video));

                _timer.Stop();
                Mode = Mode == ChatRecordMode.Video ? ChatRecordMode.Voice : ChatRecordMode.Video;
            }
            else if (!_hasRecordVideo || _calledRecordRunnable)
            {
                Logger.Debug(Target.Recording, "Timer has tick, stopping recording");

                _recorder.Stop(ViewModel, false);
                _recordingAudioVideo = false;
                UpdateRecordingInterface();
            }
        }
Ejemplo n.º 5
0
        public ChatRecordButton()
        {
            DefaultStyleKey = typeof(ChatRecordButton);

            Mode = ChatRecordMode.Voice;

            ClickMode = ClickMode.Press;
            Click    += OnClick;

            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(300);
            _timer.Tick    += (s, args) =>
            {
                Logger.Debug(Target.Recording, "Timer Tick, check for permissions");

                _timer.Stop();
                RecordAudioVideoRunnable();
            };

            Recorder.Current.RecordingStarted += Current_RecordingStarted;
            Recorder.Current.RecordingStopped += Current_RecordingStopped;
            Recorder.Current.RecordingFailed  += Current_RecordingStopped;
        }
Ejemplo n.º 6
0
            public async void Start(ChatRecordMode mode)
            {
                Logger.Debug(Target.Recording, "Start invoked, mode: " + mode);

                await _recordQueue.Enqueue(async() =>
                {
                    Logger.Debug(Target.Recording, "Enqueued start invoked");

                    if (_recorder != null)
                    {
                        Logger.Debug(Target.Recording, "_recorder != null, abort");

                        RecordingFailed?.Invoke(this, EventArgs.Empty);
                        return;
                    }

                    // Create a new temporary file for the recording
                    var fileName = string.Format(mode == ChatRecordMode.Video
                        ? "video_{0:yyyy}-{0:MM}-{0:dd}_{0:HH}-{0:mm}-{0:ss}.mp4"
                        : "voice_{0:yyyy}-{0:MM}-{0:dd}_{0:HH}-{0:mm}-{0:ss}.ogg", DateTime.Now);
                    var cache = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);

                    try
                    {
                        _mode     = mode;
                        _file     = cache;
                        _recorder = new OpusRecorder(cache, mode == ChatRecordMode.Video);

                        _recorder.m_mediaCapture = new MediaCapture();

                        if (mode == ChatRecordMode.Video)
                        {
                            var cameraDevice = await _recorder.FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front);
                            if (cameraDevice == null)
                            {
                                // TODO: ...
                            }

                            // Figure out where the camera is located
                            if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
                            {
                                // No information on the location of the camera, assume it's an external camera, not integrated on the device
                                _recorder._externalCamera = true;
                            }
                            else
                            {
                                // Camera is fixed on the device
                                _recorder._externalCamera = false;

                                // Only mirror the preview if the camera is on the front panel
                                _recorder._mirroringPreview = cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front;
                            }

                            _recorder.settings.VideoDeviceId = cameraDevice.Id;
                        }

                        await _recorder.m_mediaCapture.InitializeAsync(_recorder.settings);

                        Logger.Debug(Target.Recording, "Devices initialized, starting");

                        await _recorder.StartAsync();

                        Logger.Debug(Target.Recording, "Recording started at " + DateTime.Now);

                        _start = DateTime.Now;
                    }
                    catch (Exception ex)
                    {
                        Logger.Debug(Target.Recording, "Failed to initialize devices, abort: " + ex);

                        _recorder.Dispose();
                        _recorder = null;

                        _file = null;

                        RecordingFailed?.Invoke(this, EventArgs.Empty);
                        return;
                    }

                    RecordingStarted?.Invoke(this, EventArgs.Empty);
                });
            }