blob: afdec43c745cc2cc877b416ef5150f6d27c0cecb [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>
perkj26091b12016-09-01 01:17:40 -070016#include <vector>
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000017
18#include "webrtc/common_types.h"
pbosa96b60b2016-04-18 21:12:48 -070019#include "webrtc/common_video/include/frame_callback.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000020#include "webrtc/config.h"
nissed30a1112016-04-18 05:15:22 -070021#include "webrtc/media/base/videosinkinterface.h"
solenberg4fbae2b2015-08-28 04:07:10 -070022#include "webrtc/transport.h"
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000023
24namespace webrtc {
25
solenberge5269742015-09-08 05:13:22 -070026class LoadObserver;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000027class VideoEncoder;
28
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000029// Class to deliver captured frame to the video send stream.
Peter Boström4b91bd02015-06-26 06:58:16 +020030class VideoCaptureInput {
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000031 public:
pbos@webrtc.org724947b2013-12-11 16:26:16 +000032 // These methods do not lock internally and must be called sequentially.
33 // If your application switches input sources synchronization must be done
34 // externally to make sure that any old frames are not delivered concurrently.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070035 virtual void IncomingCapturedFrame(const VideoFrame& video_frame) = 0;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000036
37 protected:
Peter Boström4b91bd02015-06-26 06:58:16 +020038 virtual ~VideoCaptureInput() {}
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000039};
40
pbos1ba8d392016-05-01 20:18:34 -070041class VideoSendStream {
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +000042 public:
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000043 struct StreamStats {
asapersson2e5cfcd2016-08-11 08:41:18 -070044 std::string ToString() const;
45
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000046 FrameCounts frame_counts;
asapersson2e5cfcd2016-08-11 08:41:18 -070047 bool is_rtx = false;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000048 int width = 0;
49 int height = 0;
50 // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
51 int total_bitrate_bps = 0;
52 int retransmit_bitrate_bps = 0;
53 int avg_delay_ms = 0;
54 int max_delay_ms = 0;
55 StreamDataCounters rtp_stats;
56 RtcpPacketTypeCounter rtcp_packet_type_counts;
57 RtcpStatistics rtcp_stats;
58 };
59
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000060 struct Stats {
asapersson2e5cfcd2016-08-11 08:41:18 -070061 std::string ToString(int64_t time_ms) const;
Peter Boströmb7d9a972015-12-18 16:01:11 +010062 std::string encoder_implementation_name = "unknown";
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +020063 int input_frame_rate = 0;
64 int encode_frame_rate = 0;
65 int avg_encode_time_ms = 0;
66 int encode_usage_percent = 0;
67 int target_media_bitrate_bps = 0;
68 int media_bitrate_bps = 0;
69 bool suspended = false;
asapersson17821db2015-12-14 02:08:12 -080070 bool bw_limited_resolution = false;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +000071 std::map<uint32_t, StreamStats> substreams;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000072 };
73
74 struct Config {
perkj26091b12016-09-01 01:17:40 -070075 public:
solenberg4fbae2b2015-08-28 04:07:10 -070076 Config() = delete;
perkj26091b12016-09-01 01:17:40 -070077 Config(Config&&) = default;
pbos2d566682015-09-28 09:59:31 -070078 explicit Config(Transport* send_transport)
solenberg4fbae2b2015-08-28 04:07:10 -070079 : send_transport(send_transport) {}
80
perkj26091b12016-09-01 01:17:40 -070081 Config& operator=(Config&&) = default;
82 Config& operator=(const Config&) = delete;
83
84 // Mostly used by tests. Avoid creating copies if you can.
85 Config Copy() const { return Config(*this); }
86
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +000087 std::string ToString() const;
88
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000089 struct EncoderSettings {
perkj26091b12016-09-01 01:17:40 -070090 EncoderSettings() = default;
91 EncoderSettings(std::string payload_name,
92 int payload_type,
93 VideoEncoder* encoder)
94 : payload_name(std::move(payload_name)),
95 payload_type(payload_type),
96 encoder(encoder) {}
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +000097 std::string ToString() const;
98
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +000099 std::string payload_name;
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200100 int payload_type = -1;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000101
sophiechang47d78cc2015-09-03 18:24:44 -0700102 // TODO(sophiechang): Delete this field when no one is using internal
103 // sources anymore.
104 bool internal_source = false;
105
Peter Boströme4499152016-02-05 11:13:28 +0100106 // Allow 100% encoder utilization. Used for HW encoders where CPU isn't
107 // expected to be the limiting factor, but a chip could be running at
108 // 30fps (for example) exactly.
109 bool full_overuse_time = false;
110
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000111 // Uninitialized VideoEncoder instance to be used for encoding. Will be
112 // initialized from inside the VideoSendStream.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200113 VideoEncoder* encoder = nullptr;
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000114 } encoder_settings;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000115
sprang@webrtc.org25fce9a2013-10-16 13:29:14 +0000116 static const size_t kDefaultMaxPacketSize = 1500 - 40; // TCP over IPv4.
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000117 struct Rtp {
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000118 std::string ToString() const;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000119
120 std::vector<uint32_t> ssrcs;
121
deadbeef13871492015-12-09 12:37:51 -0800122 // See RtcpMode for description.
123 RtcpMode rtcp_mode = RtcpMode::kCompound;
124
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000125 // Max RTP packet size delivered to send transport from VideoEngine.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200126 size_t max_packet_size = kDefaultMaxPacketSize;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000127
128 // RTP header extensions to use for this send stream.
129 std::vector<RtpExtension> extensions;
130
131 // See NackConfig for description.
132 NackConfig nack;
133
134 // See FecConfig for description.
135 FecConfig fec;
136
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000137 // Settings for RTP retransmission payload format, see RFC 4588 for
138 // details.
139 struct Rtx {
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000140 std::string ToString() const;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000141 // SSRCs to use for the RTX streams.
142 std::vector<uint32_t> ssrcs;
143
144 // Payload type to use for the RTX stream.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200145 int payload_type = -1;
pbos@webrtc.orgc279a5d2014-01-24 09:30:53 +0000146 } rtx;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000147
148 // RTCP CNAME, see RFC 3550.
149 std::string c_name;
150 } rtp;
151
solenberg4fbae2b2015-08-28 04:07:10 -0700152 // Transport for outgoing packets.
pbos2d566682015-09-28 09:59:31 -0700153 Transport* send_transport = nullptr;
solenberg4fbae2b2015-08-28 04:07:10 -0700154
solenberge5269742015-09-08 05:13:22 -0700155 // Callback for overuse and normal usage based on the jitter of incoming
156 // captured frames. 'nullptr' disables the callback.
157 LoadObserver* overuse_callback = nullptr;
158
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000159 // Called for each I420 frame before encoding the frame. Can be used for
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200160 // effects, snapshots etc. 'nullptr' disables the callback.
nissed30a1112016-04-18 05:15:22 -0700161 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000162
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200163 // Called for each encoded frame, e.g. used for file storage. 'nullptr'
Peter Boströme4499152016-02-05 11:13:28 +0100164 // disables the callback. Also measures timing and passes the time
165 // spent on encoding. This timing will not fire if encoding takes longer
166 // than the measuring window, since the sample data will have been dropped.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200167 EncodedFrameObserver* post_encode_callback = nullptr;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000168
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000169 // Expected delay needed by the renderer, i.e. the frame will be delivered
170 // this many milliseconds, if possible, earlier than expected render time.
pbos@webrtc.org1e92b0a2014-05-15 09:35:06 +0000171 // Only valid if |local_renderer| is set.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200172 int render_delay_ms = 0;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000173
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000174 // Target delay in milliseconds. A positive value indicates this stream is
175 // used for streaming instead of a real-time call.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200176 int target_delay_ms = 0;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000177
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000178 // True if the stream should be suspended when the available bitrate fall
179 // below the minimum configured bitrate. If this variable is false, the
180 // stream may send at a rate higher than the estimated available bitrate.
Fredrik Solenberg78fb3b32015-06-11 12:38:38 +0200181 bool suspend_below_min_bitrate = false;
perkj26091b12016-09-01 01:17:40 -0700182
183 private:
184 // Access to the copy constructor is private to force use of the Copy()
185 // method for those exceptional cases where we do use it.
186 Config(const Config&) = default;
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000187 };
188
pbos1ba8d392016-05-01 20:18:34 -0700189 // Starts stream activity.
190 // When a stream is active, it can receive, process and deliver packets.
191 virtual void Start() = 0;
192 // Stops stream activity.
193 // When a stream is stopped, it can't receive, process or deliver packets.
194 virtual void Stop() = 0;
195
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000196 // Gets interface used to insert captured frames. Valid as long as the
197 // VideoSendStream is valid.
Peter Boström4b91bd02015-06-26 06:58:16 +0200198 virtual VideoCaptureInput* Input() = 0;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000199
pbos@webrtc.orgf577ae92014-03-19 08:43:57 +0000200 // Set which streams to send. Must have at least as many SSRCs as configured
201 // in the config. Encoder settings are passed on to the encoder instance along
202 // with the VideoStream settings.
perkj26091b12016-09-01 01:17:40 -0700203 virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000204
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000205 virtual Stats GetStats() = 0;
pbos1ba8d392016-05-01 20:18:34 -0700206
207 protected:
208 virtual ~VideoSendStream() {}
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000209};
210
mflodman@webrtc.org65f995a2013-04-18 12:02:52 +0000211} // namespace webrtc
212
mflodman@webrtc.orgb429e512013-12-18 09:46:22 +0000213#endif // WEBRTC_VIDEO_SEND_STREAM_H_