• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023-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 OHOS {
22 namespace Media {
23 namespace Pipeline {
Init()24 void MediaSynchronousSink::Init()
25 {
26     auto syncCenter = syncCenter_.lock();
27     if (syncCenter) {
28         syncCenter->AddSynchronizer(this);
29     }
30 }
31 
~MediaSynchronousSink()32 MediaSynchronousSink::~MediaSynchronousSink()
33 {
34     MEDIA_LOG_I("~MediaSynchronousSink enter .");
35     auto syncCenter = syncCenter_.lock();
36     if (syncCenter) {
37         syncCenter->RemoveSynchronizer(this);
38     }
39 }
40 
WaitAllPrerolled(bool shouldWait)41 void MediaSynchronousSink::WaitAllPrerolled(bool shouldWait)
42 {
43     waitForPrerolled_ = shouldWait;
44 }
45 
GetPriority()46 int8_t MediaSynchronousSink::GetPriority()
47 {
48     return syncerPriority_;
49 }
50 
ResetPrerollReported()51 void MediaSynchronousSink::ResetPrerollReported()
52 {
53     hasReportedPreroll_ = false;
54 }
55 
WriteToPluginRefTimeSync(const std::shared_ptr<OHOS::Media::AVBuffer> & buffer)56 void MediaSynchronousSink::WriteToPluginRefTimeSync(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer)
57 {
58     if (!hasReportedPreroll_) {
59         auto syncCenter = syncCenter_.lock();
60         if (syncCenter) {
61             syncCenter->ReportPrerolled(this);
62         }
63         hasReportedPreroll_ = true;
64     }
65     if (waitForPrerolled_) {
66         OHOS::Media::AutoLock lock(prerollMutex_);
67         if (!prerollCond_.WaitFor(lock, Plugins::HstTime2Ms(waitPrerolledTimeout_),
68                                   [&] { return waitForPrerolled_.load(); })) {
69             MEDIA_LOG_W("Wait for preroll timeout");
70         }
71         // no need to wait for preroll next time
72         waitForPrerolled_ = false;
73     }
74     DoSyncWrite(buffer);
75 }
76 
NotifyAllPrerolled()77 void MediaSynchronousSink::NotifyAllPrerolled()
78 {
79     OHOS::Media::AutoLock lock(prerollMutex_);
80     waitForPrerolled_ = false;
81     prerollCond_.NotifyAll();
82 }
83 
UpdateMediaTimeRange(const std::shared_ptr<Meta> & meta)84 void MediaSynchronousSink::UpdateMediaTimeRange(const std::shared_ptr<Meta>& meta)
85 {
86     int64_t trackStartTime = 0;
87     meta->GetData(Tag::MEDIA_START_TIME, trackStartTime);
88     uint32_t trackId = 0;
89     FALSE_LOG(meta->GetData(Tag::REGULAR_TRACK_ID, trackId));
90     auto syncCenter = syncCenter_.lock();
91     if (syncCenter) {
92         syncCenter->SetMediaTimeRangeStart(trackStartTime, trackId);
93     }
94     int64_t trackDuration = 0;
95     if (meta->GetData(Tag::MEDIA_DURATION, trackDuration)) {
96         if (syncCenter) {
97             syncCenter->SetMediaTimeRangeEnd(trackDuration + trackStartTime, trackId);
98         }
99     } else {
100         MEDIA_LOG_W("Get duration failed");
101         if (syncCenter) {
102             syncCenter->SetMediaTimeRangeEnd(INT64_MAX, trackId);
103         }
104     }
105 }
106 } // namespace Pipeline
107 } // namespace Media
108 } // namespace OHOS