Delete VideoReceiveStream channels in destructor.

R=stefan@webrtc.org
BUG=1667

Review URL: https://webrtc-codereview.appspot.com/31909004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7611 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/video/end_to_end_tests.cc b/webrtc/video/end_to_end_tests.cc
index 06cb187..96249c3 100644
--- a/webrtc/video/end_to_end_tests.cc
+++ b/webrtc/video/end_to_end_tests.cc
@@ -2140,4 +2140,39 @@
 
   DestroyStreams();
 }
+
+// TODO(pbos): Remove this regression test when VideoEngine is no longer used as
+// a backend. This is to test that we hand channels back properly.
+TEST_F(EndToEndTest, CanCreateAndDestroyManyVideoStreams) {
+  test::NullTransport transport;
+  scoped_ptr<Call> call(Call::Create(Call::Config(&transport)));
+  test::FakeDecoder fake_decoder;
+  test::FakeEncoder fake_encoder(Clock::GetRealTimeClock());
+  for (size_t i = 0; i < 100; ++i) {
+    VideoSendStream::Config send_config;
+    send_config.encoder_settings.encoder = &fake_encoder;
+    send_config.encoder_settings.payload_name = "FAKE";
+    send_config.encoder_settings.payload_type = 123;
+
+    VideoEncoderConfig encoder_config;
+    encoder_config.streams = test::CreateVideoStreams(1);
+    send_config.rtp.ssrcs.push_back(1);
+    VideoSendStream* send_stream =
+        call->CreateVideoSendStream(send_config, encoder_config);
+    call->DestroyVideoSendStream(send_stream);
+
+    VideoReceiveStream::Config receive_config;
+    receive_config.rtp.remote_ssrc = 1;
+    receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
+    VideoReceiveStream::Decoder decoder;
+    decoder.decoder = &fake_decoder;
+    decoder.payload_type = 123;
+    decoder.payload_name = "FAKE";
+    receive_config.decoders.push_back(decoder);
+    VideoReceiveStream* receive_stream =
+        call->CreateVideoReceiveStream(receive_config);
+    call->DestroyVideoReceiveStream(receive_stream);
+  }
+}
+
 }  // namespace webrtc
diff --git a/webrtc/video/video_receive_stream.cc b/webrtc/video/video_receive_stream.cc
index b182240..5b085bb 100644
--- a/webrtc/video/video_receive_stream.cc
+++ b/webrtc/video/video_receive_stream.cc
@@ -222,7 +222,6 @@
 
   video_engine_base_->SetVoiceEngine(NULL);
   image_process_->Release();
-  video_engine_base_->Release();
   external_codec_->Release();
   codec_->DeregisterDecoderObserver(channel_);
   rtp_rtcp_->DeregisterReceiveChannelRtpStatisticsCallback(channel_,
@@ -233,6 +232,8 @@
   network_->Release();
   render_->Release();
   rtp_rtcp_->Release();
+  video_engine_base_->DeleteChannel(channel_);
+  video_engine_base_->Release();
 }
 
 void VideoReceiveStream::Start() {