mesa gn build: suppress -Wstring-conversion warnings
[chromium-blink-merge.git] / content / renderer / pepper / pepper_media_stream_track_host_base.cc
blobc7713135a159f2ca9a7875a42ff3b91aca645659
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;
21 namespace content {
23 PepperMediaStreamTrackHostBase::PepperMediaStreamTrackHostBase(
24 RendererPpapiHost* host,
25 PP_Instance instance,
26 PP_Resource resource)
27 : ResourceHost(host->GetPpapiHost(), instance, resource),
28 host_(host),
29 buffer_manager_(this) {}
31 PepperMediaStreamTrackHostBase::~PepperMediaStreamTrackHostBase() {}
33 bool PepperMediaStreamTrackHostBase::InitBuffers(int32_t number_of_buffers,
34 int32_t buffer_size,
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
46 // avoid it.
47 base::CheckedNumeric<int32_t> size = number_of_buffers * buffer_size_aligned;
48 if (!size.IsValid())
49 return false;
51 content::RenderThread* render_thread = content::RenderThread::Get();
52 scoped_ptr<base::SharedMemory> shm(
53 render_thread->HostAllocateSharedMemoryBuffer(size.ValueOrDie()).Pass());
54 if (!shm)
55 return false;
57 base::SharedMemoryHandle shm_handle = shm->handle();
58 if (!buffer_manager_.SetBuffers(number_of_buffers,
59 buffer_size_aligned.ValueOrDie(),
60 shm.Pass(),
61 true)) {
62 return false;
65 base::PlatformFile platform_file =
66 #if defined(OS_WIN)
67 shm_handle;
68 #elif defined(OS_POSIX)
69 shm_handle.fd;
70 #else
71 #error Not implemented.
72 #endif
73 SerializedHandle handle(host_->ShareHandleWithRemote(platform_file, false),
74 size.ValueOrDie());
75 bool readonly = (track_type == kRead);
76 host()->SendUnsolicitedReplyWithHandles(
77 pp_resource(),
78 PpapiPluginMsg_MediaStreamTrack_InitBuffers(
79 number_of_buffers,
80 buffer_size_aligned.ValueOrDie(),
81 readonly),
82 std::vector<SerializedHandle>(1, handle));
83 return true;
86 void PepperMediaStreamTrackHostBase::SendEnqueueBufferMessageToPlugin(
87 int32_t index) {
88 DCHECK_GE(index, 0);
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,
108 OnHostMsgClose)
109 PPAPI_END_MESSAGE_MAP()
110 return ppapi::host::ResourceHost::OnResourceMessageReceived(msg, context);
113 int32_t PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(
114 HostMessageContext* context,
115 int32_t index) {
116 buffer_manager_.EnqueueBuffer(index);
117 return PP_OK;
120 int32_t PepperMediaStreamTrackHostBase::OnHostMsgClose(
121 HostMessageContext* context) {
122 OnClose();
123 return PP_OK;
126 } // namespace content