xref: /aosp_15_r20/external/openscreen/cast/standalone_sender/looping_file_sender.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STANDALONE_SENDER_LOOPING_FILE_SENDER_H_
6 #define CAST_STANDALONE_SENDER_LOOPING_FILE_SENDER_H_
7 
8 #include <algorithm>
9 #include <memory>
10 #include <string>
11 
12 #include "cast/standalone_sender/connection_settings.h"
13 #include "cast/standalone_sender/constants.h"
14 #include "cast/standalone_sender/simulated_capturer.h"
15 #include "cast/standalone_sender/streaming_opus_encoder.h"
16 #include "cast/standalone_sender/streaming_video_encoder.h"
17 #include "cast/streaming/sender_session.h"
18 
19 namespace openscreen {
20 namespace cast {
21 
22 // Plays the media file at a given path over and over again, transcoding and
23 // streaming its audio/video.
24 class LoopingFileSender final : public SimulatedAudioCapturer::Client,
25                                 public SimulatedVideoCapturer::Client {
26  public:
27   using ShutdownCallback = std::function<void()>;
28 
29   LoopingFileSender(Environment* environment,
30                     ConnectionSettings settings,
31                     const SenderSession* session,
32                     SenderSession::ConfiguredSenders senders,
33                     ShutdownCallback shutdown_callback);
34 
35   ~LoopingFileSender() final;
36 
37   void SetPlaybackRate(double rate);
38 
39  private:
40   void UpdateEncoderBitrates();
41   void ControlForNetworkCongestion();
42   void SendFileAgain();
43 
44   // SimulatedAudioCapturer overrides.
45   void OnAudioData(const float* interleaved_samples,
46                    int num_samples,
47                    Clock::time_point capture_time) final;
48 
49   // SimulatedVideoCapturer overrides;
50   void OnVideoFrame(const AVFrame& av_frame,
51                     Clock::time_point capture_time) final;
52 
53   void UpdateStatusOnConsole();
54 
55   // SimulatedCapturer::Client overrides.
56   void OnEndOfFile(SimulatedCapturer* capturer) final;
57   void OnError(SimulatedCapturer* capturer, std::string message) final;
58 
59   const char* ToTrackName(SimulatedCapturer* capturer) const;
60 
61   std::unique_ptr<StreamingVideoEncoder> CreateVideoEncoder(
62       const StreamingVideoEncoder::Parameters& params,
63       TaskRunner* task_runner,
64       Sender* sender);
65 
66   // Holds the required injected dependencies (clock, task runner) used for Cast
67   // Streaming, and owns the UDP socket over which all communications occur with
68   // the remote's Receivers.
69   Environment* const env_;
70 
71   // The connection settings used for this session.
72   const ConnectionSettings settings_;
73 
74   // Session to query for bandwidth information.
75   const SenderSession* session_;
76 
77   // Callback for tearing down the sender process.
78   ShutdownCallback shutdown_callback_;
79 
80   int bandwidth_estimate_ = 0;
81   int bandwidth_being_utilized_;
82 
83   StreamingOpusEncoder audio_encoder_;
84   std::unique_ptr<StreamingVideoEncoder> video_encoder_;
85 
86   int num_capturers_running_ = 0;
87   Clock::time_point capture_start_time_{};
88   Clock::time_point latest_frame_time_{};
89   absl::optional<SimulatedAudioCapturer> audio_capturer_;
90   absl::optional<SimulatedVideoCapturer> video_capturer_;
91 
92   Alarm next_task_;
93   Alarm console_update_task_;
94 };
95 
96 }  // namespace cast
97 }  // namespace openscreen
98 
99 #endif  // CAST_STANDALONE_SENDER_LOOPING_FILE_SENDER_H_
100