1 /*
2  * Copyright (C) 2023 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 <memory>
17 #include "avmemory_inner_mock.h"
18 #include "native_averrors.h"
19 #include "demuxer_inner_mock.h"
20 #include "buffer/avbuffer.h"
21 
22 namespace OHOS {
23 namespace MediaAVCodec {
24 using namespace Media;
Destroy()25 int32_t DemuxerInnerMock::Destroy()
26 {
27     if (demuxer_ != nullptr) {
28         demuxer_ = nullptr;
29         return AV_ERR_OK;
30     }
31     return AV_ERR_UNKNOWN;
32 }
33 
SelectTrackByID(uint32_t trackIndex)34 int32_t DemuxerInnerMock::SelectTrackByID(uint32_t trackIndex)
35 {
36     if (demuxer_ != nullptr) {
37         return demuxer_->SelectTrackByID(trackIndex);
38     }
39     return AV_ERR_UNKNOWN;
40 }
41 
UnselectTrackByID(uint32_t trackIndex)42 int32_t DemuxerInnerMock::UnselectTrackByID(uint32_t trackIndex)
43 {
44     if (demuxer_ != nullptr) {
45         return demuxer_->UnselectTrackByID(trackIndex);
46     }
47     return AV_ERR_UNKNOWN;
48 }
49 
ReadSample(uint32_t trackIndex,std::shared_ptr<AVMemoryMock> sample,AVCodecBufferInfo * bufferInfo,uint32_t & flag,bool checkBufferInfo)50 int32_t DemuxerInnerMock::ReadSample(uint32_t trackIndex, std::shared_ptr<AVMemoryMock> sample,
51     AVCodecBufferInfo *bufferInfo, uint32_t &flag, bool checkBufferInfo)
52 {
53     if (bufferInfo == nullptr) {
54         printf("AVCodecBufferInfo is nullptr");
55         return AV_ERR_UNKNOWN;
56     }
57     auto mem = std::static_pointer_cast<AVMemoryInnerMock>(sample);
58     std::shared_ptr<AVSharedMemory> sharedMem = (mem != nullptr) ? mem->GetAVMemory() : nullptr;
59     if (sharedMem == nullptr) {
60         printf("AVSharedMemory is nullptr");
61         return AV_ERR_UNKNOWN;
62     }
63     std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(
64         sharedMem->GetBase(), sharedMem->GetSize(), sharedMem->GetSize());
65     if (buffer == nullptr || buffer->memory_ == nullptr) {
66         printf("AVBuffer is nullptr");
67         return AV_ERR_UNKNOWN;
68     }
69     if (demuxer_ != nullptr) {
70         int32_t ret = demuxer_->ReadSampleBuffer(trackIndex, buffer);
71         bufferInfo->presentationTimeUs = buffer->pts_;
72         bufferInfo->size = buffer->memory_->GetSize();
73         bufferInfo->offset = 0;
74         flag = buffer->flag_;
75         if (checkBufferInfo) {
76             std::shared_ptr<Meta> bufferMeta = buffer->meta_;
77             if (bufferMeta == nullptr) {
78                 printf("AVBuffer meta is nullptr");
79             } else {
80                 int64_t duration;
81                 int64_t dts;
82                 bufferMeta->GetData(Tag::BUFFER_DURATION, duration);
83                 bufferMeta->GetData(Tag::BUFFER_DECODING_TIMESTAMP, dts);
84                 printf("[track %d] duration %" PRId64 " dts %" PRId64 "\n", trackIndex, duration, dts);
85             }
86         }
87         return ret;
88     }
89     return AV_ERR_UNKNOWN;
90 }
91 
SeekToTime(int64_t mSeconds,SeekMode mode)92 int32_t DemuxerInnerMock::SeekToTime(int64_t mSeconds, SeekMode mode)
93 {
94     if (demuxer_ != nullptr) {
95         SeekMode seekMode = static_cast<SeekMode>(mode);
96         return demuxer_->SeekToTime(mSeconds, seekMode);
97     }
98     return AV_ERR_UNKNOWN;
99 }
100 
GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,const uint64_t relativePresentationTimeUs,uint32_t & index)101 int32_t DemuxerInnerMock::GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
102     const uint64_t relativePresentationTimeUs, uint32_t &index)
103 {
104     if (demuxer_ != nullptr) {
105         return demuxer_->GetIndexByRelativePresentationTimeUs(trackIndex, relativePresentationTimeUs, index);
106     }
107     return AV_ERR_UNKNOWN;
108 }
109 
GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,const uint32_t index,uint64_t & relativePresentationTimeUs)110 int32_t DemuxerInnerMock::GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
111     const uint32_t index, uint64_t &relativePresentationTimeUs)
112 {
113     if (demuxer_ != nullptr) {
114         return demuxer_->GetRelativePresentationTimeUsByIndex(trackIndex, index, relativePresentationTimeUs);
115     }
116     return AV_ERR_UNKNOWN;
117 }
118 }
119 }