1 /*
2 * Copyright (c) 2024-2024 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 "demuxer.h"
17
18 #include "dp_log.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
~Demuxer()23 Demuxer::~Demuxer()
24 {
25 DP_DEBUG_LOG("entered.");
26 demuxer_ = nullptr;
27 }
28
Create(const std::shared_ptr<AVSource> & source,const std::map<Media::Plugins::MediaType,std::shared_ptr<Track>> & tracks)29 MediaManagerError Demuxer::Create(const std::shared_ptr<AVSource>& source,
30 const std::map<Media::Plugins::MediaType, std::shared_ptr<Track>>& tracks)
31 {
32 DP_CHECK_ERROR_RETURN_RET_LOG(source == nullptr, ERROR_FAIL, "AVSource is nullptr.");
33
34 demuxer_ = AVDemuxerFactory::CreateWithSource(source);
35 DP_CHECK_ERROR_RETURN_RET_LOG(demuxer_ == nullptr, ERROR_FAIL, "Create demuxer failed.");
36
37 DP_DEBUG_LOG("tracks size: %{public}d", static_cast<int32_t>(tracks.size()));
38 for (const auto& [mediaType, trackPtr] : tracks) {
39 auto trackFormat = trackPtr->GetFormat();
40 SetTrackId(mediaType, trackFormat.trackId);
41 auto ret = SeletctTrackByID(trackFormat.trackId);
42 DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL,
43 "Select track by %{public}d failed, track type: %{public}d, ret: %{public}d.",
44 trackFormat.trackId, mediaType, ret);
45 }
46
47 return OK;
48 }
49
ReadStream(Media::Plugins::MediaType trackType,std::shared_ptr<AVBuffer> & sample)50 MediaManagerError Demuxer::ReadStream(Media::Plugins::MediaType trackType, std::shared_ptr<AVBuffer>& sample)
51 {
52 int32_t trackId = GetTrackId(trackType);
53 DP_CHECK_ERROR_RETURN_RET_LOG(trackId == INVALID_TRACK_ID, ERROR_FAIL,
54 "TrackType = %{public}d is not supported.", trackType);
55
56 auto ret = demuxer_->ReadSampleBuffer(trackId, sample);
57 DP_CHECK_ERROR_RETURN_RET_LOG(ret != static_cast<int32_t>(OK), ERROR_FAIL,
58 "Read sample failed, ret: %{public}d.", ret);
59 DP_CHECK_RETURN_RET_LOG(sample->flag_ == AVCODEC_BUFFER_FLAG_EOS, EOS,
60 "track(%{public}d) type: %{public}d is end.", trackId, trackType);
61 return OK;
62 }
63
GetTrackId(Media::Plugins::MediaType trackType)64 int32_t Demuxer::GetTrackId(Media::Plugins::MediaType trackType)
65 {
66 auto it = trackIds_.find(trackType);
67 return (it != trackIds_.end()) ? it->second : INVALID_TRACK_ID;
68 }
69
SetTrackId(Media::Plugins::MediaType trackType,int32_t trackId)70 void Demuxer::SetTrackId(Media::Plugins::MediaType trackType, int32_t trackId)
71 {
72 auto it = trackIds_.find(trackType);
73 if (it != trackIds_.end()) {
74 it->second = trackId;
75 } else {
76 DP_ERR_LOG("Unsupported media type: %{public}d", trackType);
77 }
78 }
79
SeekToTime(int64_t lastPts)80 MediaManagerError Demuxer::SeekToTime(int64_t lastPts)
81 {
82 DP_DEBUG_LOG("entered.");
83 DP_CHECK_ERROR_RETURN_RET_LOG(lastPts < 0, ERROR_FAIL, "Don't need to seek, demuxer from start.");
84 auto ret = demuxer_->SeekToTime(lastPts / 1000, SeekMode::SEEK_PREVIOUS_SYNC);
85 DP_CHECK_ERROR_RETURN_RET_LOG(ret != static_cast<int32_t>(OK), ERROR_FAIL, "Failed to seek, ret: %{public}d.", ret);
86 return OK;
87 }
88
SeletctTrackByID(int32_t trackId)89 MediaManagerError Demuxer::SeletctTrackByID(int32_t trackId)
90 {
91 DP_DEBUG_LOG("entered.");
92 auto ret = demuxer_->SelectTrackByID(trackId);
93 DP_CHECK_ERROR_RETURN_RET_LOG(ret != static_cast<int32_t>(OK), ERROR_FAIL,
94 "Select track by id failed, ret: %{public}d.", ret);
95 return OK;
96 }
97 } // namespace DeferredProcessing
98 } // namespace CameraStandard
99 } // namespace OHOS
100