1 /*
2 * Copyright (c) 2021 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 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14 #include "host_stream_mgr.h"
15 #include "host_stream.h"
16
17 namespace OHOS::Camera {
18 class HostStreamMgrImpl : public HostStreamMgr {
19 public:
20 RetCode CreateHostStream(const HostStreamInfo& info, BufferCb c) override;
21 RetCode DestroyHostStream(const std::vector<int>& types) override;
22 void GetStreamTypes(std::vector<int32_t>& s) const override;
23 HostStreamInfo GetStreamInfo(const int32_t& id) const override;
24 BufferCb GetBufferCb(const int32_t& type) const override;
25 void GetStreamIds(std::vector<int32_t>& s) const;
26 int32_t DesignateStreamIdForType(const int32_t streamType);
27 HostStreamMgrImpl() = default;
28 ~HostStreamMgrImpl() override = default;
29 protected:
30 std::vector<std::unique_ptr<HostStream>> streams_;
31 };
32
CreateHostStream(const HostStreamInfo & info,BufferCb c)33 RetCode HostStreamMgrImpl::CreateHostStream(const HostStreamInfo& info, BufferCb c)
34 {
35 auto it = std::find_if(streams_.begin(), streams_.end(), [info](const std::unique_ptr<HostStream>& s) {
36 return s->GetStreamId() == info.streamId_;
37 });
38 if (it != streams_.end()) {
39 CAMERA_LOGE("host stream %{public}d exists.", info.streamId_);
40 return RC_OK;
41 }
42 CAMERA_LOGI("bufferpool id = %{public}llu , stream id = %{public}d,stream type = %{public}d, encode = %{public}d",
43 info.bufferPoolId_, info.streamId_, info.type_, info.encodeType_);
44 streams_.push_back(HostStream::Create(info, c));
45 for (auto& stream : streams_) {
46 stream->SetStreamState(false);
47 }
48 return RC_OK;
49 }
50
DestroyHostStream(const std::vector<int> & streamIds)51 RetCode HostStreamMgrImpl::DestroyHostStream(const std::vector<int>& streamIds)
52 {
53 if (streamIds.empty()) {
54 return RC_OK;
55 }
56
57 for (auto& streamId : streamIds) {
58 auto it = std::find_if(streams_.begin(), streams_.end(), [streamId](const std::unique_ptr<HostStream>& s) {
59 return s->GetStreamId() == streamId;
60 });
61 if (it != streams_.end()) {
62 streams_.erase(it);
63 } else {
64 CAMERA_LOGE("stream id not found. [stream id = %{public}d]", streamId);
65 }
66 }
67 return RC_OK;
68 }
69
GetStreamTypes(std::vector<int32_t> & s) const70 void HostStreamMgrImpl::GetStreamTypes(std::vector<int32_t>& s) const
71 {
72 for (const auto& it : streams_) {
73 s.push_back(static_cast<std::underlying_type<StreamIntent>::type>(it->GetStreamType()));
74 }
75 std::sort(s.begin(), s.end(), [](const int32_t& f, const int32_t& n) {
76 return f < n;
77 });
78 }
79
GetStreamIds(std::vector<int32_t> & s) const80 void HostStreamMgrImpl::GetStreamIds(std::vector<int32_t>& s) const
81 {
82 for (auto& it : streams_) {
83 s.emplace_back(it->GetStreamId());
84 }
85 }
86
GetStreamInfo(const int32_t & id) const87 HostStreamInfo HostStreamMgrImpl::GetStreamInfo(const int32_t& id) const
88 {
89 for (auto& it : streams_) {
90 if (it->GetStreamId() == id) {
91 return it->GetStreamInfo();
92 }
93 }
94 return {};
95 }
96
GetBufferCb(const int & streamId) const97 BufferCb HostStreamMgrImpl::GetBufferCb(const int& streamId) const
98 {
99 auto it = std::find_if(streams_.begin(), streams_.end(), [streamId](const std::unique_ptr<HostStream>& s) {
100 return s->GetStreamId() == streamId;
101 });
102 if (it != streams_.end()) {
103 return (*it)->GetBufferCb();
104 }
105 return nullptr;
106 }
107
DesignateStreamIdForType(const int32_t streamType)108 int32_t HostStreamMgrImpl::DesignateStreamIdForType(const int32_t streamType)
109 {
110 for (auto& it : streams_) {
111 if (streamType == it->GetStreamType()) {
112 if (it->GetStreamState() == false) {
113 it->SetStreamState(true);
114 return it->GetStreamId();
115 }
116 }
117 }
118 return -1;
119 }
120
Create()121 std::shared_ptr<HostStreamMgr> HostStreamMgr::Create()
122 {
123 return std::make_shared<HostStreamMgrImpl>();
124 }
125 }
126