OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "media/cast/cast_sender_impl.h" | 4 #include "media/cast/cast_sender_impl.h" |
5 | 5 |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 namespace cast { | 13 namespace cast { |
14 | 14 |
15 // The LocalFrameInput class posts all incoming frames; audio and video to the | 15 // The LocalVideoFrameInput class posts all incoming video frames to the main |
16 // main cast thread for processing. | 16 // cast thread for processing. |
17 // This make the cast sender interface thread safe. | 17 class LocalVideoFrameInput : public VideoFrameInput { |
18 class LocalFrameInput : public FrameInput { | |
19 public: | 18 public: |
20 LocalFrameInput(scoped_refptr<CastEnvironment> cast_environment, | 19 LocalVideoFrameInput(scoped_refptr<CastEnvironment> cast_environment, |
21 base::WeakPtr<AudioSender> audio_sender, | 20 base::WeakPtr<VideoSender> video_sender) |
22 base::WeakPtr<VideoSender> video_sender) | 21 : cast_environment_(cast_environment), video_sender_(video_sender) {} |
23 : cast_environment_(cast_environment), | |
24 audio_sender_(audio_sender), | |
25 video_sender_(video_sender) {} | |
26 | 22 |
27 virtual void InsertRawVideoFrame( | 23 virtual void InsertRawVideoFrame( |
28 const scoped_refptr<media::VideoFrame>& video_frame, | 24 const scoped_refptr<media::VideoFrame>& video_frame, |
29 const base::TimeTicks& capture_time) OVERRIDE { | 25 const base::TimeTicks& capture_time) OVERRIDE { |
30 cast_environment_->PostTask(CastEnvironment::MAIN, | 26 cast_environment_->PostTask(CastEnvironment::MAIN, |
31 FROM_HERE, | 27 FROM_HERE, |
32 base::Bind(&VideoSender::InsertRawVideoFrame, | 28 base::Bind(&VideoSender::InsertRawVideoFrame, |
33 video_sender_, | 29 video_sender_, |
34 video_frame, | 30 video_frame, |
35 capture_time)); | 31 capture_time)); |
36 } | 32 } |
37 | 33 |
| 34 protected: |
| 35 virtual ~LocalVideoFrameInput() {} |
| 36 |
| 37 private: |
| 38 friend class base::RefCountedThreadSafe<LocalVideoFrameInput>; |
| 39 |
| 40 scoped_refptr<CastEnvironment> cast_environment_; |
| 41 base::WeakPtr<VideoSender> video_sender_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(LocalVideoFrameInput); |
| 44 }; |
| 45 |
| 46 // The LocalAudioFrameInput class posts all incoming audio frames to the main |
| 47 // cast thread for processing. Therefore frames can be inserted from any thread. |
| 48 class LocalAudioFrameInput : public AudioFrameInput { |
| 49 public: |
| 50 LocalAudioFrameInput(scoped_refptr<CastEnvironment> cast_environment, |
| 51 base::WeakPtr<AudioSender> audio_sender) |
| 52 : cast_environment_(cast_environment), audio_sender_(audio_sender) {} |
| 53 |
38 virtual void InsertAudio(const AudioBus* audio_bus, | 54 virtual void InsertAudio(const AudioBus* audio_bus, |
39 const base::TimeTicks& recorded_time, | 55 const base::TimeTicks& recorded_time, |
40 const base::Closure& done_callback) OVERRIDE { | 56 const base::Closure& done_callback) OVERRIDE { |
41 cast_environment_->PostTask(CastEnvironment::MAIN, | 57 cast_environment_->PostTask(CastEnvironment::MAIN, |
42 FROM_HERE, | 58 FROM_HERE, |
43 base::Bind(&AudioSender::InsertAudio, | 59 base::Bind(&AudioSender::InsertAudio, |
44 audio_sender_, | 60 audio_sender_, |
45 audio_bus, | 61 audio_bus, |
46 recorded_time, | 62 recorded_time, |
47 done_callback)); | 63 done_callback)); |
48 } | 64 } |
49 | 65 |
50 protected: | 66 protected: |
51 virtual ~LocalFrameInput() {} | 67 virtual ~LocalAudioFrameInput() {} |
52 | 68 |
53 private: | 69 private: |
54 friend class base::RefCountedThreadSafe<LocalFrameInput>; | 70 friend class base::RefCountedThreadSafe<LocalAudioFrameInput>; |
55 | 71 |
56 scoped_refptr<CastEnvironment> cast_environment_; | 72 scoped_refptr<CastEnvironment> cast_environment_; |
57 base::WeakPtr<AudioSender> audio_sender_; | 73 base::WeakPtr<AudioSender> audio_sender_; |
58 base::WeakPtr<VideoSender> video_sender_; | |
59 | 74 |
60 DISALLOW_COPY_AND_ASSIGN(LocalFrameInput); | 75 DISALLOW_COPY_AND_ASSIGN(LocalAudioFrameInput); |
61 }; | 76 }; |
62 | 77 |
63 CastSender* CastSender::CreateCastSender( | 78 scoped_ptr<CastSender> CastSender::Create( |
64 scoped_refptr<CastEnvironment> cast_environment, | 79 scoped_refptr<CastEnvironment> cast_environment, |
65 const AudioSenderConfig* audio_config, | |
66 const VideoSenderConfig* video_config, | |
67 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, | |
68 const CastInitializationCallback& initialization_status, | |
69 transport::CastTransportSender* const transport_sender) { | 80 transport::CastTransportSender* const transport_sender) { |
70 CHECK(cast_environment); | 81 CHECK(cast_environment); |
71 return new CastSenderImpl(cast_environment, | 82 return scoped_ptr<CastSender>( |
72 audio_config, | 83 new CastSenderImpl(cast_environment, transport_sender)); |
73 video_config, | |
74 gpu_factories, | |
75 initialization_status, | |
76 transport_sender); | |
77 } | 84 } |
78 | 85 |
79 CastSenderImpl::CastSenderImpl( | 86 CastSenderImpl::CastSenderImpl( |
80 scoped_refptr<CastEnvironment> cast_environment, | 87 scoped_refptr<CastEnvironment> cast_environment, |
81 const AudioSenderConfig* audio_config, | |
82 const VideoSenderConfig* video_config, | |
83 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, | |
84 const CastInitializationCallback& initialization_status, | |
85 transport::CastTransportSender* const transport_sender) | 88 transport::CastTransportSender* const transport_sender) |
86 : initialization_callback_(initialization_status), | 89 : cast_environment_(cast_environment), |
87 packet_receiver_( | 90 transport_sender_(transport_sender), |
88 base::Bind(&CastSenderImpl::ReceivedPacket, base::Unretained(this))), | |
89 cast_environment_(cast_environment), | |
90 weak_factory_(this) { | 91 weak_factory_(this) { |
91 CHECK(cast_environment); | 92 CHECK(cast_environment); |
92 CHECK(audio_config || video_config); | 93 } |
93 | 94 |
94 base::WeakPtr<AudioSender> audio_sender_ptr; | 95 void CastSenderImpl::InitializeAudio( |
95 base::WeakPtr<VideoSender> video_sender_ptr; | 96 const AudioSenderConfig& audio_config, |
| 97 const CastInitializationCallback& cast_initialization_cb) { |
| 98 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 99 CHECK(audio_config.use_external_encoder || |
| 100 cast_environment_->HasAudioEncoderThread()); |
96 | 101 |
97 if (audio_config) { | 102 audio_sender_.reset( |
98 CHECK(audio_config->use_external_encoder || | 103 new AudioSender(cast_environment_, audio_config, transport_sender_)); |
99 cast_environment->HasAudioEncoderThread()); | |
100 | 104 |
101 audio_sender_.reset( | 105 CastInitializationStatus status = audio_sender_->InitializationResult(); |
102 new AudioSender(cast_environment, *audio_config, transport_sender)); | |
103 ssrc_of_audio_sender_ = audio_config->incoming_feedback_ssrc; | |
104 audio_sender_ptr = audio_sender_->AsWeakPtr(); | |
105 | 106 |
106 CastInitializationStatus status = audio_sender_->InitializationResult(); | 107 if (status == STATUS_AUDIO_INITIALIZED) { |
107 if (status != STATUS_INITIALIZED || !video_config) { | 108 ssrc_of_audio_sender_ = audio_config.incoming_feedback_ssrc; |
108 if (status == STATUS_INITIALIZED && !video_config) { | 109 audio_frame_input_ = |
109 // Audio only. | 110 new LocalAudioFrameInput(cast_environment_, audio_sender_->AsWeakPtr()); |
110 frame_input_ = new LocalFrameInput( | |
111 cast_environment, audio_sender_ptr, video_sender_ptr); | |
112 } | |
113 cast_environment->PostTask( | |
114 CastEnvironment::MAIN, | |
115 FROM_HERE, | |
116 base::Bind(&CastSenderImpl::InitializationResult, | |
117 weak_factory_.GetWeakPtr(), | |
118 status)); | |
119 return; | |
120 } | |
121 } | 111 } |
122 if (video_config) { | 112 cast_initialization_cb.Run(status); |
123 CHECK(video_config->use_external_encoder || | 113 } |
124 cast_environment->HasVideoEncoderThread()); | |
125 | 114 |
126 video_sender_.reset( | 115 void CastSenderImpl::InitializeVideo( |
127 new VideoSender(cast_environment, | 116 const VideoSenderConfig& video_config, |
128 *video_config, | 117 const CastInitializationCallback& cast_initialization_cb, |
129 gpu_factories, | 118 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories) { |
130 base::Bind(&CastSenderImpl::InitializationResult, | 119 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
131 weak_factory_.GetWeakPtr()), | 120 CHECK(video_config.use_external_encoder || |
132 transport_sender)); | 121 cast_environment_->HasVideoEncoderThread()); |
133 video_sender_ptr = video_sender_->AsWeakPtr(); | |
134 ssrc_of_video_sender_ = video_config->incoming_feedback_ssrc; | |
135 } | |
136 frame_input_ = | |
137 new LocalFrameInput(cast_environment, audio_sender_ptr, video_sender_ptr); | |
138 | 122 |
139 // Handing over responsibility to call NotifyInitialization to the | 123 video_sender_.reset(new VideoSender(cast_environment_, |
140 // video sender. | 124 video_config, |
| 125 gpu_factories, |
| 126 cast_initialization_cb, |
| 127 transport_sender_)); |
| 128 |
| 129 ssrc_of_video_sender_ = video_config.incoming_feedback_ssrc; |
| 130 video_frame_input_ = |
| 131 new LocalVideoFrameInput(cast_environment_, video_sender_->AsWeakPtr()); |
141 } | 132 } |
142 | 133 |
143 CastSenderImpl::~CastSenderImpl() {} | 134 CastSenderImpl::~CastSenderImpl() {} |
144 | 135 |
145 // ReceivedPacket handle the incoming packets to the cast sender | 136 // ReceivedPacket handle the incoming packets to the cast sender |
146 // it's only expected to receive RTCP feedback packets from the remote cast | 137 // it's only expected to receive RTCP feedback packets from the remote cast |
147 // receiver. The class verifies that that it is a RTCP packet and based on the | 138 // receiver. The class verifies that that it is a RTCP packet and based on the |
148 // SSRC of the incoming packet route the packet to the correct sender; audio or | 139 // SSRC of the incoming packet route the packet to the correct sender; audio or |
149 // video. | 140 // video. |
150 // | 141 // |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 FROM_HERE, | 187 FROM_HERE, |
197 base::Bind(&VideoSender::IncomingRtcpPacket, | 188 base::Bind(&VideoSender::IncomingRtcpPacket, |
198 video_sender_->AsWeakPtr(), | 189 video_sender_->AsWeakPtr(), |
199 base::Passed(&packet))); | 190 base::Passed(&packet))); |
200 } else { | 191 } else { |
201 VLOG(1) << "Received a RTCP packet with a non matching sender SSRC " | 192 VLOG(1) << "Received a RTCP packet with a non matching sender SSRC " |
202 << ssrc_of_sender; | 193 << ssrc_of_sender; |
203 } | 194 } |
204 } | 195 } |
205 | 196 |
206 scoped_refptr<FrameInput> CastSenderImpl::frame_input() { return frame_input_; } | 197 scoped_refptr<AudioFrameInput> CastSenderImpl::audio_frame_input() { |
| 198 return audio_frame_input_; |
| 199 } |
| 200 |
| 201 scoped_refptr<VideoFrameInput> CastSenderImpl::video_frame_input() { |
| 202 return video_frame_input_; |
| 203 } |
207 | 204 |
208 transport::PacketReceiverCallback CastSenderImpl::packet_receiver() { | 205 transport::PacketReceiverCallback CastSenderImpl::packet_receiver() { |
209 return packet_receiver_; | 206 return base::Bind(&CastSenderImpl::ReceivedPacket, |
210 return base::Bind(&CastSenderImpl::ReceivedPacket, base::Unretained(this)); | 207 weak_factory_.GetWeakPtr()); |
211 } | |
212 | |
213 void CastSenderImpl::InitializationResult(CastInitializationStatus status) | |
214 const { | |
215 initialization_callback_.Run(status); | |
216 } | 208 } |
217 | 209 |
218 } // namespace cast | 210 } // namespace cast |
219 } // namespace media | 211 } // namespace media |
OLD | NEW |