1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_VIDEO_SEND_STREAM_H_
12 #define WEBRTC_VIDEO_SEND_STREAM_H_
13 
14 #include <map>
15 #include <string>
16 
17 #include "webrtc/common_types.h"
18 #include "webrtc/config.h"
19 #include "webrtc/frame_callback.h"
20 #include "webrtc/stream.h"
21 #include "webrtc/transport.h"
22 #include "webrtc/video_renderer.h"
23 
24 namespace webrtc {
25 
26 class LoadObserver;
27 class VideoEncoder;
28 
29 class EncodingTimeObserver {
30  public:
~EncodingTimeObserver()31   virtual ~EncodingTimeObserver() {}
32 
33   virtual void OnReportEncodedTime(int64_t ntp_time_ms, int encode_time_ms) = 0;
34 };
35 
36 // Class to deliver captured frame to the video send stream.
37 class VideoCaptureInput {
38  public:
39   // These methods do not lock internally and must be called sequentially.
40   // If your application switches input sources synchronization must be done
41   // externally to make sure that any old frames are not delivered concurrently.
42   virtual void IncomingCapturedFrame(const VideoFrame& video_frame) = 0;
43 
44  protected:
~VideoCaptureInput()45   virtual ~VideoCaptureInput() {}
46 };
47 
48 class VideoSendStream : public SendStream {
49  public:
50   struct StreamStats {
51     FrameCounts frame_counts;
52     int width = 0;
53     int height = 0;
54     // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
55     int total_bitrate_bps = 0;
56     int retransmit_bitrate_bps = 0;
57     int avg_delay_ms = 0;
58     int max_delay_ms = 0;
59     StreamDataCounters rtp_stats;
60     RtcpPacketTypeCounter rtcp_packet_type_counts;
61     RtcpStatistics rtcp_stats;
62   };
63 
64   struct Stats {
65     std::string encoder_implementation_name = "unknown";
66     int input_frame_rate = 0;
67     int encode_frame_rate = 0;
68     int avg_encode_time_ms = 0;
69     int encode_usage_percent = 0;
70     int target_media_bitrate_bps = 0;
71     int media_bitrate_bps = 0;
72     bool suspended = false;
73     bool bw_limited_resolution = false;
74     std::map<uint32_t, StreamStats> substreams;
75   };
76 
77   struct Config {
78     Config() = delete;
ConfigConfig79     explicit Config(Transport* send_transport)
80         : send_transport(send_transport) {}
81 
82     std::string ToString() const;
83 
84     struct EncoderSettings {
85       std::string ToString() const;
86 
87       std::string payload_name;
88       int payload_type = -1;
89 
90       // TODO(sophiechang): Delete this field when no one is using internal
91       // sources anymore.
92       bool internal_source = false;
93 
94       // Uninitialized VideoEncoder instance to be used for encoding. Will be
95       // initialized from inside the VideoSendStream.
96       VideoEncoder* encoder = nullptr;
97     } encoder_settings;
98 
99     static const size_t kDefaultMaxPacketSize = 1500 - 40;  // TCP over IPv4.
100     struct Rtp {
101       std::string ToString() const;
102 
103       std::vector<uint32_t> ssrcs;
104 
105       // See RtcpMode for description.
106       RtcpMode rtcp_mode = RtcpMode::kCompound;
107 
108       // Max RTP packet size delivered to send transport from VideoEngine.
109       size_t max_packet_size = kDefaultMaxPacketSize;
110 
111       // RTP header extensions to use for this send stream.
112       std::vector<RtpExtension> extensions;
113 
114       // See NackConfig for description.
115       NackConfig nack;
116 
117       // See FecConfig for description.
118       FecConfig fec;
119 
120       // Settings for RTP retransmission payload format, see RFC 4588 for
121       // details.
122       struct Rtx {
123         std::string ToString() const;
124         // SSRCs to use for the RTX streams.
125         std::vector<uint32_t> ssrcs;
126 
127         // Payload type to use for the RTX stream.
128         int payload_type = -1;
129       } rtx;
130 
131       // RTCP CNAME, see RFC 3550.
132       std::string c_name;
133     } rtp;
134 
135     // Transport for outgoing packets.
136     Transport* send_transport = nullptr;
137 
138     // Callback for overuse and normal usage based on the jitter of incoming
139     // captured frames. 'nullptr' disables the callback.
140     LoadObserver* overuse_callback = nullptr;
141 
142     // Called for each I420 frame before encoding the frame. Can be used for
143     // effects, snapshots etc. 'nullptr' disables the callback.
144     I420FrameCallback* pre_encode_callback = nullptr;
145 
146     // Called for each encoded frame, e.g. used for file storage. 'nullptr'
147     // disables the callback.
148     EncodedFrameObserver* post_encode_callback = nullptr;
149 
150     // Renderer for local preview. The local renderer will be called even if
151     // sending hasn't started. 'nullptr' disables local rendering.
152     VideoRenderer* local_renderer = nullptr;
153 
154     // Expected delay needed by the renderer, i.e. the frame will be delivered
155     // this many milliseconds, if possible, earlier than expected render time.
156     // Only valid if |local_renderer| is set.
157     int render_delay_ms = 0;
158 
159     // Target delay in milliseconds. A positive value indicates this stream is
160     // used for streaming instead of a real-time call.
161     int target_delay_ms = 0;
162 
163     // True if the stream should be suspended when the available bitrate fall
164     // below the minimum configured bitrate. If this variable is false, the
165     // stream may send at a rate higher than the estimated available bitrate.
166     bool suspend_below_min_bitrate = false;
167 
168     // Called for each encoded frame. Passes the total time spent on encoding.
169     // TODO(ivica): Consolidate with post_encode_callback:
170     // https://code.google.com/p/webrtc/issues/detail?id=5042
171     EncodingTimeObserver* encoding_time_observer = nullptr;
172   };
173 
174   // Gets interface used to insert captured frames. Valid as long as the
175   // VideoSendStream is valid.
176   virtual VideoCaptureInput* Input() = 0;
177 
178   // Set which streams to send. Must have at least as many SSRCs as configured
179   // in the config. Encoder settings are passed on to the encoder instance along
180   // with the VideoStream settings.
181   virtual bool ReconfigureVideoEncoder(const VideoEncoderConfig& config) = 0;
182 
183   virtual Stats GetStats() = 0;
184 };
185 
186 }  // namespace webrtc
187 
188 #endif  // WEBRTC_VIDEO_SEND_STREAM_H_
189