| 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 #ifndef SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ | 5 #ifndef SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ |
| 6 #define SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ | 6 #define SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ |
| 7 | 7 |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
| 10 #include "mojo/common/binding_set.h" | 10 #include "mojo/common/binding_set.h" |
| 11 #include "mojo/services/media/common/interfaces/media_state.mojom.h" | 11 #include "mojo/services/media/common/interfaces/media_state.mojom.h" |
| 12 #include "mojo/services/media/common/interfaces/media_transport.mojom.h" | 12 #include "mojo/services/media/common/interfaces/media_transport.mojom.h" |
| 13 #include "services/media/framework/models/active_sink.h" | 13 #include "services/media/framework/models/active_sink.h" |
| 14 #include "services/media/framework_mojo/mojo_allocator.h" | 14 #include "services/media/framework_mojo/mojo_allocator.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace media { | 17 namespace media { |
| 18 | 18 |
| 19 // Implements MediaProducer to forward a stream across mojo. | 19 // Implements MediaProducer to forward a stream across mojo. |
| 20 class MojoProducer : public MediaProducer, public ActiveSink { | 20 class MojoProducer : public MediaProducer, public ActiveSink { |
| 21 public: | 21 public: |
| 22 using StatusCallback = std::function<void(MediaState)>; | |
| 23 using PrimeConnectionCallback = mojo::Callback<void()>; | 22 using PrimeConnectionCallback = mojo::Callback<void()>; |
| 24 using FlushConnectionCallback = mojo::Callback<void()>; | 23 using FlushConnectionCallback = mojo::Callback<void()>; |
| 25 | 24 |
| 26 static std::shared_ptr<MojoProducer> Create() { | 25 static std::shared_ptr<MojoProducer> Create() { |
| 27 return std::shared_ptr<MojoProducer>(new MojoProducer()); | 26 return std::shared_ptr<MojoProducer>(new MojoProducer()); |
| 28 } | 27 } |
| 29 | 28 |
| 30 ~MojoProducer() override; | 29 ~MojoProducer() override; |
| 31 | 30 |
| 32 // Adds a binding. | 31 // Adds a binding. |
| 33 void AddBinding(InterfaceRequest<MediaProducer> producer); | 32 void AddBinding(InterfaceRequest<MediaProducer> producer); |
| 34 | 33 |
| 35 // Initiates demand to provide downstream parties with enough content to | 34 // Initiates demand to provide downstream parties with enough content to |
| 36 // start without starving. | 35 // start without starving. |
| 37 void PrimeConnection(const PrimeConnectionCallback& callback); | 36 void PrimeConnection(const PrimeConnectionCallback& callback); |
| 38 | 37 |
| 39 // Unprimes and tells the connected consumer to flush. | 38 // Unprimes and tells the connected consumer to flush. |
| 40 void FlushConnection(const FlushConnectionCallback& callback); | 39 void FlushConnection(const FlushConnectionCallback& callback); |
| 41 | 40 |
| 42 // Sets a callback for reporting status updates. | |
| 43 void SetStatusCallback(const StatusCallback& callback); | |
| 44 | |
| 45 // Gets the first presentation time seen on any packet after the most recent | |
| 46 // flush or, if there has never been a flush, the first packet supplied. | |
| 47 int64_t GetFirstPtsSinceFlush(); | |
| 48 | |
| 49 // ActiveSink implementation. | 41 // ActiveSink implementation. |
| 50 PayloadAllocator* allocator() override; | 42 PayloadAllocator* allocator() override; |
| 51 | 43 |
| 52 void SetDemandCallback(const DemandCallback& demand_callback) override; | 44 void SetDemandCallback(const DemandCallback& demand_callback) override; |
| 53 | 45 |
| 54 Demand SupplyPacket(PacketPtr packet) override; | 46 Demand SupplyPacket(PacketPtr packet) override; |
| 55 | 47 |
| 56 // MediaProducer implementation. | 48 // MediaProducer implementation. |
| 57 void Connect(InterfaceHandle<MediaConsumer> consumer, | 49 void Connect(InterfaceHandle<MediaConsumer> consumer, |
| 58 const ConnectCallback& callback) override; | 50 const ConnectCallback& callback) override; |
| 59 | 51 |
| 60 void Disconnect() override; | 52 void Disconnect() override; |
| 61 | 53 |
| 62 private: | 54 private: |
| 63 MojoProducer(); | 55 MojoProducer(); |
| 64 | 56 |
| 65 // Sends a packet to the consumer. | 57 // Sends a packet to the consumer. |
| 66 // TODO(dalesat): Don't use a raw pointer, if possible. | 58 // TODO(dalesat): Don't use a raw pointer, if possible. |
| 67 void SendPacket(Packet* packet_raw_ptr, MediaPacketPtr media_packet); | 59 void SendPacket(Packet* packet_raw_ptr, MediaPacketPtr media_packet); |
| 68 | 60 |
| 69 // Sets the current state and calls the registered callback, if there is one. | |
| 70 void SetState(MediaState state); | |
| 71 | |
| 72 // Creates a MediaPacket from a Packet. | 61 // Creates a MediaPacket from a Packet. |
| 73 MediaPacketPtr CreateMediaPacket(const PacketPtr& packet); | 62 MediaPacketPtr CreateMediaPacket(const PacketPtr& packet); |
| 74 | 63 |
| 75 // Allocates from the shared buffer. | 64 // Allocates from the shared buffer. |
| 76 MojoAllocator mojo_allocator_; | 65 MojoAllocator mojo_allocator_; |
| 77 | 66 |
| 78 BindingSet<MediaProducer> bindings_; | 67 BindingSet<MediaProducer> bindings_; |
| 79 MediaConsumerPtr consumer_; | 68 MediaConsumerPtr consumer_; |
| 80 StatusCallback status_callback_; | |
| 81 | 69 |
| 82 mutable base::Lock lock_; | 70 mutable base::Lock lock_; |
| 83 // THE FIELDS BELOW SHOULD ONLY BE ACCESSED WITH lock_ TAKEN. | 71 // THE FIELDS BELOW SHOULD ONLY BE ACCESSED WITH lock_ TAKEN. |
| 84 MediaState state_ = MediaState::UNPREPARED; | |
| 85 bool end_of_stream_ = false; | |
| 86 DemandCallback demand_callback_; | 72 DemandCallback demand_callback_; |
| 87 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 73 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 88 int64_t first_pts_since_flush_ = Packet::kUnknownPts; | |
| 89 // TODO(dalesat): Base this logic on presentation time or duration. | 74 // TODO(dalesat): Base this logic on presentation time or duration. |
| 90 uint32_t max_pushes_outstanding_ = 0; | 75 uint32_t max_pushes_outstanding_ = 0; |
| 91 uint32_t current_pushes_outstanding_ = 0; | 76 uint32_t current_pushes_outstanding_ = 0; |
| 92 // THE FIELDS ABOVE SHOULD ONLY BE ACCESSED WITH lock_ TAKEN. | 77 // THE FIELDS ABOVE SHOULD ONLY BE ACCESSED WITH lock_ TAKEN. |
| 93 }; | 78 }; |
| 94 | 79 |
| 95 } // namespace media | 80 } // namespace media |
| 96 } // namespace mojo | 81 } // namespace mojo |
| 97 | 82 |
| 98 #endif // SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ | 83 #endif // SERVICES_MEDIA_FRAMEWORK_MOJO_PUSH_PRODUCER_BASE_H_ |
| OLD | NEW |