• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
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_data_source_proxy.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "media_dfx.h"
20 #include "avdatasrcmemory.h"
21 #include "avsharedmemory_ipc.h"
22 #include "meta/any.h"
23 
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaDataSourceProxy"};
26 }
27 
28 namespace OHOS {
29 namespace Media {
30 class MediaDataSourceProxy::BufferCache : public NoCopyable {
31 public:
BufferCache()32     BufferCache()
33     {
34         caches_ = nullptr;
35     }
~BufferCache()36     ~BufferCache()
37     {
38         caches_ = nullptr;
39     }
40 
WriteToParcel(const std::shared_ptr<AVSharedMemory> & memory,MessageParcel & parcel)41     int32_t WriteToParcel(const std::shared_ptr<AVSharedMemory> &memory, MessageParcel &parcel)
42     {
43         CHECK_AND_RETURN_RET_LOG(memory != nullptr, MSERR_NO_MEMORY, "memory is nullptr");
44         CacheFlag flag;
45         if (caches_ != nullptr && caches_ == memory.get()) {
46             MEDIA_LOGI("HIT_CACHE");
47             flag = CacheFlag::HIT_CACHE;
48             parcel.WriteUint8(static_cast<uint8_t>(flag));
49             return MSERR_OK;
50         } else {
51             MEDIA_LOGI("UPDATE_CACHE");
52             flag = CacheFlag::UPDATE_CACHE;
53             caches_ = memory.get();
54             parcel.WriteUint8(static_cast<uint8_t>(flag));
55             return WriteAVSharedMemoryToParcel(memory, parcel);
56         }
57     }
58 
59 private:
60     AVSharedMemory *caches_;
61 };
62 
MediaDataCallback(const sptr<IStandardMediaDataSource> & ipcProxy)63 MediaDataCallback::MediaDataCallback(const sptr<IStandardMediaDataSource> &ipcProxy)
64     : callbackProxy_(ipcProxy)
65 {
66     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
67 }
68 
~MediaDataCallback()69 MediaDataCallback::~MediaDataCallback()
70 {
71     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
72 }
73 
74 
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos)75 int32_t MediaDataCallback::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos)
76 {
77     MEDIA_LOGD("ReadAt in");
78     CHECK_AND_RETURN_RET_LOG(callbackProxy_ != nullptr, SOURCE_ERROR_IO, "callbackProxy_ is nullptr");
79     CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_NO_MEMORY, "memory is nullptr");
80     return callbackProxy_->ReadAt(mem, length, pos, false);
81 }
82 
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)83 int32_t MediaDataCallback::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
84 {
85     MEDIA_LOGD("ReadAt in");
86     CHECK_AND_RETURN_RET_LOG(callbackProxy_ != nullptr, SOURCE_ERROR_IO, "callbackProxy_ is nullptr");
87     CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_NO_MEMORY, "memory is nullptr");
88     return callbackProxy_->ReadAt(mem, length, pos, true);
89 }
90 
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)91 int32_t MediaDataCallback::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
92 {
93     MEDIA_LOGD("ReadAt in");
94     CHECK_AND_RETURN_RET_LOG(callbackProxy_ != nullptr, SOURCE_ERROR_IO, "callbackProxy_ is nullptr");
95     CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_NO_MEMORY, "memory is nullptr");
96     return callbackProxy_->ReadAt(mem, length, 0, true);
97 }
98 
GetSize(int64_t & size)99 int32_t MediaDataCallback::GetSize(int64_t &size)
100 {
101     CHECK_AND_RETURN_RET_LOG(callbackProxy_ != nullptr, MSERR_INVALID_OPERATION, "callbackProxy_ is nullptr");
102     return callbackProxy_->GetSize(size);
103 }
104 
MediaDataSourceProxy(const sptr<IRemoteObject> & impl)105 MediaDataSourceProxy::MediaDataSourceProxy(const sptr<IRemoteObject> &impl)
106     : IRemoteProxy<IStandardMediaDataSource>(impl)
107 {
108     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
109 }
110 
~MediaDataSourceProxy()111 MediaDataSourceProxy::~MediaDataSourceProxy()
112 {
113     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
114 }
115 
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos,bool isHistreamer)116 int32_t MediaDataSourceProxy::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos,
117     bool isHistreamer)
118 {
119     MEDIA_LOGD("ReadAt in");
120     CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_NO_MEMORY, "mem is nullptr");
121     MediaTrace trace("DataSrc::ReadAt");
122     MessageParcel data;
123     MessageParcel reply;
124     MessageOption option(MessageOption::TF_SYNC);
125 
126     bool token = data.WriteInterfaceToken(MediaDataSourceProxy::GetDescriptor());
127     CHECK_AND_RETURN_RET_LOG(token, MSERR_INVALID_OPERATION, "Failed to write descriptor!");
128 
129     if (BufferCache_ == nullptr) {
130         BufferCache_ = std::make_unique<BufferCache>();
131     }
132     CHECK_AND_RETURN_RET_LOG(BufferCache_ != nullptr, MSERR_NO_MEMORY, "Failed to create BufferCache_!");
133 
134     uint32_t offset = 0;
135     if (!isHistreamer) {
136         offset = std::static_pointer_cast<AVDataSrcMemory>(mem)->GetOffset();
137     }
138     MEDIA_LOGD("offset is %{public}u", offset);
139     BufferCache_->WriteToParcel(mem, data);
140     data.WriteUint32(offset);
141     data.WriteUint32(length);
142     data.WriteInt64(pos);
143     int error = Remote()->SendRequest(static_cast<uint32_t>(ListenerMsg::READ_AT), data, reply, option);
144     CHECK_AND_RETURN_RET_LOG(error == MSERR_OK, 0, "ReadAt failed, error: %{public}d", error);
145 
146     return reply.ReadInt32();
147 }
148 
GetSize(int64_t & size)149 int32_t MediaDataSourceProxy::GetSize(int64_t &size)
150 {
151     MessageParcel data;
152     MessageParcel reply;
153     MessageOption option(MessageOption::TF_SYNC);
154 
155     bool token = data.WriteInterfaceToken(MediaDataSourceProxy::GetDescriptor());
156     CHECK_AND_RETURN_RET_LOG(token, MSERR_INVALID_OPERATION, "Failed to write descriptor!");
157 
158     int error = Remote()->SendRequest(static_cast<uint32_t>(ListenerMsg::GET_SIZE), data, reply, option);
159     CHECK_AND_RETURN_RET_LOG(error == MSERR_OK, -1, "GetSize failed, error: %{public}d", error);
160 
161     size = reply.ReadInt64();
162     return reply.ReadInt32();
163 }
164 } // namespace Media
165 } // namespace OHOS