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. Therefore frames can be inserted from any thread. |
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, | 80 const CastInitializationCallback& cast_initialization_cb, |
66 const VideoSenderConfig* video_config, | |
67 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, | |
68 const CastInitializationCallback& initialization_status, | |
69 transport::CastTransportSender* const transport_sender) { | 81 transport::CastTransportSender* const transport_sender) { |
70 CHECK(cast_environment); | 82 CHECK(cast_environment); |
71 return new CastSenderImpl(cast_environment, | 83 return scoped_ptr<CastSender>(new CastSenderImpl( |
72 audio_config, | 84 cast_environment, cast_initialization_cb, transport_sender)); |
73 video_config, | |
74 gpu_factories, | |
75 initialization_status, | |
76 transport_sender); | |
77 } | 85 } |
78 | 86 |
79 CastSenderImpl::CastSenderImpl( | 87 CastSenderImpl::CastSenderImpl( |
80 scoped_refptr<CastEnvironment> cast_environment, | 88 scoped_refptr<CastEnvironment> cast_environment, |
81 const AudioSenderConfig* audio_config, | 89 const CastInitializationCallback& cast_initialization_cb, |
82 const VideoSenderConfig* video_config, | |
83 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, | |
84 const CastInitializationCallback& initialization_status, | |
85 transport::CastTransportSender* const transport_sender) | 90 transport::CastTransportSender* const transport_sender) |
86 : initialization_callback_(initialization_status), | 91 : initialization_callback_(cast_initialization_cb), |
87 packet_receiver_( | |
88 base::Bind(&CastSenderImpl::ReceivedPacket, base::Unretained(this))), | |
89 cast_environment_(cast_environment), | 92 cast_environment_(cast_environment), |
93 transport_sender_(transport_sender), | |
94 ssrc_of_audio_sender_(0), | |
95 ssrc_of_video_sender_(0), | |
90 weak_factory_(this) { | 96 weak_factory_(this) { |
91 CHECK(cast_environment); | 97 CHECK(cast_environment); |
92 CHECK(audio_config || video_config); | 98 } |
93 | 99 |
94 base::WeakPtr<AudioSender> audio_sender_ptr; | 100 void CastSenderImpl::InitializeAudio(const AudioSenderConfig& audio_config) { |
95 base::WeakPtr<VideoSender> video_sender_ptr; | 101 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
102 CHECK(audio_config.use_external_encoder || | |
103 cast_environment_->HasAudioEncoderThread()); | |
96 | 104 |
97 if (audio_config) { | 105 audio_sender_.reset( |
98 CHECK(audio_config->use_external_encoder || | 106 new AudioSender(cast_environment_, audio_config, transport_sender_)); |
99 cast_environment->HasAudioEncoderThread()); | |
100 | 107 |
101 audio_sender_.reset( | 108 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 | 109 |
106 CastInitializationStatus status = audio_sender_->InitializationResult(); | 110 if (status == STATUS_AUDIO_INITIALIZED) { |
107 if (status != STATUS_INITIALIZED || !video_config) { | 111 ssrc_of_audio_sender_ = audio_config.incoming_feedback_ssrc; |
108 if (status == STATUS_INITIALIZED && !video_config) { | 112 audio_frame_input_ = |
109 // Audio only. | 113 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 } | 114 } |
122 if (video_config) { | |
123 CHECK(video_config->use_external_encoder || | |
124 cast_environment->HasVideoEncoderThread()); | |
125 | 115 |
126 video_sender_.reset( | 116 cast_environment_->PostTask(CastEnvironment::MAIN, |
127 new VideoSender(cast_environment, | 117 FROM_HERE, |
128 *video_config, | 118 base::Bind(&CastSenderImpl::InitializationResult, |
129 gpu_factories, | 119 weak_factory_.GetWeakPtr(), |
130 base::Bind(&CastSenderImpl::InitializationResult, | 120 status)); |
131 weak_factory_.GetWeakPtr()), | 121 } |
132 transport_sender)); | |
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 void CastSenderImpl::InitializeVideo( |
140 // video sender. | 124 const VideoSenderConfig& video_config, |
125 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories) { | |
126 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
127 CHECK(video_config.use_external_encoder || | |
128 cast_environment_->HasVideoEncoderThread()); | |
129 | |
130 video_sender_.reset( | |
131 new VideoSender(cast_environment_, | |
132 video_config, | |
133 gpu_factories, | |
134 base::Bind(&CastSenderImpl::InitializationResult, | |
135 weak_factory_.GetWeakPtr()), | |
136 transport_sender_)); | |
137 | |
138 ssrc_of_video_sender_ = video_config.incoming_feedback_ssrc; | |
139 video_frame_input_ = | |
140 new LocalVideoFrameInput(cast_environment_, video_sender_->AsWeakPtr()); | |
141 } | 141 } |
142 | 142 |
143 CastSenderImpl::~CastSenderImpl() {} | 143 CastSenderImpl::~CastSenderImpl() {} |
144 | 144 |
145 // ReceivedPacket handle the incoming packets to the cast sender | 145 // ReceivedPacket handle the incoming packets to the cast sender |
146 // it's only expected to receive RTCP feedback packets from the remote cast | 146 // 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 | 147 // 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 | 148 // SSRC of the incoming packet route the packet to the correct sender; audio or |
149 // video. | 149 // video. |
150 // | 150 // |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 FROM_HERE, | 196 FROM_HERE, |
197 base::Bind(&VideoSender::IncomingRtcpPacket, | 197 base::Bind(&VideoSender::IncomingRtcpPacket, |
198 video_sender_->AsWeakPtr(), | 198 video_sender_->AsWeakPtr(), |
199 base::Passed(&packet))); | 199 base::Passed(&packet))); |
200 } else { | 200 } else { |
201 VLOG(1) << "Received a RTCP packet with a non matching sender SSRC " | 201 VLOG(1) << "Received a RTCP packet with a non matching sender SSRC " |
202 << ssrc_of_sender; | 202 << ssrc_of_sender; |
203 } | 203 } |
204 } | 204 } |
205 | 205 |
206 scoped_refptr<FrameInput> CastSenderImpl::frame_input() { return frame_input_; } | 206 scoped_refptr<AudioFrameInput> CastSenderImpl::audio_frame_input() { |
207 return audio_frame_input_; | |
Alpha Left Google
2014/02/18 22:28:13
This method is not really thread-safe. I'd just sa
mikhal1
2014/03/05 21:44:05
Done.
| |
208 } | |
209 | |
210 scoped_refptr<VideoFrameInput> CastSenderImpl::video_frame_input() { | |
211 return video_frame_input_; | |
212 } | |
207 | 213 |
208 transport::PacketReceiverCallback CastSenderImpl::packet_receiver() { | 214 transport::PacketReceiverCallback CastSenderImpl::packet_receiver() { |
209 return packet_receiver_; | 215 return base::Bind(&CastSenderImpl::ReceivedPacket, |
210 return base::Bind(&CastSenderImpl::ReceivedPacket, base::Unretained(this)); | 216 weak_factory_.GetWeakPtr()); |
211 } | 217 } |
212 | 218 |
213 void CastSenderImpl::InitializationResult(CastInitializationStatus status) | 219 void CastSenderImpl::InitializationResult(CastInitializationStatus status) |
214 const { | 220 const { |
215 initialization_callback_.Run(status); | 221 initialization_callback_.Run(status); |
216 } | 222 } |
217 | 223 |
218 } // namespace cast | 224 } // namespace cast |
219 } // namespace media | 225 } // namespace media |
OLD | NEW |