OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 4 |
5 #include "content/renderer/pepper_plugin_delegate_impl.h" | 5 #include "content/renderer/pepper_plugin_delegate_impl.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "content/common/pepper_plugin_registry.h" | 28 #include "content/common/pepper_plugin_registry.h" |
29 #include "content/common/pepper_messages.h" | 29 #include "content/common/pepper_messages.h" |
30 #include "content/common/quota_dispatcher.h" | 30 #include "content/common/quota_dispatcher.h" |
31 #include "content/common/view_messages.h" | 31 #include "content/common/view_messages.h" |
32 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
33 #include "content/public/renderer/content_renderer_client.h" | 33 #include "content/public/renderer/content_renderer_client.h" |
34 #include "content/renderer/gpu/command_buffer_proxy.h" | 34 #include "content/renderer/gpu/command_buffer_proxy.h" |
35 #include "content/renderer/gpu/gpu_channel_host.h" | 35 #include "content/renderer/gpu/gpu_channel_host.h" |
36 #include "content/renderer/gpu/renderer_gl_context.h" | 36 #include "content/renderer/gpu/renderer_gl_context.h" |
37 #include "content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h" | 37 #include "content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h" |
| 38 #include "content/renderer/media/audio_input_message_filter.h" |
38 #include "content/renderer/media/audio_message_filter.h" | 39 #include "content/renderer/media/audio_message_filter.h" |
39 #include "content/renderer/media/video_capture_impl_manager.h" | 40 #include "content/renderer/media/video_capture_impl_manager.h" |
40 #include "content/renderer/p2p/p2p_transport_impl.h" | 41 #include "content/renderer/p2p/p2p_transport_impl.h" |
41 #include "content/renderer/pepper_platform_context_3d_impl.h" | 42 #include "content/renderer/pepper_platform_context_3d_impl.h" |
42 #include "content/renderer/pepper_platform_video_decoder_impl.h" | 43 #include "content/renderer/pepper_platform_video_decoder_impl.h" |
43 #include "content/renderer/render_thread_impl.h" | 44 #include "content/renderer/render_thread_impl.h" |
44 #include "content/renderer/render_view_impl.h" | 45 #include "content/renderer/render_view_impl.h" |
45 #include "content/renderer/render_widget_fullscreen_pepper.h" | 46 #include "content/renderer/render_widget_fullscreen_pepper.h" |
46 #include "content/renderer/webplugin_delegate_proxy.h" | 47 #include "content/renderer/webplugin_delegate_proxy.h" |
47 #include "ipc/ipc_channel_handle.h" | 48 #include "ipc/ipc_channel_handle.h" |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 // occurred while the request was in-flight, so we need to NULL check. | 330 // occurred while the request was in-flight, so we need to NULL check. |
330 if (client_) | 331 if (client_) |
331 client_->StreamCreated(handle, length, socket_handle); | 332 client_->StreamCreated(handle, length, socket_handle); |
332 } else { | 333 } else { |
333 main_message_loop_->PostTask(FROM_HERE, | 334 main_message_loop_->PostTask(FROM_HERE, |
334 NewRunnableMethod(this, &PlatformAudioImpl::OnLowLatencyCreated, | 335 NewRunnableMethod(this, &PlatformAudioImpl::OnLowLatencyCreated, |
335 handle, socket_handle, length)); | 336 handle, socket_handle, length)); |
336 } | 337 } |
337 } | 338 } |
338 | 339 |
| 340 class PlatformAudioInputImpl |
| 341 : public webkit::ppapi::PluginDelegate::PlatformAudioInput, |
| 342 public AudioInputMessageFilter::Delegate, |
| 343 public base::RefCountedThreadSafe<PlatformAudioInputImpl> { |
| 344 public: |
| 345 PlatformAudioInputImpl() |
| 346 : client_(NULL), stream_id_(0), |
| 347 main_message_loop_(MessageLoop::current()) { |
| 348 filter_ = RenderThreadImpl::current()->audio_input_message_filter(); |
| 349 } |
| 350 |
| 351 virtual ~PlatformAudioInputImpl() { |
| 352 // Make sure we have been shut down. Warning: this will usually happen on |
| 353 // the I/O thread! |
| 354 DCHECK_EQ(0, stream_id_); |
| 355 DCHECK(!client_); |
| 356 } |
| 357 |
| 358 // Initialize this audio context. StreamCreated() will be called when the |
| 359 // stream is created. |
| 360 bool Initialize( |
| 361 uint32_t sample_rate, uint32_t sample_count, |
| 362 webkit::ppapi::PluginDelegate::PlatformAudioInput::Client* client); |
| 363 |
| 364 // PlatformAudio implementation (called on main thread). |
| 365 virtual bool StartCapture(); |
| 366 virtual bool StopCapture(); |
| 367 virtual void ShutDown(); |
| 368 |
| 369 private: |
| 370 // I/O thread backends to above functions. |
| 371 void InitializeOnIOThread(const AudioParameters& params); |
| 372 void StartCaptureOnIOThread(); |
| 373 void StopCaptureOnIOThread(); |
| 374 void ShutDownOnIOThread(); |
| 375 |
| 376 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
| 377 base::SyncSocket::Handle socket_handle, |
| 378 uint32 length); |
| 379 |
| 380 virtual void OnVolume(double volume) {} |
| 381 |
| 382 virtual void OnStateChanged(AudioStreamState state) {} |
| 383 |
| 384 virtual void OnDeviceReady(int index) {} |
| 385 |
| 386 // The client to notify when the stream is created. THIS MUST ONLY BE |
| 387 // ACCESSED ON THE MAIN THREAD. |
| 388 webkit::ppapi::PluginDelegate::PlatformAudioInput::Client* client_; |
| 389 |
| 390 // MessageFilter used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE |
| 391 // I/O thread except to send messages and get the message loop. |
| 392 scoped_refptr<AudioInputMessageFilter> filter_; |
| 393 |
| 394 // Our ID on the MessageFilter. THIS MUST ONLY BE ACCESSED ON THE I/O THREAD |
| 395 // or else you could race with the initialize function which sets it. |
| 396 int32 stream_id_; |
| 397 |
| 398 MessageLoop* main_message_loop_; |
| 399 |
| 400 DISALLOW_COPY_AND_ASSIGN(PlatformAudioInputImpl); |
| 401 }; |
| 402 |
| 403 bool PlatformAudioInputImpl::Initialize( |
| 404 uint32_t sample_rate, uint32_t sample_count, |
| 405 webkit::ppapi::PluginDelegate::PlatformAudioInput::Client* client) { |
| 406 DCHECK(client); |
| 407 // Make sure we don't call init more than once. |
| 408 DCHECK_EQ(0, stream_id_); |
| 409 |
| 410 client_ = client; |
| 411 |
| 412 AudioParameters params; |
| 413 params.format = AudioParameters::AUDIO_PCM_LINEAR; |
| 414 params.channels = 1; |
| 415 params.sample_rate = sample_rate; |
| 416 params.bits_per_sample = 16; |
| 417 params.samples_per_packet = sample_count; |
| 418 |
| 419 ChildProcess::current()->io_message_loop()->PostTask( |
| 420 FROM_HERE, |
| 421 NewRunnableMethod(this, &PlatformAudioInputImpl::InitializeOnIOThread, |
| 422 params)); |
| 423 return true; |
| 424 } |
| 425 |
| 426 bool PlatformAudioInputImpl::StartCapture() { |
| 427 ChildProcess::current()->io_message_loop()->PostTask( |
| 428 FROM_HERE, |
| 429 NewRunnableMethod(this, |
| 430 &PlatformAudioInputImpl::StartCaptureOnIOThread)); |
| 431 return true; |
| 432 } |
| 433 |
| 434 bool PlatformAudioInputImpl::StopCapture() { |
| 435 ChildProcess::current()->io_message_loop()->PostTask( |
| 436 FROM_HERE, |
| 437 NewRunnableMethod(this, |
| 438 &PlatformAudioInputImpl::StopCaptureOnIOThread)); |
| 439 return true; |
| 440 } |
| 441 |
| 442 void PlatformAudioInputImpl::ShutDown() { |
| 443 // Called on the main thread to stop all audio callbacks. We must only change |
| 444 // the client on the main thread, and the delegates from the I/O thread. |
| 445 client_ = NULL; |
| 446 ChildProcess::current()->io_message_loop()->PostTask( |
| 447 FROM_HERE, |
| 448 base::Bind(&PlatformAudioInputImpl::ShutDownOnIOThread, this)); |
| 449 } |
| 450 |
| 451 void PlatformAudioInputImpl::InitializeOnIOThread( |
| 452 const AudioParameters& params) { |
| 453 stream_id_ = filter_->AddDelegate(this); |
| 454 filter_->Send(new AudioInputHostMsg_CreateStream(stream_id_, params, true)); |
| 455 } |
| 456 |
| 457 void PlatformAudioInputImpl::StartCaptureOnIOThread() { |
| 458 if (stream_id_) |
| 459 filter_->Send(new AudioInputHostMsg_RecordStream(stream_id_)); |
| 460 } |
| 461 |
| 462 void PlatformAudioInputImpl::StopCaptureOnIOThread() { |
| 463 if (stream_id_) |
| 464 filter_->Send(new AudioInputHostMsg_CloseStream(stream_id_)); |
| 465 } |
| 466 |
| 467 void PlatformAudioInputImpl::ShutDownOnIOThread() { |
| 468 // Make sure we don't call shutdown more than once. |
| 469 if (!stream_id_) |
| 470 return; |
| 471 |
| 472 filter_->Send(new AudioInputHostMsg_CloseStream(stream_id_)); |
| 473 filter_->RemoveDelegate(stream_id_); |
| 474 stream_id_ = 0; |
| 475 |
| 476 Release(); // Release for the delegate, balances out the reference taken in |
| 477 // PepperPluginDelegateImpl::CreateAudioInput. |
| 478 } |
| 479 |
| 480 void PlatformAudioInputImpl::OnLowLatencyCreated( |
| 481 base::SharedMemoryHandle handle, |
| 482 base::SyncSocket::Handle socket_handle, |
| 483 uint32 length) { |
| 484 |
| 485 #if defined(OS_WIN) |
| 486 DCHECK(handle); |
| 487 DCHECK(socket_handle); |
| 488 #else |
| 489 DCHECK_NE(-1, handle.fd); |
| 490 DCHECK_NE(-1, socket_handle); |
| 491 #endif |
| 492 DCHECK(length); |
| 493 |
| 494 if (MessageLoop::current() == main_message_loop_) { |
| 495 // Must dereference the client only on the main thread. Shutdown may have |
| 496 // occurred while the request was in-flight, so we need to NULL check. |
| 497 if (client_) |
| 498 client_->StreamCreated(handle, length, socket_handle); |
| 499 } else { |
| 500 main_message_loop_->PostTask(FROM_HERE, |
| 501 NewRunnableMethod(this, &PlatformAudioInputImpl::OnLowLatencyCreated, |
| 502 handle, socket_handle, length)); |
| 503 } |
| 504 } |
| 505 |
339 class DispatcherDelegate : public ppapi::proxy::ProxyChannel::Delegate { | 506 class DispatcherDelegate : public ppapi::proxy::ProxyChannel::Delegate { |
340 public: | 507 public: |
341 virtual ~DispatcherDelegate() {} | 508 virtual ~DispatcherDelegate() {} |
342 | 509 |
343 // ProxyChannel::Delegate implementation. | 510 // ProxyChannel::Delegate implementation. |
344 virtual base::MessageLoopProxy* GetIPCMessageLoop() { | 511 virtual base::MessageLoopProxy* GetIPCMessageLoop() { |
345 // This is called only in the renderer so we know we have a child process. | 512 // This is called only in the renderer so we know we have a child process. |
346 DCHECK(ChildProcess::current()) << "Must be in the renderer."; | 513 DCHECK(ChildProcess::current()) << "Must be in the renderer."; |
347 return ChildProcess::current()->io_message_loop_proxy(); | 514 return ChildProcess::current()->io_message_loop_proxy(); |
348 } | 515 } |
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 webkit::ppapi::PluginDelegate::PlatformAudio::Client* client) { | 1258 webkit::ppapi::PluginDelegate::PlatformAudio::Client* client) { |
1092 scoped_refptr<PlatformAudioImpl> audio(new PlatformAudioImpl()); | 1259 scoped_refptr<PlatformAudioImpl> audio(new PlatformAudioImpl()); |
1093 if (audio->Initialize(sample_rate, sample_count, client)) { | 1260 if (audio->Initialize(sample_rate, sample_count, client)) { |
1094 // Balanced by Release invoked in PlatformAudioImpl::ShutDownOnIOThread(). | 1261 // Balanced by Release invoked in PlatformAudioImpl::ShutDownOnIOThread(). |
1095 return audio.release(); | 1262 return audio.release(); |
1096 } else { | 1263 } else { |
1097 return NULL; | 1264 return NULL; |
1098 } | 1265 } |
1099 } | 1266 } |
1100 | 1267 |
| 1268 webkit::ppapi::PluginDelegate::PlatformAudioInput* |
| 1269 PepperPluginDelegateImpl::CreateAudioInput( |
| 1270 uint32_t sample_rate, uint32_t sample_count, |
| 1271 webkit::ppapi::PluginDelegate::PlatformAudioInput::Client* client) { |
| 1272 scoped_refptr<PlatformAudioInputImpl> |
| 1273 audio_input(new PlatformAudioInputImpl()); |
| 1274 if (audio_input->Initialize(sample_rate, sample_count, client)) { |
| 1275 // Balanced by Release invoked in |
| 1276 // PlatformAudioInputImpl::ShutDownOnIOThread(). |
| 1277 return audio_input.release(); |
| 1278 } |
| 1279 return NULL; |
| 1280 } |
| 1281 |
1101 // If a broker has not already been created for this plugin, creates one. | 1282 // If a broker has not already been created for this plugin, creates one. |
1102 webkit::ppapi::PluginDelegate::PpapiBroker* | 1283 webkit::ppapi::PluginDelegate::PpapiBroker* |
1103 PepperPluginDelegateImpl::ConnectToPpapiBroker( | 1284 PepperPluginDelegateImpl::ConnectToPpapiBroker( |
1104 webkit::ppapi::PPB_Broker_Impl* client) { | 1285 webkit::ppapi::PPB_Broker_Impl* client) { |
1105 CHECK(client); | 1286 CHECK(client); |
1106 | 1287 |
1107 // If a broker needs to be created, this will ensure it does not get deleted | 1288 // If a broker needs to be created, this will ensure it does not get deleted |
1108 // before Connect() adds a reference. | 1289 // before Connect() adds a reference. |
1109 scoped_refptr<PpapiBrokerImpl> broker_impl; | 1290 scoped_refptr<PpapiBrokerImpl> broker_impl; |
1110 | 1291 |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1754 if (!context) | 1935 if (!context) |
1755 return NULL; | 1936 return NULL; |
1756 if (!context->makeContextCurrent() || context->isContextLost()) | 1937 if (!context->makeContextCurrent() || context->isContextLost()) |
1757 return NULL; | 1938 return NULL; |
1758 | 1939 |
1759 RendererGLContext* parent_context = context->context(); | 1940 RendererGLContext* parent_context = context->context(); |
1760 if (!parent_context) | 1941 if (!parent_context) |
1761 return NULL; | 1942 return NULL; |
1762 return parent_context; | 1943 return parent_context; |
1763 } | 1944 } |
OLD | NEW |