| 
									
										
										
										
											2020-04-27 11:07:24 +00:00
										 |  |  | #include <uvgrtp/lib.hh>
 | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2022-09-08 06:36:23 +00:00
										 |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  | /* RTP is a protocol for real-time streaming. The simplest usage
 | 
					
						
							|  |  |  |  * scenario is sending one RTP stream and receiving it. This example | 
					
						
							|  |  |  |  * Shows how to send one RTP stream. These examples perform a simple | 
					
						
							|  |  |  |  * test if they are run. You may run the receiving examples at the same | 
					
						
							|  |  |  |  * time to see the whole demo. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* parameters of this example. You may change these to reflect
 | 
					
						
							|  |  |  |  * you network environment. */ | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  | constexpr char REMOTE_ADDRESS[] = "127.0.0.1"; | 
					
						
							|  |  |  | constexpr uint16_t REMOTE_PORT = 8890; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  | // the parameters of demostration
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  | constexpr size_t PAYLOAD_LEN = 100; | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  | constexpr int    AMOUNT_OF_TEST_PACKETS = 100; | 
					
						
							|  |  |  | constexpr auto   END_WAIT = std::chrono::seconds(5); | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 06:12:50 +00:00
										 |  |  | int main(void) | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  |     std::cout << "Starting uvgRTP RTP sending example" << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 06:12:50 +00:00
										 |  |  |     /* To use the library, one must create a global RTP context object */ | 
					
						
							| 
									
										
										
										
											2021-02-19 01:13:43 +00:00
										 |  |  |     uvgrtp::context ctx; | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  |     // A session represents
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  |     uvgrtp::session *sess = ctx.create_session(REMOTE_ADDRESS); | 
					
						
							| 
									
										
										
										
											2020-02-14 06:12:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Each RTP session has one or more media streams. These media streams are bidirectional
 | 
					
						
							|  |  |  |      * and they require both source and destination ports for the connection. One must also | 
					
						
							| 
									
										
										
										
											2021-04-23 05:55:15 +00:00
										 |  |  |      * specify the media format for the stream and any configuration flags if needed. | 
					
						
							| 
									
										
										
										
											2020-01-07 08:20:20 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  |      * See Configuration example for more details about configuration. | 
					
						
							| 
									
										
										
										
											2020-01-07 08:20:20 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-02-14 06:12:50 +00:00
										 |  |  |      * First port is source port aka the port that we listen to and second port is the port | 
					
						
							|  |  |  |      * that remote listens to | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-02-14 06:12:50 +00:00
										 |  |  |      * This same object is used for both sending and receiving media | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-04-23 05:55:15 +00:00
										 |  |  |      * In this example, we have one media stream with the remote participant: H265 */ | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 07:46:50 +00:00
										 |  |  |     int flags = RCE_SEND_ONLY; | 
					
						
							|  |  |  |     uvgrtp::media_stream *hevc = sess->create_stream(REMOTE_PORT, RTP_FORMAT_H265, flags); | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  |     if (hevc) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  |         /* In this example we send packets as fast as possible. The source can be
 | 
					
						
							|  |  |  |          * a file or a real-time encoded stream */ | 
					
						
							|  |  |  |         for (int i = 0; i < AMOUNT_OF_TEST_PACKETS; ++i) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             std::unique_ptr<uint8_t[]> dummy_frame = std::unique_ptr<uint8_t[]>(new uint8_t[PAYLOAD_LEN]); | 
					
						
							| 
									
										
										
										
											2022-03-31 12:15:55 +00:00
										 |  |  |             memset(dummy_frame.get(), 'a', PAYLOAD_LEN); // NAL payload
 | 
					
						
							|  |  |  |             memset(dummy_frame.get(),     0, 3); | 
					
						
							|  |  |  |             memset(dummy_frame.get() + 3, 1, 1); | 
					
						
							|  |  |  |             memset(dummy_frame.get() + 4, 1, (19 << 1)); // Intra frame NAL type
 | 
					
						
							| 
									
										
										
										
											2021-07-27 13:51:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ((i+1)%10  == 0 || i == 0) // print every 10 frames and first
 | 
					
						
							|  |  |  |                 std::cout << "Sending frame " << i + 1 << '/' << AMOUNT_OF_TEST_PACKETS << std::endl; | 
					
						
							| 
									
										
										
										
											2021-07-27 07:36:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (hevc->push_frame(std::move(dummy_frame), PAYLOAD_LEN, RTP_NO_FLAGS) != RTP_OK) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 std::cout << "Failed to send RTP frame!" << std::endl; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          std::cout << "Sending finished. Waiting "<< END_WAIT.count() | 
					
						
							|  |  |  |                    << " seconds before exiting." << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // wait a little bit so pop-up console users have time to see the results
 | 
					
						
							|  |  |  |         std::this_thread::sleep_for(END_WAIT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         sess->destroy_stream(hevc); | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  |     if (sess) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /* Session must be destroyed manually */ | 
					
						
							|  |  |  |         ctx.destroy_session(sess); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-20 13:25:52 +00:00
										 |  |  |     return EXIT_SUCCESS; | 
					
						
							| 
									
										
										
										
											2019-08-21 05:04:45 +00:00
										 |  |  | } |