hc
2023-02-15 9c2dfff96adcfcdf2a1143893a7dd749b9f3677a
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 *  Copyright (c) 2019 Rockchip Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
#include <stdlib.h>
#include <fcntl.h>
#include "Stream.h"
#include "Isp20StatsBuffer.h"
#include "rkisp2-config.h"
#include "SensorHw.h"
#include "LensHw.h"
#include "Isp20_module_dbg.h"
#include "CamHwIsp20.h"
#include "CaptureRawData.h"
namespace RkCam {
 
const int RkPollThread::default_poll_timeout = 300; // ms
 
const char*
RKStream::poll_type_to_str[ISP_POLL_POST_MAX] =
{
    "luma_poll",
    "isp_3a_stats_poll",
    "isp_param_poll",
    "ispp_fec_param_poll",
    "ispp_tnr_param_poll",
    "ispp_nr_param_poll",
    "ispp_tnr_stats_poll",
    "ispp_nr_stats_poll",
    "isp_sof_poll",
    "isp_tx_poll",
    "isp_rx_poll",
    "isp_sp_poll",
};
 
RkPollThread::RkPollThread (const char* thName, int type, SmartPtr<V4l2Device> dev, RKStream *stream)
    :Thread(thName)
    ,_poll_callback (NULL)
    ,frameid (0)
    ,_dev(dev)
    ,_stream(stream)
    ,_dev_type(type)
{
    _poll_stop_fd[0] =  -1;
    _poll_stop_fd[1] =  -1;
 
    XCAM_LOG_DEBUG ("RkPollThread constructed");
}
 
RkPollThread::RkPollThread (const char* thName, int type, SmartPtr<V4l2SubDevice> dev, RKStream *stream)
    :Thread(thName)
    ,_poll_callback (NULL)
    ,frameid (0)
    ,_subdev(dev)
    ,_stream(stream)
    ,_dev_type(type)
{
    _poll_stop_fd[0] =  -1;
    _poll_stop_fd[1] =  -1;
 
    XCAM_LOG_DEBUG ("RkPollThread constructed");
}
 
RkPollThread::~RkPollThread ()
{
    stop();
    XCAM_LOG_DEBUG ("~RkPollThread destructed");
}
 
void RkPollThread::destroy_stop_fds () {
    if (_poll_stop_fd[1] != -1 || _poll_stop_fd[0] != -1) {
        close(_poll_stop_fd[0]);
        close(_poll_stop_fd[1]);
        _poll_stop_fd[0] = -1;
        _poll_stop_fd[1] = -1;
    }
}
 
XCamReturn RkPollThread::create_stop_fds () {
    int status = 0;
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
 
    destroy_stop_fds ();
 
    status = pipe(_poll_stop_fd);
    if (status < 0) {
        XCAM_LOG_ERROR ("Failed to create ispp poll stop pipe: %s", strerror(errno));
        ret = XCAM_RETURN_ERROR_UNKNOWN;
        goto exit_error;
    }
 
    /**
     * make the reading end of the pipe non blocking.
     * This helps during flush to read any information left there without
     * blocking
     */
    status = fcntl(_poll_stop_fd[0], F_SETFL, O_NONBLOCK);
    if (status < 0) {
        XCAM_LOG_ERROR ("Fail to set event ispp stop pipe flag: %s", strerror(errno));
        ret = XCAM_RETURN_ERROR_UNKNOWN;
        goto exit_error;
    }
 
    return XCAM_RETURN_NO_ERROR;
exit_error:
    destroy_stop_fds ();
    return ret;
}
 
XCamReturn RkPollThread::start ()
{
    if (create_stop_fds ()) {
        XCAM_LOG_ERROR("create stop fds failed !");
        return XCAM_RETURN_ERROR_UNKNOWN;
    }
 
    Thread::start();
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn RkPollThread::stop ()
{
    XCAM_LOG_DEBUG ("RkPollThread stop");
    if (_poll_stop_fd[1] != -1) {
        char buf = 0xf;  // random value to write to flush fd.
        unsigned int size = write(_poll_stop_fd[1], &buf, sizeof(char));
        if (size != sizeof(char))
            XCAM_LOG_WARNING("Flush write not completed");
    }
    Thread::stop();
    destroy_stop_fds ();
 
    return XCAM_RETURN_NO_ERROR;
}
 
bool
RkPollThread::setPollCallback (PollCallback *callback)
{
    XCAM_ASSERT (!_poll_callback);
    _poll_callback = callback;
    return true;
}
 
XCamReturn
RkPollThread::poll_buffer_loop ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    int poll_ret = 0;
    SmartPtr<V4l2Buffer> buf;
    int stop_fd = -1;
 
    stop_fd = _poll_stop_fd[0];
    poll_ret = _dev->poll_event (RkPollThread::default_poll_timeout, stop_fd);
 
    if (poll_ret == POLL_STOP_RET) {
        XCAM_LOG_DEBUG ("poll buffer stop success !");
        // stop success, return error to stop the poll thread
        return XCAM_RETURN_ERROR_UNKNOWN;
    }
 
    if (poll_ret < 0 && (errno == EAGAIN || errno == EINTR)) {
        XCAM_LOG_DEBUG("poll buffer event got interrupt(0x%x), continue\n",
                       poll_ret);
        return XCAM_RETURN_ERROR_TIMEOUT;
    } else if (poll_ret < 0) {
        XCAM_LOG_DEBUG("poll buffer event got error(0x%x) exit\n", poll_ret);
        return XCAM_RETURN_ERROR_UNKNOWN;
    } else if (poll_ret == 0) {
        XCAM_LOG_DEBUG("poll buffer event timeout(0x%x), continue\n",
                       poll_ret);
        return XCAM_RETURN_ERROR_TIMEOUT;
    }
 
    ret = _dev->dequeue_buffer (buf);
    if (ret != XCAM_RETURN_NO_ERROR) {
        XCAM_LOG_WARNING ("dequeue buffer failed");
        return ret;
    }
 
    XCAM_ASSERT (buf.ptr());
 
    if (_dev_type == ISP_POLL_TX || _dev_type == ISP_POLL_RX) {
        SmartPtr<V4l2BufferProxy> buf_proxy = _stream->new_v4l2proxy_buffer(buf, _dev);
        if (_poll_callback && buf_proxy.ptr())
            _poll_callback->poll_buffer_ready (buf_proxy, ((RKRawStream*)_stream)->_dev_index);
    } else {
        SmartPtr<VideoBuffer> video_buf = _stream->new_video_buffer(buf, _dev);
        if (_poll_callback && video_buf.ptr())
            _poll_callback->poll_buffer_ready (video_buf);
    }
 
    return ret;
}
 
RkEventPollThread::RkEventPollThread (const char* thName, int type, SmartPtr<V4l2SubDevice> dev, RKStream *stream)
    :RkPollThread(thName, type, dev, stream)
{
    XCAM_LOG_DEBUG ("RkEventPollThread constructed");
}
 
RkEventPollThread::~RkEventPollThread ()
{
    stop();
    XCAM_LOG_DEBUG ("~RkEventPollThread destructed");
}
 
XCamReturn
RkEventPollThread::poll_event_loop ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    int poll_ret = 0;
    struct v4l2_event event;
    int stop_fd = -1;
 
