@@ -93,9 +93,11 @@ int main(int argc, char* argv[]) {
9393 std::cerr << " [producer] failed to connect\n " ;
9494 exit_code = 1 ;
9595 } else {
96- auto lp = room.localParticipant ().lock ();
97- std::cout << " [producer] connected as " << (lp ? lp->identity () : std::string (" <unknown>" )) << " to room '"
98- << room.roomInfo ().name << " '\n " ;
96+ {
97+ auto lp = room.localParticipant ().lock ();
98+ std::cout << " [producer] connected as " << (lp ? lp->identity () : std::string (" <unknown>" )) << " to room '"
99+ << room.roomInfo ().name << " '\n " ;
100+ }
99101
100102 auto source = std::make_shared<VideoSource>(kFrameWidth , kFrameHeight );
101103 auto track = LocalVideoTrack::createLocalVideoTrack (" timestamped-camera" , source);
@@ -105,10 +107,13 @@ int main(int argc, char* argv[]) {
105107 publish_options.source = TrackSource::SOURCE_CAMERA ;
106108 publish_options.packet_trailer_features .user_timestamp = cli_options.use_user_timestamp ;
107109
108- if (!lp) throw std::runtime_error (" local participant unavailable" );
109- lp->publishTrack (track, publish_options);
110- std::cout << " [producer] published camera track with user timestamp "
111- << (cli_options.use_user_timestamp ? " enabled" : " disabled" ) << " \n " ;
110+ {
111+ auto lp = room.localParticipant ().lock ();
112+ if (!lp) throw std::runtime_error (" local participant unavailable" );
113+ lp->publishTrack (track, publish_options);
114+ std::cout << " [producer] published camera track with user timestamp "
115+ << (cli_options.use_user_timestamp ? " enabled" : " disabled" ) << " \n " ;
116+ }
112117
113118 VideoFrame frame = VideoFrame::create (kFrameWidth , kFrameHeight , VideoBufferType::BGRA );
114119 const auto capture_start = std::chrono::steady_clock::now ();
@@ -149,8 +154,11 @@ int main(int argc, char* argv[]) {
149154 exit_code = 1 ;
150155 }
151156
152- if (lp && track->publication ()) {
153- lp->unpublishTrack (track->publication ()->sid ());
157+ // best effort unpublish
158+ if (auto lp = room.localParticipant ().lock ()) {
159+ if (lp && track->publication ()) {
160+ lp->unpublishTrack (track->publication ()->sid ());
161+ }
154162 }
155163 }
156164 }
0 commit comments