1/*
2 * Copyright (C) 2013 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#ifndef ANDROID_SERVERS_CAMERA3_STREAM_INTERFACE_H
18#define ANDROID_SERVERS_CAMERA3_STREAM_INTERFACE_H
19
20#include <utils/RefBase.h>
21#include "Camera3StreamBufferListener.h"
22
23struct camera3_stream_buffer;
24
25namespace android {
26
27namespace camera3 {
28
29class StatusTracker;
30
31/**
32 * An interface for managing a single stream of input and/or output data from
33 * the camera device.
34 */
35class Camera3StreamInterface : public virtual RefBase {
36  public:
37    /**
38     * Get the stream's ID
39     */
40    virtual int      getId() const = 0;
41
42    /**
43     * Get the stream's dimensions and format
44     */
45    virtual uint32_t getWidth() const = 0;
46    virtual uint32_t getHeight() const = 0;
47    virtual int      getFormat() const = 0;
48
49    /**
50     * Start the stream configuration process. Returns a handle to the stream's
51     * information to be passed into the HAL device's configure_streams call.
52     *
53     * Until finishConfiguration() is called, no other methods on the stream may
54     * be called. The usage and max_buffers fields of camera3_stream may be
55     * modified between start/finishConfiguration, but may not be changed after
56     * that. The priv field of camera3_stream may be modified at any time after
57     * startConfiguration.
58     *
59     * Returns NULL in case of error starting configuration.
60     */
61    virtual camera3_stream* startConfiguration() = 0;
62
63    /**
64     * Check if the stream is mid-configuration (start has been called, but not
65     * finish).  Used for lazy completion of configuration.
66     */
67    virtual bool    isConfiguring() const = 0;
68
69    /**
70     * Completes the stream configuration process. During this call, the stream
71     * may call the device's register_stream_buffers() method. The stream
72     * information structure returned by startConfiguration() may no longer be
73     * modified after this call, but can still be read until the destruction of
74     * the stream.
75     *
76     * Returns:
77     *   OK on a successful configuration
78     *   NO_INIT in case of a serious error from the HAL device
79     *   NO_MEMORY in case of an error registering buffers
80     *   INVALID_OPERATION in case connecting to the consumer failed
81     */
82    virtual status_t finishConfiguration(camera3_device *hal3Device) = 0;
83
84    /**
85     * Cancels the stream configuration process. This returns the stream to the
86     * initial state, allowing it to be configured again later.
87     * This is done if the HAL rejects the proposed combined stream configuration
88     */
89    virtual status_t cancelConfiguration() = 0;
90
91    /**
92     * Fill in the camera3_stream_buffer with the next valid buffer for this
93     * stream, to hand over to the HAL.
94     *
95     * This method may only be called once finishConfiguration has been called.
96     * For bidirectional streams, this method applies to the output-side
97     * buffers.
98     *
99     */
100    virtual status_t getBuffer(camera3_stream_buffer *buffer) = 0;
101
102    /**
103     * Return a buffer to the stream after use by the HAL.
104     *
105     * This method may only be called for buffers provided by getBuffer().
106     * For bidirectional streams, this method applies to the output-side buffers
107     */
108    virtual status_t returnBuffer(const camera3_stream_buffer &buffer,
109            nsecs_t timestamp) = 0;
110
111    /**
112     * Fill in the camera3_stream_buffer with the next valid buffer for this
113     * stream, to hand over to the HAL.
114     *
115     * This method may only be called once finishConfiguration has been called.
116     * For bidirectional streams, this method applies to the input-side
117     * buffers.
118     *
119     */
120    virtual status_t getInputBuffer(camera3_stream_buffer *buffer) = 0;
121
122    /**
123     * Return a buffer to the stream after use by the HAL.
124     *
125     * This method may only be called for buffers provided by getBuffer().
126     * For bidirectional streams, this method applies to the input-side buffers
127     */
128    virtual status_t returnInputBuffer(const camera3_stream_buffer &buffer) = 0;
129
130    /**
131     * Whether any of the stream's buffers are currently in use by the HAL,
132     * including buffers that have been returned but not yet had their
133     * release fence signaled.
134     */
135    virtual bool     hasOutstandingBuffers() const = 0;
136
137    enum {
138        TIMEOUT_NEVER = -1
139    };
140
141    /**
142     * Set the state tracker to use for signaling idle transitions.
143     */
144    virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0;
145
146    /**
147     * Disconnect stream from its non-HAL endpoint. After this,
148     * start/finishConfiguration must be called before the stream can be used
149     * again. This cannot be called if the stream has outstanding dequeued
150     * buffers.
151     */
152    virtual status_t disconnect() = 0;
153
154    /**
155     * Debug dump of the stream's state.
156     */
157    virtual void     dump(int fd, const Vector<String16> &args) const = 0;
158
159    virtual void     addBufferListener(
160            wp<Camera3StreamBufferListener> listener) = 0;
161    virtual void     removeBufferListener(
162            const sp<Camera3StreamBufferListener>& listener) = 0;
163};
164
165} // namespace camera3
166
167} // namespace android
168
169#endif
170