• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file expected 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 #include "double_preview_test.h"
16 
17 using namespace testing::ext;
18 constexpr uint32_t SLEEP_SECOND_TWO = 2;
19 
SetUpTestCase(void)20 void DoublePreviewTest::SetUpTestCase(void)
21 {}
TearDownTestCase(void)22 void DoublePreviewTest::TearDownTestCase(void)
23 {}
SetUp(void)24 void DoublePreviewTest::SetUp(void)
25 {
26     if (display_ == nullptr) {
27         display_ = std::make_shared<TestDisplay>();
28     }
29     display_->Init();
30 }
TearDown(void)31 void DoublePreviewTest::TearDown(void)
32 {
33     display_->Close();
34 }
35 
SetStreamInfo(StreamInfo & streamInfo,const std::shared_ptr<StreamCustomer> & streamCustomer,const int streamId,const StreamIntent intent)36 void DoublePreviewTest::SetStreamInfo(StreamInfo &streamInfo,
37     const std::shared_ptr<StreamCustomer> &streamCustomer,
38     const int streamId, const StreamIntent intent)
39 {
40     sptr<OHOS::IBufferProducer> producer;
41     constexpr uint32_t DATA_SPACE = 8; // picture dataspace
42     constexpr uint32_t TUNNEL_MODE = 5; // tunnel mode
43     constexpr uint32_t BUFFER_QUEUE_SIZE = 8; // set bufferQueue size
44     if (intent == PREVIEW) {
45         streamInfo.width_ = PREVIEW_WIDTH;
46         streamInfo.height_ = PREVIEW_HEIGHT;
47         streamInfo.format_ = PIXEL_FMT_RGBA_8888;
48     }
49     streamInfo.streamId_ = streamId;
50     streamInfo.dataspace_ = DATA_SPACE;
51     streamInfo.intent_ = intent;
52     streamInfo.tunneledMode_ = TUNNEL_MODE;
53     producer = streamCustomer->CreateProducer();
54     streamInfo.bufferQueue_ = new BufferProducerSequenceable(producer);
55     ASSERT_NE(streamInfo.bufferQueue_, nullptr);
56     streamInfo.bufferQueue_->producer_->SetQueueSize(BUFFER_QUEUE_SIZE);
57 }
58 
CreateStream(int streamId,StreamIntent intent)59 void DoublePreviewTest::CreateStream(int streamId, StreamIntent intent)
60 {
61     StreamInfo streamInfo = {};
62 
63     if (intent == PREVIEW) {
64         if (streamId == display_->streamId_preview) {
65             if (streamCustomerPreview_ == nullptr) {
66                     streamCustomerPreview_ = std::make_shared<StreamCustomer>();
67                     SetStreamInfo(streamInfo, streamCustomerPreview_, streamId, intent);
68                     std::vector<StreamInfo>().swap(streamInfos_);
69                     streamInfos_.push_back(streamInfo);
70                 }
71         } else if (streamId == STREAMID_PREVIEW_DOUBLE) {
72             if (streamCustomerPreviewDouble_ == nullptr) {
73                 streamCustomerPreviewDouble_ = std::make_shared<StreamCustomer>();
74                 SetStreamInfo(streamInfo, streamCustomerPreviewDouble_, streamId, intent);
75                 std::vector<StreamInfo>().swap(streamInfos_);
76                 streamInfos_.push_back(streamInfo);
77             }
78         }
79     }
80     result_ = (CamRetCode)display_->streamOperator->CreateStreams(streamInfos_);
81     EXPECT_EQ(false, result_!= HDI::Camera::V1_0::NO_ERROR);
82     if (result_ == HDI::Camera::V1_0::NO_ERROR) {
83         std::cout << "==========[test log]CreateStreams success." << std::endl;
84     } else {
85         std::cout << "==========[test log]CreateStreams fail, result_ = " << result_ << std::endl;
86     }
87 }
88 
CommitStream()89 void DoublePreviewTest::CommitStream()
90 {
91     result_ = (CamRetCode)display_->streamOperator->CommitStreams(NORMAL, display_->ability_);
92     EXPECT_EQ(false, result_ != HDI::Camera::V1_0::NO_ERROR);
93     if (result_ == HDI::Camera::V1_0::NO_ERROR) {
94         std::cout << "==========[test log]CommitStreams preview success." << std::endl;
95     } else {
96         std::cout << "==========[test log]CommitStreams preview  fail, result_ = " << result_ << std::endl;
97     }
98 }
99 
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)100 void DoublePreviewTest::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
101 {
102     captureInfo_.streamIds_ = {streamId};
103     captureInfo_.captureSetting_ = display_->ability_;
104     captureInfo_.enableShutterCallback_ = shutterCallback;
105     result_ = (CamRetCode)display_->streamOperator->Capture(captureId, captureInfo_, isStreaming);
106     EXPECT_EQ(true, result_ == HDI::Camera::V1_0::NO_ERROR);
107     if (result_ == HDI::Camera::V1_0::NO_ERROR) {
108         std::cout << "==========[test log]check Capture: Capture success, " << captureId << std::endl;
109     } else {
110         std::cout << "==========[test log]check Capture: Capture fail, result_ = " << result_ << captureId << std::endl;
111     }
112     if (captureId == display_->captureId_preview) {
113         streamCustomerPreview_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
114             std::cout << "==========[test log]preview size= " <<
115                 size << std::endl;
116         });
117     } else if (captureId == CAPTUREID_PREVIEW_DOUBLE) {
118         streamCustomerPreviewDouble_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
119             std::cout << "==========[test log]preview double size= " <<
120                 size << std::endl;
121         });
122     } else {
123         std::cout << "==========[test log]StartCapture ignore command " << std::endl;
124     }
125     sleep(SLEEP_SECOND_TWO);
126 }
127 
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)128 void DoublePreviewTest::StopStream(std::vector<int> &captureIds, std::vector<int> &streamIds)
129 {
130     sleep(SLEEP_SECOND_TWO);
131     if (sizeof(captureIds) == 0) {
132         return;
133     }
134         for (auto &captureId : captureIds) {
135             if (captureId == display_->captureId_preview) {
136                 streamCustomerPreview_->ReceiveFrameOff();
137             } else if (captureId == CAPTUREID_PREVIEW_DOUBLE) {
138                 streamCustomerPreviewDouble_->ReceiveFrameOff();
139         }  else if (captureId == display_->captureId_capture) {
140             display_->streamCustomerCapture_->ReceiveFrameOff();
141         } else if (captureId == display_->captureId_video) {
142             display_->streamCustomerVideo_->ReceiveFrameOff();
143             sleep(SLEEP_SECOND_TWO);
144             display_->CloseFd();
145             } else {
146                 std::cout << "==========[test log]StopStream ignore command. " <<  std::endl;
147             }
148         }
149         for (auto &captureId : captureIds) {
150             result_ = (CamRetCode)display_->streamOperator->CancelCapture(captureId);
151             sleep(SLEEP_SECOND_TWO);
152             EXPECT_EQ(true, result_ == HDI::Camera::V1_0::NO_ERROR);
153             if (result_ == HDI::Camera::V1_0::NO_ERROR) {
154                 std::cout << "==========[test log]check Capture: CancelCapture success," << captureId << std::endl;
155             } else {
156                 std::cout << "==========[test log]check Capture: CancelCapture fail, result_ = " << result_;
157                 std::cout << "captureId = " << captureId << std::endl;
158         }
159     }
160 }
161 
162 /**
163   * @tc.name: double preview
164   * @tc.desc: Commit 2 streams together, Double preview streams, isStreaming is true.
165   * @tc.level: Level1
166   * @tc.size: MediumTest
167   * @tc.type: Function
168   */
169 static HWTEST_F(DoublePreviewTest, double_preview_001, TestSize.Level1)
170 {
171     // Get the stream manager
172     display_->AchieveStreamOperator();
173 
174     // Start stream
175     CreateStream(display_->streamId_preview, PREVIEW);
176     CreateStream(STREAMID_PREVIEW_DOUBLE, PREVIEW);
177 
178     // Commit stream
179     CommitStream();
180 
181     // Get preview
182     StartCapture(display_->streamId_preview, display_->captureId_preview, false, true);
183     StartCapture(STREAMID_PREVIEW_DOUBLE, CAPTUREID_PREVIEW_DOUBLE, false, true);
184 
185     constexpr uint32_t SLEEP_SECOND_TEN = 10; // sleep ten second
186     sleep(SLEEP_SECOND_TEN);
187 
188     std::vector<int> streamIds = {display_->streamId_preview, STREAMID_PREVIEW_DOUBLE};
189     std::vector<int> captureIds = {display_->captureId_preview, CAPTUREID_PREVIEW_DOUBLE};
190     StopStream(captureIds, streamIds);
191 }
192 
193 /**
194   * @tc.name: double preview and still_capture
195   * @tc.desc: Commit 3 streams together, Double preview and still_capture streams, isStreaming is true.
196   * @tc.level: Level1
197   * @tc.size: MediumTest
198   * @tc.type: Function
199   */
200 static HWTEST_F(DoublePreviewTest, double_preview_002, TestSize.Level1)
201 {
202     // Get the stream manager
203     display_->AchieveStreamOperator();
204 
205     // Start stream
206     CreateStream(display_->streamId_preview, PREVIEW);
207     CreateStream(STREAMID_PREVIEW_DOUBLE, PREVIEW);
208     display_->intents = {STILL_CAPTURE};
209     display_->StartStream(display_->intents);
210 
211     // Get preview
212     StartCapture(display_->streamId_preview, display_->captureId_preview, false, true);
213     StartCapture(STREAMID_PREVIEW_DOUBLE, CAPTUREID_PREVIEW_DOUBLE, false, true);
214     // add dumy exif info
215     constexpr double latitude = 27.987500; // dummy data: Qomolangma latitde
216     constexpr double longitude = 86.927500; // dummy data: Qomolangma longituude
217     constexpr double altitude = 8848.86; // dummy data: Qomolangma altitude
218     constexpr size_t entryCapacity = 100;
219     constexpr size_t dataCapacity = 2000;
220     std::shared_ptr<CameraSetting>  captureSetting =
221         std::make_shared<CameraSetting>(entryCapacity, dataCapacity);
222     std::vector<double> gps;
223     gps.push_back(latitude);
224     gps.push_back(longitude);
225     gps.push_back(altitude);
226     captureSetting->addEntry(OHOS_JPEG_GPS_COORDINATES, gps.data(), gps.size());
227     std::vector<uint8_t> setting;
228     MetadataUtils::ConvertMetadataToVec(captureSetting, setting);
229 
230     CaptureInfo captureInfo = {};
231     captureInfo.streamIds_ = {display_->streamId_capture};
232     captureInfo.captureSetting_ = setting;
233     captureInfo.enableShutterCallback_ = false;
234     display_->rc = (CamRetCode)display_->streamOperator->Capture(display_->captureId_capture, captureInfo, true);
235     EXPECT_EQ(true, display_->rc == HDI::Camera::V1_0::NO_ERROR);
236     if (display_->rc == HDI::Camera::V1_0::NO_ERROR) {
237         std::cout << "==========[test log]check Capture: Capture success, " << display_->captureId_capture << std::endl;
238     } else {
239         std::cout << "==========[test log]check Capture: Capture fail, rc = " << display_->rc
240             << display_->captureId_capture << std::endl;
241     }
__anonee0ffa580302(void* addr, const uint32_t size) 242     display_->streamCustomerCapture_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
243         display_->StoreImage(addr, size);
244     });
245 
246     constexpr uint32_t SLEEP_SECOND_FIVE = 5; // sleep five second
247     sleep(SLEEP_SECOND_FIVE);
248 
249     std::vector<int> captureIds = {display_->captureId_preview, CAPTUREID_PREVIEW_DOUBLE,
250         display_->captureId_capture};
251     std::vector<int> streamIds = {display_->streamId_preview, STREAMID_PREVIEW_DOUBLE, display_->streamId_capture};
252     StopStream(captureIds, streamIds);
253 }
254 
255 /**
256   * @tc.name: double preview and video
257   * @tc.desc: Commit 3 streams together, Double preview and video streams, isStreaming is true.
258   * @tc.level: Level1
259   * @tc.size: MediumTest
260   * @tc.type: Function
261   */
262 static HWTEST_F(DoublePreviewTest, double_preview_003, TestSize.Level1)
263 {
264     // Get the stream manager
265     display_->AchieveStreamOperator();
266 
267     // Start stream
268     CreateStream(display_->streamId_preview, PREVIEW);
269     CreateStream(STREAMID_PREVIEW_DOUBLE, PREVIEW);
270     display_->intents = {VIDEO};
271     display_->StartStream(display_->intents);
272 
273     // Get preview
274     StartCapture(display_->streamId_preview, display_->captureId_preview, false, true);
275     StartCapture(STREAMID_PREVIEW_DOUBLE, CAPTUREID_PREVIEW_DOUBLE, false, true);
276     display_->StartCapture(display_->streamId_video, display_->captureId_video, false, true);
277 
278     constexpr uint32_t SLEEP_SECOND_FIVE = 5; // sleep five second
279     sleep(SLEEP_SECOND_FIVE);
280 
281     std::vector<int> captureIds = {display_->captureId_preview, CAPTUREID_PREVIEW_DOUBLE,
282         display_->captureId_video};
283     std::vector<int> streamIds = {display_->streamId_preview, STREAMID_PREVIEW_DOUBLE, display_->streamId_video};
284     StopStream(captureIds, streamIds);
285 }
286