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 #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
UpdateMediaTimeRange(const std::shared_ptr<Meta> & meta)88 void MediaSynchronousSink::UpdateMediaTimeRange(const std::shared_ptr<Meta>& meta)
89 {
90 FALSE_RETURN_MSG(meta != nullptr, "meta is null!");
91 int64_t trackStartTime = 0;
92 meta->GetData(Tag::MEDIA_START_TIME, trackStartTime);
93 uint32_t trackId = 0;
94 FALSE_LOG(meta->GetData(Tag::REGULAR_TRACK_ID, trackId));
95 auto syncCenter = syncCenter_.lock();
96 if (syncCenter) {
97 syncCenter->SetMediaTimeRangeStart(trackStartTime, trackId, this);
98 }
99 int64_t trackDuration = 0;
100 if (meta->GetData(Tag::MEDIA_DURATION, trackDuration)) {
101 if (syncCenter) {
102 syncCenter->SetMediaTimeRangeEnd(trackDuration + trackStartTime, trackId, this);
103 }
104 } else {
105 MEDIA_LOG_W("Get duration failed");
106 if (syncCenter) {
107 syncCenter->SetMediaTimeRangeEnd(INT64_MAX, trackId, this);
108 }
109 }
110 }
111 } // namespace Pipeline
112 } // namespace Media
113 } // namespace OHOS