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 "native_averrors.h"
17 #include "avmemory_capi_mock.h"
18 #include "demuxer_capi_mock.h"
19 #include "native_avbuffer.h"
20 
21 namespace OHOS {
22 namespace MediaAVCodec {
Destroy()23 int32_t DemuxerCapiMock::Destroy()
24 {
25     if (demuxer_ != nullptr) {
26         int32_t ret = OH_AVDemuxer_Destroy(demuxer_);
27         demuxer_ = nullptr;
28         return ret;
29     }
30     return AV_ERR_UNKNOWN;
31 }
32 
SelectTrackByID(uint32_t trackIndex)33 int32_t DemuxerCapiMock::SelectTrackByID(uint32_t trackIndex)
34 {
35     if (demuxer_ != nullptr) {
36         return OH_AVDemuxer_SelectTrackByID(demuxer_, trackIndex);
37     }
38     return AV_ERR_UNKNOWN;
39 }
40 
UnselectTrackByID(uint32_t trackIndex)41 int32_t DemuxerCapiMock::UnselectTrackByID(uint32_t trackIndex)
42 {
43     if (demuxer_ != nullptr) {
44         return OH_AVDemuxer_UnselectTrackByID(demuxer_, trackIndex);
45     }
46     return AV_ERR_UNKNOWN;
47 }
48 
ReadSample(uint32_t trackIndex,std::shared_ptr<AVMemoryMock> sample,AVCodecBufferInfo * bufferInfo,uint32_t & flag,bool checkBufferInfo)49 int32_t DemuxerCapiMock::ReadSample(uint32_t trackIndex, std::shared_ptr<AVMemoryMock> sample,
50     AVCodecBufferInfo *bufferInfo, uint32_t &flag, bool checkBufferInfo)
51 {
52     auto mem = std::static_pointer_cast<AVMemoryCapiMock>(sample);
53     if (mem == nullptr) {
54         printf("AVMemoryCapiMock is nullptr\n");
55         return AV_ERR_UNKNOWN;
56     }
57     int32_t cap = OH_AVMemory_GetSize(mem->GetAVMemory());
58     OH_AVBuffer *avBuffer = OH_AVBuffer_Create(cap);
59     if (avBuffer == nullptr) {
60         printf("OH_AVBuffer is nullptr\n");
61         return AV_ERR_UNKNOWN;
62     }
63     if (demuxer_ != nullptr) {
64         int32_t ret = OH_AVDemuxer_ReadSampleBuffer(demuxer_, trackIndex, avBuffer);
65         if (ret != AV_ERR_OK) {
66             return ret;
67         }
68         OH_AVCodecBufferAttr bufferAttr;
69         ret = OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr);
70         if (ret != AV_ERR_OK) {
71             return ret;
72         }
73         bufferInfo->presentationTimeUs = bufferAttr.pts;
74         bufferInfo->size = bufferAttr.size;
75         bufferInfo->offset = bufferAttr.offset;
76         flag = bufferAttr.flags;
77         if (checkBufferInfo) {
78             OH_AVFormat *format = OH_AVBuffer_GetParameter(avBuffer);
79             if (format == nullptr) {
80                 printf("OH_AVBuffer format is nullptr\n");
81             } else {
82                 int64_t duration;
83                 int64_t dts;
84                 OH_AVFormat_GetLongValue(format, OH_MD_KEY_BUFFER_DURATION, &duration);
85                 OH_AVFormat_GetLongValue(format, OH_MD_KEY_DECODING_TIMESTAMP, &dts);
86                 printf("[track %d] duration %" PRId64 " dts %" PRId64 "\n", trackIndex, duration, dts);
87             }
88             OH_AVFormat_Destroy(format);
89         }
90         OH_AVBuffer_Destroy(avBuffer);
91         return ret;
92     }
93     OH_AVBuffer_Destroy(avBuffer);
94     return AV_ERR_UNKNOWN;
95 }
96 
SeekToTime(int64_t mSeconds,Media::SeekMode mode)97 int32_t DemuxerCapiMock::SeekToTime(int64_t mSeconds, Media::SeekMode mode)
98 {
99     if (demuxer_ != nullptr) {
100         OH_AVSeekMode seekMode = static_cast<OH_AVSeekMode>(mode);
101         return OH_AVDemuxer_SeekToTime(demuxer_, mSeconds, seekMode);
102     }
103     return AV_ERR_UNKNOWN;
104 }
105 
SetMediaKeySystemInfoCallback(bool isNull)106 int32_t DemuxerCapiMock::SetMediaKeySystemInfoCallback(bool isNull)
107 {
108     return AV_ERR_OK;
109 }
110 
SetDemuxerMediaKeySystemInfoCallback(bool isNull)111 int32_t DemuxerCapiMock::SetDemuxerMediaKeySystemInfoCallback(bool isNull)
112 {
113     return AV_ERR_OK;
114 }
115 
GetMediaKeySystemInfo()116 int32_t DemuxerCapiMock::GetMediaKeySystemInfo()
117 {
118     return AV_ERR_OK;
119 }
120 
GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,const uint64_t relativePresentationTimeUs,uint32_t & index)121 int32_t DemuxerCapiMock::GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
122     const uint64_t relativePresentationTimeUs, uint32_t &index)
123 {
124     return AV_ERR_OK;
125 }
126 
GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,const uint32_t index,uint64_t & relativePresentationTimeUs)127 int32_t DemuxerCapiMock::GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
128     const uint32_t index, uint64_t &relativePresentationTimeUs)
129 {
130     return AV_ERR_OK;
131 }
132 }
133 }