blob: d5f88a941add3df7e8a2bcdc3efe0a0ab62eb073 [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
32#ifndef TALK_APP_WEBRTC_RTPSENDER_H_
33#define TALK_APP_WEBRTC_RTPSENDER_H_
34
35#include <string>
36
37#include "talk/app/webrtc/mediastreamprovider.h"
38#include "talk/app/webrtc/rtpsenderinterface.h"
deadbeeffac06552015-11-25 11:26:01 -080039#include "talk/app/webrtc/statscollector.h"
deadbeef70ab1a12015-09-28 16:53:55 -070040#include "talk/media/base/audiorenderer.h"
41#include "webrtc/base/basictypes.h"
42#include "webrtc/base/criticalsection.h"
43#include "webrtc/base/scoped_ptr.h"
44
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,
60 int number_of_channels,
61 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
81 // Randomly generates id and stream_id.
82 AudioRtpSender(AudioProviderInterface* provider, StatsCollector* stats);
deadbeef70ab1a12015-09-28 16:53:55 -070083
84 virtual ~AudioRtpSender();
85
86 // ObserverInterface implementation
87 void OnChanged() override;
88
89 // RtpSenderInterface implementation
90 bool SetTrack(MediaStreamTrackInterface* track) override;
91 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
92 return track_.get();
93 }
94
deadbeeffac06552015-11-25 11:26:01 -080095 void SetSsrc(uint32_t ssrc) override;
96
97 uint32_t ssrc() const override { return ssrc_; }
98
99 cricket::MediaType media_type() const override {
100 return cricket::MEDIA_TYPE_AUDIO;
101 }
102
deadbeef70ab1a12015-09-28 16:53:55 -0700103 std::string id() const override { return id_; }
104
deadbeeffac06552015-11-25 11:26:01 -0800105 void set_stream_id(const std::string& stream_id) override {
106 stream_id_ = stream_id;
107 }
108 std::string stream_id() const override { return stream_id_; }
109
deadbeef70ab1a12015-09-28 16:53:55 -0700110 void Stop() override;
111
112 private:
deadbeeffac06552015-11-25 11:26:01 -0800113 bool can_send_track() const { return track_ && ssrc_; }
114 // Helper function to construct options for
115 // AudioProviderInterface::SetAudioSend.
116 void SetAudioSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700117
118 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800119 std::string stream_id_;
deadbeef5def7b92015-11-20 11:43:22 -0800120 AudioProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800121 StatsCollector* stats_;
122 rtc::scoped_refptr<AudioTrackInterface> track_;
123 uint32_t ssrc_ = 0;
124 bool cached_track_enabled_ = false;
125 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700126
127 // Used to pass the data callback from the |track_| to the other end of
128 // cricket::AudioRenderer.
129 rtc::scoped_ptr<LocalAudioSinkAdapter> sink_adapter_;
130};
131
132class VideoRtpSender : public ObserverInterface,
133 public rtc::RefCountedObject<RtpSenderInterface> {
134 public:
135 VideoRtpSender(VideoTrackInterface* track,
deadbeeffac06552015-11-25 11:26:01 -0800136 const std::string& stream_id,
deadbeef70ab1a12015-09-28 16:53:55 -0700137 VideoProviderInterface* provider);
138
deadbeeffac06552015-11-25 11:26:01 -0800139 // Randomly generates id and stream_id.
140 explicit VideoRtpSender(VideoProviderInterface* provider);
141
deadbeef70ab1a12015-09-28 16:53:55 -0700142 virtual ~VideoRtpSender();
143
144 // ObserverInterface implementation
145 void OnChanged() override;
146
147 // RtpSenderInterface implementation
148 bool SetTrack(MediaStreamTrackInterface* track) override;
149 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
150 return track_.get();
151 }
152
deadbeeffac06552015-11-25 11:26:01 -0800153 void SetSsrc(uint32_t ssrc) override;
154
155 uint32_t ssrc() const override { return ssrc_; }
156
157 cricket::MediaType media_type() const override {
158 return cricket::MEDIA_TYPE_VIDEO;
159 }
160
deadbeef70ab1a12015-09-28 16:53:55 -0700161 std::string id() const override { return id_; }
162
deadbeeffac06552015-11-25 11:26:01 -0800163 void set_stream_id(const std::string& stream_id) override {
164 stream_id_ = stream_id;
165 }
166 std::string stream_id() const override { return stream_id_; }
167
deadbeef70ab1a12015-09-28 16:53:55 -0700168 void Stop() override;
169
170 private:
deadbeeffac06552015-11-25 11:26:01 -0800171 bool can_send_track() const { return track_ && ssrc_; }
172 // Helper function to construct options for
173 // VideoProviderInterface::SetVideoSend.
174 void SetVideoSend();
deadbeef70ab1a12015-09-28 16:53:55 -0700175
176 std::string id_;
deadbeeffac06552015-11-25 11:26:01 -0800177 std::string stream_id_;
deadbeef5def7b92015-11-20 11:43:22 -0800178 VideoProviderInterface* provider_;
deadbeeffac06552015-11-25 11:26:01 -0800179 rtc::scoped_refptr<VideoTrackInterface> track_;
180 uint32_t ssrc_ = 0;
181 bool cached_track_enabled_ = false;
182 bool stopped_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700183};
184
185} // namespace webrtc
186
187#endif // TALK_APP_WEBRTC_RTPSENDER_H_