how the youtube video quality change function works? I am trying to create my own video hosting and add to it the ability to change the video quality for users. How could this functionality be implemented?
The video quality selection you see in YouTube and similar services uses ABR streaming, Adaptive Bit Rate Streaming.
The client device or player downloads the video in chunks, e.g 10 second chunks, and selects the next chunk from the bit rate most appropriate to the current network conditions. See some more info in this answer: https://stackoverflow.com/a/42365034/334402
To use ABR you would typically use a dedicated streaming server - you could implement it yourself but it would be a lot of work.
Open source streaming server exist including: