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