blob: 74e9d5a5592af5d0868ae40d9f49278c2bbac3d2 [file] [log] [blame]
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +00001/*
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
mflodman@webrtc.orgb429e512013-12-18 09:46:22 +000011#ifndef WEBRTC_VIDEO_SEND_STREAM_H_
12#define WEBRTC_VIDEO_SEND_STREAM_H_
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000013
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000014#include <map>
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000015#include <string>
palmkviste75f2042016-09-28 06:19:48 -070016#include <utility>
perkj26091b12016-09-01 01:17:40 -070017#include <vector>
Pera48ddb72016-09-29 11:48:50 +020018#include <utility>
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000019
palmkviste75f2042016-09-28 06:19:48 -070020#include "webrtc/base/platform_file.h"
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000021#include "webrtc/common_types.h"
pbosa96b60b2016-04-18 21:12:48 -070022#include "webrtc/common_video/include/frame_callback.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000023#include "webrtc/config.h"
nissed30a1112016-04-18 05:15:22 -070024#include "webrtc/media/base/videosinkinterface.h"
perkja49cbd32016-09-16 07:53:41 -070025#include "webrtc/media/base/videosourceinterface.h"
solenberg4fbae2b2015-08-28 04:07:10 -070026#include "webrtc/transport.h"
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000027
28namespace webrtc {
29
30class VideoEncoder;
31
pbos1ba8d392016-05-01 20:18:34 -070032class VideoSendStream {
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000033 public:
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000034 struct StreamStats {
asapersson2e5cfcd2016-08-11 08:41:18 -070035 std::string ToString() const;
36
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000037 FrameCounts frame_counts;
asapersson2e5cfcd2016-08-11 08:41:18 -070038 bool is_rtx = false;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000039 int width = 0;
40 int height = 0;
41 // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
42 int total_bitrate_bps = 0;
43 int retransmit_bitrate_bps = 0;
44 int avg_delay_ms = 0;
45 int max_delay_ms = 0;
46 StreamDataCounters rtp_stats;
47 RtcpPacketTypeCounter rtcp_packet_type_counts;
48 RtcpStatistics rtcp_stats;
49 };
50
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000051 struct Stats {
asapersson2e5cfcd2016-08-11 08:41:18 -070052 std::string ToString(int64_t time_ms) const;
Peter Boströmb7d9a972015-12-18 16:01:11 +010053 std::string encoder_implementation_name = "unknown";
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +020054 int input_frame_rate = 0;
55 int encode_frame_rate = 0;
56 int avg_encode_time_ms = 0;
57 int encode_usage_percent = 0;
sakal43536c32016-10-24 01:46:43 -070058 uint32_t frames_encoded = 0;
sakal87da4042016-10-31 06:53:47 -070059 rtc::Optional<uint64_t> qp_sum;
Pera48ddb72016-09-29 11:48:50 +020060 // Bitrate the encoder is currently configured to use due to bandwidth
61 // limitations.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +020062 int target_media_bitrate_bps = 0;
Pera48ddb72016-09-29 11:48:50 +020063 // Bitrate the encoder is actually producing.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +020064 int media_bitrate_bps = 0;
Pera48ddb72016-09-29 11:48:50 +020065 // Media bitrate this VideoSendStream is configured to prefer if there are
66 // no bandwidth limitations.
67 int preferred_media_bitrate_bps = 0;
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +020068 bool suspended = false;
asapersson17821db2015-12-14 02:08:12 -080069 bool bw_limited_resolution = false;
perkj803d97f2016-11-01 11:45:46 -070070 bool cpu_limited_resolution = false;
71 // Total number of times resolution as been requested to be changed due to
72 // CPU adaptation.
73 int number_of_cpu_adapt_changes = 0;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000074 std::map<uint32_t, StreamStats> substreams;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000075 };
76
77 struct Config {
perkj26091b12016-09-01 01:17:40 -070078 public:
solenberg4fbae2b2015-08-28 04:07:10 -070079 Config() = delete;
perkj26091b12016-09-01 01:17:40 -070080 Config(Config&&) = default;
pbos2d566682015-09-28 09:59:31 -070081 explicit Config(Transport* send_transport)
solenberg4fbae2b2015-08-28 04:07:10 -070082 : send_transport(send_transport) {}
83
perkj26091b12016-09-01 01:17:40 -070084 Config& operator=(Config&&) = default;
85 Config& operator=(const Config&) = delete;
86
87 // Mostly used by tests. Avoid creating copies if you can.
88 Config Copy() const { return Config(*this); }
89
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +000090 std::string ToString() const;
91
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000092 struct EncoderSettings {
perkj26091b12016-09-01 01:17:40 -070093 EncoderSettings() = default;
94 EncoderSettings(std::string payload_name,
95 int payload_type,
96 VideoEncoder* encoder)
97 : payload_name(std::move(payload_name)),
98 payload_type(payload_type),
99 encoder(encoder) {}
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000100 std::string ToString() const;
101
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000102 std::string payload_name;
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200103 int payload_type = -1;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000104
sophiechang47d78cc2015-09-03 18:24:44 -0700105 // TODO(sophiechang): Delete this field when no one is using internal
106 // sources anymore.
107 bool internal_source = false;
108
Peter Boströme4499152016-02-05 11:13:28 +0100109 // Allow 100% encoder utilization. Used for HW encoders where CPU isn't
110 // expected to be the limiting factor, but a chip could be running at
111 // 30fps (for example) exactly.
112 bool full_overuse_time = false;
113
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000114 // Uninitialized VideoEncoder instance to be used for encoding. Will be
115 // initialized from inside the VideoSendStream.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200116 VideoEncoder* encoder = nullptr;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000117 } encoder_settings;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000118
sprang@webrtc.org25fce9a2013-10-16 13:29:14 +0000119 static const size_t kDefaultMaxPacketSize = 1500 - 40; // TCP over IPv4.
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000120 struct Rtp {
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000121 std::string ToString() const;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000122
123 std::vector<uint32_t> ssrcs;
124
deadbeef13871492015-12-09 12:37:51 -0800125 // See RtcpMode for description.
126 RtcpMode rtcp_mode = RtcpMode::kCompound;
127
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000128 // Max RTP packet size delivered to send transport from VideoEngine.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200129 size_t max_packet_size = kDefaultMaxPacketSize;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000130
131 // RTP header extensions to use for this send stream.
132 std::vector<RtpExtension> extensions;
133
134 // See NackConfig for description.
135 NackConfig nack;
136
brandtrb5f2c3f2016-10-04 23:28:39 -0700137 // See UlpfecConfig for description.
138 UlpfecConfig ulpfec;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000139
brandtre950cad2016-11-15 05:25:41 -0800140 // See FlexfecConfig for description.
141 // TODO(brandtr): Move this config to a new class FlexfecSendStream
142 // when we support multistream protection.
143 FlexfecConfig flexfec;
144
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000145 // Settings for RTP retransmission payload format, see RFC 4588 for
146 // details.
147 struct Rtx {
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000148 std::string ToString() const;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000149 // SSRCs to use for the RTX streams.
150 std::vector<uint32_t> ssrcs;
151
152 // Payload type to use for the RTX stream.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200153 int payload_type = -1;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000154 } rtx;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000155
156 // RTCP CNAME, see RFC 3550.
157 std::string c_name;
158 } rtp;
159
solenberg4fbae2b2015-08-28 04:07:10 -0700160 // Transport for outgoing packets.
pbos2d566682015-09-28 09:59:31 -0700161 Transport* send_transport = nullptr;
solenberg4fbae2b2015-08-28 04:07:10 -0700162
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000163 // Called for each I420 frame before encoding the frame. Can be used for
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200164 // effects, snapshots etc. 'nullptr' disables the callback.
nissed30a1112016-04-18 05:15:22 -0700165 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000166
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200167 // Called for each encoded frame, e.g. used for file storage. 'nullptr'
Peter Boströme4499152016-02-05 11:13:28 +0100168 // disables the callback. Also measures timing and passes the time
169 // spent on encoding. This timing will not fire if encoding takes longer
170 // than the measuring window, since the sample data will have been dropped.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200171 EncodedFrameObserver* post_encode_callback = nullptr;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000172
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000173 // Expected delay needed by the renderer, i.e. the frame will be delivered
174 // this many milliseconds, if possible, earlier than expected render time.
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000175 // Only valid if |local_renderer| is set.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200176 int render_delay_ms = 0;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000177
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000178 // Target delay in milliseconds. A positive value indicates this stream is
179 // used for streaming instead of a real-time call.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200180 int target_delay_ms = 0;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000181
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000182 // True if the stream should be suspended when the available bitrate fall
183 // below the minimum configured bitrate. If this variable is false, the
184 // stream may send at a rate higher than the estimated available bitrate.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200185 bool suspend_below_min_bitrate = false;
perkj26091b12016-09-01 01:17:40 -0700186
187 private:
188 // Access to the copy constructor is private to force use of the Copy()
189 // method for those exceptional cases where we do use it.
190 Config(const Config&) = default;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000191 };
192
pbos1ba8d392016-05-01 20:18:34 -0700193 // Starts stream activity.
194 // When a stream is active, it can receive, process and deliver packets.
195 virtual void Start() = 0;
196 // Stops stream activity.
197 // When a stream is stopped, it can't receive, process or deliver packets.
198 virtual void Stop() = 0;
199
perkj803d97f2016-11-01 11:45:46 -0700200 // Based on the spec in
201 // https://w3c.github.io/webrtc-pc/#idl-def-rtcdegradationpreference.
202 enum class DegradationPreference {
203 kMaintainResolution,
204 // TODO(perkj): Implement kMaintainFrameRate. kBalanced will drop frames
205 // if the encoder overshoots or the encoder can not encode fast enough.
206 kBalanced,
207 };
perkja49cbd32016-09-16 07:53:41 -0700208 virtual void SetSource(
perkj803d97f2016-11-01 11:45:46 -0700209 rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
210 const DegradationPreference& degradation_preference) = 0;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000211
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000212 // Set which streams to send. Must have at least as many SSRCs as configured
213 // in the config. Encoder settings are passed on to the encoder instance along
214 // with the VideoStream settings.
perkj26091b12016-09-01 01:17:40 -0700215 virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000216
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000217 virtual Stats GetStats() = 0;
pbos1ba8d392016-05-01 20:18:34 -0700218
palmkviste75f2042016-09-28 06:19:48 -0700219 // Takes ownership of each file, is responsible for closing them later.
220 // Calling this method will close and finalize any current logs.
221 // Some codecs produce multiple streams (VP8 only at present), each of these
222 // streams will log to a separate file. kMaxSimulcastStreams in common_types.h
223 // gives the max number of such streams. If there is no file for a stream, or
224 // the file is rtc::kInvalidPlatformFileValue, frames from that stream will
225 // not be logged.
226 // If a frame to be written would make the log too large the write fails and
227 // the log is closed and finalized. A |byte_limit| of 0 means no limit.
228 virtual void EnableEncodedFrameRecording(
229 const std::vector<rtc::PlatformFile>& files,
230 size_t byte_limit) = 0;
231 inline void DisableEncodedFrameRecording() {
232 EnableEncodedFrameRecording(std::vector<rtc::PlatformFile>(), 0);
233 }
234
pbos1ba8d392016-05-01 20:18:34 -0700235 protected:
236 virtual ~VideoSendStream() {}
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000237};
238
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000239} // namespace webrtc
240
mflodman@webrtc.orgb429e512013-12-18 09:46:22 +0000241#endif // WEBRTC_VIDEO_SEND_STREAM_H_