• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef HISTREAMER_APP_CLIENT_H
17 #define HISTREAMER_APP_CLIENT_H
18 
19 #include <string>
20 #include "network/network_client.h"
21 #include <memory>
22 #include "avcodec_log.h"
23 #include "avcodec_errors.h"
24 #include "plugin/plugin_buffer.h"
25 #include "osal/task/mutex.h"
26 #include "osal/task/condition_variable.h"
27 #include "common/status.h"
28 #include "common/log.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Plugins {
33 namespace HttpPlugin {
34 class AppClient : public NetworkClient {
35 public:
36     AppClient(RxHeader headCallback, RxBody bodyCallback, void* userParam);
37 
38     ~AppClient() override;
39 
40     Status Init() override;
41 
42     Status Open(const std::string& url, const std::map<std::string, std::string>& httpHeader,
43                 int32_t timeoutMs) override;
44 
45     Status RequestData(long startPos, int len, const RequestInfo& requestInfo,
46         HandleResponseCbFunc completedCb) override;
47 
48     Status Close(bool isAsync) override;
49 
50     Status Deinit() override;
51 
52     Status GetIp(std::string &ip) override;
53 
54     void SetAppUid(int32_t appUid) override;
55 
56     void SetLoader(std::shared_ptr<IMediaSourceLoader> sourceLoader) override;
57 
58     int32_t RespondHeader(int64_t uuid, const std::map<std::string, std::string>& httpHeader,
59                        std::string redirectUrl) override;
60 
61     int32_t RespondData(int64_t uuid, int64_t offset, const std::shared_ptr<AVSharedMemory> memory) override;
62 
63     int32_t FinishLoading(int64_t uuid, LoadingRequestError state) override;
64 
65     void SetUuid(int64_t uuid) override;
66 
67     std::string GetRedirectUrl() override;
68 
69 private:
70     void NotifyResponseDataEnd(LoadingRequestError state);
71     void MapToString(std::map<std::string, std::string> httpHeader);
72 
73 private:
74     RxHeader rxHeader_;
75     RxBody rxBody_;
76     void *userParam_;
77 
78     std::shared_ptr<IMediaSourceLoader> sourceLoader_;
79     int64_t uuid_ {0};
80     std::atomic<bool> isResponseCompleted_ {false};
81     ConditionVariable responseCondition_{};
82     LoadingRequestError requestState_ = LoadingRequestError::LOADING_ERROR_SUCCESS;
83     mutable FairMutex mutex_;
84     int dataInFlight_ {0};
85     long startPos_ {0};
86     int len_ {0};
87     std::string redirectUrl_ {""};
88     int64_t curOffset_ {-2};
89 };
90 }
91 }
92 }
93 }
94 #endif