1 /*
2  * Copyright (c) 2020-2021 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 #ifndef PLAYER_SYNC_H
17 #define PLAYER_SYNC_H
18 
19 #include <pthread.h>
20 #include "player_define.h"
21 
22 namespace OHOS {
23 namespace Media {
24 typedef enum SyncRet {
25     SYNC_RET_PLAY,
26     SYNC_RET_REPEAT,
27     SYNC_RET_DROP,
28     SYNC_RET_BUTT
29 } SyncRet;
30 
31 typedef enum SyncChn {
32     SYNC_CHN_VID,
33     SYNC_CHN_AUD,
34     SYNC_CHN_SUB,
35     SYNC_CHN_BUTT
36 } SyncChn;
37 
38 typedef struct {
39     int64_t firstVidPts;
40     int64_t lastVidPts;
41     int64_t firstAudPts;
42     int64_t lastAudPts;
43     int64_t localTime;
44     int64_t diffTime;
45 } PlayerSyncStatus;
46 
47 typedef enum {
48     PLAYER_SYNC_REF_VID,
49     PLAYER_SYNC_REF_AUD,
50     PLAYER_SYNC_REF_NONE,
51     PLAYER_SYNC_REF_BUTT
52 } PlayerSyncRefType;
53 
54 
55 typedef struct {
56     /* Plus time range during video synchronization */
57     uint32_t vidPlusTime;
58     /* Negative time range during video synchronization */
59     uint32_t vidNegativeTime;
60 } PlayerSyncRegion;
61 
62 typedef struct {
63     PlayerSyncRefType refType;
64     PlayerSyncRegion syncStartRegion;
65     PlayerSyncRegion syncStopRegion;
66     bool isQuickOutput;
67 } PlayerSyncAttr;
68 
69 
70 class PlayerSync {
71 public:
72     PlayerSync();
73 
74     virtual ~PlayerSync();
75     int32_t Init(void);
76     int32_t Deinit(void);
77     int32_t ProcVidFrame(int64_t pts, SyncRet &result);
78     int32_t ProcAudFrame(int64_t pts, SyncRet &result);
79     int32_t Reset(SyncChn syncChn);
80     int32_t Stop(SyncChn syncChn);
81     int32_t Start(SyncChn syncChn);
82     int32_t GetStatus(PlaySyncStatus &info);
83     int32_t SetAttr(PlayerSyncAttr &syncAttr);
84     int32_t GetAttr(PlayerSyncAttr &syncAttr);
85     int32_t SetSpeed(float speed, TplayDirect  tplayDirect);
86     int32_t Resume();
87 
88 private:
89     SyncRet CheckAVDiff(int64_t latenessMs, uint32_t &continueLost);
90     int32_t TPlayProcess(int64_t timestampUs, SyncRet &syncRet);
91     void UpdateCurTimeWithAudio(int64_t& nowUs);
92     SyncRet OnVideoFirstFrame(int64_t ptsUs);
93 
94 private:
95     PlayerSyncAttr syncAttr_;
96     pthread_mutex_t audSyncLock_;
97     pthread_mutex_t vidSyncLock_;
98     bool isVidEnable_;
99     bool isAudEnable_;
100     bool isFristVidFrame_;
101     int64_t vidTimeSourceDelta_;
102     int64_t lastVideoTsUs_;  // pts in us
103     int64_t firstVidFrameTs_;
104     int64_t frstVidFrameTime_;
105     uint32_t continousVidLost_;  // continous video frame lost count
106     bool  isFristAudFrame_;
107     int64_t audTimeSourceDelta_;
108     int64_t lastAudioTsUs_;      // pts in us
109     int64_t lastAudioRealTsUs_;  // clock time in us
110     int64_t firstAudFrameTs_;
111     int64_t firstAudFrameTime_;
112     bool  isInTrickPlayMode_;
113     float speed_;
114     TplayDirect  tplayDirect_;
115     int64_t diffAvRenderTime_;
116     bool isInited_;
117 };
118 };
119 };
120 #endif /* PLAYER_SYNC_H */
121