| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "services/media/framework_mojo/mojo_pull_mode_producer.h" | 6 #include "services/media/framework_mojo/mojo_pull_mode_producer.h" |
| 7 | 7 |
| 8 namespace mojo { | 8 namespace mojo { |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 MojoPullModeProducer::MojoPullModeProducer() : | 11 MojoPullModeProducer::MojoPullModeProducer() |
| 12 state_(MediaState::UNPREPARED), | 12 : state_(MediaState::UNPREPARED), |
| 13 demand_(Demand::kNegative), | 13 demand_(Demand::kNegative), |
| 14 pts_(0), | 14 pts_(0), |
| 15 cached_packet_(nullptr) {} | 15 cached_packet_(nullptr) {} |
| 16 | 16 |
| 17 MojoPullModeProducer::~MojoPullModeProducer() { | 17 MojoPullModeProducer::~MojoPullModeProducer() { |
| 18 base::AutoLock lock(lock_); | 18 base::AutoLock lock(lock_); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void MojoPullModeProducer::AddBinding( | 21 void MojoPullModeProducer::AddBinding( |
| 22 InterfaceRequest<MediaPullModeProducer> producer) { | 22 InterfaceRequest<MediaPullModeProducer> producer) { |
| 23 bindings_.AddBinding(this, producer.Pass()); | 23 bindings_.AddBinding(this, producer.Pass()); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void MojoPullModeProducer::GetBuffer(const GetBufferCallback& callback) { | 26 void MojoPullModeProducer::GetBuffer(const GetBufferCallback& callback) { |
| 27 if (!mojo_allocator_.initialized()) { | 27 if (!mojo_allocator_.initialized()) { |
| 28 mojo_allocator_.InitNew(256 * 1024); // TODO(dalesat): Made up! | 28 mojo_allocator_.InitNew(256 * 1024); // TODO(dalesat): Made up! |
| 29 } | 29 } |
| 30 | 30 |
| 31 { | 31 { |
| 32 base::AutoLock lock(lock_); | 32 base::AutoLock lock(lock_); |
| 33 if (state_ == MediaState::UNPREPARED) { | 33 if (state_ == MediaState::UNPREPARED) { |
| 34 state_ = MediaState::PAUSED; | 34 state_ = MediaState::PAUSED; |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 callback.Run(mojo_allocator_.GetDuplicateHandle()); | 38 callback.Run(mojo_allocator_.GetDuplicateHandle()); |
| 39 | 39 |
| 40 DCHECK(!cached_packet_); | 40 DCHECK(!cached_packet_); |
| 41 DCHECK(demand_callback_); | 41 DCHECK(demand_callback_); |
| 42 demand_callback_(Demand::kPositive); | 42 demand_callback_(Demand::kPositive); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void MojoPullModeProducer::PullPacket( | 45 void MojoPullModeProducer::PullPacket(MediaPacketPtr to_release, |
| 46 MediaPacketPtr to_release, | 46 const PullPacketCallback& callback) { |
| 47 const PullPacketCallback& callback) { | |
| 48 if (to_release) { | 47 if (to_release) { |
| 49 // The client has piggy-backed a release on this pull request. | 48 // The client has piggy-backed a release on this pull request. |
| 50 ReleasePacket(to_release.Pass()); | 49 ReleasePacket(to_release.Pass()); |
| 51 } | 50 } |
| 52 | 51 |
| 53 { | 52 { |
| 54 base::AutoLock lock(lock_); | 53 base::AutoLock lock(lock_); |
| 55 | 54 |
| 56 if (state_ == MediaState::UNPREPARED) { | 55 if (state_ == MediaState::UNPREPARED) { |
| 57 // The consumer has yet to call GetBuffer. This request will have to wait. | 56 // The consumer has yet to call GetBuffer. This request will have to wait. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 71 } | 70 } |
| 72 | 71 |
| 73 DCHECK(demand_callback_); | 72 DCHECK(demand_callback_); |
| 74 demand_callback_(Demand::kPositive); | 73 demand_callback_(Demand::kPositive); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void MojoPullModeProducer::ReleasePacket(MediaPacketPtr to_release) { | 76 void MojoPullModeProducer::ReleasePacket(MediaPacketPtr to_release) { |
| 78 { | 77 { |
| 79 base::AutoLock lock(lock_); | 78 base::AutoLock lock(lock_); |
| 80 uint64_t size = to_release->payload ? to_release->payload->length : 0; | 79 uint64_t size = to_release->payload ? to_release->payload->length : 0; |
| 81 void* payload = size == 0 ? nullptr : | 80 void* payload = size == 0 ? nullptr : mojo_allocator_.PtrFromOffset( |
| 82 mojo_allocator_.PtrFromOffset(to_release->payload->offset); | 81 to_release->payload->offset); |
| 83 | 82 |
| 84 for (auto iterator = unreleased_packets_.begin(); true; ++iterator) { | 83 for (auto iterator = unreleased_packets_.begin(); true; ++iterator) { |
| 85 if (iterator == unreleased_packets_.end()) { | 84 if (iterator == unreleased_packets_.end()) { |
| 86 DCHECK(false) << "released packet has bad offset and/or size"; | 85 DCHECK(false) << "released packet has bad offset and/or size"; |
| 87 break; | 86 break; |
| 88 } | 87 } |
| 89 | 88 |
| 90 if ((*iterator)->payload() == payload && (*iterator)->size() == size) { | 89 if ((*iterator)->payload() == payload && (*iterator)->size() == size) { |
| 91 unreleased_packets_.erase(iterator); | 90 unreleased_packets_.erase(iterator); |
| 92 break; | 91 break; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 147 } |
| 149 } | 148 } |
| 150 | 149 |
| 151 bool MojoPullModeProducer::MaybeHandlePullUnsafe( | 150 bool MojoPullModeProducer::MaybeHandlePullUnsafe( |
| 152 const PullPacketCallback& callback) { | 151 const PullPacketCallback& callback) { |
| 153 DCHECK(!callback.is_null()); | 152 DCHECK(!callback.is_null()); |
| 154 lock_.AssertAcquired(); | 153 lock_.AssertAcquired(); |
| 155 | 154 |
| 156 if (state_ == MediaState::ENDED) { | 155 if (state_ == MediaState::ENDED) { |
| 157 // At end-of-stream. Respond with empty end-of-stream packet. | 156 // At end-of-stream. Respond with empty end-of-stream packet. |
| 158 HandlePullWithPacketUnsafe( | 157 HandlePullWithPacketUnsafe(callback, Packet::CreateEndOfStream(pts_)); |
| 159 callback, | |
| 160 Packet::CreateEndOfStream(pts_)); | |
| 161 return true; | 158 return true; |
| 162 } | 159 } |
| 163 | 160 |
| 164 if (!cached_packet_) { | 161 if (!cached_packet_) { |
| 165 // Waiting for packet or end-of-stream indication. | 162 // Waiting for packet or end-of-stream indication. |
| 166 return false; | 163 return false; |
| 167 } | 164 } |
| 168 | 165 |
| 169 HandlePullWithPacketUnsafe(callback, std::move(cached_packet_)); | 166 HandlePullWithPacketUnsafe(callback, std::move(cached_packet_)); |
| 170 return true; | 167 return true; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 191 | 188 |
| 192 MediaPacketPtr media_packet = MediaPacket::New(); | 189 MediaPacketPtr media_packet = MediaPacket::New(); |
| 193 media_packet->pts = packet->pts(); | 190 media_packet->pts = packet->pts(); |
| 194 media_packet->end_of_stream = packet->end_of_stream(); | 191 media_packet->end_of_stream = packet->end_of_stream(); |
| 195 media_packet->payload = region.Pass(); | 192 media_packet->payload = region.Pass(); |
| 196 pts_ = packet->pts(); | 193 pts_ = packet->pts(); |
| 197 | 194 |
| 198 return media_packet.Pass(); | 195 return media_packet.Pass(); |
| 199 } | 196 } |
| 200 | 197 |
| 201 } // namespace media | 198 } // namespace media |
| 202 } // namespace mojo | 199 } // namespace mojo |
| OLD | NEW |