1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "content/public/renderer/renderer_ppapi_host.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/host/dispatch_host_message.h"
13 #include "ppapi/host/host_message_context.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/proxy/ppapi_messages.h"
16 #include "ppapi/shared_impl/media_stream_buffer.h"
18 using ppapi::host::HostMessageContext;
19 using ppapi::proxy::SerializedHandle;
23 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
24 RendererPpapiHost* host,
27 : ResourceHost(host->GetPpapiHost(), instance, resource),
29 buffer_manager_(this) {}
31 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
33 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
35 TrackType track_type) {
36 DCHECK_GT(number_of_buffers, 0);
37 DCHECK_GT(buffer_size,
38 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header)));
39 // Make each buffer 4 byte aligned.
40 base::CheckedNumeric<int32_t> buffer_size_aligned = buffer_size;
41 // TODO(amistry): "buffer size" might not == "buffer stride", in the same way
42 // that width != stride in an image buffer.
43 buffer_size_aligned += (4 - buffer_size % 4);
45 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
47 base::CheckedNumeric<int32_t> size = number_of_buffers * buffer_size_aligned;
51 content::RenderThread* render_thread = content::RenderThread::Get();
52 scoped_ptr<base::SharedMemory> shm(
53 render_thread->HostAllocateSharedMemoryBuffer(size.ValueOrDie()).Pass());
57 base::SharedMemoryHandle shm_handle = shm->handle();
58 if (!buffer_manager_.SetBuffers(number_of_buffers,
59 buffer_size_aligned.ValueOrDie(),
65 base::PlatformFile platform_file =
68 #elif defined(OS_POSIX)
71 #error Not implemented.
73 SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
75 bool readonly = (track_type == kRead);
76 host()->SendUnsolicitedReplyWithHandles(
78 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
80 buffer_size_aligned.ValueOrDie(),
82 std::vector<SerializedHandle>(1, handle));
86 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
89 DCHECK_LT(index, buffer_manager_.number_of_buffers());
90 host()->SendUnsolicitedReply(
91 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index));
94 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
95 const std::vector<int32_t>& indices) {
96 DCHECK_GE(indices.size(), 0U);
97 host()->SendUnsolicitedReply(pp_resource(),
98 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices));
101 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
102 const IPC::Message& msg,
103 HostMessageContext* context) {
104 PPAPI_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase, msg)
105 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
106 PpapiHostMsg_MediaStreamTrack_EnqueueBuffer, OnHostMsgEnqueueBuffer)
107 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close,
109 PPAPI_END_MESSAGE_MAP()
110 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
113 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
114 HostMessageContext* context,
116 buffer_manager_.EnqueueBuffer(index);
120 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
121 HostMessageContext* context) {
126 } // namespace content