1 /*
2  * Copyright 2018, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <source/StreamingSource.h>
20 
21 #include <functional>
22 #include <memory>
23 #include <thread>
24 
25 namespace android {
26 
27 struct FrameBufferSource : public StreamingSource {
28     enum class Format {
29         VP8,
30     };
31 
32     explicit FrameBufferSource(Format format);
33 
34     FrameBufferSource(const FrameBufferSource &) = delete;
35     FrameBufferSource &operator=(const FrameBufferSource &) = delete;
36 
37     ~FrameBufferSource() override;
38 
39     int32_t initCheck() const override;
40 
41     int32_t start() override;
42     int32_t stop() override;
43 
44     int32_t pause() override;
45     int32_t resume() override;
46 
47     bool paused() const override;
48 
49     int32_t requestIDRFrame() override;
50     void notifyNewStreamConsumer() override;
51     void notifyStreamConsumerDisconnected() override;
52 
53     void setScreenParams(const int32_t screenParams[4]);
54     void injectFrame(const void *data, size_t size);
55 
56 private:
57     enum State {
58         STOPPING,
59         STOPPED,
60         RUNNING,
61         PAUSED
62     };
63 
64     struct Encoder;
65     struct VPXEncoder;
66 
67     int32_t mInitCheck;
68     State mState;
69     Format mFormat;
70     std::unique_ptr<Encoder> mEncoder;
71 
72     std::mutex mLock;
73 
74     int32_t mScreenWidth, mScreenHeight, mScreenDpi, mScreenRate, mNumConsumers;
75 
76     std::function<void(const std::shared_ptr<SBuffer> &)> mOnFrameFn;
77 };
78 
79 }  // namespace android
80 
81 
82