blob: 3c9670129009ae717e2915976a6a4ef9e6f903e9 [file] [log] [blame]
Dale Satherbd2a17d2016-02-02 16:15:14 -08001// Copyright 2016 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 "base/logging.h"
Dale Satherbd2a17d2016-02-02 16:15:14 -08006#include "services/media/framework/packet.h"
Dale Satherad2cbf22016-02-10 17:00:57 -08007#include "services/media/framework/payload_allocator.h"
Dale Satherbd2a17d2016-02-02 16:15:14 -08008
9namespace mojo {
10namespace media {
11
Dale Sather4ccdd212016-03-23 09:27:19 -070012Packet::Packet(int64_t pts, bool end_of_stream, size_t size, void* payload)
13 : pts_(pts), end_of_stream_(end_of_stream), size_(size), payload_(payload) {
Dale Sather103f68e2016-03-21 17:07:19 -070014 DCHECK((size == 0) == (payload == nullptr));
15}
16
Dale Satherbd2a17d2016-02-02 16:15:14 -080017class PacketImpl : public Packet {
18 public:
Dale Sather4ccdd212016-03-23 09:27:19 -070019 PacketImpl(int64_t pts,
20 bool end_of_stream,
21 size_t size,
22 void* payload,
23 PayloadAllocator* allocator)
24 : Packet(pts, end_of_stream, size, payload), allocator_(allocator) {}
Dale Satherbd2a17d2016-02-02 16:15:14 -080025
26 protected:
Dale Sather4ccdd212016-03-23 09:27:19 -070027 ~PacketImpl() override{};
Dale Sather103f68e2016-03-21 17:07:19 -070028
Dale Satherbd2a17d2016-02-02 16:15:14 -080029 void Release() override {
Dale Sather103f68e2016-03-21 17:07:19 -070030 // In the default implementation, payload() will be nullptr if and only if
31 // allocator_ is nullptr. Subclasses have the option of having a non-null
32 // payload() and handling deallocation themselves, so allocator_ can be
33 // nullptr even when payload() is not.
34 if (payload() != nullptr && allocator_ != nullptr) {
Dale Sathereee34002016-06-20 10:47:59 -070035 allocator_->ReleasePayloadBuffer(payload());
Dale Satherbd2a17d2016-02-02 16:15:14 -080036 }
37 delete this;
38 }
39
40 private:
Dale Satherad2cbf22016-02-10 17:00:57 -080041 PayloadAllocator* allocator_;
Dale Satherbd2a17d2016-02-02 16:15:14 -080042};
43
44// static
Dale Sather4ccdd212016-03-23 09:27:19 -070045PacketPtr Packet::Create(int64_t pts,
46 bool end_of_stream,
47 size_t size,
48 void* payload,
49 PayloadAllocator* allocator) {
Dale Satherbd2a17d2016-02-02 16:15:14 -080050 DCHECK(payload == nullptr || allocator != nullptr);
Dale Sather4ccdd212016-03-23 09:27:19 -070051 return PacketPtr(
52 new PacketImpl(pts, end_of_stream, size, payload, allocator));
Dale Satherbd2a17d2016-02-02 16:15:14 -080053}
54
55// static
Dale Sather4ccdd212016-03-23 09:27:19 -070056PacketPtr Packet::CreateNoAllocator(int64_t pts,
57 bool end_of_stream,
58 size_t size,
59 void* payload) {
60 return PacketPtr(new PacketImpl(pts, end_of_stream, size, payload, nullptr));
Dale Satherbd2a17d2016-02-02 16:15:14 -080061}
62
63// static
Dale Sather103f68e2016-03-21 17:07:19 -070064PacketPtr Packet::CreateEndOfStream(int64_t pts) {
Dale Sather4ccdd212016-03-23 09:27:19 -070065 return PacketPtr(new PacketImpl(pts,
66 true, // end_of_stream
67 0, // size
68 nullptr, // payload
69 nullptr)); // allocator
Dale Satherbd2a17d2016-02-02 16:15:14 -080070}
71
72} // namespace media
73} // namespace mojo