blob: d3f0f2679e45f5441aa0c02248ee7d574b23e829 [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).
52 AudioRtpReceiver(const std::string& track_id,
Peter Boström0c4e06b2015-10-07 12:23:21 +020053 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070054 cricket::VoiceChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070055
56 virtual ~AudioRtpReceiver();
57
58 // ObserverInterface implementation
59 void OnChanged() override;
60
61 // AudioSourceInterface::AudioObserver implementation
62 void OnSetVolume(double volume) override;
63
perkjd61bf802016-03-24 03:16:19 -070064 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
65 return track_.get();
66 }
67
deadbeef70ab1a12015-09-28 16:53:55 -070068 // RtpReceiverInterface implementation
69 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
70 return track_.get();
71 }
72
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070073 cricket::MediaType media_type() const override {
74 return cricket::MEDIA_TYPE_AUDIO;
75 }
76
deadbeef70ab1a12015-09-28 16:53:55 -070077 std::string id() const override { return id_; }
78
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070079 RtpParameters GetParameters() const override;
80 bool SetParameters(const RtpParameters& parameters) override;
81
deadbeefa601f5c2016-06-06 14:27:39 -070082 // RtpReceiverInternal implementation.
83 void Stop() override;
deadbeefe814a0d2017-02-25 18:15:09 -080084 uint32_t ssrc() const override { return ssrc_; }
deadbeefa601f5c2016-06-06 14:27:39 -070085
zhihuang184a3fd2016-06-14 11:47:14 -070086 void SetObserver(RtpReceiverObserverInterface* observer) override;
87
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070088 // Does not take ownership.
89 // Should call SetChannel(nullptr) before |channel| is destroyed.
90 void SetChannel(cricket::VoiceChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -070091
hbos8d609f62017-04-10 07:39:05 -070092 std::vector<RtpSource> GetSources() const override;
93
deadbeef70ab1a12015-09-28 16:53:55 -070094 private:
95 void Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070096 void OnFirstPacketReceived(cricket::BaseChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070097
Tommif888bb52015-12-12 01:37:01 +010098 const std::string id_;
Tommif888bb52015-12-12 01:37:01 +010099 const uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700100 cricket::VoiceChannel* channel_;
perkjd61bf802016-03-24 03:16:19 -0700101 const rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef70ab1a12015-09-28 16:53:55 -0700102 bool cached_track_enabled_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700103 double cached_volume_ = 1;
104 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700105 RtpReceiverObserverInterface* observer_ = nullptr;
106 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700107};
108
zhihuang184a3fd2016-06-14 11:47:14 -0700109class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>,
110 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -0700111 public:
deadbeefe814a0d2017-02-25 18:15:09 -0800112 // An SSRC of 0 will create a receiver that will match the first SSRC it
113 // sees.
114 VideoRtpReceiver(const std::string& track_id,
perkjf0dcfe22016-03-10 18:32:00 +0100115 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200116 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700117 cricket::VideoChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700118
119 virtual ~VideoRtpReceiver();
120
perkjf0dcfe22016-03-10 18:32:00 +0100121 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
122 return track_.get();
123 }
124
deadbeef70ab1a12015-09-28 16:53:55 -0700125 // RtpReceiverInterface implementation
126 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
127 return track_.get();
128 }
129
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700130 cricket::MediaType media_type() const override {
131 return cricket::MEDIA_TYPE_VIDEO;
132 }
133
deadbeef70ab1a12015-09-28 16:53:55 -0700134 std::string id() const override { return id_; }
135
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700136 RtpParameters GetParameters() const override;
137 bool SetParameters(const RtpParameters& parameters) override;
138
deadbeefa601f5c2016-06-06 14:27:39 -0700139 // RtpReceiverInternal implementation.
140 void Stop() override;
deadbeefe814a0d2017-02-25 18:15:09 -0800141 uint32_t ssrc() const override { return ssrc_; }
deadbeefa601f5c2016-06-06 14:27:39 -0700142
zhihuang184a3fd2016-06-14 11:47:14 -0700143 void SetObserver(RtpReceiverObserverInterface* observer) override;
144
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700145 // Does not take ownership.
146 // Should call SetChannel(nullptr) before |channel| is destroyed.
147 void SetChannel(cricket::VideoChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700148
deadbeef70ab1a12015-09-28 16:53:55 -0700149 private:
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700150 void OnFirstPacketReceived(cricket::BaseChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700151
deadbeef70ab1a12015-09-28 16:53:55 -0700152 std::string id_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153 uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700154 cricket::VideoChannel* channel_;
perkjf0dcfe22016-03-10 18:32:00 +0100155 // |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 Brandstetterba29c6a2016-06-27 16:30:35 -0700163 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700164 RtpReceiverObserverInterface* observer_ = nullptr;
165 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700166};
167
168} // namespace webrtc
169
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200170#endif // PC_RTPRECEIVER_H_