1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <future>
20 #include <mutex>
21 #include <unordered_set>
22 
23 #include <fmt/format.h>
24 #include <rtc_base/time_utils.h>
25 
26 #include "common/libs/utils/result.h"
27 #include "host/frontend/webrtc/libdevice/video_frame_buffer.h"
28 
29 namespace cuttlefish {
30 
31 class ScreenshotHandler {
32  public:
33   ScreenshotHandler() = default;
34   ~ScreenshotHandler() = default;
35 
36   using SharedFrame = std::shared_ptr<webrtc_streaming::VideoFrameBuffer>;
37   using SharedFrameFuture = std::shared_future<SharedFrame>;
38   using SharedFramePromise = std::promise<SharedFrame>;
39 
40   Result<void> Screenshot(std::uint32_t display_number,
41                           const std::string& screenshot_path);
42 
43   void OnFrame(std::uint32_t display_number, SharedFrame& buffer);
44 
45  private:
46   std::mutex pending_screenshot_displays_mutex_;
47   // Promises used to share a frame for a given display from the display handler
48   // thread to the snapshot thread for processing.
49   std::unordered_map<std::uint32_t, SharedFramePromise>
50       pending_screenshot_displays_;
51 };
52 
53 }  // namespace cuttlefish
54