    stop_fd = _poll_stop_fd[0];
    poll_ret = _subdev->poll_event (RkPollThread::default_poll_timeout, stop_fd);
 
    if (poll_ret == POLL_STOP_RET) {
        XCAM_LOG_DEBUG ("poll event stop success !");
        // stop success, return error to stop the poll thread
        return XCAM_RETURN_ERROR_UNKNOWN;
    }
 
    if (poll_ret < 0 && (errno == EAGAIN || errno == EINTR)) {
        XCAM_LOG_DEBUG("poll buffer event got interrupt(0x%x), continue\n",
                       poll_ret);
        return XCAM_RETURN_ERROR_TIMEOUT;
    } else if (poll_ret < 0) {
        XCAM_LOG_DEBUG("poll buffer event got error(0x%x) exit\n", poll_ret);
        return XCAM_RETURN_ERROR_UNKNOWN;
    } else if (poll_ret == 0) {
        XCAM_LOG_WARNING ("poll event timeout and continue");
        return XCAM_RETURN_ERROR_TIMEOUT;
    }
    xcam_mem_clear (event);
 
    ret = _subdev->dequeue_event (event);
    if (ret != XCAM_RETURN_NO_ERROR) {
        XCAM_LOG_WARNING ("dequeue event failed on dev:%s", XCAM_STR(_subdev->get_device_name()));
        return XCAM_RETURN_ERROR_IOCTL;
    }
 
