blob: 4b3bd4d4abc431e0412c3b8b2c296e450c0863e6 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#ifndef WEBRTC_API_RTPRECEIVER_H_
16#define WEBRTC_API_RTPRECEIVER_H_
deadbeef70ab1a12015-09-28 16:53:55 -070017
18#include <string>
19
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070020#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/rtpreceiverinterface.h"
perkjd61bf802016-03-24 03:16:19 -070022#include "webrtc/api/remoteaudiosource.h"
perkjf0dcfe22016-03-10 18:32:00 +010023#include "webrtc/api/videotracksource.h"
pbos7eb0e232017-01-02 07:32: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"
deadbeef70ab1a12015-09-28 16:53:55 -070028
29namespace webrtc {
30
deadbeefa601f5c2016-06-06 14:27:39 -070031// Internal class used by PeerConnection.
32class RtpReceiverInternal : public RtpReceiverInterface {
33 public:
34 virtual void Stop() = 0;
35};
36
deadbeef70ab1a12015-09-28 16:53:55 -070037class AudioRtpReceiver : public ObserverInterface,
38 public AudioSourceInterface::AudioObserver,
zhihuang184a3fd2016-06-14 11:47:14 -070039 public rtc::RefCountedObject<RtpReceiverInternal>,
40 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -070041 public:
perkjd61bf802016-03-24 03:16:19 -070042 AudioRtpReceiver(MediaStreamInterface* stream,
43 const std::string& track_id,
Peter Boström0c4e06b2015-10-07 12:23:21 +020044 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070045 cricket::VoiceChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070046
47 virtual ~AudioRtpReceiver();
48
49 // ObserverInterface implementation
50 void OnChanged() override;
51
52 // AudioSourceInterface::AudioObserver implementation
53 void OnSetVolume(double volume) override;
54
perkjd61bf802016-03-24 03:16:19 -070055 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
56 return track_.get();
57 }
58
deadbeef70ab1a12015-09-28 16:53:55 -070059 // RtpReceiverInterface implementation
60 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
61 return track_.get();
62 }
63
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070064 cricket::MediaType media_type() const override {
65 return cricket::MEDIA_TYPE_AUDIO;
66 }
67
deadbeef70ab1a12015-09-28 16:53:55 -070068 std::string id() const override { return id_; }
69
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070070 RtpParameters GetParameters() const override;
71 bool SetParameters(const RtpParameters& parameters) override;
72
deadbeefa601f5c2016-06-06 14:27:39 -070073 // RtpReceiverInternal implementation.
74 void Stop() override;
75
zhihuang184a3fd2016-06-14 11:47:14 -070076 void SetObserver(RtpReceiverObserverInterface* observer) override;
77
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070078 // Does not take ownership.
79 // Should call SetChannel(nullptr) before |channel| is destroyed.
80 void SetChannel(cricket::VoiceChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -070081
deadbeef70ab1a12015-09-28 16:53:55 -070082 private:
83 void Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070084 void OnFirstPacketReceived(cricket::BaseChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -070085
Tommif888bb52015-12-12 01:37:01 +010086 const std::string id_;
Tommif888bb52015-12-12 01:37:01 +010087 const uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070088 cricket::VoiceChannel* channel_;
perkjd61bf802016-03-24 03:16:19 -070089 const rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef70ab1a12015-09-28 16:53:55 -070090 bool cached_track_enabled_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070091 double cached_volume_ = 1;
92 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -070093 RtpReceiverObserverInterface* observer_ = nullptr;
94 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -070095};
96
zhihuang184a3fd2016-06-14 11:47:14 -070097class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>,
98 public sigslot::has_slots<> {
deadbeef70ab1a12015-09-28 16:53:55 -070099 public:
perkjf0dcfe22016-03-10 18:32:00 +0100100 VideoRtpReceiver(MediaStreamInterface* stream,
101 const std::string& track_id,
102 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700104 cricket::VideoChannel* channel);
deadbeef70ab1a12015-09-28 16:53:55 -0700105
106 virtual ~VideoRtpReceiver();
107
perkjf0dcfe22016-03-10 18:32:00 +0100108 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
109 return track_.get();
110 }
111
deadbeef70ab1a12015-09-28 16:53:55 -0700112 // RtpReceiverInterface implementation
113 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
114 return track_.get();
115 }
116
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700117 cricket::MediaType media_type() const override {
118 return cricket::MEDIA_TYPE_VIDEO;
119 }
120
deadbeef70ab1a12015-09-28 16:53:55 -0700121 std::string id() const override { return id_; }
122
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700123 RtpParameters GetParameters() const override;
124 bool SetParameters(const RtpParameters& parameters) override;
125
deadbeefa601f5c2016-06-06 14:27:39 -0700126 // RtpReceiverInternal implementation.
127 void Stop() override;
128
zhihuang184a3fd2016-06-14 11:47:14 -0700129 void SetObserver(RtpReceiverObserverInterface* observer) override;
130
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700131 // Does not take ownership.
132 // Should call SetChannel(nullptr) before |channel| is destroyed.
133 void SetChannel(cricket::VideoChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700134
deadbeef70ab1a12015-09-28 16:53:55 -0700135 private:
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700136 void OnFirstPacketReceived(cricket::BaseChannel* channel);
zhihuang184a3fd2016-06-14 11:47:14 -0700137
deadbeef70ab1a12015-09-28 16:53:55 -0700138 std::string id_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200139 uint32_t ssrc_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700140 cricket::VideoChannel* channel_;
perkjf0dcfe22016-03-10 18:32:00 +0100141 // |broadcaster_| is needed since the decoder can only handle one sink.
142 // It might be better if the decoder can handle multiple sinks and consider
143 // the VideoSinkWants.
144 rtc::VideoBroadcaster broadcaster_;
145 // |source_| is held here to be able to change the state of the source when
146 // the VideoRtpReceiver is stopped.
147 rtc::scoped_refptr<VideoTrackSource> source_;
148 rtc::scoped_refptr<VideoTrackInterface> track_;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700149 bool stopped_ = false;
zhihuang184a3fd2016-06-14 11:47:14 -0700150 RtpReceiverObserverInterface* observer_ = nullptr;
151 bool received_first_packet_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700152};
153
154} // namespace webrtc
155
Henrik Kjellander15583c12016-02-10 10:53:12 +0100156#endif // WEBRTC_API_RTPRECEIVER_H_