1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "image_sink_processor.h"
17 
18 #include "dscreen_errcode.h"
19 #include "dscreen_hisysevent.h"
20 #include "dscreen_hitrace.h"
21 #include "dscreen_log.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
ConfigureImageProcessor(const VideoParam & localParam,const VideoParam & remoteParam,const std::shared_ptr<IImageSinkProcessorListener> & imageListener)25 int32_t ImageSinkProcessor::ConfigureImageProcessor(
26     const VideoParam &localParam, const VideoParam &remoteParam,
27     const std::shared_ptr<IImageSinkProcessorListener> &imageListener)
28 {
29     DHLOGI("%{public}s: ConfigureImageProcessor.", DSCREEN_LOG_TAG);
30     localParam_ = localParam;
31     remoteParam_ = remoteParam;
32 
33     imageDecoder_ = std::make_shared<ImageSinkDecoder>(imageListener);
34     imageJpeg_ = std::make_shared<JpegImageProcessor>(remoteParam);
35     int32_t ret = imageDecoder_->ConfigureDecoder(localParam);
36     if (ret != DH_SUCCESS) {
37         DHLOGE("%{public}s: ConfigureDecoder failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
38         return ret;
39     }
40 
41     return DH_SUCCESS;
42 }
43 
ReleaseImageProcessor()44 int32_t ImageSinkProcessor::ReleaseImageProcessor()
45 {
46     DHLOGI("%{public}s: ReleaseImageProcessor.", DSCREEN_LOG_TAG);
47     if (imageDecoder_ == nullptr) {
48         DHLOGE("%{public}s: Decoder is null.", DSCREEN_LOG_TAG);
49         ReportOptFail(DSCREEN_OPT_FAIL, ERR_DH_SCREEN_TRANS_NULL_VALUE, "ReleaseImageProcessor Decoder is null.");
50         return ERR_DH_SCREEN_TRANS_NULL_VALUE;
51     }
52 
53     StartTrace(DSCREEN_HITRACE_LABEL, DSCREEN_RELEASE_DECODER_START);
54     int32_t ret = imageDecoder_->ReleaseDecoder();
55     FinishTrace(DSCREEN_HITRACE_LABEL);
56     if (ret != DH_SUCCESS) {
57         DHLOGE("%{public}s: ReleaseDecoder failed.", DSCREEN_LOG_TAG);
58         ReportOptFail(DSCREEN_OPT_FAIL, ret, "ReleaseDecoder failed.");
59         return ret;
60     }
61 
62     return DH_SUCCESS;
63 }
64 
StartImageProcessor()65 int32_t ImageSinkProcessor::StartImageProcessor()
66 {
67     DHLOGI("%{public}s: StartImageProcessor.", DSCREEN_LOG_TAG);
68     if (imageDecoder_ == nullptr) {
69         DHLOGE("%{public}s: Decoder is null.", DSCREEN_LOG_TAG);
70         ReportOptFail(DSCREEN_OPT_FAIL, ERR_DH_SCREEN_TRANS_NULL_VALUE, "StartImageProcessor Decoder is null.");
71         return ERR_DH_SCREEN_TRANS_NULL_VALUE;
72     }
73 
74     StartTrace(DSCREEN_HITRACE_LABEL, DSCREEN_START_DECODER_START);
75     int32_t ret = imageDecoder_->StartDecoder();
76     FinishTrace(DSCREEN_HITRACE_LABEL);
77     if (ret != DH_SUCCESS) {
78         DHLOGE("%{public}s: StartDecoder failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
79         ReportOptFail(DSCREEN_OPT_FAIL, ret, "StartDecoder failed.");
80         return ret;
81     }
82 
83     return DH_SUCCESS;
84 }
85 
StopImageProcessor()86 int32_t ImageSinkProcessor::StopImageProcessor()
87 {
88     DHLOGI("%{public}s: StopImageProcessor.", DSCREEN_LOG_TAG);
89     if (imageDecoder_ == nullptr) {
90         DHLOGE("%{public}s: Decoder is null.", DSCREEN_LOG_TAG);
91         ReportOptFail(DSCREEN_OPT_FAIL, ERR_DH_SCREEN_TRANS_NULL_VALUE, "StopImageProcessor Decoder is null.");
92         return ERR_DH_SCREEN_TRANS_NULL_VALUE;
93     }
94 
95     StartTrace(DSCREEN_HITRACE_LABEL, DSCREEN_STOP_DECODER_START);
96     int32_t ret = imageDecoder_->StopDecoder();
97     FinishTrace(DSCREEN_HITRACE_LABEL);
98     if (ret != DH_SUCCESS) {
99         DHLOGE("%{public}s: StopDecoder failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
100         ReportOptFail(DSCREEN_OPT_FAIL, ret, "StopDecoder failed.");
101         return ret;
102     }
103 
104     return DH_SUCCESS;
105 }
106 
SetImageSurface(sptr<Surface> & surface)107 int32_t ImageSinkProcessor::SetImageSurface(sptr<Surface> &surface)
108 {
109     DHLOGI("%{public}s: SetImageSurface.", DSCREEN_LOG_TAG);
110     if (imageDecoder_ == nullptr || surface == nullptr) {
111         DHLOGE("%{public}s: Decoder or surface is null.", DSCREEN_LOG_TAG);
112         ReportOptFail(DSCREEN_OPT_FAIL, ERR_DH_SCREEN_TRANS_NULL_VALUE, "SetImageSurface Decoder is null.");
113         return ERR_DH_SCREEN_TRANS_NULL_VALUE;
114     }
115 
116     int32_t ret = imageDecoder_->SetOutputSurface(surface);
117     if (ret != DH_SUCCESS) {
118         DHLOGE("%{public}s: SetOutputSurface failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
119         ReportOptFail(DSCREEN_OPT_FAIL, ret, "SetOutputSurface failed.");
120         return ret;
121     }
122 
123     ret = imageJpeg_->SetOutputSurface(surface);
124     if (ret != DH_SUCCESS) {
125         DHLOGE("%{public}s: imageJpeg SetImageSurface failed ret: %{public}" PRId32, DSCREEN_LOG_TAG, ret);
126         ReportOptFail(DSCREEN_OPT_FAIL, ret, "imageJpeg SetOutputSurface failed.");
127         return ret;
128     }
129     return DH_SUCCESS;
130 }
131 
ProcessImage(const std::shared_ptr<DataBuffer> & data)132 int32_t ImageSinkProcessor::ProcessImage(const std::shared_ptr<DataBuffer> &data)
133 {
134     DHLOGI("%{public}s: ProcessImage.", DSCREEN_LOG_TAG);
135     if (imageDecoder_ == nullptr || data == nullptr) {
136         DHLOGE("%{public}s: Decoder or data is null.", DSCREEN_LOG_TAG);
137         ReportOptFail(DSCREEN_OPT_FAIL, ERR_DH_SCREEN_TRANS_NULL_VALUE, "processImage Decoder is null.");
138         return ERR_DH_SCREEN_TRANS_NULL_VALUE;
139     }
140 
141     if (data->DataType() == VIDEO_FULL_SCREEN_DATA) {
142         int32_t ret = imageDecoder_->InputScreenData(data);
143         if (ret != DH_SUCCESS) {
144             DHLOGE("%{public}s: InputScreenData failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
145             ReportOptFail(DSCREEN_OPT_FAIL, ret, "InputScreenData failed.");
146             return ret;
147         }
148     } else if (data->DataType() == VIDEO_PART_SCREEN_DATA) {
149         int32_t ret = imageJpeg_->FillDirtyImages2Surface(data, imageDecoder_->GetLastFrame());
150         if (ret != DH_SUCCESS) {
151             DHLOGE("%{public}s: FillDirtyImages2Surface failed ret:%{public}" PRId32, DSCREEN_LOG_TAG, ret);
152             ReportOptFail(DSCREEN_OPT_FAIL, ret, "FillDirtyImages2Surface failed.");
153             return ret;
154         }
155     } else {
156         DHLOGE("%{public}s: data type is invalid.", DSCREEN_LOG_TAG);
157         return ERR_DH_SCREEN_DATA_TYPE_INVALID;
158     }
159     return DH_SUCCESS;
160 }
161 } // namespace DistributedHardware
162 } // namespace OHOS