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/common/pepper_file_util.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "content/public/renderer/renderer_ppapi_host.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/media_stream_buffer.h"
19 using ppapi::host::HostMessageContext
;
20 using ppapi::proxy::SerializedHandle
;
24 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
25 RendererPpapiHost
* host
,
28 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
30 buffer_manager_(this) {}
32 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
34 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers
,
36 TrackType track_type
) {
37 DCHECK_GT(number_of_buffers
, 0);
38 DCHECK_GT(buffer_size
,
39 static_cast<int32_t>(sizeof(ppapi::MediaStreamBuffer::Header
)));
40 // Make each buffer 4 byte aligned.
41 base::CheckedNumeric
<int32_t> buffer_size_aligned
= buffer_size
;
42 // TODO(amistry): "buffer size" might not == "buffer stride", in the same way
43 // that width != stride in an image buffer.
44 buffer_size_aligned
+= (4 - buffer_size
% 4);
46 // TODO(penghuang): |HostAllocateSharedMemoryBuffer| uses sync IPC. We should
48 base::CheckedNumeric
<int32_t> size
= number_of_buffers
* buffer_size_aligned
;
52 content::RenderThread
* render_thread
= content::RenderThread::Get();
53 scoped_ptr
<base::SharedMemory
> shm(
54 render_thread
->HostAllocateSharedMemoryBuffer(size
.ValueOrDie()).Pass());
58 base::SharedMemoryHandle shm_handle
= shm
->handle();
59 if (!buffer_manager_
.SetBuffers(number_of_buffers
,
60 buffer_size_aligned
.ValueOrDie(),
66 base::PlatformFile platform_file
=
67 PlatformFileFromSharedMemoryHandle(shm_handle
);
68 SerializedHandle
handle(host_
->ShareHandleWithRemote(platform_file
, false),
70 bool readonly
= (track_type
== kRead
);
71 host()->SendUnsolicitedReplyWithHandles(
73 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
75 buffer_size_aligned
.ValueOrDie(),
77 std::vector
<SerializedHandle
>(1, handle
));
81 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
84 DCHECK_LT(index
, buffer_manager_
.number_of_buffers());
85 host()->SendUnsolicitedReply(
86 pp_resource(), PpapiPluginMsg_MediaStreamTrack_EnqueueBuffer(index
));
89 void PepperMediaStreamTrackHostBase::SendEnqueueBuffersMessageToPlugin(
90 const std::vector
<int32_t>& indices
) {
91 DCHECK_GE(indices
.size(), 0U);
92 host()->SendUnsolicitedReply(pp_resource(),
93 PpapiPluginMsg_MediaStreamTrack_EnqueueBuffers(indices
));
96 int32_t PepperMediaStreamTrackHostBase::OnResourceMessageReceived(
97 const IPC::Message
& msg
,
98 HostMessageContext
* context
) {
99 PPAPI_BEGIN_MESSAGE_MAP(PepperMediaStreamTrackHostBase
, msg
)
100 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
101 PpapiHostMsg_MediaStreamTrack_EnqueueBuffer
, OnHostMsgEnqueueBuffer
)
102 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_MediaStreamTrack_Close
,
104 PPAPI_END_MESSAGE_MAP()
105 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg
, context
);
108 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
109 HostMessageContext
* context
,
111 buffer_manager_
.EnqueueBuffer(index
);
115 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
116 HostMessageContext
* context
) {
121 } // namespace content