    if (_poll_callback) {
        SmartPtr<VideoBuffer> video_buf = _stream->new_video_buffer(event, _subdev);
        _poll_callback->poll_buffer_ready (video_buf);
    }
 
 
    return ret;
}
 
RKStream::RKStream (SmartPtr<V4l2Device> dev, int type)
    :_dev(dev)
    ,_dev_type(type)
    ,_dev_prepared(false)
{
    _poll_thread = new RkPollThread(RKStream::poll_type_to_str[type], type, dev, this);
    XCAM_LOG_DEBUG ("RKStream constructed");
}
 
RKStream::RKStream (SmartPtr<V4l2SubDevice> dev, int type)
    :_subdev(dev)
    ,_dev_type(type)
    ,_dev_prepared(false)
{
    _poll_thread = new RkEventPollThread(RKStream::poll_type_to_str[type], type, dev, this);
    XCAM_LOG_DEBUG ("RKStream constructed");
}
 
RKStream::RKStream (const char *path, int type)
    :_dev_type(type)
    ,_dev_prepared(false)
{
    _dev = new V4l2Device(path);
    _poll_thread = new RkPollThread(RKStream::poll_type_to_str[type], type, _dev, this);
    XCAM_LOG_DEBUG ("RKStream constructed");
}
 
RKStream::~RKStream()
{
    XCAM_LOG_DEBUG ("~RKStream destructed");
}
 
void
RKStream::start()
{
    if (!_dev->is_activated())
        _dev->start(_dev_prepared);
    _poll_thread->start();
}
 
void
RKStream::startThreadOnly()
{
    _poll_thread->start();
}
 
void
RKStream::startDeviceOnly()
{
    if (!_dev->is_activated())
        _dev->start(_dev_prepared);
}
 
void
RKStream::stop()
{
    _poll_thread->stop();
    _dev->stop();
    _dev_prepared = false;
}
 
void
RKStream::stopThreadOnly()
{
    _poll_thread->stop();
}
 
void
RKStream::stopDeviceOnly()
{
    _dev->stop();
    _dev_prepared = false;
}
 
void
RKStream::pause()
{
}
 
void
RKStream::resume()
{
}
 
void
RKStream::set_device_prepared(bool prepare)
{
    _dev_prepared = prepare;
}
 
SmartPtr<VideoBuffer> RKStream::new_video_buffer(SmartPtr<V4l2Buffer> buf,
                                       SmartPtr<V4l2Device> dev)
    SmartPtr<VideoBuffer> video_buf = new V4l2BufferProxy (buf, dev);
    video_buf->_buf_type = _dev_type;
    return video_buf;
}
 
bool
RKStream::setPollCallback (PollCallback *callback)
{
    return _poll_thread->setPollCallback(callback);
}
 
XCamReturn RKStream::getFormat(struct v4l2_format &format)
{
    return _dev->get_format (format);
}
 
XCamReturn RKStream::getFormat(struct v4l2_subdev_format &format)
{
    return _subdev->getFormat (format);
}
 
RKStatsStream::RKStatsStream (SmartPtr<V4l2Device> dev, int type)
    :RKStream(dev, type)
{
    XCAM_LOG_DEBUG ("RKStream constructed");
}
 
//RKStatsStream::RKStatsStream (const char *name, int type)
//    :RKStream(name, type)
//{
//    XCAM_LOG_DEBUG ("RKStream constructed");
//}
 
RKStatsStream::~RKStatsStream()
{
    XCAM_LOG_DEBUG ("~RKStream destructed");
}
 
bool RKStatsStream::set_event_handle_dev(SmartPtr<BaseSensorHw> &dev)
{
    _event_handle_dev = dev;
    return true;
}
 
bool RKStatsStream::set_iris_handle_dev(SmartPtr<LensHw> &dev)
{
    _iris_handle_dev = dev;
    return true;
}
 
bool RKStatsStream::set_focus_handle_dev(SmartPtr<LensHw> &dev)
{
    _focus_handle_dev = dev;
    return true;
}
 
bool RKStatsStream::set_rx_handle_dev(CamHwIsp20* dev)
{
    _rx_handle_dev = dev;
    return true;
}
 
