1 /* 2 * Copyright (c) 2025 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 "video_audio_sync.h" 17 #include "common/media_log.h" 18 19 namespace OHOS { 20 namespace Sharing { 21 VideoAudioSync()22VideoAudioSync::VideoAudioSync() 23 { 24 SHARING_LOGD("Construct in"); 25 } 26 ~VideoAudioSync()27VideoAudioSync::~VideoAudioSync() 28 { 29 SHARING_LOGD("Destruct in"); 30 if (audioPlayController_) { 31 audioPlayController_.reset(); 32 } 33 } 34 IsNeedDrop(int64_t videoTimestamp)35bool VideoAudioSync::IsNeedDrop(int64_t videoTimestamp) 36 { 37 if (isFirstFrame_) { 38 isFirstFrame_ = false; 39 continueDropCount_ = 0; 40 return false; 41 } 42 return ProcessAVSyncStrategy(videoTimestamp); 43 } 44 ProcessAVSyncStrategy(int64_t videoTimestamp)45bool VideoAudioSync::ProcessAVSyncStrategy(int64_t videoTimestamp) 46 { 47 int64_t audioPts = 0; 48 if (audioPlayController_ == nullptr) { 49 return false; 50 } 51 52 audioPts = audioPlayController_->GetAudioDecoderTimestamp(); 53 if (audioPts == 0) { 54 continueDropCount_ = 0; 55 return false; 56 } 57 58 int64_t earlyUs = videoTimestamp - audioPts; 59 if (earlyUs < VIDEO_TOO_LATE_US) { 60 SHARING_LOGE("Video is too late, something may wrong!"); 61 } else if (earlyUs < VIDEO_LATE_US && continueDropCount_ <= 0) { 62 ++continueDropCount_; 63 return true; 64 } else if (earlyUs > AUDIO_TOO_LATE_US) { 65 SHARING_LOGE("Audio is too late, something may wrong!"); 66 std::this_thread::sleep_for(std::chrono::microseconds(DROP_ONE_FRAME_TIME)); 67 } else if (earlyUs > AUDIO_LATE_US) { 68 audioPlayController_->DropOneFrame(); 69 } 70 71 continueDropCount_ = 0; 72 return false; 73 } 74 SetAudioPlayController(std::shared_ptr<AudioPlayController> audioPlayController)75void VideoAudioSync::SetAudioPlayController(std::shared_ptr<AudioPlayController> audioPlayController) 76 { 77 audioPlayController_ = audioPlayController; 78 } 79 80 } // namespace Sharing 81 } // namespace OHOS