[TTVD] Release decoder resource during video capture suspension 78/315478/5
authorJakub Gajownik <j.gajownik2@samsung.com>
Mon, 29 Jul 2024 09:58:54 +0000 (11:58 +0200)
committerBot Blink <blinkbot@samsung.com>
Fri, 2 Aug 2024 12:33:49 +0000 (12:33 +0000)
When video capture device is suspended, we should also
release decoder resource and then reclaim it when
application is resumed.

Bug: https://jira-eu.sec.samsung.net/browse/VDGAME-535
Change-Id: I8759589d163dd42bcc039f3bfa544357c4885db9
Signed-off-by: Jakub Gajownik <j.gajownik2@samsung.com>
media/capture/video/tizen/video_capture_device_tizen_tv.cc

index 0e5fa9cad1325681beb92b23e37a739dbb98cb92..69e4e85ffbbb742d83863e8106370625ff3061b0 100644 (file)
@@ -210,6 +210,10 @@ class VideoCaptureDeviceTizenTv::Impl {
   void UpdatePreviewRotation(media::VideoRotation);
   media::VideoRotation GetVideoRotation();
 
+  bool PrepareVideoDecoder();
+
+  bool suspended_ = false;
+
   Camera* const camera_;
   std::unique_ptr<Client> client_;
   VideoCaptureParams params_;
@@ -263,29 +267,10 @@ void VideoCaptureDeviceTizenTv::Impl::AllocateAndStart(
 
   TIZEN_MEDIA_LOG(INFO) << "Camera started";
 
-  if (IsEncodedCameraFormat(camera_format_)) {
-    MediaVideoCodec codec = CameraToMediaVideoCodec(camera_format_);
-    // TODO(vd.wasm) For now, we'll use predefined settings (bit depth and
-    //               chroma sampling). It should be replaced later with proper
-    //               received frame parsing.
-    auto maybe_configuration = GetVideoConfiguration(codec);
-    if (!maybe_configuration) {
-      client_->OnError(VideoCaptureError::kV4L2VidiocStreamonFailed, FROM_HERE,
-                       "Wrong video codec, cannot initialize decoder");
-      return;
-    }
-
-    TIZEN_MEDIA_LOG(INFO) << "Start video decoder initialization for camera";
-    decoder_ = std::make_unique<TTvdVideoDecoderBase>(
-        base::BindRepeating(&CreateDecoderFacadeVideo));
-    decoder_->Initialize(
-        codec, kNoVideoCodecLevel, params.SuggestConstraints().max_frame_size,
-        maybe_configuration->chroma_sampling, maybe_configuration->bit_depth,
-        true,
-        base::BindOnce(&VideoCaptureDeviceTizenTv::Impl::OnDecoderInitialized,
-                       weak_factory_.GetWeakPtr()),
-        base::BindRepeating(&VideoCaptureDeviceTizenTv::Impl::OnDecodedFrame,
-                            weak_factory_.GetWeakPtr()));
+  if (IsEncodedCameraFormat(camera_format_) && !PrepareVideoDecoder()) {
+    client_->OnError(VideoCaptureError::kV4L2VidiocStreamonFailed, FROM_HERE,
+                     "Wrong video codec, cannot initialize decoder");
+    return;
   }
 
   // For now mark it's started early. There's some problems with
@@ -407,10 +392,43 @@ media::VideoRotation VideoCaptureDeviceTizenTv::Impl::GetVideoRotation() {
   return clockwise_rotation;
 }
 
+bool VideoCaptureDeviceTizenTv::Impl::PrepareVideoDecoder() {
+  MediaVideoCodec codec = CameraToMediaVideoCodec(camera_format_);
+  // TODO(vd.wasm) For now, we'll use predefined settings (bit depth and
+  //               chroma sampling). It should be replaced later with proper
+  //               received frame parsing.
+  auto maybe_configuration = GetVideoConfiguration(codec);
+  if (!maybe_configuration) {
+    return false;
+  }
+
+  TIZEN_MEDIA_LOG(INFO) << "Start video decoder initialization for camera";
+  decoder_ = std::make_unique<TTvdVideoDecoderBase>(
+      base::BindRepeating(&CreateDecoderFacadeVideo));
+  decoder_->Initialize(
+      codec, kNoVideoCodecLevel, params_.SuggestConstraints().max_frame_size,
+      maybe_configuration->chroma_sampling, maybe_configuration->bit_depth,
+      true,
+      base::BindOnce(&VideoCaptureDeviceTizenTv::Impl::OnDecoderInitialized,
+                     weak_factory_.GetWeakPtr()),
+      base::BindRepeating(&VideoCaptureDeviceTizenTv::Impl::OnDecodedFrame,
+                          weak_factory_.GetWeakPtr()));
+  return true;
+}
+
 void VideoCaptureDeviceTizenTv::Impl::ProcessEncodedCameraCapture(
     scoped_refptr<DecoderBuffer> buffer,
     media::VideoRotation rotation) {
   UpdatePreviewRotation(rotation);
+
+  if (suspended_) {
+    return;
+  }
+
+  if (!decoder_) {
+    TIZEN_MEDIA_LOG(ERROR) << "Cannot process encoded frame without decoder";
+    return;
+  }
   decoder_->Decode(std::move(buffer), base::DoNothing());
   decoder_->GetNextVideoFrame();
 }
@@ -572,11 +590,17 @@ void VideoCaptureDeviceTizenTv::Impl::OnDecoderInitialized(
 }
 
 void VideoCaptureDeviceTizenTv::Impl::MaybeSuspend() {
+  suspended_ = true;
   CAMERA_CHECK_AND_RETURN(StopCameraPreview());
+  decoder_.reset();
 }
 
 void VideoCaptureDeviceTizenTv::Impl::Resume() {
+  suspended_ = false;
   CAMERA_CHECK_AND_RETURN(StartCameraPreview());
+  if (IsEncodedCameraFormat(camera_format_) && !PrepareVideoDecoder()) {
+    TIZEN_MEDIA_LOG(ERROR) << "Cannot resume decoder";
+  }
 }
 
 void VideoCaptureDeviceTizenTv::Impl::OnDecodedFrame(RawFrame frame) {