1 /*
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 
28 // 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"
39 #include "talk/app/webrtc/statscollector.h"
40 #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 
45 namespace webrtc {
46 
47 // LocalAudioSinkAdapter receives data callback as a sink to the local
48 // AudioTrack, and passes the data to the sink of AudioRenderer.
49 class 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               size_t 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 
71 class AudioRtpSender : public ObserverInterface,
72                        public rtc::RefCountedObject<RtpSenderInterface> {
73  public:
74   // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
75   // at the appropriate times.
76   AudioRtpSender(AudioTrackInterface* track,
77                  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);
83 
84   virtual ~AudioRtpSender();
85 
86   // ObserverInterface implementation
87   void OnChanged() override;
88 
89   // RtpSenderInterface implementation
90   bool SetTrack(MediaStreamTrackInterface* track) override;
track()91   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
92     return track_.get();
93   }
94 
95   void SetSsrc(uint32_t ssrc) override;
96 
ssrc()97   uint32_t ssrc() const override { return ssrc_; }
98 
media_type()99   cricket::MediaType media_type() const override {
100     return cricket::MEDIA_TYPE_AUDIO;
101   }
102 
id()103   std::string id() const override { return id_; }
104 
set_stream_id(const std::string & stream_id)105   void set_stream_id(const std::string& stream_id) override {
106     stream_id_ = stream_id;
107   }
stream_id()108   std::string stream_id() const override { return stream_id_; }
109 
110   void Stop() override;
111 
112  private:
can_send_track()113   bool can_send_track() const { return track_ && ssrc_; }
114   // Helper function to construct options for
115   // AudioProviderInterface::SetAudioSend.
116   void SetAudioSend();
117 
118   std::string id_;
119   std::string stream_id_;
120   AudioProviderInterface* provider_;
121   StatsCollector* stats_;
122   rtc::scoped_refptr<AudioTrackInterface> track_;
123   uint32_t ssrc_ = 0;
124   bool cached_track_enabled_ = false;
125   bool stopped_ = false;
126 
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 
132 class VideoRtpSender : public ObserverInterface,
133                        public rtc::RefCountedObject<RtpSenderInterface> {
134  public:
135   VideoRtpSender(VideoTrackInterface* track,
136                  const std::string& stream_id,
137                  VideoProviderInterface* provider);
138 
139   // Randomly generates id and stream_id.
140   explicit VideoRtpSender(VideoProviderInterface* provider);
141 
142   virtual ~VideoRtpSender();
143 
144   // ObserverInterface implementation
145   void OnChanged() override;
146 
147   // RtpSenderInterface implementation
148   bool SetTrack(MediaStreamTrackInterface* track) override;
track()149   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
150     return track_.get();
151   }
152 
153   void SetSsrc(uint32_t ssrc) override;
154 
ssrc()155   uint32_t ssrc() const override { return ssrc_; }
156 
media_type()157   cricket::MediaType media_type() const override {
158     return cricket::MEDIA_TYPE_VIDEO;
159   }
160 
id()161   std::string id() const override { return id_; }
162 
set_stream_id(const std::string & stream_id)163   void set_stream_id(const std::string& stream_id) override {
164     stream_id_ = stream_id;
165   }
stream_id()166   std::string stream_id() const override { return stream_id_; }
167 
168   void Stop() override;
169 
170  private:
can_send_track()171   bool can_send_track() const { return track_ && ssrc_; }
172   // Helper function to construct options for
173   // VideoProviderInterface::SetVideoSend.
174   void SetVideoSend();
175 
176   std::string id_;
177   std::string stream_id_;
178   VideoProviderInterface* provider_;
179   rtc::scoped_refptr<VideoTrackInterface> track_;
180   uint32_t ssrc_ = 0;
181   bool cached_track_enabled_ = false;
182   bool stopped_ = false;
183 };
184 
185 }  // namespace webrtc
186 
187 #endif  // TALK_APP_WEBRTC_RTPSENDER_H_
188