blob: 6df4642718a952d695ccd9d5e94abce396c337ef [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
deadbeef6979b022015-09-24 16:47:53 -07009 */
10
deadbeef70ab1a12015-09-28 16:53:55 -070011// This file contains classes that implement RtpReceiverInterface.
12// An RtpReceiver associates a MediaStreamTrackInterface with an underlying
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070013// transport (provided by cricket::VoiceChannel/cricket::VideoChannel)
deadbeef70ab1a12015-09-28 16:53:55 -070014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#ifndef PC_RTPRECEIVER_H_
16#define PC_RTPRECEIVER_H_
deadbeef70ab1a12015-09-28 16:53:55 -070017
pbosc7c26a02017-01-02 08:42:32 -080018#include <stdint.h>
19
deadbeef70ab1a12015-09-28 16:53:55 -070020#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070021#include <vector>
deadbeef70ab1a12015-09-28 16:53:55 -070022
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#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"
deadbeef70ab1a12015-09-28 16:53:55 -070031
32namespace webrtc {
33
deadbeefa601f5c2016-06-06 14:27:39 -070034// Internal class used by PeerConnection.
35class RtpReceiverInternal : public RtpReceiverInterface {
36 public:
37 virtual void Stop() = 0;
deadbeefe814a0d2017-02-25 18:15:09 -080038 // This SSRC is used as an identifier for the receiver between the API layer
eladalonf1841382017-06-12 01:16:46 -070039 // and the WebRtcVideoEngine, WebRtcVoiceEngine layer.
deadbeefe814a0d2017-02-25 18:15:09 -080040 virtual uint32_t ssrc() const = 0;
deadbeefa601f5c2016-06-06 14:27:39 -070041};
42
deadbeef70ab1a12015-09-28 16:53:55 -070043class AudioRtpReceiver : public ObserverInterface,
44 public AudioSourceInterface::AudioObserver,
zhihuang184a3fd2016-06-14 11:47:14 -070045 public rtc::RefCountedObject<RtpReceiverInternal>,
46 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -070047 public:
deadbeefe814a0d2017-02-25 18:15:09 -080048 // 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).
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010052 AudioRtpReceiver(
Steve Anton9158ef62017-11-27 13:01:52 -080053 const std::string& receiver_id,
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010054 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams,
55 uint32_t ssrc,
56 cricket::VoiceChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070057 virtual ~AudioRtpReceiver();
58
59 // ObserverInterface implementation
60 void OnChanged() override;
61
62 // AudioSourceInterface::AudioObserver implementation
63 void OnSetVolume(double volume) override;
64
perkjd61bf802016-03-24 03:16:19 -070065 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
66 return track_.get();
67 }
68
deadbeef70ab1a12015-09-28 16:53:55 -070069 // RtpReceiverInterface implementation
70 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
71 return track_.get();
72 }
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010073 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
74 const override {
75 return streams_;
76 }
deadbeef70ab1a12015-09-28 16:53:55 -070077
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070078 cricket::MediaType media_type() const override {
79 return cricket::MEDIA_TYPE_AUDIO;
80 }
81
deadbeef70ab1a12015-09-28 16:53:55 -070082 std::string id() const override { return id_; }
83
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070084 RtpParameters GetParameters() const override;
85 bool SetParameters(const RtpParameters& parameters) override;
86
deadbeefa601f5c2016-06-06 14:27:39 -070087 // RtpReceiverInternal implementation.
88 void Stop() override;
deadbeefe814a0d2017-02-25 18:15:09 -080089 uint32_t ssrc() const override { return ssrc_; }
deadbeefa601f5c2016-06-06 14:27:39 -070090
zhihuang184a3fd2016-06-14 11:47:14 -070091 void SetObserver(RtpReceiverObserverInterface* observer) override;
92
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070093 // Does not take ownership.
94 // Should call SetChannel(nullptr) before |channel| is destroyed.
95 void SetChannel(cricket::VoiceChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -070096
hbos8d609f62017-04-10 07:39:05 -070097 std::vector<RtpSource> GetSources() const override;
98
deadbeef70ab1a12015-09-28 16:53:55 -070099 private:
100 void Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700101 void OnFirstPacketReceived(cricket::BaseChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700102
Tommif888bb52015-12-12 01:37:01 +0100103 const std::string id_;
Tommif888bb52015-12-12 01:37:01 +0100104 const uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700105 cricket::VoiceChannel* channel_;
perkjd61bf802016-03-24 03:16:19 -0700106 const rtc::scoped_refptr<AudioTrackInterface> track_;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100107 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
deadbeef70ab1a12015-09-28 16:53:55 -0700108 bool cached_track_enabled_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700109 double cached_volume_ = 1;
110 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700111 RtpReceiverObserverInterface* observer_ = nullptr;
112 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700113};
114
zhihuang184a3fd2016-06-14 11:47:14 -0700115class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>,
116 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -0700117 public:
deadbeefe814a0d2017-02-25 18:15:09 -0800118 // An SSRC of 0 will create a receiver that will match the first SSRC it
119 // sees.
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100120 VideoRtpReceiver(
121 const std::string& track_id,
122 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams,
123 rtc::Thread* worker_thread,
124 uint32_t ssrc,
125 cricket::VideoChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700126
127 virtual ~VideoRtpReceiver();
128
perkjf0dcfe22016-03-10 18:32:00 +0100129 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
130 return track_.get();
131 }
132
deadbeef70ab1a12015-09-28 16:53:55 -0700133 // RtpReceiverInterface implementation
134 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
135 return track_.get();
136 }
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100137 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
138 const override {
139 return streams_;
140 }
deadbeef70ab1a12015-09-28 16:53:55 -0700141
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700142 cricket::MediaType media_type() const override {
143 return cricket::MEDIA_TYPE_VIDEO;
144 }
145
deadbeef70ab1a12015-09-28 16:53:55 -0700146 std::string id() const override { return id_; }
147
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700148 RtpParameters GetParameters() const override;
149 bool SetParameters(const RtpParameters& parameters) override;
150
deadbeefa601f5c2016-06-06 14:27:39 -0700151 // RtpReceiverInternal implementation.
152 void Stop() override;
deadbeefe814a0d2017-02-25 18:15:09 -0800153 uint32_t ssrc() const override { return ssrc_; }
deadbeefa601f5c2016-06-06 14:27:39 -0700154
zhihuang184a3fd2016-06-14 11:47:14 -0700155 void SetObserver(RtpReceiverObserverInterface* observer) override;
156
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700157 // Does not take ownership.
158 // Should call SetChannel(nullptr) before |channel| is destroyed.
159 void SetChannel(cricket::VideoChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700160
deadbeef70ab1a12015-09-28 16:53:55 -0700161 private:
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700162 void OnFirstPacketReceived(cricket::BaseChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700163
deadbeef70ab1a12015-09-28 16:53:55 -0700164 std::string id_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200165 uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700166 cricket::VideoChannel* channel_;
perkjf0dcfe22016-03-10 18:32:00 +0100167 // |broadcaster_| is needed since the decoder can only handle one sink.
168 // It might be better if the decoder can handle multiple sinks and consider
169 // the VideoSinkWants.
170 rtc::VideoBroadcaster broadcaster_;
171 // |source_| is held here to be able to change the state of the source when
172 // the VideoRtpReceiver is stopped.
173 rtc::scoped_refptr<VideoTrackSource> source_;
174 rtc::scoped_refptr<VideoTrackInterface> track_;
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100175 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700176 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700177 RtpReceiverObserverInterface* observer_ = nullptr;
178 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700179};
180
181} // namespace webrtc
182
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200183#endif // PC_RTPRECEIVER_H_