diff --git a/common/lib/xmodule/xmodule/video_module/video_module.py b/common/lib/xmodule/xmodule/video_module/video_module.py index af1d5ca506..e38d90301f 100644 --- a/common/lib/xmodule/xmodule/video_module/video_module.py +++ b/common/lib/xmodule/xmodule/video_module/video_module.py @@ -701,10 +701,32 @@ class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandler 'default_value': [get_youtube_link(youtube_id_1_0['default_value'])] }) - youtube_id_1_0_value = get_youtube_link(youtube_id_1_0['value']) + source_url = self.create_youtube_url(youtube_id_1_0['value']) - if youtube_id_1_0_value: - video_url['value'].insert(0, youtube_id_1_0_value) + # First try a lookup in VAL. If any video encoding is found given the video id then + # override the source_url with it. + if self.edx_video_id and edxval_api: + + val_profiles = ['youtube', 'desktop_webm', 'desktop_mp4'] + if HLSPlaybackEnabledFlag.feature_enabled(self.runtime.course_id.for_branch(None)): + val_profiles.append('hls') + + # Get video encodings for val profiles. + val_video_encodings = edxval_api.get_urls_for_profiles(self.edx_video_id, val_profiles) + + # If multiple encodings are there in val, the priority will be: youtube > hls > mp4 and webm. + if val_video_encodings.get('youtube'): + source_url = self.create_youtube_url(val_video_encodings['youtube']) + elif val_video_encodings.get('hls'): + source_url = val_video_encodings['hls'] + elif val_video_encodings.get('desktop_mp4'): + source_url = val_video_encodings['desktop_mp4'] + elif val_video_encodings.get('desktop_webm'): + source_url = val_video_encodings['desktop_webm'] + + # Only add if html5 sources do not already contain source_url. + if source_url and source_url not in video_url['value']: + video_url['value'].insert(0, source_url) metadata = { 'display_name': display_name, diff --git a/lms/djangoapps/courseware/tests/test_video_mongo.py b/lms/djangoapps/courseware/tests/test_video_mongo.py index 33fda8cc82..a56278deef 100644 --- a/lms/djangoapps/courseware/tests/test_video_mongo.py +++ b/lms/djangoapps/courseware/tests/test_video_mongo.py @@ -982,6 +982,7 @@ class TestVideoCDNRewriting(BaseTestXmodule): @attr(shard=1) +@ddt.ddt class TestVideoDescriptorInitialization(BaseTestXmodule): """ Make sure that module initialization works correctly. @@ -1051,6 +1052,68 @@ class TestVideoDescriptorInitialization(BaseTestXmodule): self.assertNotIn('source', fields) self.assertFalse(self.item_descriptor.download_video) + @ddt.data( + ( + { + 'desktop_webm': 'https://webm.com/dw.webm', + 'hls': 'https://hls.com/hls.m3u8', + 'youtube': 'v0TFmdO4ZP0', + 'desktop_mp4': 'https://mp4.com/dm.mp4' + }, + ['https://www.youtube.com/watch?v=v0TFmdO4ZP0'] + ), + ( + { + 'desktop_webm': 'https://webm.com/dw.webm', + 'hls': 'https://hls.com/hls.m3u8', + 'youtube': None, + 'desktop_mp4': 'https://mp4.com/dm.mp4' + }, + ['https://hls.com/hls.m3u8'] + ), + ( + { + 'desktop_webm': 'https://webm.com/dw.webm', + 'hls': None, + 'youtube': None, + 'desktop_mp4': 'https://mp4.com/dm.mp4' + }, + ['https://mp4.com/dm.mp4'] + ), + ( + { + 'desktop_webm': 'https://webm.com/dw.webm', + 'hls': None, + 'youtube': None, + 'desktop_mp4': None + }, + ['https://webm.com/dw.webm'] + ), + ( + { + 'desktop_webm': None, + 'hls': None, + 'youtube': None, + 'desktop_mp4': None + }, + ['https://www.youtube.com/watch?v=3_yD_cEKoCk'] + ), + ) + @ddt.unpack + @patch('xmodule.video_module.video_module.HLSPlaybackEnabledFlag.feature_enabled', Mock(return_value=True)) + def test_val_encoding_in_context(self, val_video_encodings, video_url): + """ + Tests that the val encodings correctly override the video url when the edx video id is set and + one or more encodings are present. + """ + with patch('xmodule.video_module.video_module.edxval_api.get_urls_for_profiles') as get_urls_for_profiles: + get_urls_for_profiles.return_value = val_video_encodings + self.initialize_module( + data='' + ) + context = self.item_descriptor.get_context() + self.assertEqual(context['transcripts_basic_tab_metadata']['video_url']['value'], video_url) + @attr(shard=1) @ddt.ddt