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_stub.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "media_data_source.h"
20 #include "avdatasrcmemory.h"
21 #include "avsharedmemory_ipc.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaDataSourceStub"};
25 }
26
27 namespace OHOS {
28 namespace Media {
29 class MediaDataSourceStub::BufferCache : public NoCopyable {
30 public:
31 BufferCache() = default;
32 ~BufferCache() = default;
33
ReadFromParcel(MessageParcel & parcel,std::shared_ptr<AVSharedMemory> & memory)34 int32_t ReadFromParcel(MessageParcel &parcel, std::shared_ptr<AVSharedMemory> &memory)
35 {
36 CacheFlag flag = static_cast<CacheFlag>(parcel.ReadUint8());
37 if (flag == CacheFlag::HIT_CACHE) {
38 MEDIA_LOGD("HIT_CACHE");
39 memory = caches_;
40 return MSERR_OK;
41 } else {
42 MEDIA_LOGD("UPDATE_CACHE");
43 memory = ReadAVDataSrcMemoryFromParcel(parcel);
44 CHECK_AND_RETURN_RET_LOG(memory != nullptr, MSERR_INVALID_VAL, "ReadAVDataSrcMemory failed");
45 caches_ = memory;
46 return MSERR_OK;
47 }
48 }
49
50 private:
51 std::shared_ptr<AVSharedMemory> caches_;
52 };
53
MediaDataSourceStub(const std::shared_ptr<IMediaDataSource> & dataSrc)54 MediaDataSourceStub::MediaDataSourceStub(const std::shared_ptr<IMediaDataSource> &dataSrc)
55 : dataSrc_(dataSrc)
56 {
57 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
58 }
59
~MediaDataSourceStub()60 MediaDataSourceStub::~MediaDataSourceStub()
61 {
62 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
63 }
64
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int MediaDataSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
66 MessageOption &option)
67 {
68 auto remoteDescriptor = data.ReadInterfaceToken();
69 if (MediaDataSourceStub::GetDescriptor() != remoteDescriptor) {
70 MEDIA_LOGE("Invalid descriptor");
71 return MSERR_INVALID_OPERATION;
72 }
73
74 if (BufferCache_ == nullptr) {
75 BufferCache_ = std::make_unique<BufferCache>();
76 }
77
78 switch (static_cast<ListenerMsg>(code)) {
79 case ListenerMsg::READ_AT: {
80 std::shared_ptr<AVSharedMemory> memory = nullptr;
81 if (BufferCache_ != nullptr) {
82 int32_t ret = BufferCache_->ReadFromParcel(data, memory);
83 CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
84 }
85 uint32_t offset = data.ReadUint32();
86 uint32_t length = data.ReadUint32();
87 int64_t pos = data.ReadInt64();
88 CHECK_AND_RETURN_RET_LOG(memory != nullptr, MSERR_NO_MEMORY, "memory is nullptr");
89 std::static_pointer_cast<AVDataSrcMemory>(memory)->SetOffset(offset);
90 MEDIA_LOGD("offset is %{public}u", offset);
91 int32_t realLen = ReadAt(memory, length, pos);
92 reply.WriteInt32(realLen);
93 return MSERR_OK;
94 }
95 case ListenerMsg::GET_SIZE: {
96 int64_t size = 0;
97 int32_t ret = GetSize(size);
98 reply.WriteInt64(size);
99 reply.WriteInt32(ret);
100 return MSERR_OK;
101 }
102 default: {
103 MEDIA_LOGE("default case, need check MediaDataSourceStub");
104 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105 }
106 }
107 }
108
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos,bool isHiStreamer)109 int32_t MediaDataSourceStub::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos,
110 bool isHiStreamer)
111 {
112 MEDIA_LOGD("ReadAt in isHiStreamer = %{public}d", isHiStreamer);
113 CHECK_AND_RETURN_RET_LOG(dataSrc_ != nullptr, SOURCE_ERROR_IO, "dataSrc_ is nullptr");
114 CHECK_AND_RETURN_RET_LOG(mem != nullptr, MSERR_NO_MEMORY, "mem is nullptr");
115 return dataSrc_->ReadAt(mem, length, pos);
116 }
117
GetSize(int64_t & size)118 int32_t MediaDataSourceStub::GetSize(int64_t &size)
119 {
120 CHECK_AND_RETURN_RET_LOG(dataSrc_ != nullptr, MSERR_INVALID_OPERATION, "dataSrc_ is nullptr");
121 return dataSrc_->GetSize(size);
122 }
123 } // namespace Media
124 } // namespace OHOS