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)23inline 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()30FrameSource::~FrameSource() 31 { 32 SHARING_LOGD("trace."); 33 std::unique_lock<std::shared_mutex> alock(audioDstsMutex_); 34 for (auto &audioDst : audioDsts_) { 35 audioDst->UnsetAudioSource(); 36 } 37 alock.unlock(); 38 audioDsts_.clear(); 39 }; 40 AddAudioDestination(std::shared_ptr<FrameDestination> dst)41void FrameSource::AddAudioDestination(std::shared_ptr<FrameDestination> dst) 42 { 43 SHARING_LOGD("trace."); 44 RETURN_IF_NULL(dst); 45 std::unique_lock<std::shared_mutex> lock(audioDstsMutex_); 46 audioDsts_.push_back(dst); 47 lock.unlock(); 48 dst->SetAudioSource(shared_from_this()); 49 } 50 RemoveAudioDestination(std::shared_ptr<FrameDestination> dst)51void FrameSource::RemoveAudioDestination(std::shared_ptr<FrameDestination> dst) 52 { 53 SHARING_LOGD("trace."); 54 RETURN_IF_NULL(dst); 55 std::unique_lock<std::shared_mutex> lock(audioDstsMutex_); 56 audioDsts_.remove(dst); 57 lock.unlock(); 58 dst->UnsetAudioSource(); 59 } 60 DeliverFrame(const Frame::Ptr & frame)61void FrameSource::DeliverFrame(const Frame::Ptr &frame) 62 { 63 RETURN_IF_NULL(frame); 64 if (IsAudioFrame(frame)) { 65 std::shared_lock<std::shared_mutex> lock(audioDstsMutex_); 66 for (auto &audioDst_ : audioDsts_) { 67 audioDst_->OnFrame(frame); 68 } 69 } else { 70 SHARING_LOGE("unknown frame Type!"); 71 } 72 } 73 SetAudioSource(std::shared_ptr<FrameSource> src)74void FrameDestination::SetAudioSource(std::shared_ptr<FrameSource> src) 75 { 76 SHARING_LOGD("trace."); 77 std::unique_lock<std::shared_mutex> lock(audioSrcMutex_); 78 audioSrc_ = src; 79 } 80 UnsetAudioSource()81void FrameDestination::UnsetAudioSource() 82 { 83 SHARING_LOGD("trace."); 84 std::unique_lock<std::shared_mutex> lock(audioSrcMutex_); 85 audioSrc_.reset(); 86 } 87 } // namespace Sharing 88 } // namespace OHOS