blob: 2ca402626ce2b56c03862ca5cbc76ec11c1ff032 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_VIDEO_RECEIVE_STREAM_H_
12#define VIDEO_VIDEO_RECEIVE_STREAM_H_
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013
kwiberg27f982b2016-03-01 11:52:33 -080014#include <memory>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000015#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "call/rtp_packet_sink_interface.h"
18#include "call/syncable.h"
19#include "call/video_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_video/libyuv/include/webrtc_libyuv.h"
21#include "modules/rtp_rtcp/include/flexfec_receiver.h"
22#include "modules/video_coding/frame_buffer2.h"
23#include "modules/video_coding/video_coding_impl.h"
24#include "rtc_base/sequenced_task_checker.h"
25#include "system_wrappers/include/clock.h"
26#include "video/receive_statistics_proxy.h"
27#include "video/rtp_streams_synchronizer.h"
28#include "video/rtp_video_stream_receiver.h"
29#include "video/transport_adapter.h"
30#include "video/video_stream_decoder.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031
32namespace webrtc {
33
mflodmane3787022015-10-21 13:24:28 +020034class CallStats;
sprang3911c262016-04-15 01:24:14 -070035class IvfFileWriter;
Peter Boströmd1d66ba2016-02-08 14:07:14 +010036class ProcessThread;
mflodman4cd27902016-08-05 06:28:45 -070037class RTPFragmentationHeader;
nisse0f15f922017-06-21 01:05:22 -070038class RtpStreamReceiverInterface;
39class RtpStreamReceiverControllerInterface;
nisseca5706d2017-09-11 02:32:16 -070040class RtxReceiveStream;
philipelfd5a20f2016-11-15 00:57:57 -080041class VCMTiming;
42class VCMJitterEstimator;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000043
44namespace internal {
45
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000046class VideoReceiveStream : public webrtc::VideoReceiveStream,
nisse30f118e2016-05-03 01:09:11 -070047 public rtc::VideoSinkInterface<VideoFrame>,
philipel83f831a2016-03-12 03:30:23 -080048 public EncodedImageCallback,
49 public NackSender,
philipelfd5a20f2016-11-15 00:57:57 -080050 public KeyFrameRequestSender,
solenberg3ebbcb52017-01-31 03:58:40 -080051 public video_coding::OnCompleteFrameCallback,
philipele21be1d2017-09-25 06:37:12 -070052 public Syncable,
53 public CallStatsObserver {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054 public:
nisse0f15f922017-06-21 01:05:22 -070055 VideoReceiveStream(RtpStreamReceiverControllerInterface* receiver_controller,
56 int num_cpu_cores,
nisse0245da02016-11-30 03:35:20 -080057 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020058 VideoReceiveStream::Config config,
mflodmane3787022015-10-21 13:24:28 +020059 ProcessThread* process_thread,
nisse05843312017-04-18 23:38:35 -070060 CallStats* call_stats);
Jelena Marusiccd670222015-07-16 09:30:09 +020061 ~VideoReceiveStream() override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000062
brandtr090c9402017-01-25 08:28:02 -080063 const Config& config() const { return config_; }
64
pbos1ba8d392016-05-01 20:18:34 -070065 void SignalNetworkState(NetworkState state);
66 bool DeliverRtcp(const uint8_t* packet, size_t length);
Jelena Marusiccd670222015-07-16 09:30:09 +020067
solenberg3ebbcb52017-01-31 03:58:40 -080068 void SetSync(Syncable* audio_syncable);
brandtr090c9402017-01-25 08:28:02 -080069
70 // Implements webrtc::VideoReceiveStream.
pbos1ba8d392016-05-01 20:18:34 -070071 void Start() override;
72 void Stop() override;
73
Jelena Marusiccd670222015-07-16 09:30:09 +020074 webrtc::VideoReceiveStream::Stats GetStats() const override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000075
palmkviste75f2042016-09-28 06:19:48 -070076 // Takes ownership of the file, is responsible for closing it later.
77 // Calling this method will close and finalize any current log.
78 // Giving rtc::kInvalidPlatformFileValue disables logging.
79 // If a frame to be written would make the log too large the write fails and
80 // the log is closed and finalized. A |byte_limit| of 0 means no limit.
81 void EnableEncodedFrameRecording(rtc::PlatformFile file,
82 size_t byte_limit) override;
83
eladalonc0d481a2017-08-02 07:39:07 -070084 void AddSecondarySink(RtpPacketSinkInterface* sink) override;
85 void RemoveSecondarySink(const RtpPacketSinkInterface* sink) override;
86
brandtr090c9402017-01-25 08:28:02 -080087 // Implements rtc::VideoSinkInterface<VideoFrame>.
88 void OnFrame(const VideoFrame& video_frame) override;
89
90 // Implements EncodedImageCallback.
91 EncodedImageCallback::Result OnEncodedImage(
92 const EncodedImage& encoded_image,
93 const CodecSpecificInfo* codec_specific_info,
94 const RTPFragmentationHeader* fragmentation) override;
95
96 // Implements NackSender.
97 void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
98
99 // Implements KeyFrameRequestSender.
100 void RequestKeyFrame() override;
101
102 // Implements video_coding::OnCompleteFrameCallback.
103 void OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100104 std::unique_ptr<video_coding::EncodedFrame> frame) override;
brandtr090c9402017-01-25 08:28:02 -0800105
philipele21be1d2017-09-25 06:37:12 -0700106 // Implements CallStatsObserver::OnRttUpdate
107 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
108
solenberg3ebbcb52017-01-31 03:58:40 -0800109 // Implements Syncable.
110 int id() const override;
111 rtc::Optional<Syncable::Info> GetInfo() const override;
112 uint32_t GetPlayoutTimestamp() const override;
113 void SetMinimumPlayoutDelay(int delay_ms) override;
114
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000115 private:
tommic8ece432017-06-20 02:44:38 -0700116 static void DecodeThreadFunction(void* ptr);
philipel2dfea3e2017-02-28 07:19:43 -0800117 bool Decode();
Peter Boströmca835252016-02-11 15:59:46 +0100118
eladalona28122f2017-08-18 04:02:48 -0700119 rtc::SequencedTaskChecker worker_sequence_checker_;
120 rtc::SequencedTaskChecker module_process_sequence_checker_;
solenberg3ebbcb52017-01-31 03:58:40 -0800121
pbos@webrtc.orge75a1bf2013-09-18 11:52:42 +0000122 TransportAdapter transport_adapter_;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000123 const VideoReceiveStream::Config config_;
sprang113bdca2016-10-11 03:10:10 -0700124 const int num_cpu_cores_;
Peter Boström1d04ac62016-02-05 11:25:46 +0100125 ProcessThread* const process_thread_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000126 Clock* const clock_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000127
Peter Boströmca835252016-02-11 15:59:46 +0100128 rtc::PlatformThread decode_thread_;
129
mflodmane3787022015-10-21 13:24:28 +0200130 CallStats* const call_stats_;
Peter Boström45553ae2015-05-08 13:54:38 +0200131
nisseca5706d2017-09-11 02:32:16 -0700132 // Shared by media and rtx stream receivers, since the latter has no RtpRtcp
133 // module of its own.
134 const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
135
philipel721d4022016-12-15 07:10:57 -0800136 std::unique_ptr<VCMTiming> timing_; // Jitter buffer experiment.
Peter Boström0b250722016-04-22 18:23:15 +0200137 vcm::VideoReceiver video_receiver_;
tommi2e82f382016-06-21 00:26:43 -0700138 std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> incoming_video_stream_;
Peter Boströmf751bf82016-02-05 14:00:53 +0100139 ReceiveStatisticsProxy stats_proxy_;
nisseb1f2ff92017-06-09 04:01:55 -0700140 RtpVideoStreamReceiver rtp_video_stream_receiver_;
tommi2e82f382016-06-21 00:26:43 -0700141 std::unique_ptr<VideoStreamDecoder> video_stream_decoder_;
mflodman4cd27902016-08-05 06:28:45 -0700142 RtpStreamsSynchronizer rtp_stream_sync_;
sprang3911c262016-04-15 01:24:14 -0700143
palmkviste75f2042016-09-28 06:19:48 -0700144 rtc::CriticalSection ivf_writer_lock_;
danilchapa37de392017-09-09 04:17:22 -0700145 std::unique_ptr<IvfFileWriter> ivf_writer_ RTC_GUARDED_BY(ivf_writer_lock_);
philipelfd5a20f2016-11-15 00:57:57 -0800146
147 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800148 std::unique_ptr<VCMJitterEstimator> jitter_estimator_;
149 std::unique_ptr<video_coding::FrameBuffer> frame_buffer_;
nisse0f15f922017-06-21 01:05:22 -0700150
151 std::unique_ptr<RtpStreamReceiverInterface> media_receiver_;
nisseca5706d2017-09-11 02:32:16 -0700152 std::unique_ptr<RtxReceiveStream> rtx_receive_stream_;
nisse0f15f922017-06-21 01:05:22 -0700153 std::unique_ptr<RtpStreamReceiverInterface> rtx_receiver_;
philipel3042c2d2017-08-18 04:55:02 -0700154
155 // Whenever we are in an undecodable state (stream has just started or due to
156 // a decoding error) we require a keyframe to restart the stream.
157 bool keyframe_required_ = true;
158
159 // If we have successfully decoded any frame.
160 bool frame_decoded_ = false;
philipel48462b62017-09-26 02:54:58 -0700161
162 int64_t last_keyframe_request_ms_ = 0;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000163};
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000164} // namespace internal
165} // namespace webrtc
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000166
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200167#endif // VIDEO_VIDEO_RECEIVE_STREAM_H_