Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #include "content/renderer/media/captured_encoded_video_source_impl.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop_proxy.h" | |
| 9 #include "content/common/media/encoded_video_source_messages.h" | |
| 10 #include "ipc/ipc_sender.h" | |
| 11 | |
| 12 using logging::LOG_INFO; | |
| 13 using logging::LOG_WARNING; | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 CapturedEncodedVideoBitstreamImpl::CapturedEncodedVideoBitstreamImpl( | |
| 18 CapturedEncodedVideoSourceImpl* source, | |
| 19 media::EncodedVideoBitstream::Client* client) | |
| 20 : source_(source), client_(client) { | |
| 21 } | |
| 22 | |
| 23 CapturedEncodedVideoBitstreamImpl::~CapturedEncodedVideoBitstreamImpl() { | |
| 24 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)
| |
| 25 } | |
| 26 | |
| 27 void CapturedEncodedVideoBitstreamImpl::TryConfigure( | |
| 28 const media::RuntimeVideoEncodingParameters& params) { | |
| 29 source_->TrySetBitstreamConfig(this, params); | |
| 30 } | |
| 31 | |
| 32 void CapturedEncodedVideoBitstreamImpl::NotifyStreaming( | |
| 33 const media::VideoEncodingParameters& params, | |
| 34 const std::map<int, base::SharedMemoryHandle>& buffers) { | |
| 35 std::map<int, base::SharedMemoryHandle>::const_iterator it; | |
| 36 for (it = buffers.begin(); it != buffers.end(); ++it) { | |
| 37 int buffer_id = it->first; | |
| 38 buffers_[buffer_id] = new base::SharedMemory(it->second, true); | |
| 39 } | |
| 40 client_->OnBitstreamStreaming(this, params); | |
| 41 } | |
| 42 | |
| 43 void CapturedEncodedVideoBitstreamImpl::NotifyRemoved() { | |
| 44 std::map<int, base::SharedMemory*>::iterator it; | |
| 45 for (it = buffers_.begin(); it != buffers_.end(); ++it) { | |
| 46 if (it->second) { | |
| 47 it->second->Close(); | |
| 48 delete it->second; | |
| 49 } | |
| 50 } | |
| 51 buffers_.clear(); | |
| 52 client_->OnBitstreamRemoved(this); | |
| 53 } | |
| 54 | |
| 55 void CapturedEncodedVideoBitstreamImpl::NotifyConfigChanged( | |
| 56 const media::RuntimeVideoEncodingParameters& params) { | |
| 57 client_->OnBitstreamConfigChanged(this, params); | |
| 58 } | |
| 59 | |
| 60 void CapturedEncodedVideoBitstreamImpl::NotifyBitstreamReady( | |
| 61 int buffer_id, | |
| 62 size_t size, | |
| 63 const media::BufferEncodingMetadata& metadata) { | |
| 64 if (buffers_.find(buffer_id) != buffers_.end()) { | |
| 65 base::SharedMemory* shm = buffers_[buffer_id]; | |
| 66 if (shm->Map(size)) { | |
| 67 scoped_refptr<media::EncodedBitstreamBuffer> buffer = | |
| 68 new media::EncodedBitstreamBuffer( | |
| 69 buffer_id, (uint8*)shm->memory(), size, metadata); | |
| 70 client_->OnBitstreamReady(this, buffer); | |
| 71 shm->Unmap(); | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 CapturedEncodedVideoSourceImpl::CapturedEncodedVideoSourceImpl() | |
| 77 : stream_index_(0) { | |
| 78 } | |
| 79 | |
| 80 CapturedEncodedVideoSourceImpl::~CapturedEncodedVideoSourceImpl() { | |
| 81 } | |
| 82 | |
| 83 media::VideoEncodingCapability | |
| 84 CapturedEncodedVideoSourceImpl::GetCapabilities() { | |
| 85 return capability_; | |
| 86 } | |
| 87 | |
| 88 #define DISPATCH_TO0(loop, func, obj) \ | |
| 89 loop->PostTask(FROM_HERE, base::Bind(&func, obj)) | |
| 90 #define DISPATCH_TO1(loop, func, obj, param) \ | |
| 91 loop->PostTask(FROM_HERE, base::Bind(&func, obj, param)) | |
| 92 #define DISPATCH_TO2(loop, func, obj, param1, param2) \ | |
| 93 loop->PostTask(FROM_HERE, base::Bind(&func, obj, param1, param2)) | |
| 94 #define DISPATCH_TO3(loop, func, obj, param1, param2, param3) \ | |
| 95 loop->PostTask(FROM_HERE, base::Bind(&func, obj, param1, param2, param3)) | |
| 96 | |
| 97 void CapturedEncodedVideoSourceImpl::OnCapabilityAvailable( | |
| 98 const media::VideoEncodingCapability& capability) { | |
| 99 set_encoding_capabilities(capability); | |
| 100 FOR_EACH_OBSERVER(media::EncodedVideoSource::Observer, | |
| 101 capabilities_observers_, | |
| 102 OnCapabilitiesAvailable(this)); | |
| 103 } | |
| 104 | |
| 105 void CapturedEncodedVideoSourceImpl::AddCapabilitiesObserver( | |
| 106 Observer* observer) { | |
| 107 capabilities_observers_.AddObserver(observer); | |
| 108 } | |
| 109 | |
| 110 void CapturedEncodedVideoSourceImpl::RemoveCapabilitiesObserver( | |
| 111 Observer* observer) { | |
| 112 capabilities_observers_.RemoveObserver(observer); | |
| 113 } | |
| 114 | |
| 115 void CapturedEncodedVideoSourceImpl::StartFetchCapabilities() { | |
| 116 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_GetCapability"; | |
| 117 DISPATCH_TO1(app_loop_proxy(), | |
| 118 CapturedEncodedVideoSourceImpl::SendToIo, | |
| 119 base::Unretained(this), | |
| 120 new EncodedVideoSourceHostMsg_GetCapability(device_id())); | |
| 121 } | |
| 122 | |
| 123 scoped_refptr<media::EncodedVideoBitstream> | |
| 124 CapturedEncodedVideoSourceImpl::OpenBitstream( | |
| 125 media::EncodedVideoBitstream::Client* client, | |
| 126 const media::VideoEncodingParameters& params) { | |
| 127 scoped_refptr<CapturedEncodedVideoBitstreamImpl> bitstream( | |
| 128 new CapturedEncodedVideoBitstreamImpl(this, client)); | |
| 129 bitstreams_[stream_index_] = bitstream; | |
| 130 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_AddBitstream"; | |
| 131 DISPATCH_TO1(app_loop_proxy(), | |
| 132 CapturedEncodedVideoSourceImpl::SendToIo, | |
| 133 base::Unretained(this), | |
| 134 new EncodedVideoSourceHostMsg_CreateBitstream(device_id(), | |
| 135 stream_index_, | |
| 136 params)); | |
| 137 return bitstreams_[stream_index_++]; | |
| 138 } | |
| 139 | |
| 140 void CapturedEncodedVideoSourceImpl::CloseBitstream( | |
| 141 scoped_refptr<media::EncodedVideoBitstream> bitstream) { | |
| 142 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_RemoveBitstream"; | |
| 143 DISPATCH_TO1(app_loop_proxy(), | |
| 144 CapturedEncodedVideoSourceImpl::SendToIo, | |
| 145 base::Unretained(this), | |
| 146 new EncodedVideoSourceHostMsg_DestroyBitstream( | |
| 147 device_id(), FindStreamId(bitstream))); | |
| 148 } | |
| 149 | |
| 150 void CapturedEncodedVideoSourceImpl::ReturnBitstreamBuffer( | |
| 151 scoped_refptr<media::EncodedVideoBitstream> bitstream, | |
| 152 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { | |
| 153 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamBufferConsumed"; | |
| 154 DISPATCH_TO1(app_loop_proxy(), | |
| 155 CapturedEncodedVideoSourceImpl::SendToIo, | |
| 156 base::Unretained(this), | |
| 157 new EncodedVideoSourceHostMsg_BitstreamBufferConsumed( | |
| 158 device_id(), FindStreamId(bitstream), buffer->buffer_id())); | |
| 159 } | |
| 160 | |
| 161 void CapturedEncodedVideoSourceImpl::OnBitstreamCreated( | |
| 162 int stream_id, | |
| 163 const media::VideoEncodingParameters& params, | |
| 164 const std::map<int, base::SharedMemoryHandle>& buffers) { | |
| 165 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamAdded"; | |
| 166 DISPATCH_TO2(app_loop_proxy(), | |
| 167 CapturedEncodedVideoBitstreamImpl::NotifyStreaming, | |
| 168 FindBitstream(stream_id), | |
| 169 params, | |
| 170 buffers); | |
| 171 } | |
| 172 | |
| 173 void CapturedEncodedVideoSourceImpl::OnBitstreamDestroyed(int stream_id) { | |
| 174 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamRemoved"; | |
| 175 DISPATCH_TO0(app_loop_proxy(), | |
| 176 CapturedEncodedVideoBitstreamImpl::NotifyRemoved, | |
| 177 FindBitstream(stream_id)); | |
| 178 } | |
| 179 | |
| 180 void CapturedEncodedVideoSourceImpl::OnBitstreamConfigChanged( | |
| 181 int stream_id, const media::RuntimeVideoEncodingParameters& params) { | |
| 182 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_ConfigChanged"; | |
| 183 DISPATCH_TO1(app_loop_proxy(), | |
| 184 CapturedEncodedVideoBitstreamImpl::NotifyConfigChanged, | |
| 185 FindBitstream(stream_id), | |
| 186 params); | |
| 187 } | |
| 188 | |
| 189 void CapturedEncodedVideoSourceImpl::OnBitstreamReady( | |
| 190 int stream_id, | |
| 191 int buffer_id, | |
| 192 size_t size, | |
| 193 const media::BufferEncodingMetadata& metadata) { | |
| 194 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_BitstreamReady"; | |
| 195 DISPATCH_TO3(app_loop_proxy(), | |
| 196 CapturedEncodedVideoBitstreamImpl::NotifyBitstreamReady, | |
| 197 FindBitstream(stream_id), | |
| 198 buffer_id, | |
| 199 size, | |
| 200 metadata); | |
| 201 } | |
| 202 | |
| 203 void CapturedEncodedVideoSourceImpl::TrySetBitstreamConfig( | |
| 204 scoped_refptr<CapturedEncodedVideoBitstreamImpl> bitstream, | |
| 205 const media::RuntimeVideoEncodingParameters& params) { | |
| 206 DVLOG(LOG_INFO) << ": EncodedVideoSourceHostMsg_TrySetBitstreamConfig"; | |
| 207 DISPATCH_TO1(app_loop_proxy(), | |
| 208 CapturedEncodedVideoSourceImpl::SendToIo, | |
| 209 base::Unretained(this), | |
| 210 new EncodedVideoSourceHostMsg_TryConfigureBitstream( | |
| 211 device_id(), FindStreamId(bitstream), params)); | |
| 212 } | |
| 213 | |
| 214 scoped_refptr<CapturedEncodedVideoBitstreamImpl> | |
| 215 CapturedEncodedVideoSourceImpl::FindBitstream(int stream_id) { | |
| 216 std::map<int, scoped_refptr<CapturedEncodedVideoBitstreamImpl> >::iterator it; | |
| 217 it = bitstreams_.find(stream_id); | |
| 218 if (it == bitstreams_.end()) { | |
| 219 DVLOG(LOG_WARNING) << ": Unknown video stream id coming to Renderer."; | |
| 220 return NULL; | |
| 221 } | |
| 222 return it->second; | |
| 223 } | |
| 224 | |
| 225 int CapturedEncodedVideoSourceImpl::FindStreamId( | |
| 226 scoped_refptr<media::EncodedVideoBitstream> bitstream) { | |
| 227 std::map<int, scoped_refptr<CapturedEncodedVideoBitstreamImpl> >::iterator it; | |
| 228 for (it = bitstreams_.begin(); it != bitstreams_.end(); it++) { | |
| 229 if (it->second == bitstream) { | |
| 230 return it->first; | |
| 231 } | |
| 232 } | |
| 233 DVLOG(LOG_WARNING) << ": Unknown bitstream in Renderer."; | |
| 234 DCHECK(false); // Should never happen. | |
| 235 return -1; | |
| 236 } | |
| 237 | |
| 238 void CapturedEncodedVideoSourceImpl::SendToIo(IPC::Message* message) { | |
| 239 io_loop_proxy()->PostTask(FROM_HERE, | |
| 240 base::Bind(base::IgnoreResult(&CapturedEncodedVideoSourceImpl::Send), | |
| 241 base::Unretained(this), message)); | |
| 242 } | |
| 243 | |
| 244 } // namespace content | |
| 245 | |
| OLD | NEW |