SmartPtr<VideoBuffer>
RKStatsStream::new_video_buffer(SmartPtr<V4l2Buffer> buf,
                                       SmartPtr<V4l2Device> dev)
{
    ENTER_CAMHW_FUNCTION();
    //SmartPtr<VideoBuffer> video_buf = nullptr;
    SmartPtr<Isp20StatsBuffer> isp20stats_buf = nullptr;
 
    // SmartPtr<RkAiqIspParamsProxy> ispParams = nullptr;
    rkisp_effect_params_v20 ispParams = {0};
    SmartPtr<RkAiqExpParamsProxy> expParams = nullptr;
    SmartPtr<RkAiqIrisParamsProxy> irisParams = nullptr;
    SmartPtr<RkAiqAfInfoProxy> afParams = nullptr;
 
    if (_focus_handle_dev.ptr()) {
        _focus_handle_dev->getAfInfoParams(afParams, buf->get_buf().sequence);
        _focus_handle_dev->getIrisInfoParams(irisParams, buf->get_buf().sequence);
    }
 
    isp20stats_buf = new Isp20StatsBuffer(buf, dev, _event_handle_dev, _rx_handle_dev, afParams, irisParams);
    isp20stats_buf->_buf_type = _dev_type;
    isp20stats_buf->getEffectiveIspParams(buf->get_buf().sequence, ispParams);
    isp20stats_buf->getEffectiveExpParams(buf->get_buf().sequence, expParams);
    CaptureRawData::getInstance().save_metadata_and_register(buf->get_buf().sequence, ispParams, expParams, afParams, _rx_handle_dev->get_workingg_mode());
 
    EXIT_CAMHW_FUNCTION();
 
    return isp20stats_buf;
}
 
/*--------------------sof event stream---------------------------*/
 
RKSofEventStream::RKSofEventStream (SmartPtr<V4l2SubDevice> dev, int type)
    :RKStream(dev, type)
{
    XCAM_LOG_DEBUG ("RKSofEventStream constructed");
}
 
//RKSofEventStream::RKSofEventStream (const char *name, int type)
//    :RKStream(name, type)
//{
//    XCAM_LOG_DEBUG ("RKSofEventStream constructed");
//}
 
RKSofEventStream::~RKSofEventStream()
{
    XCAM_LOG_DEBUG ("~RKSofEventStream destructed");
}
 
void
RKSofEventStream::start()
{
    _subdev->start(_dev_prepared);
    _subdev->subscribe_event(V4L2_EVENT_FRAME_SYNC);
    _poll_thread->start();
}
 
void
RKSofEventStream::stop()
{
    _poll_thread->stop();
    _subdev->unsubscribe_event(V4L2_EVENT_FRAME_SYNC);
    _subdev->stop();
}
 
SmartPtr<VideoBuffer>
RKSofEventStream::new_video_buffer(struct v4l2_event &event,
                                       SmartPtr<V4l2Device> dev)
{
    ENTER_CAMHW_FUNCTION();
    SmartPtr<VideoBuffer> video_buf = nullptr;
    int64_t tv_sec = event.timestamp.tv_sec;
    int64_t tv_nsec = event.timestamp.tv_nsec;
    int exp_id = event.u.frame_sync.frame_sequence;
    SmartPtr<SofEventData> evtdata = new SofEventData();
    evtdata->_timestamp = tv_sec * 1000 * 1000 * 1000 + tv_nsec;
    evtdata->_frameid = exp_id;
 
    video_buf = new SofEventBuffer(evtdata, dev);
    video_buf->_buf_type = _dev_type;
    EXIT_CAMHW_FUNCTION();
 
    return video_buf;
}
 
/*--------------------Output stream---------------------------*/
 
RKRawStream::RKRawStream (SmartPtr<V4l2Device> dev, int index, int type)
    :RKStream(dev, type)
    ,_dev_index(index)
{
    XCAM_LOG_DEBUG ("RKRawStream constructed");
}
 
RKRawStream::~RKRawStream()
{
    XCAM_LOG_DEBUG ("~RKRawStream destructed");
}
 
SmartPtr<V4l2BufferProxy>
RKRawStream::new_v4l2proxy_buffer(SmartPtr<V4l2Buffer> buf,
                                       SmartPtr<V4l2Device> dev)
{
    ENTER_CAMHW_FUNCTION();
    SmartPtr<V4l2BufferProxy> buf_proxy = new V4l2BufferProxy(buf, dev);
    buf_proxy->_buf_type = _dev_type;
    EXIT_CAMHW_FUNCTION();
 
    return buf_proxy;
}
 
}; //namspace RkCam