blob: 45b765de9f4a25b2c3c64ae4e766d5489a8b8f1a [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
deadbeef70ab1a12015-09-28 16:53:55 -070028// This file contains classes that implement RtpSenderInterface.
29// An RtpSender associates a MediaStreamTrackInterface with an underlying
30// transport (provided by AudioProviderInterface/VideoProviderInterface)
31
Henrik Kjellander15583c12016-02-10 10:53:12 +010032#ifndef WEBRTC_API_RTPSENDER_H_
33#define WEBRTC_API_RTPSENDER_H_
deadbeef70ab1a12015-09-28 16:53:55 -070034
35#include <string>
36
Henrik Kjellander15583c12016-02-10 10:53:12 +010037#include "webrtc/api/mediastreamprovider.h"
38#include "webrtc/api/rtpsenderinterface.h"
39#include "webrtc/api/statscollector.h"
deadbeef70ab1a12015-09-28 16:53:55 -070040#include "webrtc/base/basictypes.h"
41#include "webrtc/base/criticalsection.h"
42#include "webrtc/base/scoped_ptr.h"
kjellandera96e2d72016-02-04 23:52:28 -080043#include "webrtc/media/base/audiorenderer.h"
deadbeef70ab1a12015-09-28 16:53:55 -070044
45namespace webrtc {
46
47// LocalAudioSinkAdapter receives data callback as a sink to the local
48// AudioTrack, and passes the data to the sink of AudioRenderer.
49class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
50 public cricket::AudioRenderer {
51 public:
52 LocalAudioSinkAdapter();
53 virtual ~LocalAudioSinkAdapter();
54
55 private:
56 // AudioSinkInterface implementation.
57 void OnData(const void* audio_data,
58 int bits_per_sample,
59 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080060 size_t number_of_channels,
deadbeef70ab1a12015-09-28 16:53:55 -070061 size_t number_of_frames) override;
62
63 // cricket::AudioRenderer implementation.
64 void SetSink(cricket::AudioRenderer::Sink* sink) override;
65
66 cricket::AudioRenderer::Sink* sink_;
67 // Critical section protecting |sink_|.
68 rtc::CriticalSection lock_;
69};
70
71class AudioRtpSender : public ObserverInterface,
72 public rtc::RefCountedObject<RtpSenderInterface> {
73 public:
deadbeeffac06552015-11-25 11:26:01 -080074 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
75 // at the appropriate times.
deadbeef70ab1a12015-09-28 16:53:55 -070076 AudioRtpSender(AudioTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -080077 const std::string& stream_id,
78 AudioProviderInterface* provider,
79 StatsCollector* stats);
80
deadbeefe1f9d832016-01-14 15:35:42 -080081 // Randomly generates stream_id.
82 AudioRtpSender(AudioTrackInterface* track,
83 AudioProviderInterface* provider,
84 StatsCollector* stats);
85
deadbeeffac06552015-11-25 11:26:01 -080086 // Randomly generates id and stream_id.
87 AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats);
deadbeef70ab1a12015-09-28 16:53:55 -070088
89 virtual ~AudioRtpSender();
90
91 // ObserverInterface implementation
92 void OnChanged() override;
93
94 // RtpSenderInterface implementation
95 bool SetTrack(MediaStreamTrackInterface* track) override;
96 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
97 return track_.get();
98 }
99
deadbeeffac06552015-11-25 11:26:01 -0800100 void SetSsrc(uint32_t ssrc) override;
101
102 uint32_t ssrc() const override { return ssrc_; }
103
104 cricket::MediaType media_type() const override {
105 return cricket::MEDIA_TYPE_AUDIO;
106 }
107
deadbeef70ab1a12015-09-28 16:53:55 -0700108 std::string id() const override { return id_; }
109
deadbeeffac06552015-11-25 11:26:01 -0800110 void set_stream_id(const std::string& stream_id) override {
111 stream_id_ = stream_id;
112 }
113 std::string stream_id() const override { return stream_id_; }
114
deadbeef70ab1a12015-09-28 16:53:55 -0700115 void Stop() override;
116
117 private:
deadbeeffac06552015-11-25 11:26:01 -0800118 bool can_send_track() const { return track_ && ssrc_; }
119 // Helper function to construct options for
120 // AudioProviderInterface::SetAudioSend.
121 void SetAudioSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700122
123 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800124 std::string stream_id_;
deadbeef5def7b92015-11-20 11:43:22 -0800125 AudioProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800126 StatsCollector* stats_;
127 rtc::scoped_refptr<AudioTrackInterface> track_;
128 uint32_t ssrc_ = 0;
129 bool cached_track_enabled_ = false;
130 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700131
132 // Used to pass the data callback from the |track_| to the other end of
133 // cricket::AudioRenderer.
134 rtc::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_;
135};
136
137class VideoRtpSender : public ObserverInterface,
138 public rtc::RefCountedObject<RtpSenderInterface> {
139 public:
140 VideoRtpSender(VideoTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -0800141 const std::string& stream_id,
deadbeef70ab1a12015-09-28 16:53:55 -0700142 VideoProviderInterface* provider);
143
deadbeefe1f9d832016-01-14 15:35:42 -0800144 // Randomly generates stream_id.
145 VideoRtpSender(VideoTrackInterface* track, VideoProviderInterface* provider);
146
deadbeeffac06552015-11-25 11:26:01 -0800147 // Randomly generates id and stream_id.
148 explicit VideoRtpSender(VideoProviderInterface* provider);
149
deadbeef70ab1a12015-09-28 16:53:55 -0700150 virtual ~VideoRtpSender();
151
152 // ObserverInterface implementation
153 void OnChanged() override;
154
155 // RtpSenderInterface implementation
156 bool SetTrack(MediaStreamTrackInterface* track) override;
157 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
158 return track_.get();
159 }
160
deadbeeffac06552015-11-25 11:26:01 -0800161 void SetSsrc(uint32_t ssrc) override;
162
163 uint32_t ssrc() const override { return ssrc_; }
164
165 cricket::MediaType media_type() const override {
166 return cricket::MEDIA_TYPE_VIDEO;
167 }
168
deadbeef70ab1a12015-09-28 16:53:55 -0700169 std::string id() const override { return id_; }
170
deadbeeffac06552015-11-25 11:26:01 -0800171 void set_stream_id(const std::string& stream_id) override {
172 stream_id_ = stream_id;
173 }
174 std::string stream_id() const override { return stream_id_; }
175
deadbeef70ab1a12015-09-28 16:53:55 -0700176 void Stop() override;
177
178 private:
deadbeeffac06552015-11-25 11:26:01 -0800179 bool can_send_track() const { return track_ && ssrc_; }
180 // Helper function to construct options for
181 // VideoProviderInterface::SetVideoSend.
182 void SetVideoSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700183
184 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800185 std::string stream_id_;
deadbeef5def7b92015-11-20 11:43:22 -0800186 VideoProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800187 rtc::scoped_refptr<VideoTrackInterface> track_;
188 uint32_t ssrc_ = 0;
189 bool cached_track_enabled_ = false;
190 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700191};
192
193} // namespace webrtc
194
Henrik Kjellander15583c12016-02-10 10:53:12 +0100195#endif // WEBRTC_API_RTPSENDER_H_