1 /*
2 * Copyright (c) 2024-2024 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 "media_source_loading_request.h"
17 #include "avcodec_trace.h"
18
19 namespace OHOS {
20 namespace Media {
21 namespace Plugins {
22 namespace HttpPlugin {
23
24 // -------------------------------LoadingRequestElements----------------------------------------
~LoadingRequestElements()25 LoadingRequestElements::~LoadingRequestElements()
26 {
27 MEDIA_LOG_I("0x%{public}06" PRIXPTR " Elements dtor, uuid " PUBLIC_LOG_D64, FAKE_POINTER(this), uuid_);
28 if (client_ != nullptr) {
29 client_.reset();
30 }
31 }
32
RespondData(int64_t uuid,int64_t offset,const std::shared_ptr<AVSharedMemory> & data)33 int32_t LoadingRequestElements::RespondData(int64_t uuid, int64_t offset, const std::shared_ptr<AVSharedMemory> &data)
34 {
35 if (client_ != nullptr) {
36 return client_->RespondData(uuid, offset, data);
37 }
38 return 0;
39 }
40
RespondHeader(int64_t uuid,std::map<std::string,std::string> header,std::string redirectUrl)41 int32_t LoadingRequestElements::RespondHeader(int64_t uuid, std::map<std::string, std::string> header,
42 std::string redirectUrl)
43 {
44 if (client_ != nullptr) {
45 return client_->RespondHeader(uuid, header, redirectUrl);
46 }
47 return 0;
48 }
49
FinishLoading(int64_t uuid,LoadingRequestError state)50 int32_t LoadingRequestElements::FinishLoading(int64_t uuid, LoadingRequestError state)
51 {
52 if (client_ != nullptr) {
53 return client_->FinishLoading(uuid, state);
54 }
55 return 0;
56 }
57
58 // -------------------------------MediaSourceLoadingRequest----------------------------------------
Open(int64_t uuid,const std::shared_ptr<NetworkClient> & client)59 int64_t MediaSourceLoadingRequest::Open(int64_t uuid, const std::shared_ptr<NetworkClient> &client)
60 {
61 MediaAVCodec::AVCodecTrace trace("MediaSourceLoadingRequest Open, uuid: " + std::to_string(uuid));
62 MEDIA_LOG_I("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest Open, uuid: " PUBLIC_LOG_D64,
63 FAKE_POINTER(this), uuid);
64 AutoLock lock(clientMutex_);
65 auto it = requestMap_.find(uuid);
66 if (it != requestMap_.end()) {
67 MEDIA_LOG_W("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest Open, uuid has opened: " PUBLIC_LOG_D64,
68 FAKE_POINTER(this), uuid);
69 return 0;
70 }
71 auto element = std::make_shared<LoadingRequestElements>(uuid, client);
72 FALSE_RETURN_V_MSG(element != nullptr, 0, "MediaSourceLoadingRequest Open, no enough memory.");
73 requestMap_.emplace(uuid, element);
74 return 0;
75 }
76
Close(int64_t uuid)77 int32_t MediaSourceLoadingRequest::Close(int64_t uuid)
78 {
79 MediaAVCodec::AVCodecTrace trace("MediaSourceLoadingRequest Close, uuid: " + std::to_string(uuid));
80 MEDIA_LOG_I("0x%{public}06" PRIXPTR "MediaSOurceLoadingRequest Close, uuid: " PUBLIC_LOG_D64,
81 FAKE_POINTER(this), uuid);
82 AutoLock lock(clientMutex_);
83 auto it = requestMap_.find(uuid);
84 if (it != requestMap_.end()) {
85 requestMap_.erase(it);
86 } else {
87 MEDIA_LOG_W("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest Close, invalid uuid: " PUBLIC_LOG_D64,
88 FAKE_POINTER(this), uuid);
89 }
90 return 0;
91 }
92
~MediaSourceLoadingRequest()93 MediaSourceLoadingRequest::~MediaSourceLoadingRequest()
94 {
95 MEDIA_LOG_I("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest dtor in.", FAKE_POINTER(this));
96 AutoLock lock(clientMutex_);
97 requestMap_.clear();
98 }
99
RespondData(int64_t uuid,int64_t offset,const std::shared_ptr<AVSharedMemory> & request)100 int32_t MediaSourceLoadingRequest::RespondData(int64_t uuid, int64_t offset,
101 const std::shared_ptr<AVSharedMemory> &request)
102 {
103 MediaAVCodec::AVCodecTrace trace("MediaSourceLoadingRequest RespondData, uuid: " + std::to_string(uuid) +
104 ", offset: " + std::to_string(offset));
105 MEDIA_LOG_D("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest RespondData, uuid: " PUBLIC_LOG_D64
106 " offset: " PUBLIC_LOG_D64, FAKE_POINTER(this), uuid, offset);
107 AutoLock lock(clientMutex_);
108 auto it = requestMap_.find(uuid);
109 if (it != requestMap_.end()) {
110 return it->second->RespondData(uuid, offset, request);
111 } else {
112 MEDIA_LOG_E("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest RespondData, invalid uuid: " PUBLIC_LOG_D64,
113 FAKE_POINTER(this), uuid);
114 }
115 return 0;
116 }
117
RespondHeader(int64_t uuid,std::map<std::string,std::string> header,std::string redirectUrl)118 int32_t MediaSourceLoadingRequest::RespondHeader(int64_t uuid, std::map<std::string, std::string> header,
119 std::string redirectUrl)
120 {
121 MediaAVCodec::AVCodecTrace trace("MediaSourceLoadingRequest RespondHeader, uuid: " + std::to_string(uuid));
122 MEDIA_LOG_D("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest RespondHeader, uuid: " PUBLIC_LOG_D64,
123 FAKE_POINTER(this), uuid);
124 AutoLock lock(clientMutex_);
125 auto it = requestMap_.find(uuid);
126 if (it != requestMap_.end()) {
127 it->second->RespondHeader(uuid, header, redirectUrl);
128 } else {
129 MEDIA_LOG_E("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest RespondHeader, invalid uuid: " PUBLIC_LOG_D64,
130 FAKE_POINTER(this), uuid);
131 }
132 return 0;
133 }
134
FinishLoading(int64_t uuid,LoadingRequestError state)135 int32_t MediaSourceLoadingRequest::FinishLoading(int64_t uuid, LoadingRequestError state)
136 {
137 MEDIA_LOG_I("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest FinishLoading, uuid: " PUBLIC_LOG_D64
138 " state: " PUBLIC_LOG_D32, FAKE_POINTER(this), uuid, static_cast<int32_t>(state));
139 AutoLock lock(clientMutex_);
140 auto it = requestMap_.find(uuid);
141 if (it != requestMap_.end()) {
142 it->second->FinishLoading(uuid, state);
143 } else {
144 MEDIA_LOG_E("0x%{public}06" PRIXPTR "MediaSourceLoadingRequest FinishLoading, invalid uuid: " PUBLIC_LOG_D64,
145 FAKE_POINTER(this), uuid);
146 }
147 return 0;
148 }
149
150 // -------------------------------MediaSourceLoaderCombinations----------------------------------------
Open(const std::string & url,const std::map<std::string,std::string> & header,std::shared_ptr<NetworkClient> & client)151 int64_t MediaSourceLoaderCombinations::Open(const std::string &url, const std::map<std::string, std::string> &header,
152 std::shared_ptr<NetworkClient> &client)
153 {
154 if (request_ == nullptr) {
155 request_ = std::make_shared<MediaSourceLoadingRequest>();
156 FALSE_RETURN_V_MSG(request_ != nullptr, 0, "MediaSourceLoaderCombinations Open, no enough memory.");
157 std::shared_ptr<IMediaSourceLoadingRequest> request =
158 std::static_pointer_cast<IMediaSourceLoadingRequest>(request_);
159 loader_->Init(request);
160 }
161 int64_t uuid = loader_->Open(url, header);
162 MEDIA_LOG_I("0x%{public}06" PRIXPTR "MediaSourceLoaderCombinations Open, uuid: " PUBLIC_LOG_D64,
163 FAKE_POINTER(this), uuid);
164 request_->Open(uuid, client);
165 return uuid;
166 }
167
Close(int64_t uuid)168 int32_t MediaSourceLoaderCombinations::Close(int64_t uuid)
169 {
170 request_->Close(uuid);
171 int32_t ret = loader_->Close(uuid);
172 return ret;
173 }
174 }
175 }
176 }
177 }