@@ -35,11 +35,9 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
3535 " The connection timeout, in seconds, after receiving a remote offer" )
3636 (" segment_duration" , bpo::value<int >()->default_value (args.segment_duration ),
3737 " The length (in seconds) of each MP4 recording." )
38- (" device" , bpo::value<std::string>()->default_value (args.device ),
39- " Read the specific camera file via V4L2, default is /dev/video0" )
40- (" use_libcamera" , bpo::bool_switch ()->default_value (args.use_libcamera ),
41- " Read YUV420 from the camera via libcamera, the `device` and `v4l2_format` "
42- " flags will be suspended" )
38+ (" camera" , bpo::value<std::string>()->default_value (args.camera ),
39+ " Specify the camera using V4L2 or Libcamera. "
40+ " Examples: \" libcamera:0\" for Libcamera, \" v4l2:/dev/video0\" for V4L2." )
4341 (" fixed_resolution" , bpo::bool_switch ()->default_value (args.fixed_resolution ),
4442 " Disable adaptive resolution scaling and keep a fixed resolution." )
4543 (" no_audio" , bpo::bool_switch ()->default_value (args.no_audio ), " Run without audio source" )
@@ -95,7 +93,7 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
9593 SetIfExists (vm, " rotation_angle" , args.rotation_angle );
9694 SetIfExists (vm, " peer_timeout" , args.peer_timeout );
9795 SetIfExists (vm, " segment_duration" , args.segment_duration );
98- SetIfExists (vm, " device " , args.device );
96+ SetIfExists (vm, " camera " , args.camera );
9997 SetIfExists (vm, " v4l2_format" , args.v4l2_format );
10098 SetIfExists (vm, " uid" , args.uid );
10199 SetIfExists (vm, " stun_url" , args.stun_url );
@@ -109,7 +107,6 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
109107 SetIfExists (vm, " http_port" , args.http_port );
110108 SetIfExists (vm, " record_path" , args.record_path );
111109
112- args.use_libcamera = vm[" use_libcamera" ].as <bool >();
113110 args.fixed_resolution = vm[" fixed_resolution" ].as <bool >();
114111 args.no_audio = vm[" no_audio" ].as <bool >();
115112 args.hw_accel = vm[" hw_accel" ].as <bool >();
@@ -134,16 +131,38 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
134131 }
135132 }
136133
137- if (args.use_libcamera ) {
134+ ParseDevice (args);
135+ }
136+
137+ void Parser::ParseDevice (Args &args) {
138+ size_t pos = args.camera .find (' :' );
139+ if (pos == std::string::npos) {
140+ throw std::runtime_error (" Unknown device format: " + args.camera );
141+ }
142+
143+ std::string prefix = args.camera .substr (0 , pos);
144+ std::string id = args.camera .substr (pos + 1 );
145+
146+ try {
147+ args.cameraId = std::stoi (id);
148+ } catch (const std::exception &e) {
149+ throw std::runtime_error (" Invalid camera ID: " + id);
150+ }
151+
152+ if (prefix == " libcamera" ) {
153+ args.use_libcamera = true ;
138154 args.format = V4L2_PIX_FMT_YUV420;
139- } else {
155+ std::cout << " Using Libcamera, ID: " << args.cameraId << std::endl;
156+ } else if (prefix == " v4l2" ) {
140157 auto it = format_map.find (args.v4l2_format );
141158 if (it != format_map.end ()) {
142159 args.format = it->second ;
143- printf ( " Use %s format source in v4l2 \n " , args.v4l2_format . c_str ()) ;
160+ std::cout << " Using V4L2: " << args.v4l2_format << std::endl ;
144161 } else {
145- std::cerr << " Unsupported format: " << args.v4l2_format << std::endl;
146- exit (1 );
162+ throw std::runtime_error (" Unsupported format: " + args.v4l2_format );
147163 }
164+ std::cout << " Using V4L2, ID: " << args.cameraId << std::endl;
165+ } else {
166+ throw std::runtime_error (" Unknown device format: " + prefix);
148167 }
149168}
0 commit comments