@@ -237,14 +237,16 @@ HRESULT CaptureControllerImpl::CreateCaptureEngine() {
237237 return hr;
238238 }
239239
240+ // Check MF_CAPTURE_ENGINE_INITIALIZED event handling
241+ // for response process.
240242 hr = capture_engine_->Initialize (capture_engine_callback_handler_.Get (),
241243 attributes.Get (), audio_source_.Get (),
242244 video_source_.Get ());
243245 return hr;
244246}
245247
246248void CaptureControllerImpl::ResetCaptureController () {
247- if (record_handler_) {
249+ if (record_handler_ && record_handler_-> CanStop () ) {
248250 if (record_handler_->IsContinuousRecording ()) {
249251 StopRecord ();
250252 } else if (record_handler_->IsTimedRecording ()) {
@@ -391,7 +393,7 @@ uint32_t CaptureControllerImpl::GetMaxPreviewHeight() const {
391393 }
392394}
393395
394- // Finds best mediat type for given source stream index and max height;
396+ // Finds best media type for given source stream index and max height;
395397bool FindBestMediaType (DWORD source_stream_index, IMFCaptureSource* source,
396398 IMFMediaType** target_media_type, uint32_t max_height,
397399 uint32_t * target_frame_width,
@@ -533,8 +535,6 @@ void CaptureControllerImpl::StopRecord() {
533535 // Check MF_CAPTURE_ENGINE_RECORD_STOPPED event handling for response
534536 // process.
535537 if (!record_handler_->StopRecord (capture_engine_.Get ())) {
536- // Destroy record handler on error cases to make sure state is resetted.
537- record_handler_ = nullptr ;
538538 return OnRecordStopped (false , " Failed to stop video recording" );
539539 }
540540}
@@ -578,6 +578,8 @@ void CaptureControllerImpl::StartPreview() {
578578 texture_handler_->UpdateTextureSize (preview_frame_width_,
579579 preview_frame_height_);
580580
581+ // TODO(loic-sharma): This does not handle duplicate calls properly.
582+ // See: https://github.com/flutter/flutter/issues/108404
581583 if (!preview_handler_) {
582584 preview_handler_ = std::make_unique<PreviewHandler>();
583585 } else if (preview_handler_->IsInitialized ()) {
@@ -605,7 +607,7 @@ void CaptureControllerImpl::StartPreview() {
605607void CaptureControllerImpl::StopPreview () {
606608 assert (capture_engine_);
607609
608- if (!IsInitialized () && !preview_handler_) {
610+ if (!IsInitialized () || !preview_handler_) {
609611 return ;
610612 }
611613
@@ -619,7 +621,7 @@ void CaptureControllerImpl::StopPreview() {
619621void CaptureControllerImpl::PausePreview () {
620622 assert (capture_controller_listener_);
621623
622- if (!preview_handler_ && !preview_handler_->IsInitialized ()) {
624+ if (!preview_handler_ || !preview_handler_->IsInitialized ()) {
623625 return capture_controller_listener_->OnPausePreviewFailed (
624626 " Preview not started" );
625627 }
@@ -638,7 +640,7 @@ void CaptureControllerImpl::PausePreview() {
638640void CaptureControllerImpl::ResumePreview () {
639641 assert (capture_controller_listener_);
640642
641- if (!preview_handler_ && !preview_handler_->IsInitialized ()) {
643+ if (!preview_handler_ || !preview_handler_->IsInitialized ()) {
642644 return capture_controller_listener_->OnResumePreviewFailed (
643645 " Preview not started" );
644646 }
@@ -722,6 +724,13 @@ void CaptureControllerImpl::OnPicture(bool success, const std::string& error) {
722724void CaptureControllerImpl::OnCaptureEngineInitialized (
723725 bool success, const std::string& error) {
724726 if (capture_controller_listener_) {
727+ if (!success) {
728+ capture_controller_listener_->OnCreateCaptureEngineFailed (
729+ " Failed to initialize capture engine" );
730+ ResetCaptureController ();
731+ return ;
732+ }
733+
725734 // Create texture handler and register new texture.
726735 texture_handler_ = std::make_unique<TextureHandler>(texture_registrar_);
727736
@@ -848,7 +857,7 @@ void CaptureControllerImpl::UpdateCaptureTime(uint64_t capture_time_us) {
848857 }
849858
850859 if (preview_handler_ && preview_handler_->IsStarting ()) {
851- // Informs that first frame is captured succeffully and preview has
860+ // Informs that first frame is captured successfully and preview has
852861 // started.
853862 OnPreviewStarted (true , " " );
854863 }
0 commit comments