deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
deadbeef | 6979b02 | 2015-09-24 16:47:53 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 11 | // This file contains classes that implement RtpReceiverInterface. |
| 12 | // An RtpReceiver associates a MediaStreamTrackInterface with an underlying |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 13 | // transport (provided by cricket::VoiceChannel/cricket::VideoChannel) |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #ifndef PC_RTPRECEIVER_H_ |
| 16 | #define PC_RTPRECEIVER_H_ |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 17 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 20 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 21 | #include <vector> |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/mediastreaminterface.h" |
| 24 | #include "api/rtpreceiverinterface.h" |
| 25 | #include "media/base/videobroadcaster.h" |
| 26 | #include "pc/channel.h" |
| 27 | #include "pc/remoteaudiosource.h" |
| 28 | #include "pc/videotracksource.h" |
| 29 | #include "rtc_base/basictypes.h" |
| 30 | #include "rtc_base/sigslot.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 34 | // Internal class used by PeerConnection. |
| 35 | class RtpReceiverInternal : public RtpReceiverInterface { |
| 36 | public: |
| 37 | virtual void Stop() = 0; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 38 | // This SSRC is used as an identifier for the receiver between the API layer |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 39 | // and the WebRtcVideoEngine, WebRtcVoiceEngine layer. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 40 | virtual uint32_t ssrc() const = 0; |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 43 | class AudioRtpReceiver : public ObserverInterface, |
| 44 | public AudioSourceInterface::AudioObserver, |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 45 | public rtc::RefCountedObject<RtpReceiverInternal>, |
| 46 | public sigslot::has_slots<> { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 47 | public: |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 48 | // An SSRC of 0 will create a receiver that will match the first SSRC it |
| 49 | // sees. |
| 50 | // TODO(deadbeef): Use rtc::Optional, or have another constructor that |
| 51 | // doesn't take an SSRC, and make this one DCHECK(ssrc != 0). |
| 52 | AudioRtpReceiver(const std::string& track_id, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 53 | uint32_t ssrc, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 54 | cricket::VoiceChannel* channel); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 55 | |
| 56 | virtual ~AudioRtpReceiver(); |
| 57 | |
| 58 | // ObserverInterface implementation |
| 59 | void OnChanged() override; |
| 60 | |
| 61 | // AudioSourceInterface::AudioObserver implementation |
| 62 | void OnSetVolume(double volume) override; |
| 63 | |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 64 | rtc::scoped_refptr<AudioTrackInterface> audio_track() const { |
| 65 | return track_.get(); |
| 66 | } |
| 67 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 68 | // RtpReceiverInterface implementation |
| 69 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 70 | return track_.get(); |
| 71 | } |
| 72 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 73 | cricket::MediaType media_type() const override { |
| 74 | return cricket::MEDIA_TYPE_AUDIO; |
| 75 | } |
| 76 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 77 | std::string id() const override { return id_; } |
| 78 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 79 | RtpParameters GetParameters() const override; |
| 80 | bool SetParameters(const RtpParameters& parameters) override; |
| 81 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 82 | // RtpReceiverInternal implementation. |
| 83 | void Stop() override; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 84 | uint32_t ssrc() const override { return ssrc_; } |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 85 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 86 | void SetObserver(RtpReceiverObserverInterface* observer) override; |
| 87 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 88 | // Does not take ownership. |
| 89 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 90 | void SetChannel(cricket::VoiceChannel* channel); |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 91 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 92 | std::vector<RtpSource> GetSources() const override; |
| 93 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 94 | private: |
| 95 | void Reconfigure(); |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 96 | void OnFirstPacketReceived(cricket::BaseChannel* channel); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 97 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 98 | const std::string id_; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 99 | const uint32_t ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 100 | cricket::VoiceChannel* channel_; |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 101 | const rtc::scoped_refptr<AudioTrackInterface> track_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 102 | bool cached_track_enabled_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 103 | double cached_volume_ = 1; |
| 104 | bool stopped_ = false; |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 105 | RtpReceiverObserverInterface* observer_ = nullptr; |
| 106 | bool received_first_packet_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 109 | class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>, |
| 110 | public sigslot::has_slots<> { |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 111 | public: |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 112 | // An SSRC of 0 will create a receiver that will match the first SSRC it |
| 113 | // sees. |
| 114 | VideoRtpReceiver(const std::string& track_id, |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 115 | rtc::Thread* worker_thread, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 116 | uint32_t ssrc, |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 117 | cricket::VideoChannel* channel); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 118 | |
| 119 | virtual ~VideoRtpReceiver(); |
| 120 | |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 121 | rtc::scoped_refptr<VideoTrackInterface> video_track() const { |
| 122 | return track_.get(); |
| 123 | } |
| 124 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 125 | // RtpReceiverInterface implementation |
| 126 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 127 | return track_.get(); |
| 128 | } |
| 129 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 130 | cricket::MediaType media_type() const override { |
| 131 | return cricket::MEDIA_TYPE_VIDEO; |
| 132 | } |
| 133 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 134 | std::string id() const override { return id_; } |
| 135 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 136 | RtpParameters GetParameters() const override; |
| 137 | bool SetParameters(const RtpParameters& parameters) override; |
| 138 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 139 | // RtpReceiverInternal implementation. |
| 140 | void Stop() override; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 141 | uint32_t ssrc() const override { return ssrc_; } |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 142 | |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 143 | void SetObserver(RtpReceiverObserverInterface* observer) override; |
| 144 | |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 145 | // Does not take ownership. |
| 146 | // Should call SetChannel(nullptr) before |channel| is destroyed. |
| 147 | void SetChannel(cricket::VideoChannel* channel); |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 148 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 149 | private: |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 150 | void OnFirstPacketReceived(cricket::BaseChannel* channel); |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 151 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 152 | std::string id_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 153 | uint32_t ssrc_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 154 | cricket::VideoChannel* channel_; |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 155 | // |broadcaster_| is needed since the decoder can only handle one sink. |
| 156 | // It might be better if the decoder can handle multiple sinks and consider |
| 157 | // the VideoSinkWants. |
| 158 | rtc::VideoBroadcaster broadcaster_; |
| 159 | // |source_| is held here to be able to change the state of the source when |
| 160 | // the VideoRtpReceiver is stopped. |
| 161 | rtc::scoped_refptr<VideoTrackSource> source_; |
| 162 | rtc::scoped_refptr<VideoTrackInterface> track_; |
Taylor Brandstetter | ba29c6a | 2016-06-27 16:30:35 -0700 | [diff] [blame] | 163 | bool stopped_ = false; |
zhihuang | 184a3fd | 2016-06-14 11:47:14 -0700 | [diff] [blame] | 164 | RtpReceiverObserverInterface* observer_ = nullptr; |
| 165 | bool received_first_packet_ = false; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | } // namespace webrtc |
| 169 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 170 | #endif // PC_RTPRECEIVER_H_ |