• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "media_frame_pipeline.h"
17 #include "common/common_macro.h"
18 #include "frame.h"
19 #include "media_log.h"
20 
21 namespace OHOS {
22 namespace Sharing {
IsAudioFrame(const Frame::Ptr & frame)23 inline bool IsAudioFrame(const Frame::Ptr &frame)
24 {
25     RETURN_FALSE_IF_NULL(frame);
26     return frame->GetCodecId() == CODEC_G711A || frame->GetCodecId() == CODEC_G711U ||
27            frame->GetCodecId() == CODEC_AAC || frame->GetCodecId() == CODEC_PCM;
28 }
29 
~FrameSource()30 FrameSource::~FrameSource()
31 {
32     SHARING_LOGD("trace.");
33     std::unique_lock<std::shared_mutex> alock(audioDstsMutex_);
34     for (auto &audioDst : audioDsts_) {
35         if (audioDst != nullptr) {
36             audioDst->UnsetAudioSource();
37         }
38     }
39     alock.unlock();
40     audioDsts_.clear();
41 };
42 
AddAudioDestination(std::shared_ptr<FrameDestination> dst)43 void FrameSource::AddAudioDestination(std::shared_ptr<FrameDestination> dst)
44 {
45     SHARING_LOGD("trace.");
46     RETURN_IF_NULL(dst);
47     std::unique_lock<std::shared_mutex> lock(audioDstsMutex_);
48     audioDsts_.push_back(dst);
49     lock.unlock();
50     dst->SetAudioSource(shared_from_this());
51 }
52 
RemoveAudioDestination(std::shared_ptr<FrameDestination> dst)53 void FrameSource::RemoveAudioDestination(std::shared_ptr<FrameDestination> dst)
54 {
55     SHARING_LOGD("trace.");
56     RETURN_IF_NULL(dst);
57     std::unique_lock<std::shared_mutex> lock(audioDstsMutex_);
58     audioDsts_.remove(dst);
59     lock.unlock();
60     dst->UnsetAudioSource();
61 }
62 
DeliverFrame(const Frame::Ptr & frame)63 void FrameSource::DeliverFrame(const Frame::Ptr &frame)
64 {
65     RETURN_IF_NULL(frame);
66     if (IsAudioFrame(frame)) {
67         std::shared_lock<std::shared_mutex> lock(audioDstsMutex_);
68         for (auto &audioDst_ : audioDsts_) {
69             audioDst_->OnFrame(frame);
70         }
71     } else {
72         SHARING_LOGE("unknown frame Type!");
73     }
74 }
75 
SetAudioSource(std::shared_ptr<FrameSource> src)76 void FrameDestination::SetAudioSource(std::shared_ptr<FrameSource> src)
77 {
78     SHARING_LOGD("trace.");
79     std::unique_lock<std::shared_mutex> lock(audioSrcMutex_);
80     audioSrc_ = src;
81 }
82 
UnsetAudioSource()83 void FrameDestination::UnsetAudioSource()
84 {
85     SHARING_LOGD("trace.");
86     std::unique_lock<std::shared_mutex> lock(audioSrcMutex_);
87     audioSrc_.reset();
88 }
89 } // namespace Sharing
90 } // namespace OHOS