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