• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 SCREEN_CAPTURE_SERVICE_SERVER_BASE_H
17 #define SCREEN_CAPTURE_SERVICE_SERVER_BASE_H
18 
19 #include <mutex>
20 #include <cstdlib>
21 #include <thread>
22 #include <string>
23 #include <memory>
24 #include <atomic>
25 #include <queue>
26 #include <vector>
27 #include <chrono>
28 
29 #include "audio_capturer_wrapper.h"
30 #include "i_screen_capture_service.h"
31 #include "nocopyable.h"
32 #include "uri_helper.h"
33 #include "task_queue.h"
34 #include "accesstoken_kit.h"
35 #include "privacy_kit.h"
36 #include "ipc_skeleton.h"
37 #include "screen_capture.h"
38 #include "audio_capturer.h"
39 #include "nativetoken_kit.h"
40 #include "token_setproc.h"
41 #include "audio_info.h"
42 #include "surface.h"
43 #include "display_manager.h"
44 #include "screen_manager.h"
45 #include "i_recorder_service.h"
46 #include "recorder_server.h"
47 #include "notification_content.h"
48 #include "notification_helper.h"
49 #include "notification_request.h"
50 #include "notification_constant.h"
51 #include "notification_slot.h"
52 #ifdef SUPPORT_CALL
53 #include "incall_observer.h"
54 #endif
55 #include "account_observer.h"
56 #include "media_data_source.h"
57 #include "meta/meta.h"
58 #include "audio_stream_manager.h"
59 #include "screen_capture_monitor_server.h"
60 #include "json/json.h"
61 #include "tokenid_kit.h"
62 #include "window_manager.h"
63 #include "limitIdGenerator.h"
64 #include "system_ability_status_change_stub.h"
65 #include "i_input_device_listener.h"
66 #include "input_manager.h"
67 
68 namespace OHOS {
69 namespace Media {
70 using namespace Rosen;
71 using namespace AudioStandard;
72 using namespace OHOS::Notification;
73 using OHOS::Security::AccessToken::PrivacyKit;
74 
75 class ScreenCaptureServer;
76 
77 class NotificationSubscriber : public OHOS::Notification::NotificationLocalLiveViewSubscriber {
78 public:
79     void OnConnected() override;
80     void OnDisconnected() override;
81     void OnResponse(int32_t notificationId,
82         OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption) override;
83     void OnDied() override;
84 };
85 
86 enum VideoPermissionState : int32_t {
87     START_VIDEO = 0,
88     STOP_VIDEO = 1
89 };
90 
91 enum AVScreenCaptureState : int32_t {
92     CREATED = 0,
93     POPUP_WINDOW = 1,
94     STARTING = 2,
95     STARTED = 3,
96     STOPPED = 4
97 };
98 
99 enum AVScreenCaptureMixMode : int32_t {
100     MIX_MODE = 0,
101     MIC_MODE = 1,
102     INNER_MODE = 2,
103     INVAILD_MODE = 3
104 };
105 
106 enum AVScreenCaptureAvType : int8_t {
107     INVALID_TYPE = -1,
108     AUDIO_TYPE = 0,
109     VIDEO_TYPE = 1,
110     AV_TYPE = 2
111 };
112 
113 enum AVScreenCaptureDataMode : int8_t {
114     BUFFER_MODE = 0,
115     SUFFACE_MODE = 1,
116     FILE_MODE = 2
117 };
118 
119 enum StopReason: int8_t {
120     NORMAL_STOPPED = 0,
121     RECEIVE_USER_PRIVACY_AUTHORITY_FAILED = 1,
122     POST_START_SCREENCAPTURE_HANDLE_FAILURE = 2,
123     REQUEST_USER_PRIVACY_AUTHORITY_FAILED = 3,
124     STOP_REASON_INVALID = 4
125 };
126 
127 struct SurfaceBufferEntry {
SurfaceBufferEntrySurfaceBufferEntry128     SurfaceBufferEntry(sptr<OHOS::SurfaceBuffer> buf, int32_t fence, int64_t timeStamp, OHOS::Rect& damage)
129         : buffer(std::move(buf)), flushFence(fence), timeStamp(timeStamp), damageRect(damage) {}
130     ~SurfaceBufferEntry() noexcept = default;
131 
132     sptr<OHOS::SurfaceBuffer> buffer;
133     int32_t flushFence;
134     int64_t timeStamp = 0;
135     OHOS::Rect damageRect = {0, 0, 0, 0};
136 };
137 
138 struct StatisticalEventInfo {
139     int32_t errCode = 0;
140     std::string errMsg;
141     int32_t captureDuration = -1;
142     bool userAgree = false;
143     bool requireMic = false;
144     bool enableMic = false;
145     std::string videoResolution;
146     StopReason stopReason = StopReason::STOP_REASON_INVALID;
147     int32_t startLatency = -1;
148 };
149 
150 enum class SCBufferMessageType {
151     EXIT,
152     GET_BUFFER
153 };
154 
155 struct SCBufferMessage {
156     SCBufferMessageType type;
157     std::string text;
158 };
159 
160 class ScreenCapBufferConsumerListener : public IBufferConsumerListener {
161 public:
ScreenCapBufferConsumerListener(sptr<Surface> consumer,const std::shared_ptr<ScreenCaptureCallBack> & screenCaptureCb)162     ScreenCapBufferConsumerListener(
163         sptr<Surface> consumer, const std::shared_ptr<ScreenCaptureCallBack> &screenCaptureCb)
164         : consumer_(consumer), screenCaptureCb_(screenCaptureCb) {}
165     ~ScreenCapBufferConsumerListener();
166 
167     void OnBufferAvailable() override;
168     int32_t AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence, int64_t &timestamp,
169         OHOS::Rect &damage);
170     int32_t ReleaseVideoBuffer();
171     int32_t Release();
172     int32_t StartBufferThread();
173     void OnBufferAvailableAction();
174     void SurfaceBufferThreadRun();
175     void StopBufferThread();
176 
177 private:
178     int32_t ReleaseBuffer();
179     void ProcessVideoBufferCallBack();
180 
181 private:
182     std::mutex bufferAvailableWorkerMtx_;
183     std::condition_variable bufferAvailableWorkerCv_;
184     std::queue<SCBufferMessage> messageQueueSCB_;
185 
186     std::mutex mutex_;
187     sptr<OHOS::Surface> consumer_ = nullptr;
188     std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_ = nullptr;
189     std::atomic<bool> isSurfaceCbInThreadStopped_ {true};
190     std::thread* surfaceCbInThread_ = nullptr;
191 
192     std::mutex bufferMutex_;
193     std::condition_variable bufferCond_;
194     std::queue<std::unique_ptr<SurfaceBufferEntry>> availBuffers_;
195 
196     static constexpr uint64_t MAX_MESSAGE_QUEUE_SIZE = 5;
197     static constexpr uint32_t MAX_BUFFER_SIZE = 3;
198     static constexpr uint32_t OPERATION_TIMEOUT_IN_MS = 1000; // 1000ms
199 };
200 
201 #ifdef SUPPORT_CALL
202 class ScreenCaptureObserverCallBack : public InCallObserverCallBack, public AccountObserverCallBack {
203 #else
204 class ScreenCaptureObserverCallBack : public AccountObserverCallBack {
205 #endif
206 public:
207     explicit ScreenCaptureObserverCallBack(std::weak_ptr<ScreenCaptureServer> screenCaptureServer);
208     ~ScreenCaptureObserverCallBack();
209     bool StopAndRelease(AVScreenCaptureStateCode state) override;
210     bool NotifyStopAndRelease(AVScreenCaptureStateCode state) override;
211 #ifdef SUPPORT_CALL
212     bool TelCallStateUpdated(bool isInCall) override;
213     bool NotifyTelCallStateUpdated(bool isInCall) override;
214 #endif
215     void Release() override;
216 private:
217     std::weak_ptr<ScreenCaptureServer> screenCaptureServer_;
218     TaskQueue taskQueObserverCb_;
219     std::mutex mutex_;
220 };
221 
222 class AudioDataSource : public IAudioDataSource {
223 public:
AudioDataSource(AVScreenCaptureMixMode type,ScreenCaptureServer * screenCaptureServer)224     AudioDataSource(AVScreenCaptureMixMode type, ScreenCaptureServer* screenCaptureServer) : type_(type),
225         screenCaptureServer_(screenCaptureServer) {}
226 
227     int32_t ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length) override;
228     int32_t GetSize(int64_t &size) override;
229     int32_t RegisterAudioRendererEventListener(const int32_t clientPid,
230         const std::shared_ptr<AudioRendererStateChangeCallback> &callback);
231     int32_t UnregisterAudioRendererEventListener(const int32_t clientPid);
232     void SpeakerStateUpdate(
233         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
234     bool HasSpeakerStream(
235         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
236     void VoIPStateUpdate(
237         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
238     bool HasVoIPStream(
239         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
240     void SetAppPid(int32_t appid);
241     void SetAppName(std::string appName);
242     int32_t GetAppPid();
243     bool GetIsInVoIPCall();
244     bool GetSpeakerAliveStatus();
245 #ifdef SUPPORT_CALL
246     void TelCallAudioStateUpdate(
247         const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
248 #endif
249 
250 private:
251     int32_t MixModeBufferWrite(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
252         std::shared_ptr<AudioBuffer> &micAudioBuffer, std::shared_ptr<AVMemory> &bufferMem);
253     int32_t appPid_ { 0 };
254     std::string appName_;
255     bool speakerAliveStatus_ = true;
256     std::atomic<bool> isInVoIPCall_ = false;
257     std::mutex voipStatusChangeMutex_;
258 
259     void MixAudio(char** srcData, char* mixData, int channels, int bufferSize);
260 
261     AVScreenCaptureMixMode type_;
262     ScreenCaptureServer* screenCaptureServer_;
263 
264     static constexpr int32_t ADS_LOG_SKIP_NUM = 1000;
265 };
266 
267 class InputDeviceInfo : public MMI::InputDevice {
268 public:
269     InputDeviceInfo() = default;
270 
271     virtual ~InputDeviceInfo() = default;
272 };
273 
274 class MouseChangeListener : public MMI::IInputDeviceListener {
275 public:
276     explicit MouseChangeListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer);
277     ~MouseChangeListener() = default;
278     int32_t GetDeviceInfo(int32_t deviceId, std::shared_ptr<InputDeviceInfo> deviceInfo);
279 
280     void OnDeviceAdded(int32_t deviceId, const std::string &type) override;
281     void OnDeviceRemoved(int32_t deviceId, const std::string &type) override;
282 
283 private:
284     std::weak_ptr<ScreenCaptureServer> screenCaptureServer_;
285 };
286 
287 class MMISystemAbilityListener : public SystemAbilityStatusChangeStub {
288 public:
289     explicit MMISystemAbilityListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer);
290     ~MMISystemAbilityListener() = default;
291     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
292     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
293 
294 private:
295     std::weak_ptr<ScreenCaptureServer> screenCaptureServer_;
296 };
297 
298 class PrivateWindowListenerInScreenCapture : public DisplayManager::IPrivateWindowListener {
299 public:
300     explicit PrivateWindowListenerInScreenCapture(std::weak_ptr<ScreenCaptureServer> screenCaptureServer);
301     ~PrivateWindowListenerInScreenCapture() = default;
302     void OnPrivateWindow(bool hasPrivate) override;
303 
304 private:
305     std::weak_ptr<ScreenCaptureServer> screenCaptureServer_;
306 };
307 
308 class ScreenRendererAudioStateChangeCallback : public AudioRendererStateChangeCallback {
309 public:
310     void OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos);
311     void SetAudioSource(std::shared_ptr<AudioDataSource> audioSource);
312     void SetAppName(std::string appName);
313 private:
314     std::shared_ptr<AudioDataSource> audioSource_ = nullptr;
315     std::string appName_;
316 };
317 
318 class ScreenConnectListenerForSC : public Rosen::ScreenManager::IScreenListener {
319 public:
ScreenConnectListenerForSC(uint64_t screenId,std::weak_ptr<ScreenCaptureServer> screenCaptureServer)320     explicit ScreenConnectListenerForSC(uint64_t screenId, std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
321         : screenId_(screenId), screenCaptureServer_(screenCaptureServer) {}
322     void OnConnect(Rosen::ScreenId screenId);
323     void OnDisconnect(Rosen::ScreenId screenId);
324     void OnChange(Rosen::ScreenId screenId);
325 private:
326     uint64_t screenId_ = SCREEN_ID_INVALID;
327     std::weak_ptr<ScreenCaptureServer> screenCaptureServer_;
328 };
329 } // namespace Media
330 } // namespace OHOS
331 #endif // SCREEN_CAPTURE_SERVICE_SERVER_BASE_H
332