hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <sys/prctl.h>
 
#include "buffer.h"
#include "flow.h"
#include "stream.h"
#include "utils.h"
 
namespace easymedia
{
 
    class SourceFlow : public Flow
    {
      public:
        SourceFlow(const char* param);
        virtual ~SourceFlow();
        static const char* GetFlowName()
        {
            return "source_flow";
        }
        virtual int Control(unsigned long int request, ...) final
        {
            if (!stream) {
                return -1;
            }
            va_list vl;
            va_start(vl, request);
            void* arg = va_arg(vl, void*);
            va_end(vl);
            return stream->IoCtrl(request, arg);
        }
 
      private:
        void ReadThreadRun();
        bool loop;
        std::thread* read_thread;
        std::shared_ptr<Stream> stream;
        std::string tag;
    };
 
    SourceFlow::SourceFlow(const char* param) : loop(false), read_thread(nullptr)
    {
        std::list<std::string> separate_list;
        std::map<std::string, std::string> params;
        if (!ParseWrapFlowParams(param, params, separate_list)) {
            SetError(-EINVAL);
            return;
        }
        std::string& name = params[KEY_NAME];
        const char* stream_name = name.c_str();
        const std::string& stream_param = separate_list.back();
        stream = REFLECTOR(Stream)::Create<Stream>(stream_name, stream_param.c_str());
        if (!stream) {
            LOG("Create stream %s failed\n", stream_name);
            SetError(-EINVAL);
            return;
        }
        tag = "SourceFlow:";
        tag.append(name);
        if (!SetAsSource(std::vector<int>({0}), void_transaction00, tag)) {
            SetError(-EINVAL);
            return;
        }
        loop = true;
        read_thread = new std::thread(&SourceFlow::ReadThreadRun, this);
        if (!read_thread) {
            loop = false;
            SetError(-EINVAL);
            return;
        }
        SetFlowTag(tag);
    }
 
    SourceFlow::~SourceFlow()
    {
        loop = false;
        StopAllThread();
        int stop = 1;
        if (stream && Control(S_STREAM_OFF, &stop)) {
            LOG("Fail to stop source stream\n");
        }
        if (read_thread) {
            source_start_cond_mtx->lock();
            loop = false;
            source_start_cond_mtx->notify();
            source_start_cond_mtx->unlock();
            read_thread->join();
            delete read_thread;
        }
        stream.reset();
    }
 
    void SourceFlow::ReadThreadRun()
    {
        prctl(PR_SET_NAME, this->tag.c_str());
        source_start_cond_mtx->lock();
        if (down_flow_num == 0 && IsEnable()) {
            source_start_cond_mtx->wait();
        }
        source_start_cond_mtx->unlock();
        while (loop) {
            if (stream->Eof()) {
                // TODO: tell that I reach eof
                SetDisable();
                break;
            }
            auto buffer = stream->Read();
            SendInput(buffer, 0);
        }
    }
 
    DEFINE_FLOW_FACTORY(SourceFlow, Flow)
    const char* FACTORY(SourceFlow)::ExpectedInputDataType()
    {
        return nullptr;
    }
 
    // TODO!
    const char* FACTORY(SourceFlow)::OutPutDataType()
    {
        return "";
    }
 
} // namespace easymedia