blob: c135f227c03a47adcfd7912fc249a5f71b29b5bf [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
ossu7bb87ee2017-01-23 04:56:25 -080015#ifndef WEBRTC_PC_RTPRECEIVER_H_
16#define WEBRTC_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>
21
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070022#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/rtpreceiverinterface.h"
ossu7bb87ee2017-01-23 04:56:25 -080024#include "webrtc/base/basictypes.h"
zhihuang184a3fd2016-06-14 11:47:14 -070025#include "webrtc/base/sigslot.h"
perkjf0dcfe22016-03-10 18:32:00 +010026#include "webrtc/media/base/videobroadcaster.h"
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070027#include "webrtc/pc/channel.h"
ossu7bb87ee2017-01-23 04:56:25 -080028#include "webrtc/pc/remoteaudiosource.h"
29#include "webrtc/pc/videotracksource.h"
deadbeef70ab1a12015-09-28 16:53:55 -070030
31namespace webrtc {
32
deadbeefa601f5c2016-06-06 14:27:39 -070033// Internal class used by PeerConnection.
34class RtpReceiverInternal : public RtpReceiverInterface {
35 public:
36 virtual void Stop() = 0;
37};
38
deadbeef70ab1a12015-09-28 16:53:55 -070039class AudioRtpReceiver : public ObserverInterface,
40 public AudioSourceInterface::AudioObserver,
zhihuang184a3fd2016-06-14 11:47:14 -070041 public rtc::RefCountedObject<RtpReceiverInternal>,
42 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -070043 public:
perkjd61bf802016-03-24 03:16:19 -070044 AudioRtpReceiver(MediaStreamInterface* stream,
45 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 12:23:21 +020046 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070047 cricket::VoiceChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070048
49 virtual ~AudioRtpReceiver();
50
51 // ObserverInterface implementation
52 void OnChanged() override;
53
54 // AudioSourceInterface::AudioObserver implementation
55 void OnSetVolume(double volume) override;
56
perkjd61bf802016-03-24 03:16:19 -070057 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
58 return track_.get();
59 }
60
deadbeef70ab1a12015-09-28 16:53:55 -070061 // RtpReceiverInterface implementation
62 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
63 return track_.get();
64 }
65
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070066 cricket::MediaType media_type() const override {
67 return cricket::MEDIA_TYPE_AUDIO;
68 }
69
deadbeef70ab1a12015-09-28 16:53:55 -070070 std::string id() const override { return id_; }
71
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070072 RtpParameters GetParameters() const override;
73 bool SetParameters(const RtpParameters& parameters) override;
74
deadbeefa601f5c2016-06-06 14:27:39 -070075 // RtpReceiverInternal implementation.
76 void Stop() override;
77
zhihuang184a3fd2016-06-14 11:47:14 -070078 void SetObserver(RtpReceiverObserverInterface* observer) override;
79
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070080 // Does not take ownership.
81 // Should call SetChannel(nullptr) before |channel| is destroyed.
82 void SetChannel(cricket::VoiceChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -070083
deadbeef70ab1a12015-09-28 16:53:55 -070084 private:
85 void Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070086 void OnFirstPacketReceived(cricket::BaseChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070087
Tommif888bb52015-12-12 01:37:01 +010088 const std::string id_;
Tommif888bb52015-12-12 01:37:01 +010089 const uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070090 cricket::VoiceChannel* channel_;
perkjd61bf802016-03-24 03:16:19 -070091 const rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef70ab1a12015-09-28 16:53:55 -070092 bool cached_track_enabled_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070093 double cached_volume_ = 1;
94 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -070095 RtpReceiverObserverInterface* observer_ = nullptr;
96 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -070097};
98
zhihuang184a3fd2016-06-14 11:47:14 -070099class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>,
100 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -0700101 public:
perkjf0dcfe22016-03-10 18:32:00 +0100102 VideoRtpReceiver(MediaStreamInterface* stream,
103 const std::string& track_id,
104 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700106 cricket::VideoChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700107
108 virtual ~VideoRtpReceiver();
109
perkjf0dcfe22016-03-10 18:32:00 +0100110 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
111 return track_.get();
112 }
113
deadbeef70ab1a12015-09-28 16:53:55 -0700114 // RtpReceiverInterface implementation
115 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
116 return track_.get();
117 }
118
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700119 cricket::MediaType media_type() const override {
120 return cricket::MEDIA_TYPE_VIDEO;
121 }
122
deadbeef70ab1a12015-09-28 16:53:55 -0700123 std::string id() const override { return id_; }
124
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700125 RtpParameters GetParameters() const override;
126 bool SetParameters(const RtpParameters& parameters) override;
127
deadbeefa601f5c2016-06-06 14:27:39 -0700128 // RtpReceiverInternal implementation.
129 void Stop() override;
130
zhihuang184a3fd2016-06-14 11:47:14 -0700131 void SetObserver(RtpReceiverObserverInterface* observer) override;
132
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700133 // Does not take ownership.
134 // Should call SetChannel(nullptr) before |channel| is destroyed.
135 void SetChannel(cricket::VideoChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700136
deadbeef70ab1a12015-09-28 16:53:55 -0700137 private:
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700138 void OnFirstPacketReceived(cricket::BaseChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700139
deadbeef70ab1a12015-09-28 16:53:55 -0700140 std::string id_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700142 cricket::VideoChannel* channel_;
perkjf0dcfe22016-03-10 18:32:00 +0100143 // |broadcaster_| is needed since the decoder can only handle one sink.
144 // It might be better if the decoder can handle multiple sinks and consider
145 // the VideoSinkWants.
146 rtc::VideoBroadcaster broadcaster_;
147 // |source_| is held here to be able to change the state of the source when
148 // the VideoRtpReceiver is stopped.
149 rtc::scoped_refptr<VideoTrackSource> source_;
150 rtc::scoped_refptr<VideoTrackInterface> track_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700151 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700152 RtpReceiverObserverInterface* observer_ = nullptr;
153 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700154};
155
156} // namespace webrtc
157
ossu7bb87ee2017-01-23 04:56:25 -0800158#endif // WEBRTC_PC_RTPRECEIVER_H_