blob: ab507aec6494f9869f4ec57aef24958c19986c56 [file] [log] [blame]
Ruslan Burakov501bfba2019-02-11 10:29:19 +01001/*
2 * Copyright 2019 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 PC_VIDEO_RTP_RECEIVER_H_
12#define PC_VIDEO_RTP_RECEIVER_H_
13
14#include <stdint.h>
15#include <string>
16#include <vector>
17
18#include "absl/types/optional.h"
19#include "api/crypto/frame_decryptor_interface.h"
20#include "api/media_stream_interface.h"
21#include "api/media_types.h"
22#include "api/rtp_parameters.h"
23#include "api/rtp_receiver_interface.h"
24#include "api/scoped_refptr.h"
25#include "api/video/video_frame.h"
26#include "api/video/video_sink_interface.h"
27#include "api/video/video_source_interface.h"
28#include "media/base/media_channel.h"
29#include "media/base/video_broadcaster.h"
30#include "pc/rtp_receiver.h"
31#include "pc/video_track_source.h"
32#include "rtc_base/ref_counted_object.h"
33#include "rtc_base/thread.h"
34
35namespace webrtc {
36
37class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal> {
38 public:
39 // An SSRC of 0 will create a receiver that will match the first SSRC it
40 // sees.
41 VideoRtpReceiver(rtc::Thread* worker_thread,
42 std::string receiver_id,
43 std::vector<std::string> streams_ids);
44 // TODO(hbos): Remove this when streams() is removed.
45 // https://crbug.com/webrtc/9480
46 VideoRtpReceiver(
47 rtc::Thread* worker_thread,
48 const std::string& receiver_id,
49 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
50
51 virtual ~VideoRtpReceiver();
52
53 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
54 return track_.get();
55 }
56
57 // RtpReceiverInterface implementation
58 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
59 return track_.get();
60 }
61 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
62 return dtls_transport_;
63 }
64 std::vector<std::string> stream_ids() const override;
65 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
66 const override {
67 return streams_;
68 }
69
70 cricket::MediaType media_type() const override {
71 return cricket::MEDIA_TYPE_VIDEO;
72 }
73
74 std::string id() const override { return id_; }
75
76 RtpParameters GetParameters() const override;
77 bool SetParameters(const RtpParameters& parameters) override;
78
79 void SetFrameDecryptor(
80 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
81
82 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
83 const override;
84
85 // RtpReceiverInternal implementation.
86 void Stop() override;
87 void SetupMediaChannel(uint32_t ssrc) override;
88 uint32_t ssrc() const override { return ssrc_.value_or(0); }
89 void NotifyFirstPacketReceived() override;
90 void set_stream_ids(std::vector<std::string> stream_ids) override;
91 void set_transport(
92 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
93 dtls_transport_ = dtls_transport;
94 }
95 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
96 streams) override;
97
98 void SetObserver(RtpReceiverObserverInterface* observer) override;
99
100 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
101
102 int AttachmentId() const override { return attachment_id_; }
103
104 std::vector<RtpSource> GetSources() const override;
105
106 private:
107 class VideoRtpTrackSource : public VideoTrackSource {
108 public:
109 VideoRtpTrackSource() : VideoTrackSource(true /* remote */) {}
110
111 rtc::VideoSourceInterface<VideoFrame>* source() override {
112 return &broadcaster_;
113 }
114 rtc::VideoSinkInterface<VideoFrame>* sink() { return &broadcaster_; }
115
116 private:
117 // |broadcaster_| is needed since the decoder can only handle one sink.
118 // It might be better if the decoder can handle multiple sinks and consider
119 // the VideoSinkWants.
120 rtc::VideoBroadcaster broadcaster_;
121 };
122
123 bool SetSink(rtc::VideoSinkInterface<VideoFrame>* sink);
124
125 rtc::Thread* const worker_thread_;
126 const std::string id_;
127 cricket::VideoMediaChannel* media_channel_ = nullptr;
128 absl::optional<uint32_t> ssrc_;
129 // |source_| is held here to be able to change the state of the source when
130 // the VideoRtpReceiver is stopped.
131 rtc::scoped_refptr<VideoRtpTrackSource> source_;
132 rtc::scoped_refptr<VideoTrackInterface> track_;
133 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
134 bool stopped_ = false;
135 RtpReceiverObserverInterface* observer_ = nullptr;
136 bool received_first_packet_ = false;
137 int attachment_id_ = 0;
138 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
139 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
140};
141
142} // namespace webrtc
143
144#endif // PC_VIDEO_RTP_RECEIVER_H_