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>& streamIds) 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 std::transform(streams_.begin(), streams_.end(), std::back_inserter(s),
73 [](auto &iter) { return static_cast<std::underlying_type<VdiStreamIntent>::type>(iter->GetStreamType()); });
74 std::sort(s.begin(), s.end(), [](const int32_t& f, const int32_t& n) {
75 return f < n;
76 });
77 }
78
GetStreamIds(std::vector<int32_t> & s) const79 void HostStreamMgrImpl::GetStreamIds(std::vector<int32_t>& s) const
80 {
81 std::transform(streams_.begin(), streams_.end(), std::back_inserter(s),
82 [](auto &it) { return it->GetStreamId(); });
83 }
84
GetStreamInfo(const int32_t & id) const85 HostStreamInfo HostStreamMgrImpl::GetStreamInfo(const int32_t& id) const
86 {
87 auto it = std::find_if(streams_.begin(), streams_.end(),
88 [id](const std::unique_ptr<HostStream>& s) { return s->GetStreamId() == id; });
89 if (it != streams_.end()) {
90 return (*it)->GetStreamInfo();
91 }
92 return {};
93 }
94
GetBufferCb(const int & streamId) const95 BufferCb HostStreamMgrImpl::GetBufferCb(const int& streamId) const
96 {
97 auto it = std::find_if(streams_.begin(), streams_.end(), [streamId](const std::unique_ptr<HostStream>& s) {
98 return s->GetStreamId() == streamId;
99 });
100 if (it != streams_.end()) {
101 return (*it)->GetBufferCb();
102 }
103 return nullptr;
104 }
105
DesignateStreamIdForType(const int32_t streamType)106 int32_t HostStreamMgrImpl::DesignateStreamIdForType(const int32_t streamType)
107 {
108 for (auto& it : streams_) {
109 if (streamType == it->GetStreamType()) {
110 if (it->GetStreamState() == false) {
111 it->SetStreamState(true);
112 return it->GetStreamId();
113 }
114 }
115 }
116 return -1;
117 }
118
Create()119 std::shared_ptr<HostStreamMgr> HostStreamMgr::Create()
120 {
121 return std::make_shared<HostStreamMgrImpl>();
122 }
123 }
124