aboutsummaryrefslogtreecommitdiff
path: root/components/include/v4l2_codec2/components/VideoFramePool.h
blob: 2978eed036c72b2892f52e0872af5e7254b339ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 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.

#ifndef ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_FRAME_POOL_H
#define ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_FRAME_POOL_H

#include <atomic>
#include <memory>
#include <optional>
#include <queue>

#include <C2Buffer.h>
#include <base/callback.h>
#include <base/memory/weak_ptr.h>
#include <base/sequenced_task_runner.h>
#include <base/threading/thread.h>
#include <ui/Size.h>

#include <v4l2_codec2/common/VideoTypes.h>
#include <v4l2_codec2/components/VideoFrame.h>

namespace android {

// Fetch C2GraphicBlock from C2BlockPool and wrap to VideoFrame.
// Provide asynchronous call which avoid the caller busy-polling while
// C2BlockPool::fetchGraphicBlock() times out.
class VideoFramePool {
public:
    using FrameWithBlockId = std::pair<std::unique_ptr<VideoFrame>, uint32_t>;
    using GetVideoFrameCB = ::base::OnceCallback<void(std::optional<FrameWithBlockId>)>;

    static std::unique_ptr<VideoFramePool> Create(
            std::shared_ptr<C2BlockPool> blockPool, const size_t numBuffers, const ui::Size& size,
            HalPixelFormat pixelFormat, bool isSecure,
            scoped_refptr<::base::SequencedTaskRunner> taskRunner);
    ~VideoFramePool();

    // Get a VideoFrame instance, which will be passed via |cb|.
    // If any error occurs, then nullptr will be passed via |cb|.
    // Return false if the previous callback has not been called, and |cb| will
    // be dropped directly.
    bool getVideoFrame(GetVideoFrameCB cb);

private:
    // |blockPool| is the C2BlockPool that we fetch graphic blocks from.
    // |size| is the resolution size of the required graphic blocks.
    // |pixelFormat| is the pixel format of the required graphic blocks.
    // |isSecure| indicates the video stream is encrypted or not.
    // All public methods and the callbacks should be run on |taskRunner|.
    VideoFramePool(std::shared_ptr<C2BlockPool> blockPool, const ui::Size& size,
                   HalPixelFormat pixelFormat, C2MemoryUsage memoryUsage,
                   scoped_refptr<::base::SequencedTaskRunner> taskRunner);
    bool initialize();
    void destroyTask();

    static void getVideoFrameTaskThunk(scoped_refptr<::base::SequencedTaskRunner> taskRunner,
                                       std::optional<::base::WeakPtr<VideoFramePool>> weakPool);
    void getVideoFrameTask();
    void onVideoFrameReady(std::optional<FrameWithBlockId> frameWithBlockId);

    // Ask |blockPool| to allocate the specified number of buffers.
    // |bufferCount| is the number of requested buffers.
    static c2_status_t requestNewBufferSet(C2BlockPool& blockPool, int32_t bufferCount,
                                           const ui::Size& size, uint32_t format,
                                           C2MemoryUsage usage);

    static std::optional<uint32_t> getBufferIdFromGraphicBlock(C2BlockPool& blockPool,
                                                               const C2Block2D& block);

    // Ask |blockPool| to notify when a block is available via |cb|.
    // Return true if |blockPool| supports notifying buffer available.
    static bool setNotifyBlockAvailableCb(C2BlockPool& blockPool, ::base::OnceClosure cb);

    std::shared_ptr<C2BlockPool> mBlockPool;
    const ui::Size mSize;
    const HalPixelFormat mPixelFormat;
    const C2MemoryUsage mMemoryUsage;

    GetVideoFrameCB mOutputCb;

    scoped_refptr<::base::SequencedTaskRunner> mClientTaskRunner;
    ::base::Thread mFetchThread{"VideoFramePoolFetchThread"};
    scoped_refptr<::base::SequencedTaskRunner> mFetchTaskRunner;

    ::base::WeakPtr<VideoFramePool> mClientWeakThis;
    ::base::WeakPtr<VideoFramePool> mFetchWeakThis;
    ::base::WeakPtrFactory<VideoFramePool> mClientWeakThisFactory{this};
    ::base::WeakPtrFactory<VideoFramePool> mFetchWeakThisFactory{this};
};

}  // namespace android

#endif  // ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_FRAME_POOL_H