• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2025 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 #define HST_LOG_TAG "MediaSyncSink"
17 #include "media_synchronous_sink.h"
18 #include "common/log.h"
19 #include "plugin/plugin_time.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "MediaSynchronousSink" };
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 namespace Pipeline {
Init()28 void MediaSynchronousSink::Init()
29 {
30     auto syncCenter = syncCenter_.lock();
31     if (syncCenter) {
32         syncCenter->AddSynchronizer(this);
33     }
34 }
35 
~MediaSynchronousSink()36 MediaSynchronousSink::~MediaSynchronousSink()
37 {
38     MEDIA_LOG_D("~MediaSynchronousSink enter .");
39     auto syncCenter = syncCenter_.lock();
40     if (syncCenter) {
41         syncCenter->RemoveSynchronizer(this);
42     }
43 }
44 
WaitAllPrerolled(bool shouldWait)45 void MediaSynchronousSink::WaitAllPrerolled(bool shouldWait)
46 {
47     waitForPrerolled_ = shouldWait;
48 }
49 
GetPriority()50 int8_t MediaSynchronousSink::GetPriority()
51 {
52     return syncerPriority_;
53 }
54 
ResetPrerollReported()55 void MediaSynchronousSink::ResetPrerollReported()
56 {
57     hasReportedPreroll_ = false;
58 }
59 
WriteToPluginRefTimeSync(const std::shared_ptr<OHOS::Media::AVBuffer> & buffer)60 void MediaSynchronousSink::WriteToPluginRefTimeSync(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer)
61 {
62     if (!hasReportedPreroll_) {
63         auto syncCenter = syncCenter_.lock();
64         if (syncCenter) {
65             syncCenter->ReportPrerolled(this);
66         }
67         hasReportedPreroll_ = true;
68     }
69     if (waitForPrerolled_) {
70         OHOS::Media::AutoLock lock(prerollMutex_);
71         if (!prerollCond_.WaitFor(lock, Plugins::HstTime2Ms(waitPrerolledTimeout_),
72                                   [&] { return waitForPrerolled_.load(); })) {
73             MEDIA_LOG_W("Wait for preroll timeout");
74         }
75         // no need to wait for preroll next time
76         waitForPrerolled_ = false;
77     }
78     DoSyncWrite(buffer);
79 }
80 
NotifyAllPrerolled()81 void MediaSynchronousSink::NotifyAllPrerolled()
82 {
83     OHOS::Media::AutoLock lock(prerollMutex_);
84     waitForPrerolled_ = false;
85     prerollCond_.NotifyAll();
86 }
87 
OnInterrupted(bool isInterruptNeeded)88 void MediaSynchronousSink::OnInterrupted(bool isInterruptNeeded)
89 {
90     MEDIA_LOG_I("MediaSynchronousSink onInterrupted %{public}d", isInterruptNeeded);
91     OHOS::Media::AutoLock lock(prerollMutex_);
92     isInterruptNeeded_ = isInterruptNeeded;
93     waitForPrerolled_ = false;
94     prerollCond_.NotifyAll();
95 }
96 
UpdateMediaTimeRange(const std::shared_ptr<Meta> & meta)97 void MediaSynchronousSink::UpdateMediaTimeRange(const std::shared_ptr<Meta>& meta)
98 {
99     FALSE_RETURN_MSG(meta != nullptr, "meta is null!");
100     int64_t trackStartTime = 0;
101     meta->GetData(Tag::MEDIA_START_TIME, trackStartTime);
102     int32_t trackId = 0;
103     (void)(meta->GetData(Tag::REGULAR_TRACK_ID, trackId));
104     auto syncCenter = syncCenter_.lock();
105     if (syncCenter) {
106         syncCenter->SetMediaTimeRangeStart(trackStartTime, trackId, this);
107     }
108     int64_t trackDuration = 0;
109     if (meta->GetData(Tag::MEDIA_DURATION, trackDuration)) {
110         if (syncCenter) {
111             syncCenter->SetMediaTimeRangeEnd(trackDuration + trackStartTime, trackId, this);
112         }
113     } else {
114         MEDIA_LOG_W("Get duration failed");
115         if (syncCenter) {
116             syncCenter->SetMediaTimeRangeEnd(INT64_MAX, trackId, this);
117         }
118     }
119 }
120 } // namespace Pipeline
121 } // namespace Media
122 } // namespace OHOS