Chromium Code Reviews| Index: content/renderer/media/captured_encoded_video_source_impl.cc |
| diff --git a/content/renderer/media/captured_encoded_video_source_impl.cc b/content/renderer/media/captured_encoded_video_source_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6cc48d126a715dae6a5f752fdf7199d695716333 |
| --- /dev/null |
| +++ b/content/renderer/media/captured_encoded_video_source_impl.cc |
| @@ -0,0 +1,245 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/media/captured_encoded_video_source_impl.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "content/common/media/encoded_video_source_messages.h" |
| +#include "ipc/ipc_sender.h" |
| + |
| +using logging::LOG_INFO; |
| +using logging::LOG_WARNING; |
| + |
| +namespace content { |
| + |
| +CapturedEncodedVideoBitstreamImpl::CapturedEncodedVideoBitstreamImpl( |
| + CapturedEncodedVideoSourceImpl* source, |
| + media::EncodedVideoBitstream::Client* client) |
| + : source_(source), client_(client) { |
| +} |
| + |
| +CapturedEncodedVideoBitstreamImpl::~CapturedEncodedVideoBitstreamImpl() { |
| + source_->CloseBitstream(this); |
|
Ami GONE FROM CHROMIUM
2013/06/08 00:18:01
This implies that a bitstream can close before the
hshi1
2013/06/11 19:51:22
N/A (file removed)
|
| +} |
| + |
| +void CapturedEncodedVideoBitstreamImpl::TryConfigure( |
| + const media::RuntimeVideoEncodingParameters& params) { |
| + source_->TrySetBitstreamConfig(this, params); |
| +} |
| + |
| +void CapturedEncodedVideoBitstreamImpl::NotifyStreaming( |
| + const media::VideoEncodingParameters& params, |
| + const std::map<int, base::SharedMemoryHandle>& buffers) { |
| + std::map<int, base::SharedMemoryHandle>::const_iterator it; |
| + for (it = buffers.begin(); it != buffers.end(); ++it) { |
| + int buffer_id = it->first; |
| + buffers_[buffer_id] = new base::SharedMemory(it->second, true); |
| + } |
| + client_->OnBitstreamStreaming(this, params); |
| +} |
| + |
| +void CapturedEncodedVideoBitstreamImpl::NotifyRemoved() { |
| + std::map<int, base::SharedMemory*>::iterator it; |
| + for (it = buffers_.begin(); it != buffers_.end(); ++it) { |
| + if (it->second) { |
| + it->second->Close(); |
| + delete it->second; |
| + } |
| + } |
| + buffers_.clear(); |
| + client_->OnBitstreamRemoved(this); |
| +} |
| + |
| +void CapturedEncodedVideoBitstreamImpl::NotifyConfigChanged( |
| + const media::RuntimeVideoEncodingParameters& params) { |
| + client_->OnBitstreamConfigChanged(this, params); |
| +} |
| + |
| +void CapturedEncodedVideoBitstreamImpl::NotifyBitstreamReady( |
| + int buffer_id, |
| + size_t size, |
| + const media::BufferEncodingMetadata& metadata) { |
| + if (buffers_.find(buffer_id) != buffers_.end()) { |
| + base::SharedMemory* shm = buffers_[buffer_id]; |
| + if (shm->Map(size)) { |
| + scoped_refptr<media::EncodedBitstreamBuffer> buffer = |
| + new media::EncodedBitstreamBuffer( |
| + buffer_id, (uint8*)shm->memory(), size, metadata); |
| + client_->OnBitstreamReady(this, buffer); |
| + shm->Unmap(); |
| + } |
| + } |
| +} |
| + |
| +CapturedEncodedVideoSourceImpl::CapturedEncodedVideoSourceImpl() |
| + : stream_index_(0) { |
| +} |
| + |
| +CapturedEncodedVideoSourceImpl::~CapturedEncodedVideoSourceImpl() { |
| +} |
| + |
| +media::VideoEncodingCapability |
| + CapturedEncodedVideoSourceImpl::GetCapabilities() { |
| + return capability_; |
| +} |
| + |
| +#define DISPATCH_TO0(loop, func, obj) \ |
| + loop->PostTask(FROM_HERE, base::Bind(&func, obj)) |
| +#define DISPATCH_TO1(loop, func, obj, param) \ |
| + loop->PostTask(FROM_HERE, base::Bind(&func, obj, param)) |
| +#define DISPATCH_TO2(loop, func, obj, param1, param2) \ |
| + loop->PostTask(FROM_HERE, base::Bind(&func, obj, param1, param2)) |
| +#define DISPATCH_TO3(loop, func, obj, param1, param2, param3) \ |
| + loop->PostTask(FROM_HERE, base::Bind(&func, obj, param1, param2, param3)) |
| + |
| +void CapturedEncodedVideoSourceImpl::OnCapabilityAvailable( |
| + const media::VideoEncodingCapability& capability) { |
| + set_encoding_capabilities(capability); |
| + FOR_EACH_OBSERVER(media::EncodedVideoSource::Observer, |
| + capabilities_observers_, |
| + OnCapabilitiesAvailable(this)); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::AddCapabilitiesObserver( |
| + Observer* observer) { |
| + capabilities_observers_.AddObserver(observer); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::RemoveCapabilitiesObserver( |
| + Observer* observer) { |
| + capabilities_observers_.RemoveObserver(observer); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::StartFetchCapabilities() { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_GetCapability"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoSourceImpl::SendToIo, |
| + base::Unretained(this), |
| + new EncodedVideoSourceHostMsg_GetCapability(device_id())); |
| +} |
| + |
| +scoped_refptr<media::EncodedVideoBitstream> |
| + CapturedEncodedVideoSourceImpl::OpenBitstream( |
| + media::EncodedVideoBitstream::Client* client, |
| + const media::VideoEncodingParameters& params) { |
| + scoped_refptr<CapturedEncodedVideoBitstreamImpl> bitstream( |
| + new CapturedEncodedVideoBitstreamImpl(this, client)); |
| + bitstreams_[stream_index_] = bitstream; |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_AddBitstream"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoSourceImpl::SendToIo, |
| + base::Unretained(this), |
| + new EncodedVideoSourceHostMsg_CreateBitstream(device_id(), |
| + stream_index_, |
| + params)); |
| + return bitstreams_[stream_index_++]; |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::CloseBitstream( |
| + scoped_refptr<media::EncodedVideoBitstream> bitstream) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_RemoveBitstream"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoSourceImpl::SendToIo, |
| + base::Unretained(this), |
| + new EncodedVideoSourceHostMsg_DestroyBitstream( |
| + device_id(), FindStreamId(bitstream))); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::ReturnBitstreamBuffer( |
| + scoped_refptr<media::EncodedVideoBitstream> bitstream, |
| + scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamBufferConsumed"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoSourceImpl::SendToIo, |
| + base::Unretained(this), |
| + new EncodedVideoSourceHostMsg_BitstreamBufferConsumed( |
| + device_id(), FindStreamId(bitstream), buffer->buffer_id())); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::OnBitstreamCreated( |
| + int stream_id, |
| + const media::VideoEncodingParameters& params, |
| + const std::map<int, base::SharedMemoryHandle>& buffers) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamAdded"; |
| + DISPATCH_TO2(app_loop_proxy(), |
| + CapturedEncodedVideoBitstreamImpl::NotifyStreaming, |
| + FindBitstream(stream_id), |
| + params, |
| + buffers); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::OnBitstreamDestroyed(int stream_id) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamRemoved"; |
| + DISPATCH_TO0(app_loop_proxy(), |
| + CapturedEncodedVideoBitstreamImpl::NotifyRemoved, |
| + FindBitstream(stream_id)); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::OnBitstreamConfigChanged( |
| + int stream_id, const media::RuntimeVideoEncodingParameters& params) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_ConfigChanged"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoBitstreamImpl::NotifyConfigChanged, |
| + FindBitstream(stream_id), |
| + params); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::OnBitstreamReady( |
| + int stream_id, |
| + int buffer_id, |
| + size_t size, |
| + const media::BufferEncodingMetadata& metadata) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamReady"; |
| + DISPATCH_TO3(app_loop_proxy(), |
| + CapturedEncodedVideoBitstreamImpl::NotifyBitstreamReady, |
| + FindBitstream(stream_id), |
| + buffer_id, |
| + size, |
| + metadata); |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::TrySetBitstreamConfig( |
| + scoped_refptr<CapturedEncodedVideoBitstreamImpl> bitstream, |
| + const media::RuntimeVideoEncodingParameters& params) { |
| + DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_TrySetBitstreamConfig"; |
| + DISPATCH_TO1(app_loop_proxy(), |
| + CapturedEncodedVideoSourceImpl::SendToIo, |
| + base::Unretained(this), |
| + new EncodedVideoSourceHostMsg_TryConfigureBitstream( |
| + device_id(), FindStreamId(bitstream), params)); |
| +} |
| + |
| +scoped_refptr<CapturedEncodedVideoBitstreamImpl> |
| + CapturedEncodedVideoSourceImpl::FindBitstream(int stream_id) { |
| + std::map<int, scoped_refptr<CapturedEncodedVideoBitstreamImpl> >::iterator it; |
| + it = bitstreams_.find(stream_id); |
| + if (it == bitstreams_.end()) { |
| + DVLOG(LOG_WARNING) << ": Unknown video stream id coming to Renderer."; |
| + return NULL; |
| + } |
| + return it->second; |
| +} |
| + |
| +int CapturedEncodedVideoSourceImpl::FindStreamId( |
| + scoped_refptr<media::EncodedVideoBitstream> bitstream) { |
| + std::map<int, scoped_refptr<CapturedEncodedVideoBitstreamImpl> >::iterator it; |
| + for (it = bitstreams_.begin(); it != bitstreams_.end(); it++) { |
| + if (it->second == bitstream) { |
| + return it->first; |
| + } |
| + } |
| + DVLOG(LOG_WARNING) << ": Unknown bitstream in Renderer."; |
| + DCHECK(false); // Should never happen. |
| + return -1; |
| +} |
| + |
| +void CapturedEncodedVideoSourceImpl::SendToIo(IPC::Message* message) { |
| + io_loop_proxy()->PostTask(FROM_HERE, |
| + base::Bind(base::IgnoreResult(&CapturedEncodedVideoSourceImpl::Send), |
| + base::Unretained(this), message)); |
| +} |
| + |
| +} // namespace content |
| + |